1 package org.kuali.ole.sys.batch;
2
3 import org.apache.commons.beanutils.PropertyUtils;
4 import org.kuali.ole.sys.businessobject.format.BatchDateFormatter;
5 import org.kuali.rice.core.web.format.Formatter;
6 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
7 import org.springframework.util.StringUtils;
8
9
10
11
12
13
14 public abstract class AbstractFlatFilePropertySpecificationBase implements FlatFilePropertySpecification {
15 protected String propertyName;
16 protected boolean rightTrim;
17 protected boolean leftTrim;
18 protected Class<? extends Formatter> formatterClass = Formatter.class;
19 protected String dateFormat;
20 protected boolean formatToTimestamp = false;
21
22
23
24
25 @Override
26 public String getPropertyName() {
27 return propertyName;
28 }
29
30
31
32
33
34
35
36
37 @Override
38 public void setProperty(String value, Object businessObject, int lineNumber) {
39 if (leftTrim) {
40 value = StringUtils.trimLeadingWhitespace(value);
41 }
42
43 if (rightTrim) {
44 value = StringUtils.trimTrailingWhitespace(value);
45 }
46 try {
47 PropertyUtils.setProperty(businessObject, propertyName, getFormattedObject(value, businessObject));
48 }
49 catch (Exception e) {
50 throw new RuntimeException("Exception occurred on line " + lineNumber + " while setting value " + value + " for property " + propertyName + "." , e);
51 }
52 }
53
54
55
56
57
58
59 protected Class<?> getFormatterClass(Object parsedObject) {
60 if (Formatter.class.isAssignableFrom(formatterClass) ) {
61 Class<? extends Formatter> attributeFormatter = KRADServiceLocatorWeb.getDataDictionaryService().getAttributeFormatter(parsedObject.getClass(), this.propertyName);
62 if (attributeFormatter != null) {
63 this.formatterClass = attributeFormatter;
64 }
65 }
66
67 if (!Formatter.class.isAssignableFrom(this.formatterClass)) {
68 throw new RuntimeException("formatterClass is not a valid instance of " + Formatter.class.getName() + " instead was: " + formatterClass.getName());
69 }
70 return formatterClass;
71 }
72
73
74
75
76
77
78 protected Formatter getFormatter(Object parsedObject) {
79 Formatter formatter = null;
80 try {
81 formatter = (Formatter) getFormatterClass(parsedObject).newInstance();
82 }
83 catch (InstantiationException ie) {
84 throw new RuntimeException("Could not instantiate object of class " + formatterClass.getName(), ie);
85 }
86 catch (IllegalAccessException iae) {
87 throw new RuntimeException("Illegal access attempting to instantiate object of class " + formatterClass.getName(), iae);
88 }
89 return formatter;
90 }
91
92
93
94
95
96 public void setFormatterClass(Class<? extends Formatter> formatterClass) {
97 if (!Formatter.class.isAssignableFrom(formatterClass)) {
98 throw new RuntimeException("formatterClass is not a valid instance of " + Formatter.class.getName() + " instead was: " + formatterClass.getName());
99 }
100 this.formatterClass = formatterClass;
101 }
102
103
104
105
106
107
108
109 protected Object getFormattedObject(String subString, Object parsedObject) {
110 Formatter formatter = getFormatter(parsedObject);
111 if (formatter instanceof BatchDateFormatter) {
112 ((BatchDateFormatter) formatter).setDateFormat(dateFormat);
113 if (formatToTimestamp) {
114 ((BatchDateFormatter) formatter).setFormatToTimestamp(true);
115 }
116 }
117 return formatter.convertFromPresentationFormat(subString);
118 }
119
120
121
122
123
124 public void setPropertyName(String propertyName) {
125 this.propertyName = propertyName;
126 }
127
128
129
130
131
132 public void setRightTrim(boolean rightTrim) {
133 this.rightTrim = rightTrim;
134 }
135
136
137
138
139
140 public void setLeftTrim(boolean leftTrim) {
141 this.leftTrim = leftTrim;
142 }
143
144
145
146
147
148
149 public void setDateFormat(String dateFormat) {
150 this.dateFormat = dateFormat;
151 }
152
153
154
155
156
157
158 public void setFormatToTimestamp(boolean formatToTimestamp) {
159 this.formatToTimestamp = formatToTimestamp;
160 }
161
162 }