01: package com.bm.utils;
02:
03: import java.util.List;
04:
05: import org.apache.log4j.Logger;
06:
07: import com.bm.introspectors.Introspector;
08: import com.bm.introspectors.PersistentPropertyInfo;
09: import com.bm.introspectors.Property;
10:
11: /**
12: * Sets nullable fields to null.
13: *
14: * @author Daniel Wiese
15: * @since 16.10.2005
16: */
17: public final class NullableSetter {
18:
19: private static final Logger log = Logger
20: .getLogger(NullableSetter.class);
21:
22: private NullableSetter() {
23: // intetinally empty
24: }
25:
26: /**
27: * Sets nullable fields to null.
28: *
29: * @author Daniel Wiese
30: * @since 16.10.2005
31: * @param <T> -
32: * the type of the bean
33: * @param beans -
34: * collection of beans
35: * @param intro -
36: * the introspector of the bean class
37: */
38: public static <T> void setFieldsToNull(List<T> beans,
39: Introspector<T> intro) {
40:
41: // iterate over all beans
42: for (T aktBean : beans) {
43: setFieldsToNull(aktBean, intro);
44: }
45: }
46:
47: /**
48: * Sets nullable fields to null.
49: *
50: * @author Daniel Wiese
51: * @since 16.10.2005
52: * @param <T> -
53: * the type of the bean
54: * @param bean -
55: * the bean
56: * @param intro -
57: * the introspector of the bean class
58: */
59: public static <T> void setFieldsToNull(T bean, Introspector<T> intro) {
60: List<Property> fields = intro.getPersitentProperties();
61:
62: for (Property aktProperty : fields) {
63: final PersistentPropertyInfo pfi = intro
64: .getPresistentFieldInfo(aktProperty);
65: // pk fields are by default not nullable
66: if (pfi.isNullable()
67: && !intro.getPkFields().contains(aktProperty)) {
68: try {
69: intro.setField(bean, aktProperty, null);
70: } catch (IllegalAccessException e) {
71: log.error("Canīt set the field "
72: + aktProperty.getName() + " to null");
73: throw new RuntimeException("Canīt set the field "
74: + aktProperty.getName() + " to null");
75: }
76: }
77: }
78: }
79:
80: }
|