1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.coa.batch.dataaccess.impl;
17
18 import java.sql.Date;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.GregorianCalendar;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.kuali.ole.coa.businessobject.AccountingPeriod;
27 import org.kuali.ole.sys.OLEPropertyConstants;
28 import org.kuali.ole.sys.batch.dataaccess.impl.FiscalYearMakerImpl;
29 import org.kuali.ole.sys.businessobject.FiscalYearBasedBusinessObject;
30
31
32
33
34 public class AccountingPeriodFiscalYearMakerImpl extends FiscalYearMakerImpl {
35
36 public AccountingPeriodFiscalYearMakerImpl() {
37 super();
38
39 super.setAllowOverrideTargetYear(false);
40 }
41
42
43
44
45
46
47
48 @Override
49 public void changeForNewYear(Integer baseFiscalYear, FiscalYearBasedBusinessObject currentRecord) {
50 super.changeForNewYear(baseFiscalYear, currentRecord);
51
52 AccountingPeriod accountingPeriod = (AccountingPeriod) currentRecord;
53
54
55 String fiscalPeriodName = accountingPeriod.getUniversityFiscalPeriodName();
56
57 String oldCalendarStartYear = new Integer(accountingPeriod.getUniversityFiscalYear() - 2).toString();
58 String oldCalendarEndYear = new Integer(accountingPeriod.getUniversityFiscalYear() - 1).toString();
59
60 String newCalendarStartYear = new Integer(accountingPeriod.getUniversityFiscalYear() - 1).toString();
61 String newCalendarEndYear = new Integer(accountingPeriod.getUniversityFiscalYear()).toString();
62
63
64 if (StringUtils.contains(fiscalPeriodName, oldCalendarEndYear)) {
65 fiscalPeriodName = StringUtils.replace(fiscalPeriodName, oldCalendarEndYear, newCalendarEndYear);
66 }
67 else if (StringUtils.contains(fiscalPeriodName, oldCalendarStartYear)) {
68 fiscalPeriodName = StringUtils.replace(fiscalPeriodName, oldCalendarStartYear, newCalendarStartYear);
69 }
70 else {
71 fiscalPeriodName = updateTwoDigitYear(newCalendarEndYear.substring(2, 4), oldCalendarEndYear.substring(2, 4), fiscalPeriodName);
72 fiscalPeriodName = updateTwoDigitYear(newCalendarStartYear.substring(2, 4), oldCalendarStartYear.substring(2, 4), fiscalPeriodName);
73 }
74
75 accountingPeriod.setUniversityFiscalPeriodName(fiscalPeriodName);
76
77
78 accountingPeriod.setUniversityFiscalPeriodEndDate(addYearToDate(accountingPeriod.getUniversityFiscalPeriodEndDate()));
79
80
81 accountingPeriod.setActive(false);
82 }
83
84
85
86
87
88
89 @Override
90 public void performCustomProcessing(Integer baseFiscalYear, boolean firstCopyYear) {
91 if (!firstCopyYear) {
92 return;
93 }
94 Collection<AccountingPeriod> accountingPeriods = businessObjectService.findMatching(AccountingPeriod.class,Collections.singletonMap(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, baseFiscalYear + 1));
95 for (AccountingPeriod accountingPeriod : accountingPeriods) {
96 accountingPeriod.setActive(true);
97 businessObjectService.save(accountingPeriod);
98 }
99 }
100
101
102
103
104
105
106
107 protected java.sql.Date addYearToDate(Date inDate) {
108 GregorianCalendar currentCalendarDate = new GregorianCalendar();
109 currentCalendarDate.clear();
110
111 currentCalendarDate.setTimeInMillis(inDate.getTime());
112 currentCalendarDate.add(GregorianCalendar.YEAR, 1);
113
114 return new Date(currentCalendarDate.getTimeInMillis());
115 }
116
117
118
119
120
121
122
123
124
125
126 protected String updateTwoDigitYear(String newYear, String oldYear, String currentString) {
127
128
129
130 String regExpString = "(([^0-9]{1}" + oldYear + ")|^(" + oldYear + "))";
131 Pattern pattern = Pattern.compile(regExpString);
132 Matcher matcher = pattern.matcher(currentString);
133
134
135 boolean matched = matcher.find();
136 if (!matched) {
137
138 return currentString;
139 }
140
141
142
143 String returnString = currentString;
144 StringBuffer outString = new StringBuffer();
145
146 if (matcher.group(3) != null) {
147
148
149 matcher.appendReplacement(outString, newYear);
150
151 matched = matcher.find();
152 }
153
154 while (matched) {
155
156
157
158
159 String newYearString = matcher.group(2).substring(0, 1) + newYear;
160 matcher.appendReplacement(outString, newYearString);
161 matched = matcher.find();
162 }
163
164
165 matcher.appendTail(outString);
166
167 return outString.toString();
168 }
169
170 }