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.core.Content;
37  import info.magnolia.cms.core.ItemType;
38  import info.magnolia.cms.util.ContentUtil;
39  import info.magnolia.cms.util.QueryUtil;
40  import info.magnolia.module.InstallContext;
41  import info.magnolia.module.delta.AbstractRepositoryTask;
42  import info.magnolia.module.delta.TaskExecutionException;
43  import info.magnolia.repository.RepositoryConstants;
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      @Override
74      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
75          final Collection<Content> dialogs = QueryUtil.query(RepositoryConstants.CONFIG, "modules/*/dialogs", Query.XPATH);
76          List<Content> dialogNodes = new ArrayList<Content>();
77  
78          for (Iterator<Content> iterator = dialogs.iterator(); iterator.hasNext();) {
79              collectDialogNodes(iterator.next(), dialogNodes);
80          }
81          log.debug("Found {} dialog(s)", dialogNodes.size());
82  
83          for (Content srcNode : dialogNodes) {
84              final String handle = srcNode.getHandle();
85              try {
86                  log.debug("Checking if {} needs to be replaced due to incorrect dialog type...", handle);
87                  if(!srcNode.isNodeType(ItemType.CONTENTNODE.getSystemName())){
88                      ContentUtil.changeNodeType(srcNode, ItemType.CONTENTNODE, false);
89                  }
90              }
91              catch (RepositoryException e) {
92                  installContext.error("Can't replace " + handle, e);
93              }
94          }
95      }
96  
97      //the following private methods were copied from DialogHandlerManager
98      private void collectDialogNodes(Content current, List<Content> dialogNodes) throws RepositoryException {
99          if(isDialogNode(current)){
100             dialogNodes.add(current);
101             return;
102         }
103         for (Content child : ContentUtil.getAllChildren(current)) {
104             collectDialogNodes(child, dialogNodes);
105         }
106     }
107 
108     private boolean isDialogNode(Content node) throws RepositoryException{
109         if(isDialogControlNode(node)){
110             return false;
111         }
112 
113         // if leaf
114         if(ContentUtil.getAllChildren(node).isEmpty()){
115             return true;
116         }
117 
118         // if has node datas
119         if(!node.getNodeDataCollection().isEmpty()){
120             return true;
121         }
122 
123         // if one subnode is a control
124         for (Content child : node.getChildren(ItemType.CONTENTNODE)) {
125             if (isDialogControlNode(child)) {
126                 return true;
127             }
128         }
129         return false;
130     }
131 
132     private boolean isDialogControlNode(Content node) throws RepositoryException{
133         return node.hasNodeData("controlType") || node.hasNodeData("reference");
134     }
135 
136 }