1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.batch.dataaccess.impl;
17
18 import java.sql.Date;
19 import java.util.Calendar;
20 import java.util.Collections;
21 import java.util.GregorianCalendar;
22
23 import org.apache.log4j.Logger;
24 import org.kuali.ole.sys.OLEPropertyConstants;
25 import org.kuali.ole.sys.businessobject.SystemOptions;
26 import org.kuali.ole.sys.businessobject.UniversityDate;
27 import org.kuali.rice.krad.service.BusinessObjectService;
28
29
30
31
32 public class UniversityDateFiscalYearMakerImpl extends FiscalYearMakerImpl {
33 private static final Logger LOG = org.apache.log4j.Logger.getLogger(UniversityDateFiscalYearMakerImpl.class);
34
35
36
37
38 @Override
39 public void performCustomProcessing(Integer baseFiscalYear, boolean firstCopyYear) {
40 int fiscalYearStartMonth = getFiscalYearStartMonth(baseFiscalYear);
41
42
43 int startDateYear = baseFiscalYear;
44 if (Calendar.JANUARY == fiscalYearStartMonth) {
45 startDateYear += 1;
46 }
47 getPersistenceBrokerTemplate();
48
49 GregorianCalendar univPeriodDate = new GregorianCalendar(startDateYear, fiscalYearStartMonth, 1);
50
51
52 GregorianCalendar enddate = new GregorianCalendar(univPeriodDate.get(Calendar.YEAR), univPeriodDate.get(Calendar.MONTH), univPeriodDate.get(Calendar.DAY_OF_MONTH));
53 enddate.add(Calendar.MONTH, 12);
54 enddate.add(Calendar.DAY_OF_MONTH, -1);
55
56
57 Integer nextFiscalYear = enddate.get(Calendar.YEAR);
58
59
60 deleteNewYearRows(nextFiscalYear);
61
62
63 int period = 1;
64 String periodString = String.format("%02d", period);
65 int compareMonth = univPeriodDate.get(Calendar.MONTH);
66 int currentMonth = compareMonth;
67
68
69 while (univPeriodDate.compareTo(enddate) <= 0) {
70
71 if (period == 13) {
72 LOG.error("Hit period 13 while creating university date records");
73 throw new RuntimeException("Hit period 13 while creating university date records");
74 }
75
76
77 UniversityDate universityDate = new UniversityDate();
78 universityDate.setUniversityFiscalYear(nextFiscalYear);
79 universityDate.setUniversityDate(new Date(univPeriodDate.getTimeInMillis()));
80 universityDate.setUniversityFiscalAccountingPeriod(periodString);
81
82 businessObjectService.save(universityDate);
83
84
85 univPeriodDate.add(Calendar.DAY_OF_MONTH, 1);
86
87
88 compareMonth = univPeriodDate.get(Calendar.MONTH);
89 if (currentMonth != compareMonth) {
90 period = period + 1;
91 periodString = String.format("%02d", period);
92 currentMonth = compareMonth;
93 }
94 }
95 }
96
97
98
99
100
101
102 protected void deleteNewYearRows(Integer requestYear) {
103 businessObjectService.deleteMatching(UniversityDate.class, Collections.singletonMap(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, requestYear));
104
105 LOG.warn(String.format("\n rows for %d deleted", requestYear));
106 }
107
108
109
110
111
112
113
114 protected int getFiscalYearStartMonth(Integer baseFiscalYear) {
115 SystemOptions systemOptions = new SystemOptions();
116 systemOptions.setUniversityFiscalYear(baseFiscalYear);
117
118 SystemOptions foundOptions = (SystemOptions) businessObjectService.retrieve(systemOptions);
119 if (foundOptions == null) {
120 LOG.error("Unable to retrieve system options record for fiscal year " + baseFiscalYear);
121 throw new RuntimeException("Unable to retrieve system options record for fiscal year " + baseFiscalYear);
122 }
123
124 Integer fiscalYearStartMonth = Integer.parseInt(foundOptions.getUniversityFiscalYearStartMo());
125
126 return fiscalYearStartMonth - 1;
127 }
128
129
130
131
132 @Override
133 public boolean doCustomProcessingOnly() {
134 return true;
135 }
136
137
138
139
140
141
142 protected BusinessObjectService getBusinessObjectService() {
143 return businessObjectService;
144 }
145
146
147
148
149
150
151 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
152 this.businessObjectService = businessObjectService;
153 }
154
155 }