View Javadoc
1   /**
2    * This file Copyright (c) 2013-2017 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.googlesitemap.setup.for2_1;
35  
36  import info.magnolia.jcr.util.NodeTypeTemplateUtil;
37  import info.magnolia.module.googlesitemap.SiteMapNodeTypes;
38  import info.magnolia.setup.for5_0.AbstractNodeTypeRegistrationTask;
39  
40  import java.util.ArrayList;
41  import java.util.Arrays;
42  import java.util.LinkedList;
43  import java.util.List;
44  
45  import javax.jcr.PropertyType;
46  import javax.jcr.RepositoryException;
47  import javax.jcr.nodetype.NodeType;
48  import javax.jcr.nodetype.NodeTypeDefinition;
49  import javax.jcr.nodetype.NodeTypeManager;
50  import javax.jcr.nodetype.NodeTypeTemplate;
51  import javax.jcr.version.OnParentVersionAction;
52  
53  import org.slf4j.Logger;
54  import org.slf4j.LoggerFactory;
55  
56  /**
57   * Add GoogleSiteMap mixIn to the mgnl:pages nodeType. <br>
58   * Update the existing SiteMap nodeType definition.
59   */
60  public class Register21NodeTypeTask extends AbstractNodeTypeRegistrationTask {
61      private final Logger log = LoggerFactory.getLogger(Register21NodeTypeTask.class);
62      private List<String> nodeTypeToAddMixIn;
63  
64      public Register21NodeTypeTask(String name, String description, String workspaceName, List<String> nodeTypeToAddMixIn) {
65          super(name, description, workspaceName);
66          this.nodeTypeToAddMixIn = nodeTypeToAddMixIn != null ? nodeTypeToAddMixIn : new ArrayList<String>();
67      }
68  
69      @Override
70      public List<NodeTypeDefinition> getNodeTypesToRegister(NodeTypeManager nodeTypeManager) throws RepositoryException {
71          LinkedList<NodeTypeDefinition> res = new LinkedList<NodeTypeDefinition>();
72          for (String nodeTypeName : nodeTypeToAddMixIn) {
73              if (nodeTypeManager.hasNodeType(nodeTypeName)) {
74                  NodeType nodeType = nodeTypeManager.getNodeType(nodeTypeName);
75                  res.add(updateExistingNodeType(nodeType, nodeTypeManager));
76              } else {
77                  log.warn("NodeType {} is not register. {} will not be register as super type to this definition", nodeTypeName, SiteMapNodeTypes.GoogleSiteMap.NAME);
78              }
79          }
80          // Update the siteMap nodeType definition
81          if (nodeTypeManager.hasNodeType(SiteMapNodeTypes.SiteMap.NAME)) {
82              NodeType nodeType = nodeTypeManager.getNodeType(SiteMapNodeTypes.SiteMap.NAME);
83              res.add(updateSiteMapNodeType(nodeType, nodeTypeManager));
84          }
85          return res;
86      }
87  
88      @Override
89      public List<String> getNodeTypesToUnregister(NodeTypeManager nodeTypeManager) throws RepositoryException {
90          return null;
91      }
92  
93      private NodeTypeTemplate updateExistingNodeType(NodeType nodeType, NodeTypeManager nodeTypeManager) throws RepositoryException {
94          NodeTypeTemplate nodeTypeTemplate = null;
95          // Add Google SiteMap as superType
96          ArrayList<String> superType = new ArrayList<String>(Arrays.asList(nodeType.getDeclaredSupertypeNames()));
97          superType.add(SiteMapNodeTypes.GoogleSiteMap.NAME);
98          // Copy all others values
99          nodeTypeTemplate = NodeTypeTemplateUtil.createNodeType(nodeTypeManager, nodeType.getName(), superType.toArray(new String[superType.size()]), nodeType.isMixin(), nodeType.hasOrderableChildNodes(), nodeType.getPrimaryItemName(), nodeType.isQueryable());
100         nodeTypeTemplate.getNodeDefinitionTemplates().addAll(Arrays.asList(nodeType.getDeclaredChildNodeDefinitions()));
101         nodeTypeTemplate.getPropertyDefinitionTemplates().addAll(Arrays.asList(nodeType.getDeclaredPropertyDefinitions()));
102 
103         return nodeTypeTemplate;
104     }
105 
106     private NodeTypeTemplate updateSiteMapNodeType(NodeType nodeType, NodeTypeManager nodeTypeManager) throws RepositoryException {
107         NodeTypeTemplate nodeTypeTemplate = null;
108         // Copy basic initial values
109         nodeTypeTemplate = NodeTypeTemplateUtil.createNodeType(nodeTypeManager, nodeType.getName(), nodeType.getDeclaredSupertypeNames(), nodeType.isMixin(), nodeType.hasOrderableChildNodes(), nodeType.getPrimaryItemName(), nodeType.isQueryable());
110         // Add the new properties
111         nodeTypeTemplate.getPropertyDefinitionTemplates().add(NodeTypeTemplateUtil.createPropertyDefinition(nodeTypeManager, "mgnl:googleSiteMapDisplayName", false, false, false, false, false, true, OnParentVersionAction.COPY, PropertyType.STRING, null, null, null));
112 
113         nodeTypeTemplate.getPropertyDefinitionTemplates().add(NodeTypeTemplateUtil.createPropertyDefinition(nodeTypeManager, "mgnl:googleSiteMapType", false, false, false, false, false, true, OnParentVersionAction.COPY, PropertyType.STRING, null, null, null));
114         nodeTypeTemplate.getPropertyDefinitionTemplates().add(NodeTypeTemplateUtil.createPropertyDefinition(nodeTypeManager, "mgnl:googleSiteMapURL", false, false, false, false, false, true, OnParentVersionAction.COPY, PropertyType.STRING, null, null, null));
115         nodeTypeTemplate.getPropertyDefinitionTemplates().add(NodeTypeTemplateUtil.createPropertyDefinition(nodeTypeManager, "mgnl:googleSiteMapPages", false, false, false, true, false, true, OnParentVersionAction.COPY, PropertyType.STRING, null, null, null));
116         nodeTypeTemplate.getPropertyDefinitionTemplates().add(NodeTypeTemplateUtil.createPropertyDefinition(nodeTypeManager, "mgnl:googleSiteMapIncludeVirtualUri", false, false, false, false, false, true, OnParentVersionAction.COPY, PropertyType.BOOLEAN, null, null, null));
117         return nodeTypeTemplate;
118     }
119 }