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.beans.PropertyDescriptor; |
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 | |
import java.util.Set; |
26 | |
|
27 | |
import org.apache.commons.beanutils.PropertyUtils; |
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.EntityDescriptor; |
30 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.JoinColumnDescriptor; |
31 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.MetadataManager; |
32 | |
import org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor; |
33 | |
import org.kuali.rice.kns.bo.BusinessObjectRelationship; |
34 | |
import org.kuali.rice.kns.bo.PersistableBusinessObject; |
35 | |
import org.kuali.rice.kns.exception.ObjectNotABusinessObjectRuntimeException; |
36 | |
import org.kuali.rice.kns.exception.ReferenceAttributeDoesntExistException; |
37 | |
import org.kuali.rice.kns.exception.ReferenceAttributeNotAnOjbReferenceException; |
38 | |
import org.kuali.rice.kns.service.PersistenceStructureService; |
39 | |
import org.kuali.rice.kns.util.ForeignKeyFieldsPopulationState; |
40 | |
import org.kuali.rice.kns.util.spring.Cached; |
41 | |
|
42 | 0 | public class PersistenceStructureServiceJpaImpl extends PersistenceServiceImplBase implements PersistenceStructureService { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | 0 | public static Map<Class, Class> referenceConversionMap = new HashMap<Class, Class>(); |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
@Cached |
65 | |
public boolean isPersistable(Class clazz) { |
66 | 0 | boolean isPersistable = false; |
67 | |
|
68 | 0 | if (MetadataManager.getEntityDescriptor(clazz) != null) { |
69 | 0 | isPersistable = true; |
70 | |
} |
71 | |
|
72 | 0 | return isPersistable; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
@Cached |
79 | |
public List getPrimaryKeys(Class clazz) { |
80 | 0 | List pkList = new ArrayList(); |
81 | |
|
82 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz); |
83 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getPrimaryKeys()) { |
84 | 0 | pkList.add(field.getName()); |
85 | |
} |
86 | |
|
87 | 0 | return pkList; |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@Cached |
94 | |
public List listFieldNames(Class clazz) { |
95 | 0 | List fieldNames = new ArrayList(); |
96 | |
|
97 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(clazz); |
98 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { |
99 | 0 | fieldNames.add(field.getName()); |
100 | |
} |
101 | |
|
102 | 0 | return fieldNames; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public Object clearPrimaryKeyFields(Object persistableObject) { |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 0 | return persistableObject; |
133 | |
} |
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
@Cached |
139 | |
|
140 | |
|
141 | |
public List listPersistableSubclasses(Class superclazz) { |
142 | 0 | List persistableSubclasses = new ArrayList(); |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | 0 | return persistableSubclasses; |
158 | |
} |
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
@Cached |
165 | |
public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName, String attributePrefix) { |
166 | 0 | if (persistableClass == null) { |
167 | 0 | throw new IllegalArgumentException("invalid (null) persistableClass"); |
168 | |
} |
169 | 0 | if (StringUtils.isBlank(attributeName)) { |
170 | 0 | throw new IllegalArgumentException("invalid (blank) attributeName"); |
171 | |
} |
172 | |
|
173 | 0 | Map<String, BusinessObjectRelationship> relationships = new HashMap<String, BusinessObjectRelationship>(); |
174 | |
|
175 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableClass); |
176 | 0 | for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { |
177 | 0 | List<String> fks = objectDescriptor.getForeignKeyFields(); |
178 | 0 | if (fks.contains(attributeName) || objectDescriptor.getAttributeName().equals(attributeName)) { |
179 | 0 | Map<String, String> fkToPkRefs = getForeignKeysForReference(persistableClass, objectDescriptor.getAttributeName()); |
180 | 0 | BusinessObjectRelationship rel = new BusinessObjectRelationship(persistableClass, objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity()); |
181 | 0 | for (Map.Entry<String, String> ref : fkToPkRefs.entrySet()) { |
182 | 0 | if (StringUtils.isBlank(attributePrefix)) { |
183 | 0 | rel.getParentToChildReferences().put(ref.getKey(), ref.getValue()); |
184 | |
} else { |
185 | 0 | rel.getParentToChildReferences().put(attributePrefix + "." + ref.getKey(), ref.getValue()); |
186 | |
} |
187 | |
} |
188 | 0 | relationships.put(objectDescriptor.getAttributeName(), rel); |
189 | |
} |
190 | 0 | } |
191 | |
|
192 | 0 | return relationships; |
193 | |
} |
194 | |
|
195 | |
@Cached |
196 | |
|
197 | |
public Map<String, BusinessObjectRelationship> getRelationshipMetadata(Class persistableClass, String attributeName) { |
198 | 0 | return getRelationshipMetadata(persistableClass, attributeName, null); |
199 | |
} |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
@Cached |
206 | |
public String getForeignKeyFieldName(Class persistableObjectClass, String attributeName, String pkName) { |
207 | 0 | String fkName = null; |
208 | |
|
209 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
210 | 0 | ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(attributeName); |
211 | 0 | if (objectDescriptor == null) { |
212 | 0 | throw new RuntimeException("Attribute name " + attributeName + " is not a valid reference to class " + persistableObjectClass.getName()); |
213 | |
} |
214 | 0 | List<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor> matches = new ArrayList<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor>(); |
215 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor field : descriptor.getFields()) { |
216 | 0 | String column = field.getColumn(); |
217 | 0 | for (JoinColumnDescriptor join : objectDescriptor.getJoinColumnDescriptors()) { |
218 | 0 | if (column != null && column.equals(join.getName())) { |
219 | 0 | matches.add(field); |
220 | |
} |
221 | |
} |
222 | 0 | } |
223 | |
|
224 | 0 | if (matches.size() == 1) { |
225 | 0 | fkName = matches.get(0).getName(); |
226 | |
} else { |
227 | 0 | throw new RuntimeException("Implement me!"); |
228 | |
} |
229 | |
|
230 | 0 | return fkName; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
@Cached |
238 | |
public Map getReferencesForForeignKey(Class persistableObjectClass, String attributeName) { |
239 | 0 | Map referenceClasses = new HashMap(); |
240 | |
|
241 | 0 | if (PersistableBusinessObject.class.isAssignableFrom(persistableObjectClass)) { |
242 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
243 | 0 | for (ObjectDescriptor objectDescriptor : descriptor.getObjectRelationships()) { |
244 | 0 | List<String> refFkNames = objectDescriptor.getForeignKeyFields(); |
245 | 0 | for (String fk : refFkNames) { |
246 | 0 | if (fk.equals(attributeName)) { |
247 | 0 | referenceClasses.put(objectDescriptor.getAttributeName(), objectDescriptor.getTargetEntity()); |
248 | |
} |
249 | |
} |
250 | 0 | } |
251 | |
} |
252 | |
|
253 | 0 | return referenceClasses; |
254 | |
} |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
@Cached |
273 | |
public Map getForeignKeysForReference(Class clazz, String attributeName) { |
274 | |
|
275 | |
|
276 | 0 | if (clazz == null) { |
277 | 0 | throw new IllegalArgumentException("The Class passed in for the clazz argument was null."); |
278 | |
} |
279 | 0 | if (attributeName == null) { |
280 | 0 | throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); |
281 | |
} |
282 | |
|
283 | |
|
284 | 0 | Class attributeClass = getBusinessObjectAttributeClass(clazz, attributeName); |
285 | 0 | if (attributeClass == null) { |
286 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + attributeName + "' does not exist on class: '" + clazz.getName() + "'."); |
287 | |
} |
288 | |
|
289 | |
|
290 | |
|
291 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(attributeClass)) { |
292 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + attributeName + ") is of class: '" + attributeClass.getName() + "' and is not a descendent of BusinessObject. Only descendents of BusinessObject can be used."); |
293 | |
} |
294 | |
|
295 | 0 | return determineFkMap(clazz, attributeName, attributeClass); |
296 | |
} |
297 | |
|
298 | |
private Map determineFkMap(Class clazz, String attributeName, Class attributeClass) { |
299 | 0 | Map fkMap = new HashMap(); |
300 | 0 | EntityDescriptor entityDescriptor = MetadataManager.getEntityDescriptor(clazz); |
301 | 0 | ObjectDescriptor objectDescriptor = entityDescriptor.getObjectDescriptorByName(attributeName); |
302 | 0 | if (objectDescriptor == null) { |
303 | 0 | throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + attributeName + ") is not defined in JPA annotations for class: '" + clazz.getName() + "'"); |
304 | |
} |
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | 0 | if (!attributeClass.equals(objectDescriptor.getTargetEntity())) { |
312 | 0 | if (referenceConversionMap.containsKey(attributeClass)) { |
313 | 0 | attributeClass = referenceConversionMap.get(attributeClass); |
314 | |
} else { |
315 | 0 | throw new RuntimeException("The Class of the Java member [" + attributeClass.getName() + "] '" + attributeName + "' does not match the class of the reference [" + objectDescriptor.getTargetEntity().getName() + "]. " + "This is an unhandled special case for which special code needs to be written in this class."); |
316 | |
} |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | 0 | List<String> fkFields = objectDescriptor.getForeignKeyFields(); |
322 | 0 | Iterator<String> fkIterator = fkFields.iterator(); |
323 | |
|
324 | |
|
325 | |
|
326 | 0 | List pkFields = getPrimaryKeys(attributeClass); |
327 | 0 | Iterator pkIterator = pkFields.iterator(); |
328 | |
|
329 | |
|
330 | |
|
331 | 0 | if (pkFields.size() != fkFields.size()) { |
332 | 0 | throw new RuntimeException("KualiPersistenceStructureService Error: The number of " + "foreign keys doesnt match the number of primary keys."); |
333 | |
} |
334 | |
|
335 | |
|
336 | 0 | while (fkIterator.hasNext()) { |
337 | |
|
338 | |
|
339 | |
|
340 | 0 | if (!pkIterator.hasNext()) { |
341 | 0 | throw new RuntimeException("The number of foriegn keys dont match the number of primary keys for the reference '" + attributeName + "', on BO of type '" + clazz.getName() + "'. " + "This should never happen under normal circumstances."); |
342 | |
} |
343 | |
|
344 | |
|
345 | 0 | String fkFieldName = (String) fkIterator.next(); |
346 | 0 | String pkFieldName = (String) pkIterator.next(); |
347 | |
|
348 | |
|
349 | 0 | fkMap.put(fkFieldName, pkFieldName); |
350 | 0 | } |
351 | 0 | return fkMap; |
352 | |
} |
353 | |
|
354 | |
@Cached |
355 | |
public Map<String, String> getInverseForeignKeysForCollection(Class boClass, String collectionName) { |
356 | |
|
357 | 0 | if (boClass == null) { |
358 | 0 | throw new IllegalArgumentException("The Class passed in for the boClass argument was null."); |
359 | |
} |
360 | 0 | if (collectionName == null) { |
361 | 0 | throw new IllegalArgumentException("The String passed in for the attributeName argument was null."); |
362 | |
} |
363 | |
|
364 | 0 | PropertyDescriptor propertyDescriptor = null; |
365 | |
|
366 | |
|
367 | |
Object classInstance; |
368 | |
try { |
369 | 0 | classInstance = boClass.newInstance(); |
370 | 0 | } catch (Exception e) { |
371 | 0 | throw new RuntimeException(e); |
372 | 0 | } |
373 | |
|
374 | |
|
375 | |
try { |
376 | 0 | propertyDescriptor = PropertyUtils.getPropertyDescriptor(classInstance, collectionName); |
377 | 0 | } catch (Exception e) { |
378 | 0 | throw new RuntimeException(e); |
379 | 0 | } |
380 | 0 | if (propertyDescriptor == null) { |
381 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + collectionName + "' does not exist " + "on class: '" + boClass.getName() + "'. GFK"); |
382 | |
} |
383 | |
|
384 | |
|
385 | 0 | Class attributeClass = propertyDescriptor.getPropertyType(); |
386 | |
|
387 | |
|
388 | |
|
389 | 0 | if (!Collection.class.isAssignableFrom(attributeClass)) { |
390 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + collectionName + ") is of class: " + "'" + attributeClass.getName() + "' and is not a " + "descendent of Collection"); |
391 | |
} |
392 | |
|
393 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
394 | 0 | org.kuali.rice.core.framework.persistence.jpa.metadata.CollectionDescriptor cd = descriptor.getCollectionDescriptorByName(collectionName); |
395 | 0 | List<String> childPrimaryKeys = cd.getForeignKeyFields(); |
396 | |
|
397 | 0 | List parentForeignKeys = getPrimaryKeys(boClass); |
398 | |
|
399 | 0 | if (parentForeignKeys.size() != childPrimaryKeys.size()) { |
400 | 0 | throw new RuntimeException("The number of keys in the class descriptor and the inverse foreign key mapping for the collection descriptors do not match."); |
401 | |
} |
402 | |
|
403 | 0 | Map<String, String> fkToPkMap = new HashMap<String, String>(); |
404 | 0 | Iterator pFKIter = parentForeignKeys.iterator(); |
405 | 0 | Iterator cPKIterator = childPrimaryKeys.iterator(); |
406 | |
|
407 | 0 | while (pFKIter.hasNext()) { |
408 | 0 | String parentForeignKey = (String) pFKIter.next(); |
409 | 0 | String childPrimaryKey = (String) cPKIterator.next(); |
410 | |
|
411 | 0 | fkToPkMap.put(parentForeignKey, childPrimaryKey); |
412 | 0 | } |
413 | 0 | return fkToPkMap; |
414 | |
} |
415 | |
|
416 | |
|
417 | |
|
418 | |
|
419 | |
@Cached |
420 | |
public Map getNestedForeignKeyMap(Class persistableObjectClass) { |
421 | 0 | Map fkMap = new HashMap(); |
422 | |
|
423 | |
|
424 | |
|
425 | |
|
426 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(persistableObjectClass); |
427 | 0 | for (ObjectDescriptor objectReferenceDescriptor : descriptor.getObjectRelationships()) { |
428 | 0 | EntityDescriptor referenceDescriptor = MetadataManager.getEntityDescriptor(objectReferenceDescriptor.getTargetEntity()); |
429 | 0 | List<String> fkFields = objectReferenceDescriptor.getForeignKeyFields(); |
430 | 0 | Set<org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor> pkFields = referenceDescriptor.getPrimaryKeys(); |
431 | 0 | int i = 0; |
432 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.FieldDescriptor fd : pkFields) { |
433 | 0 | fkMap.put(objectReferenceDescriptor.getAttributeName() + "." + fd.getName(), fkFields.get(i)); |
434 | 0 | i++; |
435 | |
} |
436 | 0 | } |
437 | |
|
438 | 0 | return fkMap; |
439 | |
} |
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
public boolean hasPrimaryKeyFieldValues(Object persistableObject) { |
445 | 0 | Map keyFields = getPrimaryKeyFieldValues(persistableObject); |
446 | |
|
447 | 0 | boolean emptyField = false; |
448 | 0 | for (Iterator i = keyFields.entrySet().iterator(); !emptyField && i.hasNext();) { |
449 | 0 | Map.Entry e = (Map.Entry) i.next(); |
450 | |
|
451 | 0 | Object fieldValue = e.getValue(); |
452 | 0 | if (fieldValue == null) { |
453 | 0 | emptyField = true; |
454 | 0 | } else if (fieldValue instanceof String) { |
455 | 0 | if (StringUtils.isEmpty((String) fieldValue)) { |
456 | 0 | emptyField = true; |
457 | |
} else { |
458 | 0 | emptyField = false; |
459 | |
} |
460 | |
} |
461 | 0 | } |
462 | |
|
463 | 0 | return !emptyField; |
464 | |
} |
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
public ForeignKeyFieldsPopulationState getForeignKeyFieldsPopulationState(PersistableBusinessObject bo, String referenceName) { |
471 | |
|
472 | 0 | boolean allFieldsPopulated = true; |
473 | 0 | boolean anyFieldsPopulated = false; |
474 | 0 | List<String> unpopulatedFields = new ArrayList<String>(); |
475 | |
|
476 | |
|
477 | 0 | if (bo == null) { |
478 | 0 | throw new IllegalArgumentException("The Class passed in for the BusinessObject argument was null."); |
479 | |
} |
480 | 0 | if (StringUtils.isBlank(referenceName)) { |
481 | 0 | throw new IllegalArgumentException("The String passed in for the referenceName argument was null or empty."); |
482 | |
} |
483 | |
|
484 | 0 | PropertyDescriptor propertyDescriptor = null; |
485 | |
|
486 | |
|
487 | |
try { |
488 | 0 | propertyDescriptor = PropertyUtils.getPropertyDescriptor(bo, referenceName); |
489 | 0 | } catch (Exception e) { |
490 | 0 | throw new RuntimeException(e); |
491 | 0 | } |
492 | 0 | if (propertyDescriptor == null) { |
493 | 0 | throw new ReferenceAttributeDoesntExistException("Requested attribute: '" + referenceName + "' does not exist " + "on class: '" + bo.getClass().getName() + "'."); |
494 | |
} |
495 | |
|
496 | |
|
497 | 0 | Class referenceClass = propertyDescriptor.getPropertyType(); |
498 | |
|
499 | |
|
500 | |
|
501 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(referenceClass)) { |
502 | 0 | throw new ObjectNotABusinessObjectRuntimeException("Attribute requested (" + referenceName + ") is of class: " + "'" + referenceClass.getName() + "' and is not a " + "descendent of BusinessObject. Only descendents of BusinessObject " + "can be used."); |
503 | |
} |
504 | |
|
505 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(bo.getClass()); |
506 | 0 | ObjectDescriptor objectDescriptor = descriptor.getObjectDescriptorByName(referenceName); |
507 | |
|
508 | 0 | if (objectDescriptor == null) { |
509 | 0 | throw new ReferenceAttributeNotAnOjbReferenceException("Attribute requested (" + referenceName + ") is not listed " + "in OJB as a reference-descriptor for class: '" + bo.getClass().getName() + "'"); |
510 | |
} |
511 | |
|
512 | 0 | List<String> fkFields = objectDescriptor.getForeignKeyFields(); |
513 | 0 | Iterator fkIterator = fkFields.iterator(); |
514 | |
|
515 | |
|
516 | 0 | while (fkIterator.hasNext()) { |
517 | |
|
518 | |
|
519 | 0 | String fkFieldName = (String) fkIterator.next(); |
520 | |
|
521 | |
|
522 | 0 | Object fkFieldValue = null; |
523 | |
try { |
524 | 0 | fkFieldValue = PropertyUtils.getSimpleProperty(bo, fkFieldName); |
525 | |
} |
526 | |
|
527 | |
|
528 | 0 | catch (Exception e) { |
529 | 0 | throw new RuntimeException(e); |
530 | 0 | } |
531 | |
|
532 | |
|
533 | 0 | if (fkFieldValue == null) { |
534 | 0 | allFieldsPopulated = false; |
535 | 0 | unpopulatedFields.add(fkFieldName); |
536 | 0 | } else if (fkFieldValue instanceof String) { |
537 | 0 | if (StringUtils.isBlank((String) fkFieldValue)) { |
538 | 0 | allFieldsPopulated = false; |
539 | 0 | unpopulatedFields.add(fkFieldName); |
540 | |
} else { |
541 | 0 | anyFieldsPopulated = true; |
542 | |
} |
543 | |
} else { |
544 | 0 | anyFieldsPopulated = true; |
545 | |
} |
546 | 0 | } |
547 | |
|
548 | |
|
549 | |
|
550 | 0 | if (allFieldsPopulated) { |
551 | 0 | if (!unpopulatedFields.isEmpty()) { |
552 | 0 | throw new RuntimeException("The flag is set that indicates all fields are populated, but there " + "are fields present in the unpopulatedFields list. This should never happen, and indicates " + "that the logic in this method is broken."); |
553 | |
} |
554 | |
} |
555 | |
|
556 | 0 | return new ForeignKeyFieldsPopulationState(allFieldsPopulated, anyFieldsPopulated, unpopulatedFields); |
557 | |
} |
558 | |
|
559 | |
|
560 | |
|
561 | |
|
562 | |
@Cached |
563 | |
public Map<String, Class> listReferenceObjectFields(Class boClass) { |
564 | |
|
565 | 0 | if (boClass == null) { |
566 | 0 | throw new IllegalArgumentException("Class specified in the parameter was null."); |
567 | |
} |
568 | 0 | if (!PersistableBusinessObject.class.isAssignableFrom(boClass)) { |
569 | 0 | throw new IllegalArgumentException("Class specified [" + boClass.getName() + "] must be a class that " + "inherits from BusinessObject."); |
570 | |
} |
571 | |
|
572 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
573 | 0 | Map<String, Class> references = new HashMap(); |
574 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.ObjectDescriptor od : descriptor.getObjectRelationships()) { |
575 | 0 | references.put(od.getAttributeName(), od.getTargetEntity()); |
576 | |
} |
577 | |
|
578 | 0 | return references; |
579 | |
} |
580 | |
|
581 | |
@Cached |
582 | |
public Map<String, Class> listCollectionObjectTypes(Class boClass) { |
583 | 0 | if (boClass == null) { |
584 | 0 | throw new IllegalArgumentException("Class specified in the parameter was null."); |
585 | |
} |
586 | |
|
587 | 0 | Map<String, Class> references = new HashMap(); |
588 | |
|
589 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
590 | 0 | if (descriptor == null) { |
591 | 0 | return references; |
592 | |
} |
593 | |
|
594 | 0 | for (org.kuali.rice.core.framework.persistence.jpa.metadata.CollectionDescriptor cd : descriptor.getCollectionRelationships()) { |
595 | 0 | references.put(cd.getAttributeName(), cd.getTargetEntity()); |
596 | |
} |
597 | |
|
598 | 0 | return references; |
599 | |
} |
600 | |
|
601 | |
public Map<String, Class> listCollectionObjectTypes(PersistableBusinessObject bo) { |
602 | |
|
603 | 0 | if (bo == null) { |
604 | 0 | throw new IllegalArgumentException("BO specified in the parameter was null."); |
605 | |
} |
606 | 0 | if (!(bo instanceof PersistableBusinessObject)) { |
607 | 0 | throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); |
608 | |
} |
609 | |
|
610 | 0 | return listCollectionObjectTypes(bo.getClass()); |
611 | |
} |
612 | |
|
613 | |
|
614 | |
|
615 | |
|
616 | |
public Map<String, Class> listReferenceObjectFields(PersistableBusinessObject bo) { |
617 | |
|
618 | 0 | if (bo == null) { |
619 | 0 | throw new IllegalArgumentException("BO specified in the parameter was null."); |
620 | |
} |
621 | 0 | if (!(bo instanceof PersistableBusinessObject)) { |
622 | 0 | throw new IllegalArgumentException("BO specified [" + bo.getClass().getName() + "] must be a class that " + "inherits from BusinessObject."); |
623 | |
} |
624 | |
|
625 | 0 | return listReferenceObjectFields(bo.getClass()); |
626 | |
} |
627 | |
|
628 | |
@Cached |
629 | |
public boolean isReferenceUpdatable(Class boClass, String referenceName) { |
630 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
631 | 0 | return descriptor.getObjectDescriptorByName(referenceName).isUpdateable(); |
632 | |
|
633 | |
|
634 | |
|
635 | |
|
636 | |
|
637 | |
|
638 | |
} |
639 | |
|
640 | |
@Cached |
641 | |
public boolean isCollectionUpdatable(Class boClass, String collectionName) { |
642 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
643 | 0 | return descriptor.getCollectionDescriptorByName(collectionName).isUpdateable(); |
644 | |
|
645 | |
|
646 | |
|
647 | |
|
648 | |
|
649 | |
|
650 | |
} |
651 | |
|
652 | |
@Cached |
653 | |
public boolean hasCollection(Class boClass, String collectionName) { |
654 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
655 | 0 | return descriptor.getCollectionDescriptorByName(collectionName) != null; |
656 | |
} |
657 | |
|
658 | |
@Cached |
659 | |
public boolean hasReference(Class boClass, String referenceName) { |
660 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
661 | 0 | return descriptor.getObjectDescriptorByName(referenceName) != null; |
662 | |
} |
663 | |
|
664 | |
|
665 | |
|
666 | |
|
667 | |
@Cached |
668 | |
public String getTableName(Class<? extends PersistableBusinessObject> boClass) { |
669 | 0 | EntityDescriptor descriptor = MetadataManager.getEntityDescriptor(boClass); |
670 | 0 | return descriptor.getTable(); |
671 | |
} |
672 | |
} |
673 | |
|