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.List ; 23 import java.util.Map ; 24 import java.util.TreeMap ; 25 26 import org.alfresco.service.cmr.dictionary.AspectDefinition; 27 import org.alfresco.service.cmr.dictionary.AssociationDefinition; 28 import org.alfresco.service.cmr.dictionary.ClassDefinition; 29 import org.alfresco.service.cmr.dictionary.DictionaryException; 30 import org.alfresco.service.cmr.dictionary.ModelDefinition; 31 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 32 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 33 import org.alfresco.service.cmr.dictionary.TypeDefinition; 34 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver; 35 import org.alfresco.service.namespace.NamespaceException; 36 import org.alfresco.service.namespace.NamespacePrefixResolver; 37 import org.alfresco.service.namespace.QName; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 42 55 class CompiledModel implements ModelQuery 56 { 57 58 private static final Log logger = LogFactory.getLog(DictionaryDAOImpl.class); 60 61 private M2Model model; 62 private ModelDefinition modelDefinition; 63 private Map <QName, DataTypeDefinition> dataTypes = new HashMap <QName, DataTypeDefinition>(); 64 private Map <QName, ClassDefinition> classes = new HashMap <QName, ClassDefinition>(); 65 private Map <QName, TypeDefinition> types = new HashMap <QName, TypeDefinition>(); 66 private Map <QName, AspectDefinition> aspects = new HashMap <QName, AspectDefinition>(); 67 private Map <QName, PropertyDefinition> properties = new HashMap <QName, PropertyDefinition>(); 68 private Map <QName, AssociationDefinition> associations = new HashMap <QName, AssociationDefinition>(); 69 70 71 78 CompiledModel(M2Model model, DictionaryDAO dictionaryDAO, NamespaceDAO namespaceDAO) 79 { 80 try 81 { 82 this.model = model; 85 constructDefinitions(model, dictionaryDAO, namespaceDAO); 86 87 ModelQuery query = new DelegateModelQuery(this, dictionaryDAO); 89 resolveDependencies(query); 90 91 resolveInheritance(query); 93 } 94 catch(Exception e) 95 { 96 throw new DictionaryException("Failed to compile model " + model.getName(), e); 97 } 98 } 99 100 101 104 M2Model getM2Model() 105 { 106 return model; 107 } 108 109 110 117 private void constructDefinitions(M2Model model, DictionaryDAO dictionaryDAO, NamespaceDAO namespaceDAO) 118 { 119 NamespacePrefixResolver localPrefixes = createLocalPrefixResolver(model, namespaceDAO); 120 121 modelDefinition = new M2ModelDefinition(model, localPrefixes); 123 124 for (M2DataType propType : model.getPropertyTypes()) 126 { 127 M2DataTypeDefinition def = new M2DataTypeDefinition(modelDefinition, propType, localPrefixes); 128 if (dataTypes.containsKey(def.getName())) 129 { 130 throw new DictionaryException("Found duplicate property type definition " + propType.getName()); 131 } 132 dataTypes.put(def.getName(), def); 133 } 134 135 for (M2Type type : model.getTypes()) 137 { 138 M2TypeDefinition def = new M2TypeDefinition(modelDefinition, type, localPrefixes, properties, associations); 139 if (classes.containsKey(def.getName())) 140 { 141 throw new DictionaryException("Found duplicate class definition " + type.getName() + " (a type)"); 142 } 143 classes.put(def.getName(), def); 144 types.put(def.getName(), def); 145 } 146 147 for (M2Aspect aspect : model.getAspects()) 149 { 150 M2AspectDefinition def = new M2AspectDefinition(modelDefinition, aspect, localPrefixes, properties, associations); 151 if (classes.containsKey(def.getName())) 152 { 153 throw new DictionaryException("Found duplicate class definition " + aspect.getName() + " (an aspect)"); 154 } 155 classes.put(def.getName(), def); 156 aspects.put(def.getName(), def); 157 } 158 } 159 160 161 169 private NamespacePrefixResolver createLocalPrefixResolver(M2Model model, NamespaceDAO namespaceDAO) 170 { 171 Collection <String > uris = namespaceDAO.getURIs(); 173 174 DynamicNamespacePrefixResolver prefixResolver = new DynamicNamespacePrefixResolver(null); 177 for (M2Namespace imported : model.getImports()) 178 { 179 String uri = imported.getUri(); 180 if (!uris.contains(uri)) 181 { 182 throw new NamespaceException("URI " + uri + " cannot be imported as it is not defined (with prefix " + imported.getPrefix()); 183 } 184 prefixResolver.registerNamespace(imported.getPrefix(), uri); 185 } 186 for (M2Namespace defined : model.getNamespaces()) 187 { 188 prefixResolver.registerNamespace(defined.getPrefix(), defined.getUri()); 189 } 190 return prefixResolver; 191 } 192 193 194 199 private void resolveDependencies(ModelQuery query) 200 { 201 for (DataTypeDefinition def : dataTypes.values()) 202 { 203 ((M2DataTypeDefinition)def).resolveDependencies(query); 204 } 205 for (ClassDefinition def : classes.values()) 206 { 207 ((M2ClassDefinition)def).resolveDependencies(query); 208 } 209 } 210 211 212 217 private void resolveInheritance(ModelQuery query) 218 { 219 Map <Integer ,List <ClassDefinition>> order = new TreeMap <Integer ,List <ClassDefinition>>(); 221 for (ClassDefinition def : classes.values()) 222 { 223 int depth = 0; 225 QName parentName = def.getParentName(); 226 while (parentName != null) 227 { 228 ClassDefinition parentClass = getClass(parentName); 229 if (parentClass == null) 230 { 231 break; 232 } 233 depth = depth +1; 234 parentName = parentClass.getParentName(); 235 } 236 237 List <ClassDefinition> classes = order.get(depth); 239 if (classes == null) 240 { 241 classes = new ArrayList <ClassDefinition>(); 242 order.put(depth, classes); 243 } 244 classes.add(def); 245 246 if (logger.isDebugEnabled()) 247 logger.debug("Resolving inheritance: class " + def.getName() + " found at depth " + depth); 248 } 249 250 for (int depth = 0; depth < order.size(); depth++) 252 { 253 for (ClassDefinition def : order.get(depth)) 254 { 255 ((M2ClassDefinition)def).resolveInheritance(query); 256 } 257 } 258 } 259 260 261 264 public ModelDefinition getModelDefinition() 265 { 266 return modelDefinition; 267 } 268 269 270 273 public Collection <DataTypeDefinition> getDataTypes() 274 { 275 return dataTypes.values(); 276 } 277 278 279 282 public Collection <TypeDefinition> getTypes() 283 { 284 return types.values(); 285 } 286 287 288 291 public Collection <AspectDefinition> getAspects() 292 { 293 return aspects.values(); 294 } 295 296 297 300 public DataTypeDefinition getDataType(QName name) 301 { 302 return dataTypes.get(name); 303 } 304 305 306 309 public DataTypeDefinition getDataType(Class javaClass) 310 { 311 for (DataTypeDefinition dataTypeDef : dataTypes.values()) 312 { 313 if (dataTypeDef.getJavaClassName().equals(javaClass.getName())) 314 { 315 return dataTypeDef; 316 } 317 } 318 return null; 319 } 320 321 322 325 public TypeDefinition getType(QName name) 326 { 327 return types.get(name); 328 } 329 330 331 334 public AspectDefinition getAspect(QName name) 335 { 336 return aspects.get(name); 337 } 338 339 340 343 public ClassDefinition getClass(QName name) 344 { 345 return classes.get(name); 346 } 347 348 349 352 public PropertyDefinition getProperty(QName name) 353 { 354 return properties.get(name); 355 } 356 357 358 361 public AssociationDefinition getAssociation(QName name) 362 { 363 return associations.get(name); 364 } 365 366 } 367 | Popular Tags |