1 17 package org.alfresco.repo.dictionary; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Map ; 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 43 public class DictionaryDAOImpl implements DictionaryDAO 44 { 45 51 private NamespaceDAO namespaceDAO; 53 54 private Map <String ,QName> namespaceToModel = new HashMap <String ,QName>(); 56 57 private Map <QName,CompiledModel> compiledModels = new HashMap <QName,CompiledModel>(); 59 60 private static Log logger = LogFactory.getLog(DictionaryDAO.class); 62 63 64 69 public DictionaryDAOImpl(NamespaceDAO namespaceDAO) 70 { 71 this.namespaceDAO = namespaceDAO; 72 } 73 74 75 78 public void putModel(M2Model model) 79 { 80 CompiledModel compiledModel = model.compile(this, namespaceDAO); 82 QName modelName = compiledModel.getModelDefinition().getName(); 83 84 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 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 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 120 public void removeModel(QName modelName) 121 { 122 CompiledModel compiledModel = this.compiledModels.get(modelName); 123 if (compiledModel != null) 124 { 125 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 this.compiledModels.remove(modelName); 136 } 137 } 138 139 140 144 private CompiledModel getCompiledModelForNamespace(String uri) 145 { 146 QName modelName = namespaceToModel.get(uri); 147 return (modelName == null) ? null : getCompiledModel(modelName); 148 } 149 150 151 155 private CompiledModel getCompiledModel(QName modelName) 156 { 157 CompiledModel model = compiledModels.get(modelName); 158 if (model == null) 159 { 160 throw new DictionaryException("Model " + modelName + " does not exist"); 162 } 163 return model; 164 } 165 166 167 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 180 public DataTypeDefinition getDataType(Class 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 197 public Collection <DataTypeDefinition> getDataTypes(QName modelName) 198 { 199 CompiledModel model = getCompiledModel(modelName); 200 return model.getDataTypes(); 201 } 202 203 204 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 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 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 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 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 257 public Collection <QName> getModels() 258 { 259 return compiledModels.keySet(); 260 } 261 262 263 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 280 public Collection <TypeDefinition> getTypes(QName modelName) 281 { 282 CompiledModel model = getCompiledModel(modelName); 283 return model.getTypes(); 284 } 285 286 287 290 public Collection <AspectDefinition> getAspects(QName modelName) 291 { 292 CompiledModel model = getCompiledModel(modelName); 293 return model.getAspects(); 294 } 295 296 297 300 public TypeDefinition getAnonymousType(QName type, Collection <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 <AspectDefinition> aspectDefs = new ArrayList <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 |