KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > M2ClassDefinition


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.dictionary;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.alfresco.service.cmr.dictionary.AspectDefinition;
27 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
28 import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
29 import org.alfresco.service.cmr.dictionary.ClassDefinition;
30 import org.alfresco.service.cmr.dictionary.DictionaryException;
31 import org.alfresco.service.cmr.dictionary.ModelDefinition;
32 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
33 import org.alfresco.service.namespace.NamespacePrefixResolver;
34 import org.alfresco.service.namespace.QName;
35
36
37 /**
38  * Compiled Class Definition
39  *
40  * @author David Caruana
41  */

42 /*package*/ class M2ClassDefinition implements ClassDefinition
43 {
44     protected ModelDefinition model;
45     protected M2Class m2Class;
46     protected QName name;
47     protected QName parentName = null;
48     
49     private Map JavaDoc<QName, M2PropertyOverride> propertyOverrides = new HashMap JavaDoc<QName, M2PropertyOverride>();
50     private Map JavaDoc<QName, PropertyDefinition> properties = new HashMap JavaDoc<QName, PropertyDefinition>();
51     private Map JavaDoc<QName, PropertyDefinition> inheritedProperties = new HashMap JavaDoc<QName, PropertyDefinition>();
52     private Map JavaDoc<QName, AssociationDefinition> associations = new HashMap JavaDoc<QName, AssociationDefinition>();
53     private Map JavaDoc<QName, AssociationDefinition> inheritedAssociations = new HashMap JavaDoc<QName, AssociationDefinition>();
54     private Map JavaDoc<QName, ChildAssociationDefinition> inheritedChildAssociations = new HashMap JavaDoc<QName, ChildAssociationDefinition>();
55     private List JavaDoc<AspectDefinition> defaultAspects = new ArrayList JavaDoc<AspectDefinition>();
56     private List JavaDoc<QName> defaultAspectNames = new ArrayList JavaDoc<QName>();
57     private List JavaDoc<AspectDefinition> inheritedDefaultAspects = new ArrayList JavaDoc<AspectDefinition>();
58     
59
60     /**
61      * Construct
62      *
63      * @param m2Class class definition
64      * @param resolver namepsace resolver
65      * @param modelProperties global list of model properties
66      * @param modelAssociations global list of model associations
67      */

68     /*package*/ M2ClassDefinition(ModelDefinition model, M2Class m2Class, NamespacePrefixResolver resolver, Map JavaDoc<QName, PropertyDefinition> modelProperties, Map JavaDoc<QName, AssociationDefinition> modelAssociations)
69     {
70         this.model = model;
71         this.m2Class = m2Class;
72         
73         // Resolve Names
74
this.name = QName.createQName(m2Class.getName(), resolver);
75         if (m2Class.getParentName() != null && m2Class.getParentName().length() > 0)
76         {
77             this.parentName = QName.createQName(m2Class.getParentName(), resolver);
78         }
79         
80         // Construct Properties
81
for (M2Property property : m2Class.getProperties())
82         {
83             PropertyDefinition def = new M2PropertyDefinition(this, property, resolver);
84             if (properties.containsKey(def.getName()))
85             {
86                 throw new DictionaryException("Found duplicate property definition " + def.getName().toPrefixString() + " within class " + name.toPrefixString());
87             }
88             
89             // Check for existence of property elsewhere within the model
90
PropertyDefinition existingDef = modelProperties.get(def.getName());
91             if (existingDef != null)
92             {
93                 // TODO: Consider sharing property, if property definitions are equal
94
throw new DictionaryException("Found duplicate property definition " + def.getName().toPrefixString() + " within class "
95                     + name.toPrefixString() + " and class " + existingDef.getContainerClass().getName().toPrefixString());
96             }
97             
98             properties.put(def.getName(), def);
99             modelProperties.put(def.getName(), def);
100         }
101         
102         // Construct Associations
103
for (M2ClassAssociation assoc : m2Class.getAssociations())
104         {
105             AssociationDefinition def;
106             if (assoc instanceof M2ChildAssociation)
107             {
108                 def = new M2ChildAssociationDefinition(this, (M2ChildAssociation)assoc, resolver);
109             }
110             else
111             {
112                 def = new M2AssociationDefinition(this, assoc, resolver);
113             }
114             if (associations.containsKey(def.getName()))
115             {
116                 throw new DictionaryException("Found duplicate association definition " + def.getName().toPrefixString() + " within class " + name.toPrefixString());
117             }
118             
119             // Check for existence of association elsewhere within the model
120
AssociationDefinition existingDef = modelAssociations.get(def.getName());
121             if (existingDef != null)
122             {
123                 // TODO: Consider sharing association, if association definitions are equal
124
throw new DictionaryException("Found duplicate association definition " + def.getName().toPrefixString() + " within class "
125                     + name.toPrefixString() + " and class " + existingDef.getSourceClass().getName().toPrefixString());
126             }
127             
128             associations.put(def.getName(), def);
129             modelAssociations.put(def.getName(), def);
130         }
131         
132         // Construct Property overrides
133
for (M2PropertyOverride override : m2Class.getPropertyOverrides())
134         {
135             QName overrideName = QName.createQName(override.getName(), resolver);
136             if (properties.containsKey(overrideName))
137             {
138                 throw new DictionaryException("Found duplicate property and property override definition " + overrideName.toPrefixString() + " within class " + name.toPrefixString());
139             }
140             if (propertyOverrides.containsKey(overrideName))
141             {
142                 throw new DictionaryException("Found duplicate property override definition " + overrideName.toPrefixString() + " within class " + name.toPrefixString());
143             }
144             propertyOverrides.put(overrideName, override);
145         }
146         
147         // Resolve qualified names
148
for (String JavaDoc aspectName : m2Class.getMandatoryAspects())
149         {
150             QName name = QName.createQName(aspectName, resolver);
151             if (!defaultAspectNames.contains(name))
152             {
153                 defaultAspectNames.add(name);
154             }
155         }
156     }
157     
158     @Override JavaDoc
159     public String JavaDoc toString()
160     {
161         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(120);
162         sb.append("ClassDef ")
163           .append("[ name=").append(name)
164           .append("]");
165         return sb.toString();
166     }
167     
168     
169     /*package*/ void resolveDependencies(ModelQuery query)
170     {
171         if (parentName != null)
172         {
173             ClassDefinition parent = query.getClass(parentName);
174             if (parent == null)
175             {
176                 throw new DictionaryException("Parent class " + parentName.toPrefixString() + " of class " + name.toPrefixString() + " is not found");
177             }
178         }
179         
180         for (PropertyDefinition def : properties.values())
181         {
182             ((M2PropertyDefinition)def).resolveDependencies(query);
183         }
184         for (AssociationDefinition def : associations.values())
185         {
186             ((M2AssociationDefinition)def).resolveDependencies(query);
187         }
188         
189         for (QName aspectName : defaultAspectNames)
190         {
191             AspectDefinition aspect = query.getAspect(aspectName);
192             if (aspect == null)
193             {
194                 throw new DictionaryException("Mandatory aspect " + aspectName.toPrefixString() + " of class " + name.toPrefixString() + " is not found");
195             }
196             defaultAspects.add(aspect);
197         }
198     }
199     
200
201     /*package*/ void resolveInheritance(ModelQuery query)
202     {
203         // Retrieve parent class
204
ClassDefinition parentClass = (parentName == null) ? null : query.getClass(parentName);
205         
206         // Build list of inherited properties (and process overridden values)
207
if (parentClass != null)
208         {
209             for (PropertyDefinition def : parentClass.getProperties().values())
210             {
211                 M2PropertyOverride override = propertyOverrides.get(def.getName());
212                 if (override == null)
213                 {
214                     inheritedProperties.put(def.getName(), def);
215                 }
216                 else
217                 {
218                     inheritedProperties.put(def.getName(), new M2PropertyDefinition(this, def, override));
219                 }
220             }
221         }
222         
223         // Append list of defined properties
224
for (PropertyDefinition def : properties.values())
225         {
226             if (inheritedProperties.containsKey(def.getName()))
227             {
228                 throw new DictionaryException("Duplicate property definition " + def.getName().toPrefixString() + " found in class hierarchy of " + name.toPrefixString());
229             }
230             inheritedProperties.put(def.getName(), def);
231         }
232         
233         // Build list of inherited associations
234
if (parentClass != null)
235         {
236             inheritedAssociations.putAll(parentClass.getAssociations());
237         }
238         
239         // Append list of defined associations
240
for (AssociationDefinition def : associations.values())
241         {
242             if (inheritedAssociations.containsKey(def.getName()))
243             {
244                 throw new DictionaryException("Duplicate association definition " + def.getName().toPrefixString() + " found in class hierarchy of " + name.toPrefixString());
245             }
246             inheritedAssociations.put(def.getName(), def);
247         }
248
249         // Derive Child Associations
250
for (AssociationDefinition def : inheritedAssociations.values())
251         {
252             if (def instanceof ChildAssociationDefinition)
253             {
254                 inheritedChildAssociations.put(def.getName(), (ChildAssociationDefinition)def);
255             }
256         }
257         
258         // Build list of inherited default aspects
259
if (parentClass != null)
260         {
261             inheritedDefaultAspects.addAll(parentClass.getDefaultAspects());
262         }
263         
264         // Append list of defined default aspects
265
for (AspectDefinition def : defaultAspects)
266         {
267             if (!inheritedDefaultAspects.contains(def))
268             {
269                 inheritedDefaultAspects.add(def);
270             }
271         }
272     }
273     
274     /* (non-Javadoc)
275      * @see org.alfresco.service.cmr.dictionary.ClassDefinition#getModel()
276      */

277     public ModelDefinition getModel()
278     {
279         return model;
280     }
281
282     /* (non-Javadoc)
283      * @see org.alfresco.repo.dictionary.ClassDefinition#getName()
284      */

285     public QName getName()
286     {
287         return name;
288     }
289
290     /* (non-Javadoc)
291      * @see org.alfresco.repo.dictionary.ClassDefinition#getTitle()
292      */

293     public String JavaDoc getTitle()
294     {
295         String JavaDoc value = M2Label.getLabel(model, "class", name, "title");
296         if (value == null)
297         {
298             value = m2Class.getTitle();
299         }
300         return value;
301     }
302
303     /* (non-Javadoc)
304      * @see org.alfresco.repo.dictionary.ClassDefinition#getDescription()
305      */

306     public String JavaDoc getDescription()
307     {
308         String JavaDoc value = M2Label.getLabel(model, "class", name, "description");
309         if (value == null)
310         {
311             value = m2Class.getDescription();
312         }
313         return value;
314     }
315     
316     /* (non-Javadoc)
317      * @see org.alfresco.repo.dictionary.ClassDefinition#isAspect()
318      */

319     public boolean isAspect()
320     {
321         return (m2Class instanceof M2Aspect);
322     }
323
324     /* (non-Javadoc)
325      * @see org.alfresco.repo.dictionary.ClassDefinition#getParentName()
326      */

327     public QName getParentName()
328     {
329         return parentName;
330     }
331     
332     /* (non-Javadoc)
333      * @see org.alfresco.repo.dictionary.ClassDefinition#getProperties()
334      */

335     public Map JavaDoc<QName, PropertyDefinition> getProperties()
336     {
337         return Collections.unmodifiableMap(inheritedProperties);
338     }
339  
340     /**
341      * @see org.alfresco.service.cmr.dictionary.ClassDefinition#getDefaultValues()
342      */

343     public Map JavaDoc<QName, Serializable JavaDoc> getDefaultValues()
344     {
345         Map JavaDoc<QName, Serializable JavaDoc> result = new HashMap JavaDoc<QName, Serializable JavaDoc>(5);
346         
347         for(Map.Entry JavaDoc<QName, PropertyDefinition> entry : inheritedProperties.entrySet())
348         {
349             PropertyDefinition propertyDefinition = entry.getValue();
350             String JavaDoc defaultValue = propertyDefinition.getDefaultValue();
351             if (defaultValue != null)
352             {
353                 result.put(entry.getKey(), defaultValue);
354             }
355         }
356         
357         return Collections.unmodifiableMap(result);
358     }
359
360     /* (non-Javadoc)
361      * @see org.alfresco.repo.dictionary.ClassDefinition#getAssociations()
362      */

363     public Map JavaDoc<QName, AssociationDefinition> getAssociations()
364     {
365         return Collections.unmodifiableMap(inheritedAssociations);
366     }
367     
368     /**
369      * @see org.alfresco.service.cmr.dictionary.ClassDefinition#getDefaultAspects()
370      */

371     public List JavaDoc<AspectDefinition> getDefaultAspects()
372     {
373         return inheritedDefaultAspects;
374     }
375
376     /* (non-Javadoc)
377      * @see org.alfresco.service.cmr.dictionary.ClassDefinition#isContainer()
378      */

379     public boolean isContainer()
380     {
381         return !inheritedChildAssociations.isEmpty();
382     }
383     
384     /* (non-Javadoc)
385      * @see org.alfresco.repo.dictionary.ClassDefinition#getChildAssociations()
386      */

387     public Map JavaDoc<QName, ChildAssociationDefinition> getChildAssociations()
388     {
389         return Collections.unmodifiableMap(inheritedChildAssociations);
390     }
391
392     @Override JavaDoc
393     public int hashCode()
394     {
395         return name.hashCode();
396     }
397
398     @Override JavaDoc
399     public boolean equals(Object JavaDoc obj)
400     {
401         if (!(obj instanceof M2ClassDefinition))
402         {
403             return false;
404         }
405         return name.equals(((M2ClassDefinition)obj).name);
406     }
407
408 }
409
Popular Tags