Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RelationshipDefinition |
|
| 2.1333333333333333;2.133 |
1 | /* | |
2 | * Copyright 2006-2007 The Kuali Foundation | |
3 | * | |
4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.opensource.org/licenses/ecl2.php | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | package org.kuali.rice.kns.datadictionary; | |
18 | ||
19 | import org.apache.commons.lang.StringUtils; | |
20 | import org.kuali.rice.kns.bo.BusinessObject; | |
21 | import org.kuali.rice.kns.datadictionary.exception.AttributeValidationException; | |
22 | ||
23 | import java.util.ArrayList; | |
24 | import java.util.List; | |
25 | ||
26 | /** | |
27 | * A single Relationship definition in the DataDictionary, which contains information concerning which primitive attributes of this | |
28 | * class can be used to retrieve an instance of some related Object instance | |
29 | * | |
30 | The relationship element defines how primitive attributes of this | |
31 | class can be used to retrieve an instance of some related Object instance | |
32 | DD: See RelationshipDefinition.java. | |
33 | ||
34 | JSTL: relationship is a Map which is accessed using a key which is the | |
35 | objectAttributeName of a relationship. The map contains a single entry | |
36 | with a key of "primitiveAttributes" and value which is an attributesMap ExportMap. | |
37 | ||
38 | The attributesMap ExportMap contains the following keys: | |
39 | * 0 (for first primitiveAttribute) | |
40 | * 1 (for second primitiveAttribute) | |
41 | etc. | |
42 | The corresponding value for each entry is an primitiveAttribute ExportMap | |
43 | which contains the following keys: | |
44 | * "sourceName" | |
45 | * "targetName" | |
46 | * | |
47 | */ | |
48 | public class RelationshipDefinition extends DataDictionaryDefinitionBase { | |
49 | private static final long serialVersionUID = 2946722646095412576L; | |
50 | ||
51 | protected String objectAttributeName; //Same as parentAttributeName of BusinessObjectRelationship | |
52 | protected Class<?> sourceClass; //parentClass | |
53 | ||
54 | /** | |
55 | * For 1:1 relationships, this class represents the type of the reference class. For 1:n references, this class represents the type of the element | |
56 | * of the collection | |
57 | */ | |
58 | protected Class<? extends BusinessObject> targetClass; //relatedClass | |
59 | ||
60 | 0 | protected List<PrimitiveAttributeDefinition> primitiveAttributes = new ArrayList<PrimitiveAttributeDefinition>(); //parentToChildReferences |
61 | 0 | protected List<SupportAttributeDefinition> supportAttributes = new ArrayList<SupportAttributeDefinition>(); //parentToChildReferences |
62 | ||
63 | ||
64 | 0 | public RelationshipDefinition() {} |
65 | ||
66 | public String getObjectAttributeName() { | |
67 | 0 | return objectAttributeName; |
68 | } | |
69 | ||
70 | public Class<?> getSourceClass() { | |
71 | 0 | return sourceClass; |
72 | } | |
73 | ||
74 | /** | |
75 | * Returns the {@link #targetClass} | |
76 | * | |
77 | * @param targetClass | |
78 | */ | |
79 | public Class<? extends BusinessObject> getTargetClass() { | |
80 | 0 | if (targetClass == null) { |
81 | 0 | Class propertyClass = DataDictionary.getAttributeClass(sourceClass, objectAttributeName); |
82 | 0 | if (propertyClass == null) { |
83 | 0 | throw new AttributeValidationException("cannot get valid class for property '" + objectAttributeName + "' as an attribute of '" + sourceClass + "'"); |
84 | } | |
85 | 0 | if (!BusinessObject.class.isAssignableFrom(propertyClass)) { |
86 | 0 | throw new AttributeValidationException("property '" + objectAttributeName + "' is not a BusinessObject (" + propertyClass.getName() + ") on sourceClass (" + sourceClass +")"); |
87 | } | |
88 | ||
89 | ||
90 | 0 | targetClass = propertyClass; |
91 | } | |
92 | 0 | return targetClass; |
93 | } | |
94 | ||
95 | /** | |
96 | * Sets the {@link #targetClass} | |
97 | * | |
98 | * @param targetClass | |
99 | */ | |
100 | public void setTargetClass(Class<? extends BusinessObject> targetClass) { | |
101 | 0 | this.targetClass = targetClass; |
102 | 0 | } |
103 | ||
104 | /** | |
105 | * Name of the business object property on the containing business object that is linked | |
106 | * by the contained PrimitiveAttributeDefinition objects. | |
107 | */ | |
108 | public void setObjectAttributeName(String objectAttributeName) { | |
109 | 0 | if (StringUtils.isBlank(objectAttributeName)) { |
110 | 0 | throw new IllegalArgumentException("invalid (blank) objectAttributeName"); |
111 | } | |
112 | ||
113 | 0 | this.objectAttributeName = objectAttributeName; |
114 | 0 | } |
115 | ||
116 | public List<PrimitiveAttributeDefinition> getPrimitiveAttributes() { | |
117 | 0 | return primitiveAttributes; |
118 | } | |
119 | ||
120 | public List<SupportAttributeDefinition> getSupportAttributes() { | |
121 | 0 | return supportAttributes; |
122 | } | |
123 | ||
124 | public boolean hasIdentifier() { | |
125 | 0 | for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) { |
126 | 0 | if ( supportAttributeDefinition.isIdentifier() ) { |
127 | 0 | return true; |
128 | } | |
129 | } | |
130 | 0 | return false; |
131 | } | |
132 | ||
133 | public SupportAttributeDefinition getIdentifier() { | |
134 | 0 | for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) { |
135 | 0 | if ( supportAttributeDefinition.isIdentifier() ) { |
136 | 0 | return supportAttributeDefinition; |
137 | } | |
138 | } | |
139 | 0 | return null; |
140 | } | |
141 | ||
142 | /** | |
143 | * Directly validate simple fields, call completeValidation on Definition fields. | |
144 | * | |
145 | * @see org.kuali.rice.kns.datadictionary.DataDictionaryEntry#completeValidation() | |
146 | */ | |
147 | public void completeValidation(Class rootBusinessObjectClass, Class otherBusinessObjectClass) { | |
148 | 0 | String propertyName = objectAttributeName; |
149 | 0 | if (!DataDictionary.isPropertyOf(rootBusinessObjectClass, propertyName)) { |
150 | 0 | throw new AttributeValidationException("property '" + propertyName + "' is not an attribute of class '" + rootBusinessObjectClass + "' (" + "" + ")"); |
151 | } | |
152 | ||
153 | 0 | getTargetClass(); // performs validation when this is called the first time |
154 | ||
155 | 0 | for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitiveAttributes) { |
156 | 0 | primitiveAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass); |
157 | } | |
158 | 0 | for (SupportAttributeDefinition supportAttributeDefinition : supportAttributes) { |
159 | 0 | supportAttributeDefinition.completeValidation(rootBusinessObjectClass, targetClass); |
160 | } | |
161 | 0 | } |
162 | ||
163 | ||
164 | /** | |
165 | * @see java.lang.Object#toString() | |
166 | */ | |
167 | @Override | |
168 | public String toString() { | |
169 | 0 | return "RelationshipDefinition for relationship " + getObjectAttributeName(); |
170 | } | |
171 | ||
172 | /** | |
173 | * | |
174 | The primitiveAttribute element identifies one pair of | |
175 | corresponding fields in the primary business object and | |
176 | the related business object. | |
177 | ||
178 | JSTL: primitiveAttribute is a Map which is accessed by the | |
179 | sequential key of "0", "1", etc. Each entry contains the following | |
180 | keys: | |
181 | * sourceName (String) | |
182 | * targetName (String) | |
183 | The value corresponding to the sourceName key is the attribute name defined | |
184 | for the primary business object. | |
185 | The value corresponding to the targetName key is the attribute name for | |
186 | the object being referenced by objectAttributeName. | |
187 | */ | |
188 | public void setPrimitiveAttributes(List<PrimitiveAttributeDefinition> primitiveAttributes) { | |
189 | 0 | this.primitiveAttributes = primitiveAttributes; |
190 | 0 | } |
191 | ||
192 | /** | |
193 | Support attributes define additional attributes that can be used to generate | |
194 | lookup field conversions and lookup parameters. | |
195 | ||
196 | Field conversions and lookup parameters are normally generated using foreign key relationships | |
197 | defined within OJB and the DD. Because Person objects are linked in a special way (i.e. they may | |
198 | come from an external data source and not from the DB, such as LDAP), it is often necessary to define | |
199 | extra fields that are related to each other, sort of like a supplemental foreign key. | |
200 | ||
201 | sourceName is the name of the POJO property of the business object | |
202 | targetName is the name of attribute that corresponds to the sourceName in the looked up BO | |
203 | identifier when true, only the field marked as an identifier will be passed in as a lookup parameter | |
204 | at most one supportAttribute for each relationship should be defined as identifier="true" | |
205 | */ | |
206 | public void setSupportAttributes(List<SupportAttributeDefinition> supportAttributes) { | |
207 | 0 | this.supportAttributes = supportAttributes; |
208 | 0 | } |
209 | ||
210 | /** | |
211 | * @param sourceClass the sourceClass to set | |
212 | */ | |
213 | public void setSourceClass(Class<?> sourceClass) { | |
214 | 0 | this.sourceClass = sourceClass; |
215 | 0 | } |
216 | } | |
217 |