1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kns.service.impl; |
17 | |
|
18 | |
import java.lang.reflect.InvocationTargetException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Collection; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.List; |
24 | |
import java.util.Map; |
25 | |
|
26 | |
import org.apache.commons.lang.StringUtils; |
27 | |
import org.kuali.rice.kns.bo.BusinessObject; |
28 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
29 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
30 | |
import org.kuali.rice.kns.datadictionary.DataDictionaryEntry; |
31 | |
import org.kuali.rice.kns.datadictionary.FieldDefinition; |
32 | |
import org.kuali.rice.kns.datadictionary.InquirySectionDefinition; |
33 | |
import org.kuali.rice.kns.datadictionary.PrimitiveAttributeDefinition; |
34 | |
import org.kuali.rice.kns.datadictionary.RelationshipDefinition; |
35 | |
import org.kuali.rice.kns.datadictionary.SupportAttributeDefinition; |
36 | |
import org.kuali.rice.kns.lookup.valuefinder.ValueFinder; |
37 | |
import org.kuali.rice.kns.service.BusinessObjectDictionaryService; |
38 | |
import org.kuali.rice.kns.service.BusinessObjectMetaDataService; |
39 | |
import org.kuali.rice.kns.service.DataDictionaryService; |
40 | |
import org.kuali.rice.kns.service.KNSServiceLocatorWeb; |
41 | |
import org.kuali.rice.kns.service.ModuleService; |
42 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
43 | |
import org.kuali.rice.kns.util.ObjectUtils; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 0 | public class BusinessObjectMetaDataServiceImpl extends DataObjectMetaDataServiceImpl implements BusinessObjectMetaDataService { |
55 | 0 | private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
56 | |
.getLogger(BusinessObjectMetaDataServiceImpl.class); |
57 | |
|
58 | |
private BusinessObjectDictionaryService businessObjectDictionaryService; |
59 | |
|
60 | |
public Collection<String> getCollectionNames(BusinessObject bo) { |
61 | 0 | return getDataDictionaryService().getDataDictionary().getBusinessObjectEntry(bo.getClass().getName()) |
62 | |
.getCollectionNames(); |
63 | |
} |
64 | |
|
65 | |
public Collection<String> getInquirableFieldNames(Class boClass, String sectionTitle) { |
66 | 0 | return businessObjectDictionaryService.getInquiryFieldNames(boClass, sectionTitle); |
67 | |
} |
68 | |
|
69 | |
public List<String> getLookupableFieldNames(Class boClass) { |
70 | 0 | return businessObjectDictionaryService.getLookupFieldNames(boClass); |
71 | |
} |
72 | |
|
73 | |
public String getLookupFieldDefaultValue(Class businessObjectClass, String attributeName) { |
74 | 0 | return businessObjectDictionaryService.getLookupFieldDefaultValue(businessObjectClass, attributeName); |
75 | |
} |
76 | |
|
77 | |
public Class getLookupFieldDefaultValueFinderClass(Class businessObjectClass, String attributeName) { |
78 | 0 | return businessObjectDictionaryService |
79 | |
.getLookupFieldDefaultValueFinderClass(businessObjectClass, attributeName); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
public String getLookupFieldQuickfinderParameterString(Class businessObjectClass, String attributeName) { |
84 | 0 | return businessObjectDictionaryService.getLookupFieldQuickfinderParameterString(businessObjectClass, |
85 | |
attributeName); |
86 | |
} |
87 | |
|
88 | |
|
89 | |
public Class<? extends ValueFinder> getLookupFieldQuickfinderParameterStringBuilderClass(Class businessObjectClass, |
90 | |
String attributeName) { |
91 | 0 | return businessObjectDictionaryService.getLookupFieldQuickfinderParameterStringBuilderClass( |
92 | |
businessObjectClass, attributeName); |
93 | |
} |
94 | |
|
95 | |
public boolean isAttributeInquirable(Class boClass, String attributeName, String sectionTitle) { |
96 | 0 | Collection sections = businessObjectDictionaryService.getInquirySections(boClass); |
97 | 0 | boolean isInquirable = true; |
98 | |
|
99 | 0 | Iterator iter = sections.iterator(); |
100 | |
|
101 | 0 | while (iter.hasNext()) { |
102 | 0 | InquirySectionDefinition def = (InquirySectionDefinition) iter.next(); |
103 | 0 | for (FieldDefinition field : def.getInquiryFields()) { |
104 | 0 | if (field.getAttributeName().equalsIgnoreCase(attributeName)) { |
105 | 0 | isInquirable = !field.isNoInquiry(); |
106 | |
} |
107 | |
} |
108 | 0 | } |
109 | 0 | if (isInquirable) { |
110 | 0 | Object obj = null; |
111 | 0 | if (boClass != null && BusinessObject.class.isAssignableFrom(boClass)) { |
112 | 0 | obj = ObjectUtils.createNewObjectFromClass(boClass); |
113 | |
} |
114 | |
|
115 | 0 | if (obj != null) { |
116 | 0 | BusinessObject bo = (BusinessObject) obj; |
117 | 0 | Class clazz = getNestedBOClass(bo, attributeName); |
118 | 0 | if (clazz != null && BusinessObject.class.isAssignableFrom(clazz)) { |
119 | 0 | return businessObjectDictionaryService.isInquirable(clazz); |
120 | |
} |
121 | |
else { |
122 | 0 | return false; |
123 | |
} |
124 | |
} |
125 | |
else { |
126 | 0 | return false; |
127 | |
} |
128 | |
|
129 | |
} |
130 | |
|
131 | 0 | return isInquirable; |
132 | |
} |
133 | |
|
134 | |
public boolean isInquirable(Class boClass) { |
135 | 0 | boolean inquirable = false; |
136 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(boClass); |
137 | 0 | if (moduleService != null && moduleService.isExternalizable(boClass)) { |
138 | 0 | inquirable = moduleService.isExternalizableBusinessObjectInquirable(boClass); |
139 | |
} |
140 | |
else { |
141 | 0 | Boolean isLookupable = businessObjectDictionaryService.isInquirable(boClass); |
142 | 0 | if (isLookupable != null) { |
143 | 0 | inquirable = isLookupable.booleanValue(); |
144 | |
} |
145 | |
} |
146 | 0 | return inquirable; |
147 | |
} |
148 | |
|
149 | |
public boolean isAttributeLookupable(Class boClass, String attributeName) { |
150 | 0 | Object obj = null; |
151 | 0 | if (boClass != null && BusinessObject.class.isAssignableFrom(boClass)) { |
152 | 0 | obj = ObjectUtils.createNewObjectFromClass(boClass); |
153 | |
} |
154 | 0 | if (obj != null) { |
155 | 0 | BusinessObject bo = (BusinessObject) obj; |
156 | 0 | BusinessObjectRelationship relationship = getBusinessObjectRelationship(bo, attributeName); |
157 | |
|
158 | 0 | if (relationship != null && relationship.getRelatedClass() != null |
159 | |
&& BusinessObject.class.isAssignableFrom(relationship.getRelatedClass())) { |
160 | 0 | return isLookupable(relationship.getRelatedClass()); |
161 | |
} |
162 | |
else { |
163 | 0 | return false; |
164 | |
} |
165 | |
} |
166 | |
else { |
167 | 0 | return false; |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
public boolean isLookupable(Class boClass) { |
172 | 0 | boolean lookupable = false; |
173 | 0 | ModuleService moduleService = getKualiModuleService().getResponsibleModuleService(boClass); |
174 | 0 | if (moduleService != null && moduleService.isExternalizable(boClass)) { |
175 | 0 | lookupable = moduleService.isExternalizableBusinessObjectLookupable(boClass); |
176 | |
} |
177 | |
else { |
178 | 0 | Boolean isLookupable = businessObjectDictionaryService.isLookupable(boClass); |
179 | 0 | if (isLookupable != null) { |
180 | 0 | lookupable = isLookupable.booleanValue(); |
181 | |
} |
182 | |
} |
183 | 0 | return lookupable; |
184 | |
} |
185 | |
|
186 | |
public BusinessObjectRelationship getBusinessObjectRelationship(BusinessObject bo, String attributeName) { |
187 | 0 | return getBusinessObjectRelationship(bo, bo.getClass(), attributeName, "", true); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
public BusinessObjectRelationship getBusinessObjectRelationship(RelationshipDefinition ddReference, |
192 | |
BusinessObject bo, Class boClass, String attributeName, String attributePrefix, boolean keysOnly) { |
193 | |
|
194 | 0 | BusinessObjectRelationship relationship = null; |
195 | |
|
196 | |
|
197 | |
|
198 | 0 | if (ObjectUtils.isNestedAttribute(attributeName)) { |
199 | 0 | if (ddReference != null) { |
200 | 0 | relationship = new BusinessObjectRelationship(boClass, ddReference.getObjectAttributeName(), |
201 | |
ddReference.getTargetClass()); |
202 | 0 | for (PrimitiveAttributeDefinition def : ddReference.getPrimitiveAttributes()) { |
203 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
204 | 0 | relationship.getParentToChildReferences().put(attributePrefix + "." + def.getSourceName(), |
205 | |
def.getTargetName()); |
206 | |
} |
207 | |
else { |
208 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
209 | |
} |
210 | |
} |
211 | 0 | if (!keysOnly) { |
212 | 0 | for (SupportAttributeDefinition def : ddReference.getSupportAttributes()) { |
213 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
214 | 0 | relationship.getParentToChildReferences().put(attributePrefix + "." + def.getSourceName(), |
215 | |
def.getTargetName()); |
216 | 0 | if (def.isIdentifier()) { |
217 | 0 | relationship.setUserVisibleIdentifierKey(attributePrefix + "." + def.getSourceName()); |
218 | |
} |
219 | |
} |
220 | |
else { |
221 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
222 | 0 | if (def.isIdentifier()) { |
223 | 0 | relationship.setUserVisibleIdentifierKey(def.getSourceName()); |
224 | |
} |
225 | |
} |
226 | |
} |
227 | |
} |
228 | 0 | return relationship; |
229 | |
} |
230 | |
|
231 | |
|
232 | 0 | String localPrefix = StringUtils.substringBefore(attributeName, "."); |
233 | 0 | String localAttributeName = StringUtils.substringAfter(attributeName, "."); |
234 | 0 | if (bo == null) { |
235 | 0 | bo = (BusinessObject) ObjectUtils.createNewObjectFromClass(boClass); |
236 | |
} |
237 | 0 | Class nestedClass = ObjectUtils.getPropertyType(bo, localPrefix, getPersistenceStructureService()); |
238 | 0 | String fullPrefix = localPrefix; |
239 | 0 | if (StringUtils.isNotBlank(attributePrefix)) { |
240 | 0 | fullPrefix = attributePrefix + "." + localPrefix; |
241 | |
} |
242 | 0 | if (BusinessObject.class.isAssignableFrom(nestedClass)) { |
243 | 0 | relationship = getBusinessObjectRelationship(null, nestedClass, localAttributeName, fullPrefix, |
244 | |
keysOnly); |
245 | |
} |
246 | 0 | return relationship; |
247 | |
} |
248 | 0 | int maxSize = Integer.MAX_VALUE; |
249 | |
|
250 | 0 | if (PersistableBusinessObject.class.isAssignableFrom(boClass) |
251 | |
&& getPersistenceStructureService().isPersistable(boClass)) { |
252 | 0 | Map<String, BusinessObjectRelationship> rels = getPersistenceStructureService().getRelationshipMetadata(boClass, |
253 | |
attributeName, attributePrefix); |
254 | 0 | if (rels.size() > 0) { |
255 | 0 | for (BusinessObjectRelationship rel : rels.values()) { |
256 | 0 | if (rel.getParentToChildReferences().size() < maxSize && isLookupable(rel.getRelatedClass())) { |
257 | 0 | maxSize = rel.getParentToChildReferences().size(); |
258 | 0 | relationship = rel; |
259 | |
} |
260 | |
} |
261 | |
} |
262 | 0 | } |
263 | |
else { |
264 | 0 | ModuleService moduleService = KNSServiceLocatorWeb.getKualiModuleService() |
265 | |
.getResponsibleModuleService(boClass); |
266 | 0 | if (moduleService != null && moduleService.isExternalizable(boClass)) { |
267 | 0 | relationship = getRelationshipMetadata(boClass, attributeName, attributePrefix); |
268 | |
|
269 | |
|
270 | |
|
271 | 0 | if (relationship != null) { |
272 | 0 | return relationship; |
273 | |
} |
274 | |
} |
275 | |
} |
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | |
|
281 | 0 | if (ddReference != null && isLookupable(ddReference.getTargetClass()) && bo != null |
282 | |
&& ddReference.getPrimitiveAttributes().size() < maxSize) { |
283 | 0 | relationship = new BusinessObjectRelationship(boClass, ddReference.getObjectAttributeName(), |
284 | |
ddReference.getTargetClass()); |
285 | 0 | for (PrimitiveAttributeDefinition def : ddReference.getPrimitiveAttributes()) { |
286 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
287 | |
} |
288 | 0 | if (!keysOnly) { |
289 | 0 | for (SupportAttributeDefinition def : ddReference.getSupportAttributes()) { |
290 | 0 | relationship.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
291 | |
} |
292 | |
} |
293 | |
} |
294 | |
|
295 | 0 | return relationship; |
296 | |
|
297 | |
} |
298 | |
|
299 | |
|
300 | |
|
301 | |
public RelationshipDefinition getBusinessObjectRelationshipDefinition(Class c, String attributeName) { |
302 | 0 | return getDictionaryRelationship(c, attributeName); |
303 | |
} |
304 | |
|
305 | |
public RelationshipDefinition getBusinessObjectRelationshipDefinition(BusinessObject bo, String attributeName) { |
306 | 0 | return getBusinessObjectRelationshipDefinition(bo.getClass(), attributeName); |
307 | |
} |
308 | |
|
309 | |
public BusinessObjectRelationship getBusinessObjectRelationship(BusinessObject bo, Class boClass, |
310 | |
String attributeName, String attributePrefix, boolean keysOnly) { |
311 | 0 | RelationshipDefinition ddReference = getBusinessObjectRelationshipDefinition(boClass, attributeName); |
312 | 0 | return getBusinessObjectRelationship(ddReference, bo, boClass, attributeName, attributePrefix, keysOnly); |
313 | |
} |
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
public BusinessObjectDictionaryService getBusinessObjectDictionaryService() { |
321 | 0 | return businessObjectDictionaryService; |
322 | |
} |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
public void setBusinessObjectDictionaryService(BusinessObjectDictionaryService businessObjectDictionaryService) { |
331 | 0 | this.businessObjectDictionaryService = businessObjectDictionaryService; |
332 | 0 | } |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
private Class getNestedBOClass(BusinessObject bo, String attributeName) { |
343 | |
|
344 | 0 | String[] nestedAttributes = StringUtils.split(attributeName, "."); |
345 | 0 | String attributeRefName = ""; |
346 | 0 | Class clazz = null; |
347 | 0 | if (nestedAttributes.length > 1) { |
348 | 0 | String attributeStringSoFar = ""; |
349 | 0 | for (int i = 0; i < nestedAttributes.length - 1; i++) { |
350 | |
try { |
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | 0 | if (i != 0) { |
357 | 0 | attributeStringSoFar = attributeStringSoFar + "."; |
358 | |
} |
359 | 0 | attributeStringSoFar = attributeStringSoFar + nestedAttributes[i]; |
360 | 0 | clazz = ObjectUtils.easyGetPropertyType(bo, attributeStringSoFar); |
361 | |
} |
362 | 0 | catch (InvocationTargetException ite) { |
363 | 0 | LOG.info(ite); |
364 | 0 | return null; |
365 | |
} |
366 | 0 | catch (NoSuchMethodException nsme) { |
367 | 0 | LOG.info(nsme); |
368 | 0 | return null; |
369 | |
} |
370 | 0 | catch (IllegalAccessException iae) { |
371 | 0 | LOG.info(iae); |
372 | 0 | return null; |
373 | 0 | } |
374 | |
} |
375 | |
} |
376 | 0 | return clazz; |
377 | |
} |
378 | |
|
379 | |
public List<BusinessObjectRelationship> getBusinessObjectRelationships(BusinessObject bo) { |
380 | 0 | if (bo == null) { |
381 | 0 | return null; |
382 | |
} |
383 | 0 | return getBusinessObjectRelationships(bo.getClass()); |
384 | |
} |
385 | |
|
386 | |
@SuppressWarnings("unchecked") |
387 | |
public List<BusinessObjectRelationship> getBusinessObjectRelationships(Class<? extends BusinessObject> boClass) { |
388 | 0 | if (boClass == null) { |
389 | 0 | return null; |
390 | |
} |
391 | |
|
392 | 0 | Map<String, Class> referenceClasses = null; |
393 | 0 | if (PersistableBusinessObject.class.isAssignableFrom(boClass) |
394 | |
&& getPersistenceStructureService().isPersistable(boClass)) { |
395 | 0 | referenceClasses = getPersistenceStructureService().listReferenceObjectFields(boClass); |
396 | |
} |
397 | 0 | DataDictionaryEntry ddEntry = getDataDictionaryService().getDataDictionary().getDictionaryObjectEntry( |
398 | |
boClass.getName()); |
399 | 0 | List<RelationshipDefinition> ddRelationships = (ddEntry == null ? new ArrayList<RelationshipDefinition>() |
400 | |
: ddEntry.getRelationships()); |
401 | 0 | List<BusinessObjectRelationship> relationships = new ArrayList<BusinessObjectRelationship>(); |
402 | |
|
403 | |
|
404 | 0 | if (referenceClasses != null) { |
405 | 0 | for (Map.Entry<String, Class> entry : referenceClasses.entrySet()) { |
406 | 0 | if (isLookupable(entry.getValue())) { |
407 | 0 | Map<String, String> fkToPkRefs = getPersistenceStructureService().getForeignKeysForReference(boClass, |
408 | |
entry.getKey()); |
409 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship(boClass, entry.getKey(), |
410 | |
entry.getValue()); |
411 | 0 | for (Map.Entry<String, String> ref : fkToPkRefs.entrySet()) { |
412 | 0 | rel.getParentToChildReferences().put(ref.getKey(), ref.getValue()); |
413 | |
} |
414 | 0 | relationships.add(rel); |
415 | 0 | } |
416 | |
} |
417 | |
} |
418 | |
|
419 | 0 | for (RelationshipDefinition rd : ddRelationships) { |
420 | 0 | if (isLookupable(rd.getTargetClass())) { |
421 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship(boClass, rd.getObjectAttributeName(), |
422 | |
rd.getTargetClass()); |
423 | 0 | for (PrimitiveAttributeDefinition def : rd.getPrimitiveAttributes()) { |
424 | 0 | rel.getParentToChildReferences().put(def.getSourceName(), def.getTargetName()); |
425 | |
} |
426 | 0 | relationships.add(rel); |
427 | 0 | } |
428 | |
} |
429 | |
|
430 | 0 | return relationships; |
431 | |
} |
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
public Map<String, Class> getReferencesForForeignKey(BusinessObject bo, String attributeName) { |
438 | 0 | List<BusinessObjectRelationship> businessObjectRelationships = getBusinessObjectRelationships(bo); |
439 | 0 | Map<String, Class> referencesForForeignKey = new HashMap<String, Class>(); |
440 | 0 | for (BusinessObjectRelationship businessObjectRelationship : businessObjectRelationships) { |
441 | 0 | if (businessObjectRelationship != null && businessObjectRelationship.getParentToChildReferences() != null |
442 | |
&& businessObjectRelationship.getParentToChildReferences().containsKey(attributeName)) { |
443 | 0 | referencesForForeignKey.put(businessObjectRelationship.getParentAttributeName(), |
444 | |
businessObjectRelationship.getRelatedClass()); |
445 | |
} |
446 | |
} |
447 | 0 | return referencesForForeignKey; |
448 | |
} |
449 | |
|
450 | |
public String getForeignKeyFieldName(Class businessObjectClass, String attributeName, String targetName) { |
451 | |
|
452 | 0 | String fkName = ""; |
453 | |
|
454 | |
|
455 | 0 | RelationshipDefinition relationshipDefinition = getDictionaryRelationship(businessObjectClass, attributeName); |
456 | |
|
457 | 0 | if (relationshipDefinition != null) { |
458 | 0 | List<PrimitiveAttributeDefinition> primitives = relationshipDefinition.getPrimitiveAttributes(); |
459 | 0 | for (PrimitiveAttributeDefinition primitiveAttributeDefinition : primitives) { |
460 | 0 | if (primitiveAttributeDefinition.getTargetName().equals(targetName)) { |
461 | 0 | fkName = primitiveAttributeDefinition.getSourceName(); |
462 | 0 | break; |
463 | |
} |
464 | |
} |
465 | |
} |
466 | |
|
467 | |
|
468 | 0 | if (StringUtils.isBlank(fkName) && PersistableBusinessObject.class.isAssignableFrom(businessObjectClass) |
469 | |
&& getPersistenceStructureService().isPersistable(businessObjectClass)) { |
470 | 0 | fkName = getPersistenceStructureService().getForeignKeyFieldName(businessObjectClass, attributeName, |
471 | |
targetName); |
472 | |
} |
473 | 0 | return fkName; |
474 | |
} |
475 | |
} |