KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.service.cmr.dictionary.AspectDefinition;
25 import org.alfresco.service.cmr.dictionary.AssociationDefinition;
26 import org.alfresco.service.cmr.dictionary.ClassDefinition;
27 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
28 import org.alfresco.service.cmr.dictionary.DictionaryException;
29 import org.alfresco.service.cmr.dictionary.ModelDefinition;
30 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
31 import org.alfresco.service.cmr.dictionary.TypeDefinition;
32 import org.alfresco.service.namespace.QName;
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36
37 /**
38  * Default implementation of the Dictionary.
39  *
40  * @author David Caruana
41  *
42  */

43 public class DictionaryDAOImpl implements DictionaryDAO
44 {
45     // TODO: Allow for the dynamic creation of models. Supporting
46
// this requires the ability to persistently store the
47
// registration of models, the ability to load models
48
// from a persistent store, the refresh of the cache
49
// and concurrent read/write of the models.
50

51     // Namespace Data Access
52
private NamespaceDAO namespaceDAO;
53
54     // Map of namespace to model name
55
private Map JavaDoc<String JavaDoc,QName> namespaceToModel = new HashMap JavaDoc<String JavaDoc,QName>();
56     
57     // Map of model name to compiled model
58
private Map JavaDoc<QName,CompiledModel> compiledModels = new HashMap JavaDoc<QName,CompiledModel>();
59
60     // Logger
61
private static Log logger = LogFactory.getLog(DictionaryDAO.class);
62
63
64     /**
65      * Construct
66      *
67      * @param namespaceDAO namespace data access
68      */

69     public DictionaryDAOImpl(NamespaceDAO namespaceDAO)
70     {
71         this.namespaceDAO = namespaceDAO;
72     }
73     
74     
75     /* (non-Javadoc)
76      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#putModel(org.alfresco.repo.dictionary.impl.M2Model)
77      */

78     public void putModel(M2Model model)
79     {
80         // Compile model definition
81
CompiledModel compiledModel = model.compile(this, namespaceDAO);
82         QName modelName = compiledModel.getModelDefinition().getName();
83
84         // Remove namespace definitions for previous model, if it exists
85
CompiledModel previousVersion = compiledModels.get(modelName);
86         if (previousVersion != null)
87         {
88             for (M2Namespace namespace : previousVersion.getM2Model().getNamespaces())
89             {
90                 namespaceDAO.removePrefix(namespace.getPrefix());
91                 namespaceDAO.removeURI(namespace.getUri());
92                 namespaceToModel.remove(namespace.getUri());
93             }
94         }
95         
96         // Create namespace definitions for new model
97
for (M2Namespace namespace : model.getNamespaces())
98         {
99             namespaceDAO.addURI(namespace.getUri());
100             namespaceDAO.addPrefix(namespace.getPrefix(), namespace.getUri());
101             namespaceToModel.put(namespace.getUri(), modelName);
102         }
103         
104         // Publish new Model Definition
105
compiledModels.put(modelName, compiledModel);
106
107         if (logger.isInfoEnabled())
108         {
109             logger.info("Registered model " + modelName.toPrefixString(namespaceDAO));
110             for (M2Namespace namespace : model.getNamespaces())
111             {
112                 logger.info("Registered namespace '" + namespace.getUri() + "' (prefix '" + namespace.getPrefix() + "')");
113             }
114         }
115     }
116     
117     /**
118      * @see org.alfresco.repo.dictionary.DictionaryDAO#removeModel(org.alfresco.service.namespace.QName)
119      */

120     public void removeModel(QName modelName)
121     {
122         CompiledModel compiledModel = this.compiledModels.get(modelName);
123         if (compiledModel != null)
124         {
125             // Remove the namespaces from the namespace service
126
M2Model model = compiledModel.getM2Model();
127             for (M2Namespace namespace : model.getNamespaces())
128             {
129                 namespaceDAO.removePrefix(namespace.getPrefix());
130                 namespaceDAO.removeURI(namespace.getUri());
131                 namespaceToModel.remove(namespace.getUri());
132             }
133             
134             // Remove the model from the list
135
this.compiledModels.remove(modelName);
136         }
137     }
138
139     
140     /**
141      * @param uri the namespace uri
142      * @return the compiled model which defines the specified namespace
143      */

144     private CompiledModel getCompiledModelForNamespace(String JavaDoc uri)
145     {
146         QName modelName = namespaceToModel.get(uri);
147         return (modelName == null) ? null : getCompiledModel(modelName);
148     }
149     
150
151     /**
152      * @param modelName the model name
153      * @return the compiled model of the given name
154      */

155     private CompiledModel getCompiledModel(QName modelName)
156     {
157         CompiledModel model = compiledModels.get(modelName);
158         if (model == null)
159         {
160             // TODO: Load model from persistent store
161
throw new DictionaryException("Model " + modelName + " does not exist");
162         }
163         return model;
164     }
165     
166     
167     /* (non-Javadoc)
168      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getPropertyType(org.alfresco.repo.ref.QName)
169      */

170     public DataTypeDefinition getDataType(QName typeName)
171     {
172         CompiledModel model = getCompiledModelForNamespace(typeName.getNamespaceURI());
173         return (model == null) ? null : model.getDataType(typeName);
174     }
175
176
177     /* (non-Javadoc)
178      * @see org.alfresco.repo.dictionary.ModelQuery#getDataType(java.lang.Class)
179      */

180     public DataTypeDefinition getDataType(Class JavaDoc javaClass)
181     {
182         for (CompiledModel model : compiledModels.values())
183         {
184             DataTypeDefinition dataTypeDef = model.getDataType(javaClass);
185             if (dataTypeDef != null)
186             {
187                 return dataTypeDef;
188             }
189         }
190         return null;
191     }
192
193
194     /* (non-Javadoc)
195      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getPropertyTypes(org.alfresco.repo.ref.QName)
196      */

197     public Collection JavaDoc<DataTypeDefinition> getDataTypes(QName modelName)
198     {
199         CompiledModel model = getCompiledModel(modelName);
200         return model.getDataTypes();
201     }
202
203
204     /* (non-Javadoc)
205      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getType(org.alfresco.repo.ref.QName)
206      */

207     public TypeDefinition getType(QName typeName)
208     {
209         CompiledModel model = getCompiledModelForNamespace(typeName.getNamespaceURI());
210         return (model == null) ? null : model.getType(typeName);
211     }
212
213
214     /* (non-Javadoc)
215      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getAspect(org.alfresco.repo.ref.QName)
216      */

217     public AspectDefinition getAspect(QName aspectName)
218     {
219         CompiledModel model = getCompiledModelForNamespace(aspectName.getNamespaceURI());
220         return (model == null) ? null : model.getAspect(aspectName);
221     }
222
223
224     /* (non-Javadoc)
225      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getClass(org.alfresco.repo.ref.QName)
226      */

227     public ClassDefinition getClass(QName className)
228     {
229         CompiledModel model = getCompiledModelForNamespace(className.getNamespaceURI());
230         return (model == null) ? null : model.getClass(className);
231     }
232
233     
234     /* (non-Javadoc)
235      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getProperty(org.alfresco.repo.ref.QName)
236      */

237     public PropertyDefinition getProperty(QName propertyName)
238     {
239         CompiledModel model = getCompiledModelForNamespace(propertyName.getNamespaceURI());
240         return (model == null) ? null : model.getProperty(propertyName);
241     }
242     
243     
244     /* (non-Javadoc)
245      * @see org.alfresco.repo.dictionary.impl.ModelQuery#getAssociation(org.alfresco.repo.ref.QName)
246      */

247     public AssociationDefinition getAssociation(QName assocName)
248     {
249         CompiledModel model = getCompiledModelForNamespace(assocName.getNamespaceURI());
250         return (model == null) ? null : model.getAssociation(assocName);
251     }
252
253     
254     /* (non-Javadoc)
255      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getModels()
256      */

257     public Collection JavaDoc<QName> getModels()
258     {
259         return compiledModels.keySet();
260     }
261
262
263     /* (non-Javadoc)
264      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getModel(org.alfresco.repo.ref.QName)
265      */

266     public ModelDefinition getModel(QName name)
267     {
268         CompiledModel model = getCompiledModel(name);
269         if (model != null)
270         {
271             return model.getModelDefinition();
272         }
273         return null;
274     }
275
276
277     /* (non-Javadoc)
278      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getTypes(org.alfresco.repo.ref.QName)
279      */

280     public Collection JavaDoc<TypeDefinition> getTypes(QName modelName)
281     {
282         CompiledModel model = getCompiledModel(modelName);
283         return model.getTypes();
284     }
285
286
287     /* (non-Javadoc)
288      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getAspects(org.alfresco.repo.ref.QName)
289      */

290     public Collection JavaDoc<AspectDefinition> getAspects(QName modelName)
291     {
292         CompiledModel model = getCompiledModel(modelName);
293         return model.getAspects();
294     }
295
296
297     /* (non-Javadoc)
298      * @see org.alfresco.repo.dictionary.impl.DictionaryDAO#getAnonymousType(org.alfresco.repo.ref.QName, java.util.Collection)
299      */

300     public TypeDefinition getAnonymousType(QName type, Collection JavaDoc<QName> aspects)
301     {
302         TypeDefinition typeDef = getType(type);
303         if (typeDef == null)
304         {
305             throw new DictionaryException("Failed to create anonymous type as specified type " + type + " not found");
306         }
307         Collection JavaDoc<AspectDefinition> aspectDefs = new ArrayList JavaDoc<AspectDefinition>();
308         if (aspects != null)
309         {
310             for (QName aspect : aspects)
311             {
312                 AspectDefinition aspectDef = getAspect(aspect);
313                 if (typeDef == null)
314                 {
315                     throw new DictionaryException("Failed to create anonymous type as specified aspect " + aspect + " not found");
316                 }
317                 aspectDefs.add(aspectDef);
318             }
319         }
320         return new M2AnonymousTypeDefinition(typeDef, aspectDefs);
321     }
322
323
324 }
325
Popular Tags