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 public DataDictionaryEntryBase() {
47 this.attributes = new ArrayList<AttributeDefinition>();
48 this.complexAttributes = new ArrayList<ComplexAttributeDefinition>();
49 this.collections = new ArrayList<CollectionDefinition>();
50 this.relationships = new ArrayList<RelationshipDefinition>();
51 this.attributeMap = new LinkedHashMap<String, AttributeDefinition>();
52 this.complexAttributeMap = new LinkedHashMap<String, ComplexAttributeDefinition>();
53 this.collectionMap = new LinkedHashMap<String, CollectionDefinition>();
54 this.relationshipMap = new LinkedHashMap<String, RelationshipDefinition>();
55 }
56
57
58 public abstract Class<?> getEntryClass();
59
60
61
62
63
64 public AttributeDefinition getAttributeDefinition(String attributeName) {
65 if (StringUtils.isBlank(attributeName)) {
66 throw new IllegalArgumentException("invalid (blank) attributeName");
67 }
68 return attributeMap.get(attributeName);
69 }
70
71
72
73
74 public List<AttributeDefinition> getAttributes() {
75 return this.attributes;
76 }
77
78
79
80
81 public List<ComplexAttributeDefinition> getComplexAttributes() {
82 return this.complexAttributes;
83 }
84
85
86
87
88 public void setComplexAttributes(
89 List<ComplexAttributeDefinition> complexAttributes) {
90 complexAttributeMap.clear();
91 for ( ComplexAttributeDefinition complexAttribute : complexAttributes ) {
92 if (complexAttribute == null) {
93 throw new IllegalArgumentException("invalid (null) complexAttributeDefinition");
94 }
95 String complexAttributeName = complexAttribute.getName();
96 if (StringUtils.isBlank(complexAttributeName)) {
97 throw new ValidationException("invalid (blank) collectionName");
98 }
99
100 if (complexAttributeMap.containsKey(complexAttribute)){
101 throw new DuplicateEntryException("complex attribute '" + complexAttribute + "' already defined as an complex attribute for class '" + getEntryClass().getName() + "'");
102 } else if (collectionMap.containsKey(complexAttributeName)) {
103 throw new DuplicateEntryException("complex attribute '" + complexAttributeName + "' already defined as a Collection for class '" + getEntryClass().getName() + "'");
104 } else if (attributeMap.containsKey(complexAttributeName)) {
105 throw new DuplicateEntryException("complex attribute '" + complexAttributeName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'");
106 }
107
108 complexAttributeMap.put(complexAttributeName, complexAttribute);
109
110 }
111
112 this.complexAttributes = complexAttributes;
113 }
114
115
116
117
118
119 public CollectionDefinition getCollectionDefinition(String collectionName) {
120 if (StringUtils.isBlank(collectionName)) {
121 throw new IllegalArgumentException("invalid (blank) collectionName");
122 }
123 return collectionMap.get(collectionName);
124 }
125
126
127
128
129 public List<CollectionDefinition> getCollections() {
130 return this.collections;
131 }
132
133
134
135
136
137 public RelationshipDefinition getRelationshipDefinition(String relationshipName) {
138 if (StringUtils.isBlank(relationshipName)) {
139 throw new IllegalArgumentException("invalid (blank) relationshipName");
140 }
141 return relationshipMap.get(relationshipName);
142 }
143
144
145
146
147 public List<RelationshipDefinition> getRelationships() {
148 return this.relationships;
149 }
150
151
152
153
154
155 public void completeValidation() {
156
157 for ( AttributeDefinition attributeDefinition : attributes ) {
158 attributeDefinition.completeValidation(getEntryClass(), null);
159 }
160
161 for ( CollectionDefinition collectionDefinition : collections ) {
162 collectionDefinition.completeValidation(getEntryClass(), null);
163 }
164
165 for ( RelationshipDefinition relationshipDefinition : relationships ) {
166 relationshipDefinition.completeValidation(getEntryClass(), null);
167 }
168 }
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 attributeMap.clear();
221 for ( AttributeDefinition attribute : attributes ) {
222 if (attribute == null) {
223 throw new IllegalArgumentException("invalid (null) attributeDefinition");
224 }
225 String attributeName = attribute.getName();
226 if (StringUtils.isBlank(attributeName)) {
227 throw new ValidationException("invalid (blank) attributeName");
228 }
229
230 if (attributeMap.containsKey(attributeName)) {
231 throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'");
232 } else if (collectionMap.containsKey(attributeName)) {
233 throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as a Collection for class '" + getEntryClass().getName() + "'");
234 } else if (complexAttributeMap.containsKey(attributeName)){
235 throw new DuplicateEntryException("attribute '" + attributeName + "' already defined as an Complex Attribute for class '" + getEntryClass().getName() + "'");
236 }
237 attributeMap.put(attributeName, attribute);
238 }
239 this.attributes = attributes;
240 }
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 collectionMap.clear();
272 for ( CollectionDefinition collection : collections ) {
273 if (collection == null) {
274 throw new IllegalArgumentException("invalid (null) collectionDefinition");
275 }
276 String collectionName = collection.getName();
277 if (StringUtils.isBlank(collectionName)) {
278 throw new ValidationException("invalid (blank) collectionName");
279 }
280
281 if (collectionMap.containsKey(collectionName)) {
282 throw new DuplicateEntryException("collection '" + collectionName + "' already defined for class '" + getEntryClass().getName() + "'");
283 } else if (attributeMap.containsKey(collectionName)) {
284 throw new DuplicateEntryException("collection '" + collectionName + "' already defined as an Attribute for class '" + getEntryClass().getName() + "'");
285 } else if (complexAttributeMap.containsKey(collectionName)){
286 throw new DuplicateEntryException("collection '" + collectionName + "' already defined as Complex Attribute for class '" + getEntryClass().getName() + "'");
287 }
288
289 collectionMap.put(collectionName, collection);
290
291 }
292 this.collections = collections;
293 }
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 this.relationships = relationships;
328 }
329
330 public Set<String> getCollectionNames() {
331 return collectionMap.keySet();
332 }
333
334 public Set<String> getAttributeNames() {
335 return attributeMap.keySet();
336 }
337
338 public Set<String> getRelationshipNames() {
339 return relationshipMap.keySet();
340 }
341
342
343
344
345
346
347 public void afterPropertiesSet() throws Exception {
348 if ( relationships != null ) {
349 relationshipMap.clear();
350 for ( RelationshipDefinition relationship : relationships ) {
351 if (relationship == null) {
352 throw new IllegalArgumentException("invalid (null) relationshipDefinition");
353 }
354 String relationshipName = relationship.getObjectAttributeName();
355 if (StringUtils.isBlank(relationshipName)) {
356 throw new ValidationException("invalid (blank) relationshipName");
357 }
358 relationship.setSourceClass(getEntryClass());
359 relationshipMap.put(relationshipName, relationship);
360 }
361 }
362
363
364 if (complexAttributes != null){
365 for (ComplexAttributeDefinition complexAttribute:complexAttributes){
366 addNestedAttributes(complexAttribute, complexAttribute.getName());
367 }
368 }
369 }
370
371 private void addNestedAttributes(ComplexAttributeDefinition complexAttribute, String attrPath){
372 DataDictionaryEntryBase dataDictionaryEntry = (DataDictionaryEntryBase)complexAttribute.getDataObjectEntry();
373
374
375 for (AttributeDefinition attribute:dataDictionaryEntry.getAttributes()){
376 String nestedAttributeName = attrPath + "." + attribute.getName();
377 AttributeDefinition nestedAttribute = copyAttributeDefinition(attribute);
378 nestedAttribute.setName(nestedAttributeName);
379
380 if (!attributeMap.containsKey(nestedAttributeName)){
381 this.attributes.add(nestedAttribute);
382 this.attributeMap.put(nestedAttributeName, nestedAttribute);
383 }
384 }
385
386
387 List<ComplexAttributeDefinition> nestedComplexAttributes = dataDictionaryEntry.getComplexAttributes();
388 if (nestedComplexAttributes != null){
389 for (ComplexAttributeDefinition nestedComplexAttribute:nestedComplexAttributes){
390 addNestedAttributes(nestedComplexAttribute, attrPath + "." + nestedComplexAttribute.getName());
391 }
392 }
393 }
394
395 private AttributeDefinition copyAttributeDefinition(AttributeDefinition attrDefToCopy){
396 AttributeDefinition attrDefCopy = new AttributeDefinition();
397
398 try {
399 BeanUtils.copyProperties(attrDefToCopy, attrDefCopy, new String[] { "formatterClass" });
400
401
402 attrDefCopy.setRequired(attrDefToCopy.isRequired());
403
404 } catch (Exception e) {
405 e.printStackTrace();
406 }
407
408 return attrDefCopy;
409 }
410 }