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.bo.BusinessObject;
20 import org.kuali.rice.krad.datadictionary.exception.AttributeValidationException;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class RelationshipDefinition extends DataDictionaryDefinitionBase {
48 private static final long serialVersionUID = 2946722646095412576L;
49
50 protected String objectAttributeName;
51 protected Class<?> sourceClass;
52
53
54
55
56
57 protected Class<? extends BusinessObject> targetClass;
58
59 protected List<PrimitiveAttributeDefinition> primitiveAttributes = new ArrayList<PrimitiveAttributeDefinition>();
60 protected List<SupportAttributeDefinition> supportAttributes = new ArrayList<SupportAttributeDefinition>();
61
62
63 public RelationshipDefinition() {}
64
65 public String getObjectAttributeName() {
66 return objectAttributeName;
67 }
68
69 public Class<?> getSourceClass() {
70 return sourceClass;
71 }
72
73
74
75
76
77
78 public Class<? extends BusinessObject> getTargetClass() {
79 if (targetClass == null) {
80 Class propertyClass = DataDictionary.getAttributeClass(sourceClass, objectAttributeName);
81 if (propertyClass == null) {
82 throw new AttributeValidationException("cannot get valid class for property '" + objectAttributeName + "' as an attribute of '" + sourceClass + "'");
83 }
84 if (!BusinessObject.class.isAssignableFrom(propertyClass)) {
85 throw new AttributeValidationException("property '" + objectAttributeName + "' is not a BusinessObject (" + propertyClass.getName() + ") on sourceClass (" + sourceClass +")");
86 }
87
88
89 targetClass = propertyClass;
90 }
91 return targetClass;
92 }
93
94
95
96
97
98
99 public void setTargetClass(Class<? extends BusinessObject> targetClass) {
100 this.targetClass = targetClass;
101 }
102
103
104
105
106
107 public void setObjectAttributeName(String objectAttributeName) {
108 if (StringUtils.isBlank(objectAttributeName)) {
109 throw new IllegalArgumentException("invalid (blank) objectAttributeName");
110 }
111
112 this.objectAttributeName = objectAttributeName;
113 }
114
115 public List<PrimitiveAttributeDefinition> getPrimitiveAttributes() {
116 return primitiveAttributes;
117 }
118
119 public List<SupportAttributeDefinition> getSupportAttributes() {
120 return supportAttributes;
121 }
122
123 public boolean hasIdentifier() {
124 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
125 if ( supportAttributeDefinition.isIdentifier() ) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 public SupportAttributeDefinition getIdentifier() {
133 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
134 if ( supportAttributeDefinition.isIdentifier() ) {
135 return supportAttributeDefinition;
136 }
137 }
138 return null;
139 }
140
141
142
143
144
145
146 public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) {
147 String propertyName = objectAttributeName;
148 if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, propertyName)) {
149 throw new AttributeValidationException("property '" + propertyName + "' is not an attribute of class '" + rootBusinessObjectClass + "' (" + "" + ")");
150 }
151
152 getTargetClass();
153
154 for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributes) {
155 primitiveAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass);
156 }
157 for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) {
158 supportAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass);
159 }
160 }
161
162
163
164
165
166 @Override
167 public String toString() {
168 return "RelationshipDefinition for relationship " + getObjectAttributeName();
169 }
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187 public void setPrimitiveAttributes(List<PrimitiveAttributeDefinition> primitiveAttributes) {
188 this.primitiveAttributes = primitiveAttributes;
189 }
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 public void setSupportAttributes(List<SupportAttributeDefinition> supportAttributes) {
206 this.supportAttributes = supportAttributes;
207 }
208
209
210
211
212 public void setSourceClass(Class<?> sourceClass) {
213 this.sourceClass = sourceClass;
214 }
215 }
216