1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch.service.impl;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.ole.gl.batch.dataaccess.ReconciliationDao;
24 import org.kuali.ole.gl.batch.service.ReconciliationService;
25 import org.kuali.ole.gl.businessobject.OriginEntryFull;
26 import org.kuali.ole.gl.exception.LoadException;
27 import org.kuali.ole.sys.Message;
28 import org.kuali.rice.core.api.util.type.KualiDecimal;
29 import org.kuali.rice.core.api.util.type.TypeUtils;
30 import org.springframework.transaction.annotation.Transactional;
31
32
33
34
35 @Transactional
36 public class ReconciliationServiceImpl implements ReconciliationService {
37 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ReconciliationServiceImpl.class);
38
39 private ReconciliationDao reconciliationDao;
40 private Class<? extends OriginEntryFull> originEntryClass;
41
42
43
44
45
46
47
48
49
50
51
52 protected class JavaAttributeAugmentedColumnReconciliation {
53 protected ColumnReconciliation columnReconciliation;
54 protected List<String> javaAttributeNames;
55
56 protected JavaAttributeAugmentedColumnReconciliation() {
57 columnReconciliation = null;
58 javaAttributeNames = null;
59 }
60
61
62
63
64
65
66 protected ColumnReconciliation getColumnReconciliation() {
67 return columnReconciliation;
68 }
69
70
71
72
73
74
75 protected void setColumnReconciliation(ColumnReconciliation columnReconciliation) {
76 this.columnReconciliation = columnReconciliation;
77 }
78
79
80
81
82
83
84 protected void setJavaAttributeNames(List<String> javaAttributeNames) {
85 this.javaAttributeNames = javaAttributeNames;
86 }
87
88 protected String getJavaAttributeName(int index) {
89 return javaAttributeNames.get(index);
90 }
91
92
93
94
95
96
97 protected int size() {
98 return javaAttributeNames.size();
99 }
100 }
101
102
103
104
105
106
107
108
109
110
111 public void reconcile(Iterator<OriginEntryFull> entries, ReconciliationBlock reconBlock, List<Message> errorMessages) {
112 List<ColumnReconciliation> columns = reconBlock.getColumns();
113
114 int numEntriesSuccessfullyLoaded = 0;
115
116
117 int numEntriesAttemptedToLoad = 1;
118
119
120 List<JavaAttributeAugmentedColumnReconciliation> javaAttributeNames = resolveJavaAttributeNames(columns);
121 KualiDecimal[] columnSums = createColumnSumsArray(columns.size());
122
123
124
125
126
127
128
129
130
131
132 boolean entriesFullyIterated = false;
133
134
135 boolean loadExceptionEncountered = false;
136
137 while (!entriesFullyIterated) {
138 try {
139 while (entries.hasNext()) {
140 numEntriesAttemptedToLoad++;
141 OriginEntryFull entry = entries.next();
142 for (int c = 0; c < columns.size(); c++) {
143
144 KualiDecimal columnValue = KualiDecimal.ZERO;
145
146 for (int f = 0; f < javaAttributeNames.get(c).size(); f++) {
147 String javaAttributeName = javaAttributeNames.get(c).getJavaAttributeName(f);
148 Object fieldValue = entry.getFieldValue(javaAttributeName);
149
150 if (fieldValue == null) {
151
152 }
153 else {
154 if (TypeUtils.isIntegralClass(fieldValue.getClass()) || TypeUtils.isDecimalClass(fieldValue.getClass())) {
155 KualiDecimal castValue;
156 if (fieldValue instanceof KualiDecimal) {
157 castValue = (KualiDecimal) fieldValue;
158 }
159 else {
160 castValue = new KualiDecimal(fieldValue.toString());
161 }
162 columnValue = columnValue.add(castValue);
163 }
164 else {
165 throw new LoadException("The value for " + columns.get(c).getTokenizedFieldNames()[f] + " is not a numeric value.");
166 }
167 }
168 }
169 columnSums[c] = columnSums[c].add(columnValue);
170 }
171 numEntriesSuccessfullyLoaded++;
172 }
173 }
174 catch (LoadException e) {
175 loadExceptionEncountered = true;
176 LOG.error("Line " + numEntriesAttemptedToLoad + " parse error: " + e.getMessage(), e);
177 Message newMessage = new Message("Line " + numEntriesAttemptedToLoad + " parse error: " + e.getMessage(), Message.TYPE_FATAL);
178 errorMessages.add(newMessage);
179
180 numEntriesAttemptedToLoad++;
181 continue;
182 }
183 catch (Exception e) {
184
185
186
187 LOG.error("Error encountered trying to iterate through origin entry iterator", e);
188
189 Message newMessage = new Message(e.getMessage(), Message.TYPE_FATAL);
190 errorMessages.add(newMessage);
191
192 break;
193 }
194 entriesFullyIterated = true;
195 }
196
197 if (entriesFullyIterated) {
198 if (loadExceptionEncountered) {
199
200 LOG.error("Reconciliation check failed because some origin entry lines could not be parsed.");
201 Message newMessage = new Message("Reconciliation check failed because some origin entry lines could not be parsed.", Message.TYPE_FATAL);
202 errorMessages.add(newMessage);
203 }
204 else {
205
206 if (numEntriesSuccessfullyLoaded != reconBlock.getRowCount()) {
207 Message newMessage = generateRowCountMismatchMessage(reconBlock, numEntriesSuccessfullyLoaded);
208 errorMessages.add(newMessage);
209 }
210
211
212
213
214
215 for (int i = 0; i < columns.size(); i++) {
216 if (!columnSums[i].equals(columns.get(i).getDollarAmount())) {
217 Message newMessage = generateColumnSumErrorMessage(columns.get(i), columnSums[i]);
218 errorMessages.add(newMessage);
219 }
220 }
221 }
222 }
223 }
224
225
226
227
228
229
230
231
232 protected Message generateColumnSumErrorMessage(ColumnReconciliation column, KualiDecimal actualValue) {
233
234
235 StringBuilder buf = new StringBuilder();
236 buf.append("Reconciliation failed for field value(s) \"");
237 buf.append(column.getFieldName());
238 buf.append("\", expected ");
239 buf.append(column.getDollarAmount());
240 buf.append(", found value ");
241 buf.append(actualValue);
242 buf.append(".");
243
244 Message newMessage = new Message(buf.toString(), Message.TYPE_FATAL);
245 return newMessage;
246 }
247
248
249
250
251
252
253
254
255 protected Message generateRowCountMismatchMessage(ReconciliationBlock block, int actualRowCount) {
256
257
258 StringBuilder buf = new StringBuilder();
259 buf.append("Reconciliation failed because an incorrect number of origin entry rows were successfully parsed. Expected ");
260 buf.append(block.getRowCount());
261 buf.append(" row(s), parsed ");
262 buf.append(actualRowCount);
263 buf.append(" row(s).");
264
265 Message newMessage = new Message(buf.toString(), Message.TYPE_FATAL);
266 return newMessage;
267 }
268
269
270
271
272
273
274
275
276
277
278
279 protected boolean performSanityChecks(List<ColumnReconciliation> columns, List<JavaAttributeAugmentedColumnReconciliation> javaAttributeNames, KualiDecimal[] columnSums, List<Message> errorMessages) {
280 boolean success = true;
281
282 if (javaAttributeNames.size() != columnSums.length || javaAttributeNames.size() != columns.size()) {
283
284 errorMessages.add(new Message("Reconciliation error: Sizes of lists do not match", Message.TYPE_FATAL));
285 success = false;
286 }
287 for (int i = 0; i < columns.size(); i++) {
288 if (columns.get(i).getTokenizedFieldNames().length != javaAttributeNames.get(i).size()) {
289 errorMessages.add(new Message("Reconciliation error: Error tokenizing column elements. The number of database fields and java fields do not match.", Message.TYPE_FATAL));
290 success = false;
291 }
292 for (int fieldIdx = 0; fieldIdx < javaAttributeNames.get(i).size(); i++) {
293 if (StringUtils.isBlank(javaAttributeNames.get(i).getJavaAttributeName(fieldIdx))) {
294 errorMessages.add(new Message("Reconciliation error: javaAttributeName is blank for DB column: " + columns.get(i).getTokenizedFieldNames()[fieldIdx], Message.TYPE_FATAL));
295 success = false;
296 }
297 }
298 }
299 return success;
300 }
301
302
303
304
305
306
307
308 protected KualiDecimal[] createColumnSumsArray(int size) {
309 KualiDecimal[] array = new KualiDecimal[size];
310 for (int i = 0; i < array.length; i++) {
311 array[i] = KualiDecimal.ZERO;
312 }
313 return array;
314 }
315
316
317
318
319
320
321
322
323
324 protected List<JavaAttributeAugmentedColumnReconciliation> resolveJavaAttributeNames(List<ColumnReconciliation> columns) {
325 List<JavaAttributeAugmentedColumnReconciliation> attributes = new ArrayList<JavaAttributeAugmentedColumnReconciliation>();
326 for (ColumnReconciliation column : columns) {
327 JavaAttributeAugmentedColumnReconciliation c = new JavaAttributeAugmentedColumnReconciliation();
328 c.setColumnReconciliation(column);
329 c.setJavaAttributeNames(reconciliationDao.convertDBColumnNamesToJavaName(getOriginEntryClass(), column.getTokenizedFieldNames(), true));
330 attributes.add(c);
331 }
332 return attributes;
333 }
334
335
336
337
338
339
340 protected ReconciliationDao getReconciliationDao() {
341 return reconciliationDao;
342 }
343
344
345
346
347
348
349 public void setReconciliationDao(ReconciliationDao reconciliationDao) {
350 this.reconciliationDao = reconciliationDao;
351 }
352
353
354
355
356
357
358 protected Class<? extends OriginEntryFull> getOriginEntryClass() {
359 return originEntryClass;
360 }
361
362
363
364
365
366
367 public void setOriginEntryClass(Class<? extends OriginEntryFull> originEntryClass) {
368 this.originEntryClass = originEntryClass;
369 }
370 }