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 org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
20 import org.kuali.rice.krad.datadictionary.parse.BeanTag;
21 import org.kuali.rice.krad.datadictionary.parse.BeanTagAttribute;
22 import org.kuali.rice.krad.datadictionary.validator.ValidationTrace;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 @BeanTag(name = "relationshipDefinition-bean")
50 public class RelationshipDefinition extends DataDictionaryDefinitionBase {
51 private static final long serialVersionUID = 2946722646095412576L;
52
53 protected String objectAttributeName;
54 protected Class<?> sourceClass;
55
56
57
58
59
60
61 protected Class<?> targetClass;
62
63 protected List<PrimitiveAttributeDefinition> primitiveAttributes = new ArrayList<PrimitiveAttributeDefinition>();
64
65 protected List<SupportAttributeDefinition> supportAttributes = new ArrayList<SupportAttributeDefinition>();
66
67
68 public RelationshipDefinition() {}
69
70 @BeanTagAttribute(name = "objectAttributeName")
71 public String getObjectAttributeName() {
72 return objectAttributeName;
73 }
74
75 @BeanTagAttribute(name = "sourceClass")
76 public Class<?> getSourceClass() {
77 return sourceClass;
78 }
79
80
81
82
83 @BeanTagAttribute(name = "targetClass")
84 public Class<?> getTargetClass() {
85 if (targetClass == null) {
86 Class propertyClass = DataDictionary.getAttributeClass(sourceClass, objectAttributeName);
87 if (propertyClass == null) {
88 throw new AttributeValidationException("cannot get valid class for property '"
89 + objectAttributeName
90 + "' as an attribute of '"
91 + sourceClass
92 + "'");
93 }
94
95 targetClass = propertyClass;
96 }
97 return targetClass;
98 }
99
100
101
102
103
104
105 public void setTargetClass(Class<?> targetClass) {
106 this.targetClass = targetClass;
107 }
108
109
110
111
112
113 public void setObjectAttributeName(String objectAttributeName) {
114 if (StringUtils.isBlank(objectAttributeName)) {
115 throw new IllegalArgumentException("invalid (blank) objectAttributeName");
116 }
117
118 this.objectAttributeName = objectAttributeName;
119 }
120
121 @BeanTagAttribute(name = "primitiveAttributes", type = BeanTagAttribute.AttributeType.LISTBEAN)
122 public List<PrimitiveAttributeDefinition> getPrimitiveAttributes() {
123 return primitiveAttributes;
124 }
125
126 @BeanTagAttribute(name = "supportAttributes", type = BeanTagAttribute.AttributeType.LISTBEAN)
127 public List<SupportAttributeDefinition> getSupportAttributes() {
128 return supportAttributes;
129 }
130
131 public boolean hasIdentifier() {
132 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
133 if (supportAttributeDefinition.isIdentifier()) {
134 return true;
135 }
136 }
137 return false;
138 }
139
140 public SupportAttributeDefinition getIdentifier() {
141 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
142 if (supportAttributeDefinition.isIdentifier()) {
143 return supportAttributeDefinition;
144 }
145 }
146 return null;
147 }
148
149
150
151
152
153
154 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
155 String propertyName = objectAttributeName;
156 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, propertyName)) {
157 throw new AttributeValidationException("property '"
158 + propertyName
159 + "' is not an attribute of class '"
160 + rootBusinessObjectClass
161 + "' ("
162 + ""
163 + ")");
164 }
165
166 getTargetClass();
167
168 for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributes) {
169 primitiveAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass);
170 }
171 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
172 supportAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass);
173 }
174 }
175
176
177
178
179
180
181 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass,
182 ValidationTrace tracer) {
183 tracer.addBean(this.getClass().getSimpleName(), "Attribute: " + getObjectAttributeName());
184 try {
185 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, getObjectAttributeName())) {
186 String currentValues[] =
187 {"property = " + getObjectAttributeName(), "Class =" + rootBusinessObjectClass};
188 tracer.createError("Property is not an attribute of the class", currentValues);
189 }
190 } catch (RuntimeException ex) {
191 String currentValues[] = {"attribute = " + getObjectAttributeName(), "Exception = " + ex.getMessage()};
192 tracer.createError("Unable to validate attribute", currentValues);
193 }
194
195 if (targetClass == null) {
196 Class propertyClass = DataDictionary.getAttributeClass(sourceClass, objectAttributeName);
197 if (propertyClass == null) {
198 String currentValues[] =
199 {"property = " + getObjectAttributeName(), "sourceClass = " + getSourceClass()};
200 tracer.createError("Cannot get valid class for property", currentValues);
201 } else {
202 targetClass = propertyClass;
203 }
204 }
205
206 for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributes) {
207 primitiveAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass, tracer.getCopy());
208 }
209 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
210 supportAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass, tracer.getCopy());
211 }
212 }
213
214
215
216
217 @Override
218 public String toString() {
219 return "RelationshipDefinition for relationship " + getObjectAttributeName();
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237 public void setPrimitiveAttributes(List<PrimitiveAttributeDefinition> primitiveAttributes) {
238 this.primitiveAttributes = primitiveAttributes;
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255 public void setSupportAttributes(List<SupportAttributeDefinition> supportAttributes) {
256 this.supportAttributes = supportAttributes;
257 }
258
259
260
261
262 public void setSourceClass(Class<?> sourceClass) {
263 this.sourceClass = sourceClass;
264 }
265 }
266