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