View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.jcr.util;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map.Entry;
39  
40  import javax.jcr.PropertyType;
41  import javax.jcr.RepositoryException;
42  import javax.jcr.Value;
43  import javax.jcr.nodetype.NodeDefinitionTemplate;
44  import javax.jcr.nodetype.NodeTypeManager;
45  import javax.jcr.nodetype.NodeTypeTemplate;
46  import javax.jcr.nodetype.PropertyDefinitionTemplate;
47  import javax.jcr.version.OnParentVersionAction;
48  
49  import org.apache.commons.lang.StringUtils;
50  import org.slf4j.Logger;
51  import org.slf4j.LoggerFactory;
52  
53  /**
54   * Utility class used to create: <br>
55   * - {@link NodeTypeTemplate}. <br>
56   * - {@link NodeDefinitionTemplate} used to define a 'childNodeDefinition' on a {@link NodeTypeTemplate}. <br>
57   * - {@link PropertyDefinitionTemplate} used to define a 'propertyDefinition' on a {@link NodeTypeTemplate}. <br>
58   */
59  public class NodeTypeTemplateUtil {
60      private static final Logger log = LoggerFactory.getLogger(NodeTypeTemplateUtil.class);
61  
62      /**
63       * Create a simple mixin {@link NodeTypeTemplate} with a single supertype, no child nodes and several property.<br>
64       * Property are set by defaultto <br>
65       * - autoCreated="false"<br>
66       * - mandatory="false"<br>
67       * - onParentVersion="COPY"<br>
68       * - protected="false"<br>
69       * - multiple="false"<br>
70       * 
71       * @param name set as {@link NodeTypeTemplate}'s name, and property 'isMixin' is set to true.
72       * @param superTyp set as 'supertype' name.
73       * @param propertyDefinition create {@link PropertyDefinitionTemplate}
74       * @param propertyNameTypeMap defines: <br>
75       * - key : name of the property <br>
76       * - value: requiredType of the property as {@link PropertyType} constant.<br>
77       * @return the created {@link NodeTypeTemplate}.
78       */
79      public static NodeTypeTemplate createSimpleMixinNodeType(NodeTypeManager nodeTypeManager, String name, String superType, HashMap<String, Integer> propertyNameTypeMap) throws RepositoryException {
80          NodeTypeTemplate nodeType = null;
81          nodeType = createNodeType(nodeTypeManager, name, new String[] { superType }, true, false, null, false);
82          if (propertyNameTypeMap != null && !propertyNameTypeMap.isEmpty()) {
83              for (Entry<String, Integer> entry : propertyNameTypeMap.entrySet()) {
84                  PropertyDefinitionTemplate propertyDefinition = createPropertyDefinition(nodeTypeManager, entry.getKey(), false, true, false, false, false, false, OnParentVersionAction.COPY, entry.getValue(), null, null, null);
85                  nodeType.getPropertyDefinitionTemplates().add(propertyDefinition);
86              }
87          }
88          return nodeType;
89      }
90  
91      /**
92       * Create a simple {@link NodeTypeTemplate} with a list of supertype, one generic child node and two generic properties.<br>
93       */
94      public static NodeTypeTemplate createSimpleNodeType(NodeTypeManager nodeTypeManager, String name, List<String> supertype) throws RepositoryException {
95          NodeTypeTemplate nodeType = null;
96  
97          nodeType = createNodeType(nodeTypeManager, name, supertype.toArray(new String[supertype.size()]), false, true, null, true);
98          // Create generic child
99          NodeDefinitionTemplate child = createChildNodeDefinition(nodeTypeManager, false, false, false, true, null, null, OnParentVersionAction.COPY, new String[] { "nt:base" });
100         nodeType.getNodeDefinitionTemplates().add(child);
101         // Create generic property definition
102         PropertyDefinitionTemplate propertyDefinitionNotMultiple = createPropertyDefinition(nodeTypeManager, null, false, true, false, false, false, true, OnParentVersionAction.COPY, PropertyType.UNDEFINED, null, null, null);
103         nodeType.getPropertyDefinitionTemplates().add(propertyDefinitionNotMultiple);
104         PropertyDefinitionTemplate propertyDefinitionMultiple = createPropertyDefinition(nodeTypeManager, null, false, true, false, true, false, true, OnParentVersionAction.COPY, PropertyType.UNDEFINED, null, null, null);
105         nodeType.getPropertyDefinitionTemplates().add(propertyDefinitionMultiple);
106 
107         return nodeType;
108     }
109 
110     /**
111      * Create a {@link NodeTypeTemplate} used a NodeType definition.
112      */
113     public static NodeTypeTemplate createNodeType(NodeTypeManager nodeTypeManager, String name, String[] superTypeNames, boolean isMixin, boolean isOrderableChildNodes, String primaryItemName, boolean isQueryable) throws RepositoryException {
114         // Create node definition
115         NodeTypeTemplate nodeType = nodeTypeManager.createNodeTypeTemplate();
116         nodeType.setName(name);
117         nodeType.setDeclaredSuperTypeNames(superTypeNames);
118         nodeType.setMixin(isMixin);
119         nodeType.setOrderableChildNodes(isOrderableChildNodes);
120         nodeType.setPrimaryItemName(primaryItemName);
121         nodeType.setQueryable(isQueryable);
122 
123         return nodeType;
124     }
125 
126     /**
127      * Create a {@link NodeDefinitionTemplate} set as 'childNodeDefinition' on a 'nodeType'.<br>
128      * 
129      * @param name if blank, set '*' as value.
130      * @param onParentVersion if not a valid {@link OnParentVersionAction} constant, set {@link OnParentVersionAction#IGNORE} as value.
131      */
132     public static NodeDefinitionTemplate createChildNodeDefinition(NodeTypeManager nodeTypeManager, boolean isAutoCreated, boolean isMandatory, boolean isProtected, boolean allowSameNameSiblings, String defaultPrimaryTypeName, String name, int onParentVersion, String[] requiredPrimaryTypeNames) throws RepositoryException {
133         // Create child node
134         NodeDefinitionTemplate childNodeType = nodeTypeManager.createNodeDefinitionTemplate();
135 
136         if (StringUtils.isNotBlank(defaultPrimaryTypeName)) {
137             childNodeType.setDefaultPrimaryTypeName(defaultPrimaryTypeName);
138         }
139 
140         childNodeType.setName(StringUtils.isBlank(name) ? "*" : name);
141 
142         childNodeType.setMandatory(isMandatory);
143         childNodeType.setSameNameSiblings(allowSameNameSiblings);
144         childNodeType.setAutoCreated(isAutoCreated);
145         childNodeType.setProtected(isProtected);
146 
147         // Set OnParentVersionAction
148         try {
149             OnParentVersionAction.nameFromValue(onParentVersion);
150         } catch (IllegalArgumentException ie) {
151             log.warn("OnParentVersionAction constant definition '{}' is not valid. IGNORE will be set", onParentVersion);
152             onParentVersion = OnParentVersionAction.IGNORE;
153         }
154         childNodeType.setOnParentVersion(onParentVersion);
155 
156         childNodeType.setRequiredPrimaryTypeNames(requiredPrimaryTypeNames);
157 
158         return childNodeType;
159     }
160 
161     /**
162      * Create a {@link PropertyDefinitionTemplate} set as 'propertyDefinition' on a 'nodeType'.<br>
163      * 
164      * @param onParentVersion if not a valid {@link OnParentVersionAction} constant, set {@link OnParentVersionAction#IGNORE} as value.
165      * @param requiredType if not a valid {@link PropertyType} constant, set {@link PropertyType#STRING} as value.
166      * @param name if blank, set '*' as value.<br>
167      * @param availableQueryOperators if null or empty, do not set this value <br>
168      * @param defaultValues if null or empty, do not set this value <br>
169      * @param valueConstraints if null or empty, do not set this value <br>
170      */
171     public static PropertyDefinitionTemplate createPropertyDefinition(NodeTypeManager nodeTypeManager, String name, boolean isAutoCreated, boolean isFullTextSearchable, boolean isMandatory, boolean isMultiple, boolean hasProtectedStatus, boolean isQueryOrderable, int onParentVersion, int requiredType, String[] availableQueryOperators, String[] valueConstraints, Value[] defaultValues) throws RepositoryException {
172         PropertyDefinitionTemplate propertyDefinition = nodeTypeManager.createPropertyDefinitionTemplate();
173 
174         propertyDefinition.setAutoCreated(isAutoCreated);
175         propertyDefinition.setFullTextSearchable(isFullTextSearchable);
176         propertyDefinition.setMandatory(isMandatory);
177         propertyDefinition.setMultiple(isMultiple);
178         propertyDefinition.setProtected(hasProtectedStatus);
179         propertyDefinition.setQueryOrderable(isQueryOrderable);
180 
181         if (availableQueryOperators != null && availableQueryOperators.length > 0) {
182             propertyDefinition.setAvailableQueryOperators(availableQueryOperators);
183         }
184         if (defaultValues != null && defaultValues.length > 0) {
185             propertyDefinition.setDefaultValues(defaultValues);
186         }
187         if (valueConstraints != null && valueConstraints.length > 0) {
188             propertyDefinition.setValueConstraints(valueConstraints);
189         }
190 
191         propertyDefinition.setName(StringUtils.isBlank(name) ? "*" : name);
192         // Set OnParentVersionAction
193         try {
194             OnParentVersionAction.nameFromValue(onParentVersion);
195         } catch (IllegalArgumentException ie) {
196             log.warn("OnParentVersionAction constant definition '{}' is not valid. IGNORE will be set", onParentVersion);
197             onParentVersion = OnParentVersionAction.IGNORE;
198         }
199         propertyDefinition.setOnParentVersion(onParentVersion);
200 
201         // Set required type
202         try {
203             PropertyType.nameFromValue(requiredType);
204         } catch (IllegalArgumentException ie) {
205             log.warn("PropertyType constant definition '{}' is not valid. STRING will be set", requiredType);
206             requiredType = PropertyType.STRING;
207         }
208         propertyDefinition.setRequiredType(requiredType);
209 
210         return propertyDefinition;
211     }
212 }