1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.gl.batch;
17
18 import java.io.BufferedReader;
19 import java.io.FileReader;
20 import java.io.IOException;
21 import java.io.PrintStream;
22 import java.sql.Date;
23 import java.text.SimpleDateFormat;
24 import java.util.ArrayList;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.kuali.ole.coa.businessobject.Chart;
32 import org.kuali.ole.coa.businessobject.Organization;
33 import org.kuali.ole.gl.GeneralLedgerConstants;
34 import org.kuali.ole.gl.batch.service.RunDateService;
35 import org.kuali.ole.gl.batch.service.impl.OriginEntryTotals;
36 import org.kuali.ole.gl.businessobject.CollectorDetail;
37 import org.kuali.ole.gl.businessobject.CollectorHeader;
38 import org.kuali.ole.gl.businessobject.OriginEntryFull;
39 import org.kuali.ole.gl.businessobject.OriginEntryGroup;
40 import org.kuali.ole.gl.businessobject.OriginEntrySource;
41 import org.kuali.ole.gl.report.CollectorReportData;
42 import org.kuali.ole.gl.service.CollectorDetailService;
43 import org.kuali.ole.sys.OLEConstants;
44 import org.kuali.ole.sys.OLEKeyConstants;
45 import org.kuali.ole.sys.OLEPropertyConstants;
46 import org.kuali.ole.sys.ObjectUtil;
47 import org.kuali.ole.sys.context.SpringContext;
48 import org.kuali.ole.sys.exception.ParseException;
49 import org.kuali.rice.core.api.datetime.DateTimeService;
50 import org.kuali.rice.core.api.util.type.KualiDecimal;
51 import org.kuali.rice.kns.service.BusinessObjectDictionaryService;
52 import org.kuali.rice.kns.service.DataDictionaryService;
53 import org.kuali.rice.krad.bo.PersistableBusinessObjectBase;
54 import org.kuali.rice.krad.service.BusinessObjectService;
55 import org.kuali.rice.krad.util.MessageMap;
56
57
58
59
60 public class CollectorBatch extends PersistableBusinessObjectBase {
61
62 private String batchName;
63
64
65 private String universityFiscalYear;
66 private String chartOfAccountsCode;
67 private String organizationCode;
68 private Date transmissionDate;
69 private String recordType;
70
71
72 private String personUserID;
73 private Integer batchSequenceNumber;
74 private String emailAddress;
75 private String campusCode;
76 private String phoneNumber;
77 private String mailingAddress;
78 private String departmentName;
79
80 private List<OriginEntryFull> originEntries;
81 private List<CollectorDetail> collectorDetails;
82
83
84 private String firstEmptyField;
85 private String secondEmptyField;
86 private Integer totalRecords;
87 private KualiDecimal totalAmount;
88
89 private MessageMap messageMap;
90 private OriginEntryTotals originEntryTotals;
91
92 private boolean headerlessBatch;
93
94 private static CollectorBatchHeaderFieldUtil collectorBatchHeaderFieldUtil;
95 private static CollectorBatchTrailerRecordFieldUtil collectorBatchTrailerRecordFieldUtil;
96
97
98
99
100 public CollectorBatch() {
101 originEntries = new ArrayList();
102 collectorDetails = new ArrayList();
103 messageMap = new MessageMap();
104 originEntryTotals = null;
105 totalRecords = 0;
106 headerlessBatch = false;
107 }
108
109
110
111
112 public String getUniversityFiscalYear() {
113 return universityFiscalYear;
114 }
115
116
117
118
119 public void setUniversityFiscalYear(String universityFiscalYear) {
120 this.universityFiscalYear = universityFiscalYear;
121 }
122
123
124
125
126 public Integer getBatchSequenceNumber() {
127 return batchSequenceNumber;
128 }
129
130
131
132
133 public void setBatchSequenceNumber(Integer batchSequenceNumber) {
134 this.batchSequenceNumber = batchSequenceNumber;
135 }
136
137
138
139
140 public String getChartOfAccountsCode() {
141 return chartOfAccountsCode;
142 }
143
144
145
146
147 public void setChartOfAccountsCode(String chartOfAccountsCode) {
148 this.chartOfAccountsCode = chartOfAccountsCode;
149 }
150
151
152
153
154 public String getOrganizationCode() {
155 return organizationCode;
156 }
157
158
159
160
161 public void setOrganizationCode(String organizationCode) {
162 this.organizationCode = organizationCode;
163 }
164
165
166
167
168 public KualiDecimal getTotalAmount() {
169 return totalAmount;
170 }
171
172
173
174
175 public void setTotalAmount(KualiDecimal totalAmount) {
176 this.totalAmount = totalAmount;
177 }
178
179
180
181
182 public void setTotalAmount(String totalAmount) {
183 this.totalAmount = new KualiDecimal(totalAmount);
184 }
185
186
187
188
189 public void clearTotalAmount() {
190 this.totalAmount = null;
191 }
192
193
194
195
196 public String getSecondEmptyField() {
197 return secondEmptyField;
198 }
199
200
201
202
203 public void setSecondEmptyField(String secondEmptyField) {
204 this.secondEmptyField = secondEmptyField;
205 }
206
207
208
209
210 public String getFirstEmptyField() {
211 return firstEmptyField;
212 }
213
214
215
216
217 public void setFirstEmptyField(String firstEmptyField) {
218 this.firstEmptyField = firstEmptyField;
219 }
220
221
222
223
224 public Integer getTotalRecords() {
225 return totalRecords;
226 }
227
228
229
230
231 public void setTotalRecords(Integer totalRecords) {
232 this.totalRecords = totalRecords;
233 }
234
235
236
237
238 public Date getTransmissionDate() {
239 return transmissionDate;
240 }
241
242
243
244
245 public void setTransmissionDate(Date transmissionDate) {
246 this.transmissionDate = transmissionDate;
247 }
248
249
250
251
252 public String getRecordType() {
253 return recordType;
254 }
255
256
257
258
259 public void setRecordType(String recordType) {
260 this.recordType = recordType;
261 }
262
263
264
265
266 public String getEmailAddress() {
267 return emailAddress;
268 }
269
270
271
272
273 public void setEmailAddress(String emailAddress) {
274 this.emailAddress = emailAddress;
275 }
276
277
278
279
280 public String getPersonUserID() {
281 return personUserID;
282 }
283
284
285
286
287 public void setPersonUserID(String personUserID) {
288 this.personUserID = personUserID;
289 }
290
291
292
293
294 public List<CollectorDetail> getCollectorDetails() {
295 return collectorDetails;
296 }
297
298
299
300
301 public void setCollectorDetails(List<CollectorDetail> idDetails) {
302 this.collectorDetails = idDetails;
303 }
304
305
306
307
308 public List<OriginEntryFull> getOriginEntries() {
309 return originEntries;
310 }
311
312
313
314
315 public void setOriginEntries(List<OriginEntryFull> batchOriginEntry) {
316 this.originEntries = batchOriginEntry;
317 }
318
319
320
321
322 public void addOriginEntry(OriginEntryFull orginEntry) {
323 this.originEntries.add(orginEntry);
324 }
325
326
327
328
329 public void addCollectorDetail(CollectorDetail collectorDetail) {
330 this.collectorDetails.add(collectorDetail);
331 }
332
333
334
335
336
337
338 public CollectorHeader retrieveDuplicateHeader() {
339
340 CollectorHeader checkHeader = createCollectorHeaderWithPKValuesOnly();
341
342 CollectorHeader foundHeader = (CollectorHeader) SpringContext.getBean(BusinessObjectService.class).retrieve(checkHeader);
343 return foundHeader;
344 }
345
346
347
348
349
350
351
352
353 public void setDefaultsAndStore(CollectorReportData collectorReportData, String demergerOutputFileName, PrintStream originEntryOutputPs) {
354 BusinessObjectService businessObjectService = SpringContext.getBean(BusinessObjectService.class);
355 CollectorDetailService collectorDetailService = SpringContext.getBean(CollectorDetailService.class);
356
357
358 CollectorHeader persistHeader = createCollectorHeaderForStorage();
359 CollectorHeader foundHeader = retrieveDuplicateHeader();
360
361 if (foundHeader != null) {
362
363 persistHeader.setVersionNumber(foundHeader.getVersionNumber());
364 }
365 businessObjectService.save(persistHeader);
366
367
368
369 BufferedReader inputFileReader = null;
370 try {
371 inputFileReader = new BufferedReader(new FileReader(demergerOutputFileName));
372 String line = null;
373 while ((line = inputFileReader.readLine()) != null) {
374 originEntryOutputPs.printf("%s\n", line);
375 }
376 }
377 catch (IOException e) {
378 throw new RuntimeException("IO Error encountered trying to persist collector batch.", e);
379 }
380 finally {
381 IOUtils.closeQuietly(inputFileReader);
382 inputFileReader = null;
383 }
384 Date nowDate = new Date(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime());
385 RunDateService runDateService = SpringContext.getBean(RunDateService.class);
386 Date createDate = new java.sql.Date((runDateService.calculateRunDate(nowDate).getTime()));
387
388 Integer sequenceNumber = new Integer(0);
389 Integer nextSequence = collectorDetailService.getNextCreateSequence(createDate);
390 if (nextSequence != null) {
391 sequenceNumber = nextSequence;
392 }
393 int countOfdetails = collectorDetails.size();
394 for (int numSavedDetails = 0; numSavedDetails < countOfdetails; numSavedDetails++) {
395 CollectorDetail idDetail = this.collectorDetails.get(numSavedDetails);
396
397
398 idDetail.setTransactionLedgerEntrySequenceNumber( ++sequenceNumber);
399 idDetail.setCreateDate(createDate);
400 CollectorDetail foundIdDetail = (CollectorDetail) businessObjectService.retrieve(idDetail);
401 if (foundIdDetail != null) {
402 idDetail.setVersionNumber(foundIdDetail.getVersionNumber());
403 }
404
405 businessObjectService.save(idDetail);
406 }
407
408 collectorReportData.setNumSavedDetails(this, countOfdetails);
409 }
410
411
412
413
414 public void prepareDataForStorage() {
415 BusinessObjectDictionaryService businessObjectDictionaryService = SpringContext.getBean(BusinessObjectDictionaryService.class);
416 DataDictionaryService dataDictionaryService = SpringContext.getBean(DataDictionaryService.class);
417
418
419 if (dataDictionaryService.getAttributeForceUppercase(Chart.class, OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE)) {
420 setChartOfAccountsCode(getChartOfAccountsCode().toUpperCase());
421 }
422 if (dataDictionaryService.getAttributeForceUppercase(Organization.class, OLEPropertyConstants.ORGANIZATION_CODE)) {
423 setOrganizationCode(getOrganizationCode().toUpperCase());
424 }
425
426
427 for (OriginEntryFull entry : originEntries) {
428 businessObjectDictionaryService.performForceUppercase(entry);
429 }
430
431
432 for (CollectorDetail collectorDetail : collectorDetails) {
433 businessObjectDictionaryService.performForceUppercase(collectorDetail);
434 }
435 }
436
437
438
439
440
441
442 private OriginEntryGroup createOriginEntryGroup() {
443 OriginEntryGroup group = new OriginEntryGroup();
444
445 group.setSourceCode(OriginEntrySource.COLLECTOR);
446 group.setDate(new java.sql.Date(SpringContext.getBean(DateTimeService.class).getCurrentDate().getTime()));
447 group.setProcess(new Boolean(true));
448 group.setScrub(new Boolean(true));
449 group.setValid(new Boolean(true));
450
451 return group;
452 }
453
454
455
456
457
458
459 public CollectorHeader createCollectorHeaderForStorage() {
460 CollectorHeader header = new CollectorHeader();
461
462 header.setChartOfAccountsCode(getChartOfAccountsCode());
463 header.setOrganizationCode(getOrganizationCode());
464 header.setProcessTransmissionDate(getTransmissionDate());
465 header.setProcessBatchSequenceNumber(getBatchSequenceNumber());
466 header.setProcessTotalRecordCount(getTotalRecords());
467 header.setProcessTotalAmount(getTotalAmount());
468 header.setContactCampusCode(getCampusCode());
469 header.setContactPersonPhoneNumber(getPhoneNumber());
470 header.setContactMailingAddress(getMailingAddress());
471 header.setContactDepartmentName(getDepartmentName());
472
473 return header;
474 }
475
476
477
478
479
480
481
482 public CollectorHeader createCollectorHeaderWithPKValuesOnly() {
483 CollectorHeader header = new CollectorHeader();
484
485 header.setChartOfAccountsCode(getChartOfAccountsCode());
486 header.setOrganizationCode(getOrganizationCode());
487 header.setProcessTransmissionDate(getTransmissionDate());
488 header.setProcessBatchSequenceNumber(getBatchSequenceNumber());
489 header.setProcessTotalRecordCount(getTotalRecords());
490 header.setProcessTotalAmount(getTotalAmount());
491
492 return header;
493 }
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514 public String getCampusCode() {
515 return campusCode;
516 }
517
518
519
520
521
522
523 public void setCampusCode(String campusCode) {
524 this.campusCode = campusCode;
525 }
526
527
528
529
530
531
532 public String getDepartmentName() {
533 return departmentName;
534 }
535
536
537
538
539
540
541 public void setDepartmentName(String departmentName) {
542 this.departmentName = departmentName;
543 }
544
545
546
547
548
549
550 public String getMailingAddress() {
551 return mailingAddress;
552 }
553
554
555
556
557
558
559 public void setMailingAddress(String mailingAddress) {
560 this.mailingAddress = mailingAddress;
561 }
562
563
564
565
566
567
568 public String getPhoneNumber() {
569 return phoneNumber;
570 }
571
572
573
574
575
576
577 public void setPhoneNumber(String phoneNumber) {
578 this.phoneNumber = phoneNumber;
579 }
580
581
582
583
584
585
586 public String getBatchName() {
587 return batchName;
588 }
589
590
591
592
593
594
595 public void setBatchName(String batchName) {
596 this.batchName = batchName;
597 }
598
599 public MessageMap getMessageMap() {
600 return messageMap;
601 }
602
603 public void setMessageMap(MessageMap messageMap) {
604 if (messageMap == null) {
605 throw new NullPointerException("messageMap is null");
606 }
607 if (this.messageMap.hasMessages()) {
608 throw new RuntimeException("Cannot reset MessageMap unless original instance has no messages.");
609 }
610 this.messageMap = messageMap;
611 }
612
613 public OriginEntryTotals getOriginEntryTotals() {
614 return originEntryTotals;
615 }
616
617 public void setOriginEntryTotals(OriginEntryTotals originEntryTotals) {
618 this.originEntryTotals = originEntryTotals;
619 }
620
621 public boolean isHeaderlessBatch() {
622 return headerlessBatch;
623 }
624
625 public void setHeaderlessBatch(boolean headerlessBatch) {
626 this.headerlessBatch = headerlessBatch;
627 }
628
629 protected LinkedHashMap toStringMapper_RICE20_REFACTORME() {
630 LinkedHashMap map = new LinkedHashMap();
631 map.put("chartOfAccountsCode", getChartOfAccountsCode());
632 map.put("organizationCode", getOrganizationCode());
633 map.put("transmissionDate", getTransmissionDate());
634 map.put("personUserID", getPersonUserID());
635 map.put("batchSequenceNumber", getBatchSequenceNumber());
636 map.put("emailAddress", getEmailAddress());
637 map.put("campusCode", getCampusCode());
638 map.put("phoneNumber", getPhoneNumber());
639 map.put("mailingAddress", getMailingAddress());
640 map.put("departmentName", getDepartmentName());
641 map.put("firstEmptyField", getFirstEmptyField());
642 map.put("totalRecords", getTotalRecords());
643 map.put("secondEmptyField", getSecondEmptyField());
644 map.put("totalAmount", getTotalAmount());
645
646 return map;
647 }
648
649 protected Date parseSqlDate(String date) throws ParseException {
650 try {
651 return new Date(new SimpleDateFormat("yy-MM-dd").parse(date).getTime());
652 }
653 catch (java.text.ParseException e) {
654 throw new ParseException(e.getMessage(), e);
655 }
656 }
657
658 protected String getValue(String headerLine, int s, int e) {
659 return org.springframework.util.StringUtils.trimTrailingWhitespace(StringUtils.substring(headerLine, s, e));
660 }
661
662
663
664
665 protected static CollectorBatchHeaderFieldUtil getCollectorBatchHeaderFieldUtil() {
666 if (collectorBatchHeaderFieldUtil == null) {
667 collectorBatchHeaderFieldUtil = new CollectorBatchHeaderFieldUtil();
668 }
669 return collectorBatchHeaderFieldUtil;
670 }
671
672 public void setFromTextFileForCollectorBatch(String headerLine) {
673 try{
674 final Map<String, Integer> pMap = getCollectorBatchHeaderFieldUtil().getFieldBeginningPositionMap();
675
676 headerLine = org.apache.commons.lang.StringUtils.rightPad(headerLine, GeneralLedgerConstants.getSpaceAllCollectorBatchHeaderFields().length(), ' ');
677
678 setChartOfAccountsCode(getValue(headerLine, pMap.get(OLEPropertyConstants.CHART_OF_ACCOUNTS_CODE), pMap.get(OLEPropertyConstants.ORGANIZATION_CODE)));
679 setOrganizationCode(getValue(headerLine, pMap.get(OLEPropertyConstants.ORGANIZATION_CODE), pMap.get(OLEPropertyConstants.TRANSMISSION_DATE)));
680
681 String transmissionDate = org.apache.commons.lang.StringUtils.trim(getValue(headerLine, pMap.get(OLEPropertyConstants.TRANSMISSION_DATE), pMap.get(OLEPropertyConstants.COLLECTOR_BATCH_RECORD_TYPE)));
682 try {
683 setTransmissionDate(parseSqlDate(transmissionDate));
684 }
685 catch (ParseException e) {
686 getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.Collector.HEADER_BAD_TRANSMISSION_DATE_FORMAT, transmissionDate);
687 setTransmissionDate(null);
688 }
689 String batchNumber = org.apache.commons.lang.StringUtils.trim(getValue(headerLine, pMap.get(OLEPropertyConstants.BATCH_SEQUENCE_NUMBER), pMap.get(OLEPropertyConstants.EMAIL_ADDRESS)));
690
691 if (ObjectUtil.isInteger(batchNumber)) {
692 setBatchSequenceNumber(new Integer(batchNumber));
693 } else {
694 setBatchSequenceNumber(0);
695 }
696 setEmailAddress(getValue(headerLine, pMap.get(OLEPropertyConstants.EMAIL_ADDRESS), pMap.get(OLEPropertyConstants.COLLECTOR_BATCH_PERSON_USER_ID)));
697 setPersonUserID(getValue(headerLine, pMap.get(OLEPropertyConstants.COLLECTOR_BATCH_PERSON_USER_ID), pMap.get(OLEPropertyConstants.DEPARTMENT_NAME)));
698 setDepartmentName(getValue(headerLine, pMap.get(OLEPropertyConstants.DEPARTMENT_NAME), pMap.get(OLEPropertyConstants.MAILING_ADDRESS)));
699 setMailingAddress(getValue(headerLine, pMap.get(OLEPropertyConstants.MAILING_ADDRESS), pMap.get(OLEPropertyConstants.CAMPUS_CODE)));
700 setCampusCode(getValue(headerLine, pMap.get(OLEPropertyConstants.CAMPUS_CODE), pMap.get(OLEPropertyConstants.PHONE_NUMBER)));
701 setPhoneNumber(org.apache.commons.lang.StringUtils.trim(getValue(headerLine, pMap.get(OLEPropertyConstants.PHONE_NUMBER), GeneralLedgerConstants.getSpaceAllCollectorBatchHeaderFields().length())));
702 } catch (Exception e){
703 throw new RuntimeException(e + " occurred in CollectorBatch.setFromTextFileForCollectorBatch()");
704 }
705 }
706
707
708
709
710 protected static CollectorBatchTrailerRecordFieldUtil getCollectorBatchTrailerRecordFieldUtil() {
711 if (collectorBatchTrailerRecordFieldUtil == null) {
712 collectorBatchTrailerRecordFieldUtil = new CollectorBatchTrailerRecordFieldUtil();
713 }
714 return collectorBatchTrailerRecordFieldUtil;
715 }
716
717 public void setFromTextFileForCollectorBatchTrailerRecord(String trailerLine, int lineNumber) {
718 final Map<String, Integer> pMap = getCollectorBatchTrailerRecordFieldUtil().getFieldBeginningPositionMap();
719
720 trailerLine = org.apache.commons.lang.StringUtils.rightPad(trailerLine, GeneralLedgerConstants.getSpaceAllCollectorBatchTrailerFields().length(), ' ');
721 setTotalRecords(new Integer(org.apache.commons.lang.StringUtils.trim(getValue(trailerLine, pMap.get(OLEPropertyConstants.TOTAL_RECORDS), pMap.get(OLEPropertyConstants.TRAILER_RECORD_SECOND_EMPTY_FIELD)))));
722
723 String trailerAmount = org.apache.commons.lang.StringUtils.trim(getValue(trailerLine, pMap.get(OLEPropertyConstants.TOTAL_AMOUNT), GeneralLedgerConstants.getSpaceAllCollectorBatchTrailerFields().length()));
724
725 try {
726 setTotalAmount(trailerAmount);
727 }
728 catch (NumberFormatException e) {
729 setTotalAmount(KualiDecimal.ZERO);
730 getMessageMap().putError(OLEConstants.GLOBAL_ERRORS, OLEKeyConstants.ERROR_CUSTOM, "Collector trailer total amount cannot be parsed on line " + lineNumber + " amount string " + trailerAmount);
731 }
732 }
733 }