1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.jcr;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import javax.jcr.nodetype.ConstraintViolationException;
29 import javax.jcr.nodetype.NodeDefinition;
30 import javax.jcr.nodetype.PropertyDefinition;
31 import net.jcip.annotations.NotThreadSafe;
32 import org.modeshape.common.util.CheckArg;
33 import org.modeshape.graph.ExecutionContext;
34 import org.modeshape.graph.property.Name;
35 import org.modeshape.graph.property.ValueFormatException;
36 import org.modeshape.jcr.nodetype.NodeDefinitionTemplate;
37 import org.modeshape.jcr.nodetype.NodeTypeDefinition;
38 import org.modeshape.jcr.nodetype.NodeTypeTemplate;
39 import org.modeshape.jcr.nodetype.PropertyDefinitionTemplate;
40
41
42
43
44 @NotThreadSafe
45 public class JcrNodeTypeTemplate implements NodeTypeDefinition, NodeTypeTemplate {
46
47 private final ExecutionContext context;
48 private final List<NodeDefinitionTemplate> nodeDefinitionTemplates = new ArrayList<NodeDefinitionTemplate>();
49 private final List<PropertyDefinitionTemplate> propertyDefinitionTemplates = new ArrayList<PropertyDefinitionTemplate>();
50 private final boolean createdFromExistingDefinition;
51 private boolean isAbstract;
52 private boolean queryable = true;
53 private boolean mixin;
54 private boolean orderableChildNodes;
55 private Name[] declaredSupertypeNames;
56 private Name name;
57 private Name primaryItemName;
58
59 JcrNodeTypeTemplate( ExecutionContext context ) {
60 this(context, false);
61 }
62
63 JcrNodeTypeTemplate( ExecutionContext context,
64 boolean createdFromExistingDefinition ) {
65 assert context != null;
66
67 this.context = context;
68 this.createdFromExistingDefinition = createdFromExistingDefinition;
69 }
70
71 JcrNodeTypeTemplate( JcrNodeTypeTemplate original,
72 ExecutionContext context ) {
73 this.context = context;
74 this.isAbstract = original.isAbstract;
75 this.queryable = original.queryable;
76 this.mixin = original.mixin;
77 this.name = original.name;
78 this.orderableChildNodes = original.orderableChildNodes;
79 this.declaredSupertypeNames = original.declaredSupertypeNames;
80 this.primaryItemName = original.primaryItemName;
81 JcrItemDefinitionTemplate.registerMissingNamespaces(original.context, context, this.name);
82 JcrItemDefinitionTemplate.registerMissingNamespaces(original.context, context, this.declaredSupertypeNames);
83 JcrItemDefinitionTemplate.registerMissingNamespaces(original.context, context, this.primaryItemName);
84 for (NodeDefinitionTemplate childDefn : original.nodeDefinitionTemplates) {
85 this.nodeDefinitionTemplates.add(((JcrNodeDefinitionTemplate)childDefn).with(context));
86 }
87 for (PropertyDefinitionTemplate propDefn : original.propertyDefinitionTemplates) {
88 this.propertyDefinitionTemplates.add(((JcrPropertyDefinitionTemplate)propDefn).with(context));
89 }
90 this.createdFromExistingDefinition = original.createdFromExistingDefinition;
91 }
92
93 JcrNodeTypeTemplate with( ExecutionContext context ) {
94 return context == this.context ? this : new JcrNodeTypeTemplate(this, context);
95 }
96
97 ExecutionContext getExecutionContext() {
98 return context;
99 }
100
101 private String string( Name name ) {
102 if (name == null) return null;
103 return name.getString(context.getNamespaceRegistry());
104 }
105
106 Name[] declaredSupertypeNames() {
107 return this.declaredSupertypeNames;
108 }
109
110
111
112
113
114
115 public List<NodeDefinitionTemplate> getNodeDefinitionTemplates() {
116 return nodeDefinitionTemplates;
117 }
118
119
120
121
122
123
124 public List<PropertyDefinitionTemplate> getPropertyDefinitionTemplates() {
125 return propertyDefinitionTemplates;
126 }
127
128
129
130
131
132
133 public void setAbstract( boolean isAbstract ) {
134 this.isAbstract = isAbstract;
135 }
136
137
138
139
140
141
142
143 public void setDeclaredSuperTypeNames( String[] names ) throws ConstraintViolationException {
144 if (names == null) {
145 throw new ConstraintViolationException(JcrI18n.badNodeTypeName.text("names"));
146 }
147
148 Name[] supertypeNames = new Name[names.length];
149
150 for (int i = 0; i < names.length; i++) {
151 CheckArg.isNotEmpty(names[i], "names[" + i + "");
152 try {
153 supertypeNames[i] = context.getValueFactories().getNameFactory().create(names[i]);
154 } catch (ValueFormatException vfe) {
155 throw new ConstraintViolationException(vfe);
156 }
157 }
158 this.declaredSupertypeNames = supertypeNames;
159 }
160
161
162
163
164
165
166 public void setMixin( boolean mixin ) {
167 this.mixin = mixin;
168 }
169
170
171
172
173
174
175 public void setName( String name ) throws ConstraintViolationException {
176 CheckArg.isNotEmpty(name, "name");
177 try {
178 this.name = context.getValueFactories().getNameFactory().create(name);
179 } catch (ValueFormatException vfe) {
180 throw new ConstraintViolationException(vfe);
181 }
182 }
183
184
185
186
187
188
189 public void setOrderableChildNodes( boolean orderable ) {
190 this.orderableChildNodes = orderable;
191 }
192
193
194
195
196
197
198
199
200
201
202 public void setPrimaryItemName( String name ) throws ConstraintViolationException {
203 if (name == null || name.trim().length() == 0) {
204 this.primaryItemName = null;
205 } else {
206 try {
207 this.primaryItemName = context.getValueFactories().getNameFactory().create(name);
208 } catch (ValueFormatException vfe) {
209 throw new ConstraintViolationException(vfe);
210 }
211 }
212 }
213
214
215
216
217
218
219 public NodeDefinition[] getDeclaredChildNodeDefinitions() {
220 if (!createdFromExistingDefinition && nodeDefinitionTemplates.isEmpty()) return null;
221
222 return nodeDefinitionTemplates.toArray(new NodeDefinition[nodeDefinitionTemplates.size()]);
223 }
224
225
226
227
228
229
230 public PropertyDefinition[] getDeclaredPropertyDefinitions() {
231 if (!createdFromExistingDefinition && propertyDefinitionTemplates.isEmpty()) return null;
232
233 return propertyDefinitionTemplates.toArray(new PropertyDefinition[propertyDefinitionTemplates.size()]);
234 }
235
236
237
238
239
240
241 public String[] getDeclaredSupertypeNames() {
242 if (declaredSupertypeNames == null) return new String[0];
243 String[] names = new String[declaredSupertypeNames.length];
244
245 for (int i = 0; i < declaredSupertypeNames.length; i++) {
246 names[i] = declaredSupertypeNames[i].getString(context.getNamespaceRegistry());
247 }
248 return names;
249 }
250
251
252
253
254
255
256 public String getName() {
257 return string(name);
258 }
259
260
261
262
263
264
265 public String getPrimaryItemName() {
266 return string(primaryItemName);
267 }
268
269
270
271
272
273
274 public boolean hasOrderableChildNodes() {
275 return orderableChildNodes;
276 }
277
278
279
280
281
282
283 public boolean isAbstract() {
284 return isAbstract;
285 }
286
287
288
289
290
291
292 public boolean isMixin() {
293 return mixin;
294 }
295
296
297
298
299
300
301 public boolean isQueryable() {
302 return queryable;
303 }
304
305
306
307
308
309
310 public void setQueryable( boolean queryable ) {
311 this.queryable = queryable;
312 }
313 }