View Javadoc
1   /**
2    * This file Copyright (c) 2010-2014 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   * @deprecated Not used since 4.5.
65   */
66  public class ReplaceWrongDialogNodeTypeTask extends AbstractRepositoryTask {
67  
68      private final static Logger log = LoggerFactory.getLogger(ReplaceWrongDialogNodeTypeTask.class);
69  
70      public ReplaceWrongDialogNodeTypeTask() {
71          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");
72      }
73  
74      @Override
75      protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
76          final Collection<Content> dialogs = QueryUtil.query(RepositoryConstants.CONFIG, "modules/*/dialogs", Query.XPATH);
77          List<Content> dialogNodes = new ArrayList<Content>();
78  
79          for (Iterator<Content> iterator = dialogs.iterator(); iterator.hasNext();) {
80              collectDialogNodes(iterator.next(), dialogNodes);
81          }
82          log.debug("Found {} dialog(s)", dialogNodes.size());
83  
84          for (Content srcNode : dialogNodes) {
85              final String handle = srcNode.getHandle();
86              try {
87                  log.debug("Checking if {} needs to be replaced due to incorrect dialog type...", handle);
88                  if(!srcNode.isNodeType(ItemType.CONTENTNODE.getSystemName())){
89                      ContentUtil.changeNodeType(srcNode, ItemType.CONTENTNODE, false);
90                  }
91              }
92              catch (RepositoryException e) {
93                  installContext.error("Can't replace " + handle, e);
94              }
95          }
96      }
97  
98      //the following private methods were copied from DialogHandlerManager
99      private void collectDialogNodes(Content current, List<Content> dialogNodes) throws RepositoryException {
100         if(isDialogNode(current)){
101             dialogNodes.add(current);
102             return;
103         }
104         for (Content child : ContentUtil.getAllChildren(current)) {
105             collectDialogNodes(child, dialogNodes);
106         }
107     }
108 
109     private boolean isDialogNode(Content node) throws RepositoryException{
110         if(isDialogControlNode(node)){
111             return false;
112         }
113 
114         // if leaf
115         if(ContentUtil.getAllChildren(node).isEmpty()){
116             return true;
117         }
118 
119         // if has node datas
120         if(!node.getNodeDataCollection().isEmpty()){
121             return true;
122         }
123 
124         // if one subnode is a control
125         for (Content child : node.getChildren(ItemType.CONTENTNODE)) {
126             if (isDialogControlNode(child)) {
127                 return true;
128             }
129         }
130         return false;
131     }
132 
133     private boolean isDialogControlNode(Content node) throws RepositoryException{
134         return node.hasNodeData("controlType") || node.hasNodeData("reference");
135     }
136 
137 }