1 17 package org.alfresco.repo.dictionary; 18 19 import java.io.Serializable ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 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 42 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 <QName, M2PropertyOverride> propertyOverrides = new HashMap <QName, M2PropertyOverride>(); 50 private Map <QName, PropertyDefinition> properties = new HashMap <QName, PropertyDefinition>(); 51 private Map <QName, PropertyDefinition> inheritedProperties = new HashMap <QName, PropertyDefinition>(); 52 private Map <QName, AssociationDefinition> associations = new HashMap <QName, AssociationDefinition>(); 53 private Map <QName, AssociationDefinition> inheritedAssociations = new HashMap <QName, AssociationDefinition>(); 54 private Map <QName, ChildAssociationDefinition> inheritedChildAssociations = new HashMap <QName, ChildAssociationDefinition>(); 55 private List <AspectDefinition> defaultAspects = new ArrayList <AspectDefinition>(); 56 private List <QName> defaultAspectNames = new ArrayList <QName>(); 57 private List <AspectDefinition> inheritedDefaultAspects = new ArrayList <AspectDefinition>(); 58 59 60 68 M2ClassDefinition(ModelDefinition model, M2Class m2Class, NamespacePrefixResolver resolver, Map <QName, PropertyDefinition> modelProperties, Map <QName, AssociationDefinition> modelAssociations) 69 { 70 this.model = model; 71 this.m2Class = m2Class; 72 73 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 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 PropertyDefinition existingDef = modelProperties.get(def.getName()); 91 if (existingDef != null) 92 { 93 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 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 AssociationDefinition existingDef = modelAssociations.get(def.getName()); 121 if (existingDef != null) 122 { 123 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 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 for (String 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 159 public String toString() 160 { 161 StringBuilder sb = new StringBuilder (120); 162 sb.append("ClassDef ") 163 .append("[ name=").append(name) 164 .append("]"); 165 return sb.toString(); 166 } 167 168 169 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 void resolveInheritance(ModelQuery query) 202 { 203 ClassDefinition parentClass = (parentName == null) ? null : query.getClass(parentName); 205 206 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 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 if (parentClass != null) 235 { 236 inheritedAssociations.putAll(parentClass.getAssociations()); 237 } 238 239 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 for (AssociationDefinition def : inheritedAssociations.values()) 251 { 252 if (def instanceof ChildAssociationDefinition) 253 { 254 inheritedChildAssociations.put(def.getName(), (ChildAssociationDefinition)def); 255 } 256 } 257 258 if (parentClass != null) 260 { 261 inheritedDefaultAspects.addAll(parentClass.getDefaultAspects()); 262 } 263 264 for (AspectDefinition def : defaultAspects) 266 { 267 if (!inheritedDefaultAspects.contains(def)) 268 { 269 inheritedDefaultAspects.add(def); 270 } 271 } 272 } 273 274 277 public ModelDefinition getModel() 278 { 279 return model; 280 } 281 282 285 public QName getName() 286 { 287 return name; 288 } 289 290 293 public String getTitle() 294 { 295 String value = M2Label.getLabel(model, "class", name, "title"); 296 if (value == null) 297 { 298 value = m2Class.getTitle(); 299 } 300 return value; 301 } 302 303 306 public String getDescription() 307 { 308 String value = M2Label.getLabel(model, "class", name, "description"); 309 if (value == null) 310 { 311 value = m2Class.getDescription(); 312 } 313 return value; 314 } 315 316 319 public boolean isAspect() 320 { 321 return (m2Class instanceof M2Aspect); 322 } 323 324 327 public QName getParentName() 328 { 329 return parentName; 330 } 331 332 335 public Map <QName, PropertyDefinition> getProperties() 336 { 337 return Collections.unmodifiableMap(inheritedProperties); 338 } 339 340 343 public Map <QName, Serializable > getDefaultValues() 344 { 345 Map <QName, Serializable > result = new HashMap <QName, Serializable >(5); 346 347 for(Map.Entry <QName, PropertyDefinition> entry : inheritedProperties.entrySet()) 348 { 349 PropertyDefinition propertyDefinition = entry.getValue(); 350 String 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 363 public Map <QName, AssociationDefinition> getAssociations() 364 { 365 return Collections.unmodifiableMap(inheritedAssociations); 366 } 367 368 371 public List <AspectDefinition> getDefaultAspects() 372 { 373 return inheritedDefaultAspects; 374 } 375 376 379 public boolean isContainer() 380 { 381 return !inheritedChildAssociations.isEmpty(); 382 } 383 384 387 public Map <QName, ChildAssociationDefinition> getChildAssociations() 388 { 389 return Collections.unmodifiableMap(inheritedChildAssociations); 390 } 391 392 @Override 393 public int hashCode() 394 { 395 return name.hashCode(); 396 } 397 398 @Override 399 public boolean equals(Object 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 |