1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.document;
17
18 import java.math.BigDecimal;
19 import java.text.SimpleDateFormat;
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.List;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.kuali.ole.gl.businessobject.CorrectionChange;
26 import org.kuali.ole.gl.businessobject.CorrectionChangeGroup;
27 import org.kuali.ole.gl.businessobject.CorrectionCriteria;
28 import org.kuali.ole.gl.businessobject.OriginEntryFull;
29 import org.kuali.ole.gl.businessobject.OriginEntryStatistics;
30 import org.kuali.ole.gl.businessobject.options.OriginEntryFieldFinder;
31 import org.kuali.ole.sys.OLEConstants;
32 import org.kuali.ole.sys.context.SpringContext;
33 import org.kuali.rice.core.api.util.type.KualiDecimal;
34 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
35
36
37
38
39 public class CorrectionDocumentUtils {
40 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(CorrectionDocumentUtils.class);
41 public static final int DEFAULT_RECORD_COUNT_FUNCTIONALITY_LIMIT = 1000;
42
43
44
45
46 public static final int RECORD_COUNT_FUNCTIONALITY_LIMIT_IS_NONE = 0;
47
48
49
50
51 public static final int RECORD_COUNT_FUNCTIONALITY_LIMIT_IS_UNLIMITED = -1;
52
53 public static final int DEFAULT_RECORDS_PER_PAGE = 10;
54
55
56
57
58
59
60 public static int getRecordCountFunctionalityLimit() {
61 String limitString = SpringContext.getBean(ParameterService.class).getParameterValueAsString(GeneralLedgerCorrectionProcessDocument.class, OLEConstants.GeneralLedgerCorrectionProcessApplicationParameterKeys.RECORD_COUNT_FUNCTIONALITY_LIMIT);
62 if (limitString != null) {
63 return Integer.valueOf(limitString);
64 }
65
66 return DEFAULT_RECORD_COUNT_FUNCTIONALITY_LIMIT;
67 }
68
69
70
71
72
73
74 public static int getRecordsPerPage() {
75 String limitString = SpringContext.getBean(ParameterService.class).getParameterValueAsString(GeneralLedgerCorrectionProcessDocument.class, OLEConstants.GeneralLedgerCorrectionProcessApplicationParameterKeys.RECORDS_PER_PAGE);
76 if (limitString != null) {
77 return Integer.valueOf(limitString);
78 }
79 return DEFAULT_RECORDS_PER_PAGE;
80 }
81
82
83
84
85
86
87
88
89 public static boolean isRestrictedFunctionalityMode(int inputGroupSize, int recordCountFunctionalityLimit) {
90 return (recordCountFunctionalityLimit != CorrectionDocumentUtils.RECORD_COUNT_FUNCTIONALITY_LIMIT_IS_UNLIMITED && inputGroupSize >= recordCountFunctionalityLimit) || recordCountFunctionalityLimit == CorrectionDocumentUtils.RECORD_COUNT_FUNCTIONALITY_LIMIT_IS_NONE;
91 }
92
93
94
95
96
97
98
99
100 public static boolean validCorrectionCriteriaForAdding(CorrectionCriteria correctionCriteria) {
101 String fieldName = correctionCriteria.getCorrectionFieldName();
102 if (StringUtils.isBlank(fieldName)) {
103 return false;
104 }
105 return true;
106 }
107
108
109
110
111
112
113
114 public static boolean validCorrectionCriteriaForSaving(CorrectionCriteria correctionCriteria) {
115 return correctionCriteria == null || (StringUtils.isBlank(correctionCriteria.getCorrectionFieldName()) && StringUtils.isBlank(correctionCriteria.getCorrectionFieldValue()));
116 }
117
118
119
120
121
122
123
124
125 public static boolean validCorrectionChangeForAdding(CorrectionChange correctionChange) {
126 String fieldName = correctionChange.getCorrectionFieldName();
127 if (StringUtils.isBlank(fieldName)) {
128 return false;
129 }
130 return true;
131 }
132
133
134
135
136
137
138
139
140 public static boolean validCorrectionChangeForSaving(CorrectionChange correctionChange) {
141 return correctionChange == null || (StringUtils.isBlank(correctionChange.getCorrectionFieldName()) && StringUtils.isBlank(correctionChange.getCorrectionFieldValue()));
142 }
143
144
145
146
147
148
149 public static void setAllEntryIdsToNull(Collection<OriginEntryFull> originEntries) {
150 for (OriginEntryFull entry : originEntries) {
151 entry.setEntryId(null);
152 }
153 }
154
155
156
157
158
159
160 public static void setSequentialEntryIds(Collection<OriginEntryFull> originEntries) {
161 int index = 0;
162 for (OriginEntryFull entry : originEntries) {
163 entry.setEntryId(new Integer(index));
164 index++;
165 }
166 }
167
168
169
170
171
172
173
174
175
176 public static boolean entryMatchesCriteria(CorrectionCriteria cc, OriginEntryFull oe) {
177 OriginEntryFieldFinder oeff = new OriginEntryFieldFinder();
178 Object fieldActualValue = oe.getFieldValue(cc.getCorrectionFieldName());
179 String fieldTestValue = StringUtils.isBlank(cc.getCorrectionFieldValue()) ? "" : cc.getCorrectionFieldValue();
180 String fieldType = oeff.getFieldType(cc.getCorrectionFieldName());
181
182 String fieldActualValueString = convertToString(fieldActualValue, fieldType);
183
184 if ("String".equals(fieldType) || "sw".equals(cc.getCorrectionOperatorCode()) || "ew".equals(cc.getCorrectionOperatorCode()) || "ct".equals(cc.getCorrectionOperatorCode())) {
185 return compareStringData(cc, fieldTestValue, fieldActualValueString);
186 }
187 int compareTo = 0;
188 try {
189 if (fieldActualValue == null) {
190 return false;
191 }
192 if ("Integer".equals(fieldType)) {
193 compareTo = ((Integer) fieldActualValue).compareTo(Integer.parseInt(fieldTestValue));
194 }
195 if ("KualiDecimal".equals(fieldType)) {
196 compareTo = ((KualiDecimal) fieldActualValue).compareTo(new KualiDecimal(Double.parseDouble(fieldTestValue)));
197 }
198 if ("BigDecimal".equals(fieldType)) {
199 compareTo = ((BigDecimal) fieldActualValue).compareTo(new BigDecimal(Double.parseDouble(fieldTestValue)));
200
201 }
202 if ("Date".equals(fieldType)) {
203 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
204 compareTo = ((Date) fieldActualValue).compareTo(df.parse(fieldTestValue));
205 }
206 }
207 catch (Exception e) {
208
209 return false;
210 }
211 return compareTo(compareTo, cc.getCorrectionOperatorCode());
212 }
213
214
215
216
217
218
219
220
221
222
223 public static boolean compareStringData(CorrectionCriteria cc, String fieldTestValue, String fieldActualValueString) {
224 if ("eq".equals(cc.getCorrectionOperatorCode())) {
225 return fieldActualValueString.equals(fieldTestValue);
226 }
227 else if ("ne".equals(cc.getCorrectionOperatorCode())) {
228 return (!fieldActualValueString.equals(fieldTestValue));
229 }
230 else if ("sw".equals(cc.getCorrectionOperatorCode())) {
231 return fieldActualValueString.startsWith(fieldTestValue);
232 }
233 else if ("ew".equals(cc.getCorrectionOperatorCode())) {
234 return fieldActualValueString.endsWith(fieldTestValue);
235 }
236 else if ("ct".equals(cc.getCorrectionOperatorCode())) {
237 return (fieldActualValueString.indexOf(fieldTestValue) > -1);
238 }
239 else if ("lt".equals(cc.getCorrectionOperatorCode())) {
240 return (fieldActualValueString.compareTo(fieldTestValue) < 0);
241 }
242 else if ("le".equals(cc.getCorrectionOperatorCode())) {
243 return (fieldActualValueString.compareTo(fieldTestValue) <= 0);
244 }
245 else if ("gt".equals(cc.getCorrectionOperatorCode())) {
246 return (fieldActualValueString.compareTo(fieldTestValue) > 0);
247 }
248 else if ("ge".equals(cc.getCorrectionOperatorCode())) {
249 return (fieldActualValueString.compareTo(fieldTestValue) >= 0);
250 }
251 throw new IllegalArgumentException("Unknown operator: " + cc.getCorrectionOperatorCode());
252 }
253
254
255
256
257
258
259
260
261 public static boolean compareTo(int compareTo, String operatorCode) {
262 if ("eq".equals(operatorCode)) {
263 return (compareTo == 0);
264 }
265 else if ("ne".equals(operatorCode)) {
266 return (compareTo != 0);
267 }
268 else if ("lt".equals(operatorCode)) {
269 return (compareTo < 0);
270 }
271 else if ("le".equals(operatorCode)) {
272 return (compareTo <= 0);
273 }
274 else if ("gt".equals(operatorCode)) {
275 return (compareTo > 0);
276 }
277 else if ("ge".equals(operatorCode)) {
278 return (compareTo >= 0);
279 }
280 throw new IllegalArgumentException("Unknown operator: " + operatorCode);
281 }
282
283
284
285
286
287
288
289
290 public static String convertToString(Object fieldActualValue, String fieldType) {
291 if (fieldActualValue == null) {
292 return "";
293 }
294 if ("String".equals(fieldType)) {
295 return (String) fieldActualValue;
296 }
297 else if ("Integer".equals(fieldType)) {
298 Integer i = (Integer) fieldActualValue;
299 return i.toString();
300 }
301 else if ("KualiDecimal".equals(fieldType)) {
302 KualiDecimal kd = (KualiDecimal) fieldActualValue;
303 return kd.toString();
304 }
305 else if ("BigDecimal".equals(fieldType)) {
306 BigDecimal bd = (BigDecimal) fieldActualValue;
307 return bd.toString();
308 }
309 else if ("Date".equals(fieldType)) {
310 Date d = (Date) fieldActualValue;
311 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
312 return df.format(d);
313 }
314 return "";
315 }
316
317
318
319
320
321
322
323
324
325
326 public static OriginEntryFull applyCriteriaToEntry(OriginEntryFull entry, boolean matchCriteriaOnly, List<CorrectionChangeGroup> changeCriteriaGroups) {
327 if (matchCriteriaOnly && !doesEntryMatchAnyCriteriaGroups(entry, changeCriteriaGroups)) {
328 return null;
329 }
330
331 for (CorrectionChangeGroup ccg : changeCriteriaGroups) {
332 int matches = 0;
333 for (CorrectionCriteria cc : ccg.getCorrectionCriteria()) {
334 if (entryMatchesCriteria(cc, entry)) {
335 matches++;
336 }
337 }
338
339
340 if (matches == ccg.getCorrectionCriteria().size()) {
341 for (CorrectionChange change : ccg.getCorrectionChange()) {
342
343 entry.setFieldValue(change.getCorrectionFieldName(), change.getCorrectionFieldValue());
344 }
345 }
346 }
347 return entry;
348 }
349
350
351
352
353
354
355
356
357 public static boolean doesEntryMatchAnyCriteriaGroups(OriginEntryFull entry, Collection<CorrectionChangeGroup> groups) {
358 boolean anyGroupMatch = false;
359 for (CorrectionChangeGroup ccg : groups) {
360 int matches = 0;
361 for (CorrectionCriteria cc : ccg.getCorrectionCriteria()) {
362 if (CorrectionDocumentUtils.entryMatchesCriteria(cc, entry)) {
363 matches++;
364 }
365 }
366
367
368 if (matches == ccg.getCorrectionCriteria().size()) {
369 anyGroupMatch = true;
370 break;
371 }
372 }
373 return anyGroupMatch;
374 }
375
376
377
378
379
380
381
382 public static OriginEntryStatistics getStatistics(Collection<OriginEntryFull> entries) {
383 OriginEntryStatistics oes = new OriginEntryStatistics();
384
385 for (OriginEntryFull oe : entries) {
386 updateStatisticsWithEntry(oe, oes);
387 }
388 return oes;
389 }
390
391
392
393
394
395
396
397 public static boolean isDebit(OriginEntryFull oe) {
398 return (OLEConstants.GL_DEBIT_CODE.equals(oe.getTransactionDebitCreditCode()));
399 }
400
401
402
403
404
405
406
407 public static boolean isBudget(OriginEntryFull oe) {
408 return OLEConstants.GL_BUDGET_CODE.equals(oe.getTransactionDebitCreditCode());
409 }
410
411
412
413
414
415
416
417 public static boolean isCredit(OriginEntryFull oe) {
418 return OLEConstants.GL_CREDIT_CODE.equals(oe.getTransactionDebitCreditCode());
419 }
420
421
422
423
424
425
426
427 public static void updateStatisticsWithEntry(OriginEntryFull entry, OriginEntryStatistics statistics) {
428 statistics.incrementCount();
429 if (isDebit(entry)) {
430 statistics.addDebit(entry.getTransactionLedgerEntryAmount());
431 }
432 else if (isCredit(entry)) {
433 statistics.addCredit(entry.getTransactionLedgerEntryAmount());
434 }
435 else {
436 statistics.addBudget(entry.getTransactionLedgerEntryAmount());
437 }
438 }
439
440
441
442
443
444
445
446 public static void copyStatisticsToDocument(OriginEntryStatistics statistics, GeneralLedgerCorrectionProcessDocument document) {
447 document.setCorrectionCreditTotalAmount(statistics.getCreditTotalAmount());
448 document.setCorrectionDebitTotalAmount(statistics.getDebitTotalAmount());
449 document.setCorrectionBudgetTotalAmount(statistics.getBudgetTotalAmount());
450 document.setCorrectionRowCount(statistics.getRowCount());
451 }
452 }