| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.datadictionary; |
| 17 | |
|
| 18 | |
import java.io.Serializable; |
| 19 | |
import java.util.ArrayList; |
| 20 | |
import java.util.LinkedHashMap; |
| 21 | |
import java.util.List; |
| 22 | |
import java.util.Map; |
| 23 | |
import java.util.Set; |
| 24 | |
|
| 25 | |
import org.apache.commons.lang.StringUtils; |
| 26 | |
import org.kuali.rice.krad.datadictionary.exception.DuplicateEntryException; |
| 27 | |
import org.kuali.rice.krad.exception.ValidationException; |
| 28 | |
import org.springframework.beans.BeanUtils; |
| 29 | |
import org.springframework.beans.factory.InitializingBean; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
abstract public class DataDictionaryEntryBase implements DataDictionaryEntry, Serializable, InitializingBean { |
| 37 | |
protected List<AttributeDefinition> attributes; |
| 38 | |
protected List<ComplexAttributeDefinition> complexAttributes; |
| 39 | |
protected List<CollectionDefinition> collections; |
| 40 | |
protected List<RelationshipDefinition> relationships; |
| 41 | |
protected Map<String, AttributeDefinition> attributeMap; |
| 42 | |
protected Map<String, ComplexAttributeDefinition> complexAttributeMap; |
| 43 | |
protected Map<String, CollectionDefinition> collectionMap; |
| 44 | |
protected Map<String, RelationshipDefinition> relationshipMap; |
| 45 | |
|
| 46 | 100 | public DataDictionaryEntryBase() { |
| 47 | 100 | this.attributes = new ArrayList<AttributeDefinition>(); |
| 48 | 100 | this.complexAttributes = new ArrayList<ComplexAttributeDefinition>(); |
| 49 | 100 | this.collections = new ArrayList<CollectionDefinition>(); |
| 50 | 100 | this.relationships = new ArrayList<RelationshipDefinition>(); |
| 51 | 100 | this.attributeMap = new LinkedHashMap<String, AttributeDefinition>(); |
| 52 | 100 | this.complexAttributeMap = new LinkedHashMap<String, ComplexAttributeDefinition>(); |
| 53 | 100 | this.collectionMap = new LinkedHashMap<String, CollectionDefinition>(); |
| 54 | 100 | this.relationshipMap = new LinkedHashMap<String, RelationshipDefinition>(); |
| 55 | 100 | } |
| 56 | |
|
| 57 | |
|
| 58 | |
public abstract Class<?> getEntryClass(); |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public AttributeDefinition getAttributeDefinition(String attributeName) { |
| 65 | 78 | if (StringUtils.isBlank(attributeName)) { |
| 66 | 0 | throw new IllegalArgumentException("invalid (blank) attributeName"); |
| 67 | |
} |
| 68 | 78 | return attributeMap.get(attributeName); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
public List<AttributeDefinition> getAttributes() { |
| 75 | 0 | return this.attributes; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
public List<ComplexAttributeDefinition> getComplexAttributes() { |
| 82 | 0 | return this.complexAttributes; |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public void setComplexAttributes( |
| 89 | |
List<ComplexAttributeDefinition> complexAttributes) { |
| 90 | 0 | complexAttributeMap.clear(); |
| 91 | 0 | for ( ComplexAttributeDefinition complexAttribute : complexAttributes ) { |
| 92 | 0 | if (complexAttribute == null) { |
| 93 | 0 | throw new IllegalArgumentException("invalid (null) complexAttributeDefinition"); |
| 94 | |
} |
| 95 | 0 | String complexAttributeName = complexAttribute.getName(); |
| 96 | 0 | if (StringUtils.isBlank(complexAttributeName)) { |
| 97 | 0 | throw new ValidationException("invalid (blank) collectionName"); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | if (complexAttributeMap.containsKey(complexAttribute)){ |
| 101 | 0 | throw new DuplicateEntryException("complex attribute '" + complexAttribute + "' already defined as an complex attribute for class '" + getEntryClass().getName() + "'"); |
| 102 | 0 | } else if (collectionMap.containsKey(complexAttributeName)) { |
| 103 | 0 | throw new DuplicateEntryException("complex attribute '" + complexAttributeName + "' already defined as a Collection for class '" + getEntryClass().getName() + "'"); |
| 104 | 0 | } else if (attributeMap.containsKey(complexAttributeName)) { |
| 105 | 0 | throw new DuplicateEntryException("complex attribute '" + complexAttributeName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'"); |
| 106 | |
} |
| 107 | |
|
| 108 | 0 | complexAttributeMap.put(complexAttributeName, complexAttribute); |
| 109 | |
|
| 110 | 0 | } |
| 111 | |
|
| 112 | 0 | this.complexAttributes = complexAttributes; |
| 113 | 0 | } |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
public CollectionDefinition getCollectionDefinition(String collectionName) { |
| 120 | 0 | if (StringUtils.isBlank(collectionName)) { |
| 121 | 0 | throw new IllegalArgumentException("invalid (blank) collectionName"); |
| 122 | |
} |
| 123 | 0 | return collectionMap.get(collectionName); |
| 124 | |
} |
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
public List<CollectionDefinition> getCollections() { |
| 130 | 0 | return this.collections; |
| 131 | |
} |
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
public RelationshipDefinition getRelationshipDefinition(String relationshipName) { |
| 138 | 0 | if (StringUtils.isBlank(relationshipName)) { |
| 139 | 0 | throw new IllegalArgumentException("invalid (blank) relationshipName"); |
| 140 | |
} |
| 141 | 0 | return relationshipMap.get(relationshipName); |
| 142 | |
} |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
public List<RelationshipDefinition> getRelationships() { |
| 148 | 0 | return this.relationships; |
| 149 | |
} |
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public void completeValidation() { |
| 156 | |
|
| 157 | 0 | for ( AttributeDefinition attributeDefinition : attributes ) { |
| 158 | 0 | attributeDefinition.completeValidation(getEntryClass(), null); |
| 159 | |
} |
| 160 | |
|
| 161 | 0 | for ( CollectionDefinition collectionDefinition : collections ) { |
| 162 | 0 | collectionDefinition.completeValidation(getEntryClass(), null); |
| 163 | |
} |
| 164 | |
|
| 165 | 0 | for ( RelationshipDefinition relationshipDefinition : relationships ) { |
| 166 | 0 | relationshipDefinition.completeValidation(getEntryClass(), null); |
| 167 | |
} |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | |
|
| 187 | |
|
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
public void setAttributes(List<AttributeDefinition> attributes) { |
| 220 | 94 | attributeMap.clear(); |
| 221 | 94 | for ( AttributeDefinition attribute : attributes ) { |
| 222 | 252 | if (attribute == null) { |
| 223 | 0 | throw new IllegalArgumentException("invalid (null) attributeDefinition"); |
| 224 | |
} |
| 225 | 252 | String attributeName = attribute.getName(); |
| 226 | 252 | if (StringUtils.isBlank(attributeName)) { |
| 227 | 0 | throw new ValidationException("invalid (blank) attributeName"); |
| 228 | |
} |
| 229 | |
|
| 230 | 252 | if (attributeMap.containsKey(attributeName)) { |
| 231 | 0 | throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'"); |
| 232 | 252 | } else if (collectionMap.containsKey(attributeName)) { |
| 233 | 0 | throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as a Collection for class '" + getEntryClass().getName() + "'"); |
| 234 | 252 | } else if (complexAttributeMap.containsKey(attributeName)){ |
| 235 | 0 | throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as an Complex Attribute for class '" + getEntryClass().getName() + "'"); |
| 236 | |
} |
| 237 | 252 | attributeMap.put(attributeName, attribute); |
| 238 | 252 | } |
| 239 | 94 | this.attributes = attributes; |
| 240 | 94 | } |
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
public void setCollections(List<CollectionDefinition> collections) { |
| 271 | 6 | collectionMap.clear(); |
| 272 | 6 | for ( CollectionDefinition collection : collections ) { |
| 273 | 6 | if (collection == null) { |
| 274 | 0 | throw new IllegalArgumentException("invalid (null) collectionDefinition"); |
| 275 | |
} |
| 276 | 6 | String collectionName = collection.getName(); |
| 277 | 6 | if (StringUtils.isBlank(collectionName)) { |
| 278 | 0 | throw new ValidationException("invalid (blank) collectionName"); |
| 279 | |
} |
| 280 | |
|
| 281 | 6 | if (collectionMap.containsKey(collectionName)) { |
| 282 | 0 | throw new DuplicateEntryException("collection '" + collectionName + "' already defined for class '" + getEntryClass().getName() + "'"); |
| 283 | 6 | } else if (attributeMap.containsKey(collectionName)) { |
| 284 | 0 | throw new DuplicateEntryException("collection '" + collectionName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'"); |
| 285 | 6 | } else if (complexAttributeMap.containsKey(collectionName)){ |
| 286 | 0 | throw new DuplicateEntryException("collection '" + collectionName + "' already defined as Complex Attribute for class '" + getEntryClass().getName() + "'"); |
| 287 | |
} |
| 288 | |
|
| 289 | 6 | collectionMap.put(collectionName, collection); |
| 290 | |
|
| 291 | 6 | } |
| 292 | 6 | this.collections = collections; |
| 293 | 6 | } |
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
|
| 316 | |
|
| 317 | |
|
| 318 | |
|
| 319 | |
|
| 320 | |
|
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | |
public void setRelationships(List<RelationshipDefinition> relationships) { |
| 327 | 0 | this.relationships = relationships; |
| 328 | 0 | } |
| 329 | |
|
| 330 | |
public Set<String> getCollectionNames() { |
| 331 | 0 | return collectionMap.keySet(); |
| 332 | |
} |
| 333 | |
|
| 334 | |
public Set<String> getAttributeNames() { |
| 335 | 0 | return attributeMap.keySet(); |
| 336 | |
} |
| 337 | |
|
| 338 | |
public Set<String> getRelationshipNames() { |
| 339 | 0 | return relationshipMap.keySet(); |
| 340 | |
} |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
public void afterPropertiesSet() throws Exception { |
| 348 | 0 | if ( relationships != null ) { |
| 349 | 0 | relationshipMap.clear(); |
| 350 | 0 | for ( RelationshipDefinition relationship : relationships ) { |
| 351 | 0 | if (relationship == null) { |
| 352 | 0 | throw new IllegalArgumentException("invalid (null) relationshipDefinition"); |
| 353 | |
} |
| 354 | 0 | String relationshipName = relationship.getObjectAttributeName(); |
| 355 | 0 | if (StringUtils.isBlank(relationshipName)) { |
| 356 | 0 | throw new ValidationException("invalid (blank) relationshipName"); |
| 357 | |
} |
| 358 | 0 | relationship.setSourceClass(getEntryClass()); |
| 359 | 0 | relationshipMap.put(relationshipName, relationship); |
| 360 | 0 | } |
| 361 | |
} |
| 362 | |
|
| 363 | |
|
| 364 | 0 | if (complexAttributes != null){ |
| 365 | 0 | for (ComplexAttributeDefinition complexAttribute:complexAttributes){ |
| 366 | 0 | addNestedAttributes(complexAttribute, complexAttribute.getName()); |
| 367 | |
} |
| 368 | |
} |
| 369 | 0 | } |
| 370 | |
|
| 371 | |
private void addNestedAttributes(ComplexAttributeDefinition complexAttribute, String attrPath){ |
| 372 | 0 | DataDictionaryEntryBase dataDictionaryEntry = (DataDictionaryEntryBase)complexAttribute.getDataObjectEntry(); |
| 373 | |
|
| 374 | |
|
| 375 | 0 | for (AttributeDefinition attribute:dataDictionaryEntry.getAttributes()){ |
| 376 | 0 | String nestedAttributeName = attrPath + "." + attribute.getName(); |
| 377 | 0 | AttributeDefinition nestedAttribute = copyAttributeDefinition(attribute); |
| 378 | 0 | nestedAttribute.setName(nestedAttributeName); |
| 379 | |
|
| 380 | 0 | if (!attributeMap.containsKey(nestedAttributeName)){ |
| 381 | 0 | this.attributes.add(nestedAttribute); |
| 382 | 0 | this.attributeMap.put(nestedAttributeName, nestedAttribute); |
| 383 | |
} |
| 384 | 0 | } |
| 385 | |
|
| 386 | |
|
| 387 | 0 | List<ComplexAttributeDefinition> nestedComplexAttributes = dataDictionaryEntry.getComplexAttributes(); |
| 388 | 0 | if (nestedComplexAttributes != null){ |
| 389 | 0 | for (ComplexAttributeDefinition nestedComplexAttribute:nestedComplexAttributes){ |
| 390 | 0 | addNestedAttributes(nestedComplexAttribute, attrPath + "." + nestedComplexAttribute.getName()); |
| 391 | |
} |
| 392 | |
} |
| 393 | 0 | } |
| 394 | |
|
| 395 | |
private AttributeDefinition copyAttributeDefinition(AttributeDefinition attrDefToCopy){ |
| 396 | 0 | AttributeDefinition attrDefCopy = new AttributeDefinition(); |
| 397 | |
|
| 398 | |
try { |
| 399 | 0 | BeanUtils.copyProperties(attrDefToCopy, attrDefCopy, new String[] { "formatterClass" }); |
| 400 | |
|
| 401 | |
|
| 402 | 0 | attrDefCopy.setRequired(attrDefToCopy.isRequired()); |
| 403 | |
|
| 404 | 0 | } catch (Exception e) { |
| 405 | 0 | e.printStackTrace(); |
| 406 | 0 | } |
| 407 | |
|
| 408 | 0 | return attrDefCopy; |
| 409 | |
} |
| 410 | |
} |