1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kns.util;
17
18 import java.beans.PropertyDescriptor;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.ObjectInputStream;
22 import java.io.ObjectOutputStream;
23 import java.io.Serializable;
24 import java.lang.reflect.Field;
25 import java.lang.reflect.InvocationTargetException;
26 import java.security.MessageDigest;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.List;
30 import java.util.Map;
31
32 import org.apache.commons.beanutils.NestedNullException;
33 import org.apache.commons.beanutils.PropertyUtils;
34 import org.apache.commons.lang.StringUtils;
35 import org.apache.log4j.Logger;
36 import org.apache.ojb.broker.core.proxy.ProxyHelper;
37 import org.hibernate.collection.PersistentBag;
38 import org.kuali.rice.kns.bo.BusinessObject;
39 import org.kuali.rice.kns.bo.ExternalizableBusinessObject;
40 import org.kuali.rice.kns.bo.PersistableBusinessObject;
41 import org.kuali.rice.kns.bo.PersistableBusinessObjectExtension;
42 import org.kuali.rice.kns.service.KNSServiceLocator;
43 import org.kuali.rice.kns.service.ModuleService;
44 import org.kuali.rice.kns.service.PersistenceStructureService;
45 import org.kuali.rice.kns.util.cache.CopiedObject;
46 import org.kuali.rice.kns.web.format.CollectionFormatter;
47 import org.kuali.rice.kns.web.format.FormatException;
48 import org.kuali.rice.kns.web.format.Formatter;
49
50
51
52
53 public class ObjectUtils {
54 private static final Logger LOG = Logger.getLogger(ObjectUtils.class);
55
56 private ObjectUtils() {
57 }
58
59
60
61
62
63
64
65
66
67 public static Serializable deepCopy(Serializable src) {
68 CopiedObject co = deepCopyForCaching(src);
69 return co.getContent();
70 }
71
72
73
74
75
76
77
78
79
80
81
82 public static CopiedObject deepCopyForCaching(Serializable src) {
83 CopiedObject co = new CopiedObject();
84
85 co.setContent( src );
86
87 return co;
88 }
89
90
91
92
93
94
95
96
97 public static byte[] toByteArray(Object object) throws Exception {
98 ObjectOutputStream oos = null;
99 try {
100 ByteArrayOutputStream bos = new ByteArrayOutputStream();
101 oos = new ObjectOutputStream(bos);
102
103 oos.writeObject(object);
104
105 return bos.toByteArray();
106 }
107 catch (Exception e) {
108 LOG.warn("Exception in ObjectUtil = " + e);
109 throw (e);
110 }
111 finally {
112 if (oos != null) {
113 oos.close();
114 }
115 }
116 }
117
118
119
120
121
122
123
124 public static Object fromByteArray(byte[] bytes) throws Exception {
125 ObjectInputStream ois = null;
126 try {
127 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
128 ois = new ObjectInputStream(bis);
129 Object obj = ois.readObject();
130 return obj;
131 }
132 catch (Exception e) {
133 LOG.warn("Exception in ObjectUtil = " + e);
134 throw (e);
135 }
136 finally {
137 if (ois != null) {
138 ois.close();
139 }
140 }
141 }
142
143
144
145
146
147
148
149 public static String getMD5Hash(Object object) throws Exception {
150 try {
151 MessageDigest md = MessageDigest.getInstance("MD5");
152 md.update(toByteArray(object));
153 return new String(md.digest());
154 }
155 catch (Exception e) {
156 LOG.warn(e);
157 throw e;
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public static BusinessObject createHybridBusinessObject(Class businessObjectClass, BusinessObject source, Map<String, String> template) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
177 BusinessObject obj = null;
178 try {
179 ModuleService moduleService = KNSServiceLocator.getKualiModuleService().getResponsibleModuleService(businessObjectClass);
180 if (moduleService != null && moduleService.isExternalizable(businessObjectClass))
181 obj = (BusinessObject)moduleService.createNewObjectFromExternalizableClass(businessObjectClass);
182 else
183 obj = (BusinessObject) businessObjectClass.newInstance();
184 }
185 catch (Exception e) {
186 throw new RuntimeException("Cannot instantiate " + businessObjectClass.getName(), e);
187 }
188
189 createHybridBusinessObject(obj, source, template);
190
191 return obj;
192 }
193
194 public static void createHybridBusinessObject(BusinessObject businessObject, BusinessObject source, Map<String, String> template) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
195 for (String name : template.keySet()) {
196 String sourcePropertyName = template.get(name);
197 setObjectProperty(businessObject, name, easyGetPropertyType(source, sourcePropertyName), getPropertyValue(source, sourcePropertyName));
198 }
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 static public Class easyGetPropertyType(Object object, String propertyName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
216
217
218
219
220 return PropertyUtils.getPropertyType(object, propertyName);
221
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 public static Class getPropertyType(Object object, String propertyName, PersistenceStructureService persistenceStructureService) {
239 if (object == null || propertyName == null) {
240 throw new RuntimeException("Business object and property name can not be null");
241 }
242
243 Class propertyType = null;
244 try {
245 try {
246
247 propertyType = PropertyUtils.getPropertyType(object, propertyName);
248 } catch (IllegalArgumentException ex) {
249
250 } catch (NoSuchMethodException nsme) {
251
252 }
253
254
255
256
257 if (propertyType != null && propertyType.equals(PersistableBusinessObjectExtension.class)) {
258 propertyType = persistenceStructureService.getBusinessObjectAttributeClass(
259 ProxyHelper.getRealClass(object), propertyName);
260 }
261
262
263 if (null == propertyType && -1 != propertyName.indexOf('.')) {
264 if (null == persistenceStructureService) {
265 LOG.info("PropertyType couldn't be determined simply and no PersistenceStructureService was given. If you pass in a PersistenceStructureService I can look in other places to try to determine the type of the property.");
266 } else {
267 String prePeriod = StringUtils.substringBefore(propertyName, ".");
268 String postPeriod = StringUtils.substringAfter(propertyName, ".");
269
270 Class prePeriodClass = getPropertyType(object, prePeriod, persistenceStructureService);
271 Object prePeriodClassInstance = prePeriodClass.newInstance();
272 propertyType = getPropertyType(prePeriodClassInstance, postPeriod, persistenceStructureService);
273 }
274
275 } else if (Collection.class.isAssignableFrom(propertyType)) {
276 Map<String, Class> map = persistenceStructureService.listCollectionObjectTypes(object.getClass());
277 propertyType = map.get(propertyName);
278 }
279
280 } catch (Exception e) {
281 LOG.debug("unable to get property type for " + propertyName + " " + e.getMessage());
282
283 }
284
285 return propertyType;
286 }
287
288
289
290
291
292
293
294
295
296 public static Object getPropertyValue(Object businessObject, String propertyName) {
297 if (businessObject == null || propertyName == null) {
298 throw new RuntimeException("Business object and property name can not be null");
299 }
300
301 Object propertyValue = null;
302 try {
303 propertyValue = PropertyUtils.getProperty(businessObject, propertyName);
304 } catch (NestedNullException e) {
305
306 } catch (IllegalAccessException e1) {
307 LOG.error("error getting property value for " + businessObject.getClass() + "." + propertyName + " " + e1.getMessage());
308 throw new RuntimeException("error getting property value for " + businessObject.getClass() + "." + propertyName + " " + e1.getMessage(), e1);
309 } catch (InvocationTargetException e1) {
310
311 } catch (NoSuchMethodException e1) {
312 LOG.error("error getting property value for " + businessObject.getClass() + "." + propertyName + " " + e1.getMessage());
313 throw new RuntimeException("error getting property value for " + businessObject.getClass() + "." + propertyName + " " + e1.getMessage(),e1);
314 }
315
316 return propertyValue;
317 }
318
319
320
321
322
323
324
325
326
327
328
329
330
331 public static String getFormattedPropertyValue(BusinessObject businessObject, String propertyName, Formatter formatter) {
332 String propValue = KNSConstants.EMPTY_STRING;
333
334 Object prop = ObjectUtils.getPropertyValue(businessObject, propertyName);
335 if (formatter == null) {
336 propValue = formatPropertyValue(prop);
337 } else {
338 final Object formattedValue = formatter.format(prop);
339 if (formattedValue != null) {
340 propValue = String.valueOf(formattedValue);
341 }
342 }
343
344 return propValue;
345 }
346
347
348
349
350
351
352
353
354
355
356
357 public static String getFormattedPropertyValueUsingDataDictionary(BusinessObject businessObject, String propertyName) {
358 Formatter formatter = getFormatterWithDataDictionary(businessObject, propertyName);
359
360 return getFormattedPropertyValue(businessObject, propertyName, formatter);
361 }
362
363
364
365
366
367
368
369
370
371 public static String formatPropertyValue(Object propertyValue) {
372 Object propValue = KNSConstants.EMPTY_STRING;
373
374 Formatter formatter = null;
375 if (propertyValue != null) {
376 if (propertyValue instanceof Collection) {
377 formatter = new CollectionFormatter();
378 } else {
379 formatter = Formatter.getFormatter(propertyValue.getClass());
380 }
381
382 propValue = formatter != null ? formatter.format(propertyValue) : propertyValue;
383 }
384
385 return propValue != null ? String.valueOf(propValue) : KNSConstants.EMPTY_STRING;
386 }
387
388
389
390
391
392 public static void setObjectProperty(Object bo, String propertyName, Object propertyValue) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
393 Class propertyType = easyGetPropertyType(bo, propertyName);
394 setObjectProperty(bo, propertyName, propertyType, propertyValue);
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410 public static void setObjectProperty(Object bo, String propertyName, Class propertyType, Object propertyValue)
411 throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
412
413 boolean reformat = false;
414 if (propertyType != null) {
415 if (propertyValue != null && propertyType.isAssignableFrom(String.class)) {
416
417 reformat = true;
418 } else if (propertyValue != null && !propertyType.isAssignableFrom(propertyValue.getClass())) {
419
420 reformat = true;
421 }
422
423
424 if (boolean.class.isAssignableFrom(propertyType) && propertyValue == null) {
425 propertyValue = false;
426 }
427 }
428
429 Formatter formatter = getFormatterWithDataDictionary(bo, propertyName);
430 if (reformat && formatter != null) {
431 LOG.debug("reformatting propertyValue using Formatter " + formatter.getClass().getName());
432 propertyValue = formatter.convertFromPresentationFormat(propertyValue);
433 }
434
435
436 PropertyUtils.setNestedProperty(bo, propertyName, propertyValue);
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 public static void setObjectProperty(Formatter formatter, Object bo, String propertyName, Class type, Object propertyValue) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
454
455
456 if (formatter != null) {
457 propertyValue = formatter.convertFromPresentationFormat(propertyValue);
458 }
459
460
461 PropertyUtils.setNestedProperty(bo, propertyName, propertyValue);
462 }
463
464
465
466
467
468
469
470
471
472
473
474
475 public static Formatter getFormatterWithDataDictionary(Object bo, String propertyName) {
476 Formatter formatter = null;
477
478 Class boClass = bo.getClass();
479 String boPropertyName = propertyName;
480
481
482 if (StringUtils.contains(propertyName, "]")) {
483 Object collectionParent = getNestedValue(bo, StringUtils.substringBeforeLast(propertyName, "].") + "]");
484 if (collectionParent != null) {
485 boClass = collectionParent.getClass();
486 boPropertyName = StringUtils.substringAfterLast(propertyName, "].");
487 }
488 }
489
490 Class<? extends Formatter> formatterClass = KNSServiceLocator.getDataDictionaryService().getAttributeFormatter(
491 boClass, boPropertyName);
492 if (formatterClass == null) {
493 try {
494 formatterClass = Formatter.findFormatter(getPropertyType(boClass.newInstance(), boPropertyName,
495 KNSServiceLocator.getPersistenceStructureService()));
496 } catch (InstantiationException e) {
497 LOG.warn("Unable to find a formater for bo class " + boClass + " and property " + boPropertyName);
498
499 } catch (IllegalAccessException e) {
500 LOG.warn("Unable to find a formater for bo class " + boClass + " and property " + boPropertyName);
501
502 }
503 }
504
505 if (formatterClass != null) {
506 try {
507 formatter = formatterClass.newInstance();
508 } catch (Exception e) {
509 throw new RuntimeException(
510 "cannot create new instance of formatter class " + formatterClass.toString(), e);
511 }
512 }
513
514 return formatter;
515 }
516
517
518
519
520
521
522
523
524
525
526
527
528
529 public static void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
530
531
532 if (isNull(bo) || !PropertyUtils.isReadable(bo, propertyName) || (propertyValue != null && propertyValue.equals(getPropertyValue(bo, propertyName))) || (type != null && !type.equals(easyGetPropertyType(bo, propertyName)))) {
533 return;
534 }
535
536
537 materializeUpdateableCollections(bo);
538
539
540 setObjectProperty(bo, propertyName, type, propertyValue);
541
542
543 PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
544 for (int i = 0; i < propertyDescriptors.length; i++) {
545
546 PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
547
548
549 if (propertyDescriptor.getPropertyType() != null && (BusinessObject.class).isAssignableFrom(propertyDescriptor.getPropertyType()) && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) {
550 Object nestedBo = getPropertyValue(bo, propertyDescriptor.getName());
551 if ( nestedBo instanceof BusinessObject ) {
552 setObjectPropertyDeep((BusinessObject)nestedBo, propertyName, type, propertyValue);
553 }
554 }
555
556
557 else if (propertyDescriptor.getPropertyType() != null && (List.class).isAssignableFrom(propertyDescriptor.getPropertyType()) && getPropertyValue(bo, propertyDescriptor.getName()) != null) {
558
559 List propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());
560 for(Object listedBo: propertyList) {
561 if (listedBo != null && listedBo instanceof BusinessObject) {
562 setObjectPropertyDeep(listedBo, propertyName, type, propertyValue);
563 }
564 }
565 }
566 }
567 }
568
569
570
571 public static void setObjectPropertyDeep(Object bo, String propertyName, Class type, Object propertyValue, int depth) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
572
573 if (depth == 0 || isNull(bo) || !PropertyUtils.isReadable(bo, propertyName)) {
574 return;
575 }
576
577
578 materializeUpdateableCollections(bo);
579
580
581 setObjectProperty(bo, propertyName, type, propertyValue);
582
583
584 PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
585 for (int i = 0; i < propertyDescriptors.length; i++) {
586 PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
587
588
589 if (propertyDescriptor.getPropertyType() != null && (BusinessObject.class).isAssignableFrom(propertyDescriptor.getPropertyType()) && PropertyUtils.isReadable(bo, propertyDescriptor.getName())) {
590 Object nestedBo = getPropertyValue(bo, propertyDescriptor.getName());
591 if ( nestedBo instanceof BusinessObject ) {
592 setObjectPropertyDeep((BusinessObject)nestedBo, propertyName, type, propertyValue, depth - 1);
593 }
594 }
595
596
597 else if (propertyDescriptor.getPropertyType() != null && (List.class).isAssignableFrom(propertyDescriptor.getPropertyType()) && getPropertyValue(bo, propertyDescriptor.getName()) != null) {
598
599 List propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());
600
601
602 if (propertyList instanceof PersistentBag) {
603 try {
604 PersistentBag bag = (PersistentBag) propertyList;
605 PersistableBusinessObject pbo = (PersistableBusinessObject) KNSServiceLocator.getEntityManagerFactory().createEntityManager().find(bo.getClass(), bag.getKey());
606 Field field1 = pbo.getClass().getDeclaredField(propertyDescriptor.getName());
607 Field field2 = bo.getClass().getDeclaredField(propertyDescriptor.getName());
608 field1.setAccessible(true);
609 field2.setAccessible(true);
610 field2.set(bo, field1.get(pbo));
611 propertyList = (List) getPropertyValue(bo, propertyDescriptor.getName());;
612 } catch (Exception e) {
613 LOG.error(e.getMessage(), e);
614 }
615 }
616
617
618 for(Object listedBo: propertyList) {
619 if (listedBo != null && listedBo instanceof BusinessObject) {
620 setObjectPropertyDeep(listedBo, propertyName, type, propertyValue, depth - 1);
621 }
622 }
623 }
624 }
625 }
626
627
628
629
630
631
632
633
634
635
636
637 public static void materializeUpdateableCollections(Object bo) throws FormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
638 if (isNotNull(bo)) {
639 PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(bo.getClass());
640 for (int i = 0; i < propertyDescriptors.length; i++) {
641 if (KNSServiceLocator.getPersistenceStructureService().hasCollection(bo.getClass(), propertyDescriptors[i].getName()) && KNSServiceLocator.getPersistenceStructureService().isCollectionUpdatable(bo.getClass(), propertyDescriptors[i].getName())) {
642 Collection updateableCollection = (Collection) getPropertyValue(bo, propertyDescriptors[i].getName());
643 if ((updateableCollection != null) && ProxyHelper.isCollectionProxy(updateableCollection)) {
644 materializeObjects(updateableCollection);
645 }
646 }
647 }
648 }
649 }
650
651
652
653
654
655
656
657
658
659 public static String clean(String string) {
660 for (int i = 0; i < KNSConstants.QUERY_CHARACTERS.length; i++) {
661 string = StringUtils.replace(string, KNSConstants.QUERY_CHARACTERS[i], KNSConstants.EMPTY_STRING);
662 }
663 return string;
664 }
665
666
667
668
669
670
671
672
673
674
675 public static boolean equalByKeys(PersistableBusinessObject bo1, PersistableBusinessObject bo2) {
676 boolean equal = true;
677
678 if (bo1 == null && bo2 == null) {
679 equal = true;
680 }
681 else if (bo1 == null || bo2 == null) {
682 equal = false;
683 }
684 else if (!bo1.getClass().getName().equals(bo2.getClass().getName())) {
685 equal = false;
686 }
687 else {
688 Map bo1Keys = KNSServiceLocator.getPersistenceService().getPrimaryKeyFieldValues(bo1);
689 Map bo2Keys = KNSServiceLocator.getPersistenceService().getPrimaryKeyFieldValues(bo2);
690 for (Iterator iter = bo1Keys.keySet().iterator(); iter.hasNext();) {
691 String keyName = (String) iter.next();
692 if (bo1Keys.get(keyName) != null && bo2Keys.get(keyName) != null) {
693 if (!bo1Keys.get(keyName).toString().equals(bo2Keys.get(keyName).toString())) {
694 equal = false;
695 }
696 } else {
697 equal = false;
698 }
699 }
700 }
701
702
703 return equal;
704 }
705
706
707
708
709
710
711
712
713
714 public static boolean collectionContainsObjectWithIdentitcalKey(Collection<? extends PersistableBusinessObject> controlList, PersistableBusinessObject bo) {
715 boolean objectExistsInList = false;
716
717 for (Iterator i = controlList.iterator(); i.hasNext();) {
718 if (equalByKeys((PersistableBusinessObject) i.next(), bo)) {
719 return true;
720 }
721 }
722
723 return objectExistsInList;
724 }
725
726
727
728
729
730
731
732
733
734 public static int countObjectsWithIdentitcalKey(Collection<? extends PersistableBusinessObject> collection, PersistableBusinessObject bo) {
735
736 int n = 0;
737 for (PersistableBusinessObject item : collection) {
738 if (equalByKeys(item, bo)) {
739 n++;
740 }
741 }
742 return n;
743 }
744
745
746
747
748
749
750
751
752
753 public static void removeObjectWithIdentitcalKey(Collection<? extends PersistableBusinessObject> controlList, PersistableBusinessObject bo) {
754 for (Iterator<? extends PersistableBusinessObject> i = controlList.iterator(); i.hasNext();) {
755 PersistableBusinessObject listBo = i.next();
756 if (equalByKeys(listBo, bo)) {
757 i.remove();
758 }
759 }
760 }
761
762
763
764
765
766
767
768
769
770 public static BusinessObject retrieveObjectWithIdentitcalKey(Collection<? extends PersistableBusinessObject> controlList, PersistableBusinessObject bo) {
771 BusinessObject returnBo = null;
772
773 for (Iterator<? extends PersistableBusinessObject> i = controlList.iterator(); i.hasNext();) {
774 PersistableBusinessObject listBo = i.next();
775 if (equalByKeys(listBo, bo)) {
776 returnBo = listBo;
777 }
778 }
779
780 return returnBo;
781 }
782
783
784
785
786
787
788
789
790 public static boolean isNestedAttribute(String attributeName) {
791 boolean isNested = false;
792
793 if (StringUtils.contains(attributeName, ".")) {
794 isNested = true;
795 }
796
797 return isNested;
798 }
799
800
801
802
803
804
805
806
807 public static String getNestedAttributePrefix(String attributeName) {
808 String prefix = "";
809
810 if (StringUtils.contains(attributeName, ".")) {
811 prefix = StringUtils.substringBeforeLast(attributeName, ".");
812 }
813
814 return prefix;
815 }
816
817
818
819
820
821
822
823
824 public static String getNestedAttributePrimitive(String attributeName) {
825 String primitive = attributeName;
826
827 if (StringUtils.contains(attributeName, ".")) {
828 primitive = StringUtils.substringAfterLast(attributeName, ".");
829 }
830
831 return primitive;
832 }
833
834
835
836
837
838
839
840
841
842
843 public static boolean isNull(Object object) {
844
845
846 if (object == null) {
847 return true;
848 }
849
850
851
852 if (ProxyHelper.isProxy(object) || ProxyHelper.isCollectionProxy(object)) {
853 if (ProxyHelper.getRealObject(object) == null) {
854 return true;
855 }
856 }
857
858 return false;
859 }
860
861
862
863
864
865
866
867
868
869
870 public static boolean isNotNull(Object object) {
871 return !ObjectUtils.isNull(object);
872 }
873
874
875
876
877
878
879
880 public static void materializeObjects(Collection possiblyProxiedObjects) {
881 for (Iterator i = possiblyProxiedObjects.iterator(); i.hasNext();) {
882 ObjectUtils.isNotNull(i.next());
883 }
884 }
885
886
887
888
889
890
891
892
893
894
895
896
897 public static void materializeSubObjectsToDepth(PersistableBusinessObject bo, int depth) {
898 if (bo == null) {
899 throw new IllegalArgumentException("The bo passed in was null.");
900 }
901 if (depth < 0 || depth > 5) {
902 throw new IllegalArgumentException("The depth passed in was out of bounds. Only values " + "between 0 and 5, inclusively, are allowed.");
903 }
904
905
906 if (depth == 0) {
907 return;
908 }
909
910
911 if (ProxyHelper.isProxy(bo)) {
912 if (!ProxyHelper.isMaterialized(bo)) {
913 throw new IllegalArgumentException("The bo passed in is an un-materialized proxy, and cannot be used.");
914 }
915 }
916
917
918 if ( KNSServiceLocator.getPersistenceStructureService().isPersistable( bo.getClass() ) ) {
919 Map<String, Class> references = KNSServiceLocator.getPersistenceStructureService().listReferenceObjectFields(bo);
920
921
922 String referenceName = "";
923 Class referenceClass = null;
924 Object referenceValue = null;
925 Object realReferenceValue = null;
926
927
928 for (Iterator iter = references.keySet().iterator(); iter.hasNext();) {
929 referenceName = (String) iter.next();
930 referenceClass = references.get(referenceName);
931
932
933 referenceValue = getPropertyValue(bo, referenceName);
934 if (referenceValue != null) {
935 if (ProxyHelper.isProxy(referenceValue)) {
936 realReferenceValue = ProxyHelper.getRealObject(referenceValue);
937 if (realReferenceValue != null) {
938 try {
939 setObjectProperty(bo, referenceName, referenceClass, realReferenceValue);
940 }
941 catch (FormatException e) {
942 throw new RuntimeException("FormatException: could not set the property '" + referenceName + "'.", e);
943 }
944 catch (IllegalAccessException e) {
945 throw new RuntimeException("IllegalAccessException: could not set the property '" + referenceName + "'.", e);
946 }
947 catch (InvocationTargetException e) {
948 throw new RuntimeException("InvocationTargetException: could not set the property '" + referenceName + "'.", e);
949 }
950 catch (NoSuchMethodException e) {
951 throw new RuntimeException("NoSuchMethodException: could not set the property '" + referenceName + "'.", e);
952 }
953 }
954 }
955
956
957 if (realReferenceValue instanceof PersistableBusinessObject && depth > 1) {
958 materializeSubObjectsToDepth((PersistableBusinessObject) realReferenceValue, depth - 1);
959 }
960 }
961
962 }
963 }
964 }
965
966
967
968
969
970
971
972
973
974
975 public static void materializeAllSubObjects(PersistableBusinessObject bo) {
976 materializeSubObjectsToDepth(bo, 3);
977 }
978
979
980
981
982
983
984
985
986
987
988
989
990 public static Object getNestedValue(Object bo, String fieldName) {
991
992 if (bo == null) {
993 throw new IllegalArgumentException("The bo passed in was null.");
994 }
995 if (StringUtils.isBlank(fieldName)) {
996 throw new IllegalArgumentException("The fieldName passed in was blank.");
997 }
998
999
1000
1001
1002
1003 String[] fieldNameParts = fieldName.split("\\.");
1004 Object currentObject = null;
1005 Object priorObject = bo;
1006 for (int i = 0; i < fieldNameParts.length; i++) {
1007 String fieldNamePart = fieldNameParts[i];
1008
1009 try {
1010 if (fieldNamePart.indexOf("]") > 0) {
1011 currentObject = PropertyUtils.getIndexedProperty(priorObject, fieldNamePart);
1012 }
1013 else {
1014 currentObject = PropertyUtils.getSimpleProperty(priorObject, fieldNamePart);
1015 }
1016 }
1017 catch (IllegalAccessException e) {
1018 throw new RuntimeException("Caller does not have access to the property accessor method.", e);
1019 }
1020 catch (InvocationTargetException e) {
1021 throw new RuntimeException("Property accessor method threw an exception.", e);
1022 }
1023 catch (NoSuchMethodException e) {
1024 throw new RuntimeException("The accessor method requested for this property cannot be found.", e);
1025 }
1026
1027
1028 if (ProxyHelper.isProxy(currentObject)) {
1029 currentObject = ProxyHelper.getRealObject(currentObject);
1030 }
1031
1032
1033
1034 if (currentObject == null) {
1035 return currentObject;
1036 }
1037
1038 priorObject = currentObject;
1039 }
1040 return currentObject;
1041 }
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051 public static boolean nullSafeEquals(Object obj1, Object obj2) {
1052 if (obj1 != null && obj2 != null) {
1053 return obj1.equals(obj2);
1054 }
1055 else {
1056 return (obj1 == obj2);
1057 }
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070 public static Object createNewObjectFromClass(Class clazz) {
1071 if (clazz == null) {
1072 throw new RuntimeException("BO class was passed in as null");
1073 }
1074 try {
1075 if (ExternalizableBusinessObject.class.isAssignableFrom(clazz)) {
1076 Class eboInterface = ExternalizableBusinessObjectUtils.determineExternalizableBusinessObjectSubInterface(clazz);
1077 ModuleService moduleService = KNSServiceLocator.getKualiModuleService().getResponsibleModuleService(eboInterface);
1078 return moduleService.createNewObjectFromExternalizableClass(eboInterface);
1079 }
1080 else {
1081 return clazz.newInstance();
1082 }
1083 } catch (Exception e) {
1084 throw new RuntimeException("Error occured while trying to create a new instance for class " + clazz,e);
1085 }
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 public static boolean isWriteable(Object o, String p, PersistenceStructureService persistenceStructureService)
1098 throws IllegalArgumentException {
1099 if (null == o || null == p) {
1100 throw new IllegalArgumentException("Cannot check writeable status with null arguments.");
1101 }
1102
1103 boolean b = false;
1104
1105
1106 if (!(PropertyUtils.isWriteable(o, p))) {
1107
1108 if (-1 != p.indexOf('.')) {
1109 String[] parts = p.split("\\.");
1110
1111
1112 Class c = ObjectUtils.getPropertyType(o, parts[0], persistenceStructureService);
1113
1114 if (c != null) {
1115 Object i = null;
1116
1117
1118 if (Collection.class.isAssignableFrom(c)) {
1119 Map<String, Class> m = persistenceStructureService.listCollectionObjectTypes(o.getClass());
1120 c = m.get(parts[0]);
1121 }
1122
1123
1124 try {
1125 i = c.newInstance();
1126
1127 StringBuffer sb = new StringBuffer();
1128 for (int x = 1; x < parts.length; x++) {
1129 sb.append(1 == x ? "" : ".").append(parts[x]);
1130 }
1131 b = isWriteable(i, sb.toString(), persistenceStructureService);
1132
1133 } catch (Exception ex) {
1134 LOG.error("Skipping Criteria: " + p + " - Unable to instantiate class : " + c.getName(), ex);
1135 }
1136 } else {
1137 LOG.error("Skipping Criteria: " + p + " - Unable to determine class for object: "
1138 + o.getClass().getName() + " - " + parts[0]);
1139 }
1140 }
1141
1142 } else {
1143 b = true;
1144 }
1145
1146 return b;
1147 }
1148 }