View Javadoc
1   /**
2    * This file Copyright (c) 2013-2018 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.setup.for5_0;
35  
36  import info.magnolia.jcr.util.NodeTypes;
37  
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.List;
41  
42  import javax.jcr.RepositoryException;
43  import javax.jcr.nodetype.NodeDefinition;
44  import javax.jcr.nodetype.NodeType;
45  import javax.jcr.nodetype.NodeTypeDefinition;
46  import javax.jcr.nodetype.NodeTypeIterator;
47  import javax.jcr.nodetype.NodeTypeManager;
48  import javax.jcr.nodetype.NodeTypeTemplate;
49  
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * Remove metaData nodeType's defined as 'childNodeDefinition' for all registered nodeType that still define this dependency.<br>
55   * Create a list of registered nodeTypes that have metaData nodeType as 'childNodeDefinition'.<br>
56   * This list is then return by {@link info.magnolia.setup.nodetype.AbstractNodeTypeRegistrationTask#getNodeTypesToRegister(NodeTypeManager)} and handled as an update of nodeType.
57   */
58  public class RemoveMetaDataInNodeTypeDefinitionTask extends info.magnolia.setup.nodetype.AbstractNodeTypeRegistrationTask {
59  
60      private final Logger log = LoggerFactory.getLogger(RemoveMetaDataInNodeTypeDefinitionTask.class);
61  
62      public RemoveMetaDataInNodeTypeDefinitionTask(String name, String description, String workspaceName) {
63          super(name, description, workspaceName);
64      }
65  
66      /**
67       * Return a {@link NodeTypeDefinition} list where the metaData child node definition has been removed.
68       */
69      @Override
70      public List<NodeTypeDefinition> getNodeTypesToRegister(NodeTypeManager nodeTypeManager) {
71          List<NodeTypeDefinition> list = new ArrayList<NodeTypeDefinition>();
72          try {
73              NodeTypeIterator iterator = nodeTypeManager.getAllNodeTypes();
74              while (iterator.hasNext()) {
75                  NodeType nodeType = iterator.nextNodeType();
76                  if (nodeType.getDeclaredChildNodeDefinitions() != null) {
77                      // check if this type define metaData as child
78                      List<NodeDefinition> childNodeDefinition = Arrays.asList(nodeType.getDeclaredChildNodeDefinitions());
79                      for (NodeDefinition nodeDefinition : childNodeDefinition) {
80  
81                          if (nodeDefinition.getDefaultPrimaryType() != null && NodeTypes.MetaData.NAME.equals(nodeDefinition.getDefaultPrimaryType().getName())) {
82                              NodeTypeTemplate newNodeType = createNodeTypeWithoutMetaData(nodeTypeManager, nodeType, nodeDefinition);
83                              log.info("Add the following NodeType '{}' that currently defined a metaData nodeType as child node to the list.", newNodeType.getName());
84                              list.add(newNodeType);
85                              break;
86                          }
87                      }
88                  }
89              }
90          } catch (RepositoryException e) {
91              log.error("Could not create a List of existing NodeTpe declaring metaData as childNodeDefinition", e);
92          }
93  
94          return list;
95      }
96  
97      @Override
98      public List<String> getNodeTypesToUnregister(NodeTypeManager nodeTypeManager) {
99          return null;
100     }
101 
102     /**
103      * Create a new {@link NodeTypeTemplate} copy of the 'nodeType', but without <br>
104      * metaData defined as 'childNodeDefinition'.
105      */
106     private NodeTypeTemplate createNodeTypeWithoutMetaData(NodeTypeManager nodeTypeManager, NodeType nodeType, NodeDefinition metaDataNode) throws RepositoryException {
107         NodeTypeTemplate ntt = nodeTypeManager.createNodeTypeTemplate();
108         ntt.setDeclaredSuperTypeNames(nodeType.getDeclaredSupertypeNames());
109         ntt.setMixin(nodeType.isMixin());
110         ntt.setName(nodeType.getName());
111         ntt.setOrderableChildNodes(nodeType.hasOrderableChildNodes());
112         ntt.setPrimaryItemName(nodeType.getPrimaryItemName());
113         ntt.setQueryable(nodeType.isQueryable());
114         ntt.getPropertyDefinitionTemplates().addAll(Arrays.asList(nodeType.getDeclaredPropertyDefinitions()));
115         List<NodeDefinition> childNodeDefinition = new ArrayList<NodeDefinition>();
116         childNodeDefinition.addAll(Arrays.asList(nodeType.getDeclaredChildNodeDefinitions()));
117         childNodeDefinition.remove(metaDataNode);
118         ntt.getNodeDefinitionTemplates().addAll(childNodeDefinition);
119         return ntt;
120     }
121 }