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