View Javadoc

1   /**
2    * This file Copyright (c) 2010-2011 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.admininterface.setup.for4_3;
35  
36  import info.magnolia.cms.beans.config.ContentRepository;
37  import info.magnolia.cms.core.Content;
38  import info.magnolia.cms.core.ItemType;
39  import info.magnolia.cms.util.ContentUtil;
40  import info.magnolia.cms.util.QueryUtil;
41  import info.magnolia.module.InstallContext;
42  import info.magnolia.module.delta.AbstractRepositoryTask;
43  import info.magnolia.module.delta.TaskExecutionException;
44  
45  import java.util.ArrayList;
46  import java.util.Collection;
47  import java.util.Iterator;
48  import java.util.List;
49  
50  import javax.jcr.RepositoryException;
51  import javax.jcr.query.Query;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  
57  /**
58   * Check for each module in the config repository if dialogs are of the incorrect type <em>mgnl:content</em> 
59   * and attempts to replace them with the correct one <em>mgnl:contentNode<em>.<br>
60   * See also jira MAGNOLIA-2810
61   * @author fgrilli
62   * @version $Id$
63   *
64   */
65  public class ReplaceWrongDialogNodeTypeTask extends AbstractRepositoryTask {
66      
67      private final static Logger log = LoggerFactory.getLogger(ReplaceWrongDialogNodeTypeTask.class);
68      
69      public ReplaceWrongDialogNodeTypeTask() {
70          super("Replace incorrect dialog node types", "Checks for each module in the config repository if dialogs are of the incorrect type mgnl:content and replaces them with the correct one mgnl:contentNode");
71      }
72  
73      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
74          final Collection<Content> dialogs = QueryUtil.query(ContentRepository.CONFIG, "modules/*/dialogs", Query.XPATH);
75          List<Content> dialogNodes = new ArrayList<Content>();
76          
77          for (Iterator<Content> iterator = dialogs.iterator(); iterator.hasNext();) {
78              collectDialogNodes(iterator.next(), dialogNodes);
79          }
80          log.debug("Found {} dialog(s)", dialogNodes.size());
81          
82          for (Content srcNode : dialogNodes) {
83              final String handle = srcNode.getHandle();
84              try {
85                  log.debug("Checking if {} needs to be replaced due to incorrect dialog type...", handle);
86                  if(!srcNode.isNodeType(ItemType.CONTENTNODE.getSystemName()) && !srcNode.getHandle().contains("generic")){
87                      ContentUtil.changeNodeType(srcNode, ItemType.CONTENTNODE, false);
88                  }
89              }
90              catch (RepositoryException e) {
91                  installContext.error("Can't replace " + handle, e);
92              }
93          }
94      }
95  
96      //the following private methods were copied from DialogHandlerManager 
97      private void collectDialogNodes(Content current, List<Content> dialogNodes) throws RepositoryException {
98          if(isDialogNode(current)){
99              dialogNodes.add(current);
100             return;
101         }
102         for (Content child : ContentUtil.getAllChildren(current)) {
103             collectDialogNodes(child, dialogNodes);
104         }
105     }
106 
107     private boolean isDialogNode(Content node) throws RepositoryException{
108         if(isDialogControlNode(node)){
109             return false;
110         }
111 
112         // if leaf
113         if(ContentUtil.getAllChildren(node).isEmpty()){
114             return true;
115         }
116 
117         // if has node datas
118         if(!node.getNodeDataCollection().isEmpty()){
119             return true;
120         }
121 
122         // if one subnode is a control
123         for (Content child : node.getChildren(ItemType.CONTENTNODE)) {
124             if (isDialogControlNode(child)) {
125                 return true;
126             }
127         }
128         return false;
129     }
130 
131     private boolean isDialogControlNode(Content node) throws RepositoryException{
132         return node.hasNodeData("controlType") || node.hasNodeData("reference");
133     }
134 
135 }