View Javadoc
1   /*
2    * Copyright 2008-2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Performs custom fiscal year process for University Date records
31   */
32  public class UniversityDateFiscalYearMakerImpl extends FiscalYearMakerImpl {
33      private static final Logger LOG = org.apache.log4j.Logger.getLogger(UniversityDateFiscalYearMakerImpl.class);
34  
35      /**
36       * @see org.kuali.ole.coa.batch.dataaccess.impl.FiscalYearMakerHelperImpl#performCustomProcessing(java.lang.Integer)
37       */
38      @Override
39      public void performCustomProcessing(Integer baseFiscalYear, boolean firstCopyYear) {
40          int fiscalYearStartMonth = getFiscalYearStartMonth(baseFiscalYear);
41  
42          // determine start date year, if start month is not January the year will be one behind the fiscal year
43          int startDateYear = baseFiscalYear;
44          if (Calendar.JANUARY == fiscalYearStartMonth) {
45              startDateYear += 1;
46          }
47          getPersistenceBrokerTemplate();
48          // start with first day of fiscal year and create records for each year up to end date
49          GregorianCalendar univPeriodDate = new GregorianCalendar(startDateYear, fiscalYearStartMonth, 1);
50  
51          // setup end date
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          // the fiscal year is always the year of the ending date of the fiscal year
57          Integer nextFiscalYear = enddate.get(Calendar.YEAR);
58  
59          // get rid of any records already existing for next fiscal year
60          deleteNewYearRows(nextFiscalYear);
61  
62          // initialize the period variables
63          int period = 1;
64          String periodString = String.format("%02d", period);
65          int compareMonth = univPeriodDate.get(Calendar.MONTH);
66          int currentMonth = compareMonth;
67  
68          // loop through the dates until we are past end date
69          while (univPeriodDate.compareTo(enddate) <= 0) {
70              // if we hit period 13 something went wrong
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              // create the university date record
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              // add one to day for the next record
85              univPeriodDate.add(Calendar.DAY_OF_MONTH, 1);
86  
87              // does this kick us into a new month and therefore a new accounting period?
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       * Removes all UniversityDate records for the given fiscal year
99       * 
100      * @param requestYear year to delete records for
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      * Retrieves the system options record for the base fiscal year to determine the fiscal year start month
110      * 
111      * @param baseFiscalYear fiscal year to retrieve options record for
112      * @return int fiscal year start month (0 being Jan)
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      * @see org.kuali.ole.coa.batch.dataaccess.impl.FiscalYearMakerHelperImpl#doCustomProcessingOnly()
131      */
132     @Override
133     public boolean doCustomProcessingOnly() {
134         return true;
135     }
136 
137     /**
138      * Gets the businessObjectService attribute.
139      * 
140      * @return Returns the businessObjectService.
141      */
142     protected BusinessObjectService getBusinessObjectService() {
143         return businessObjectService;
144     }
145 
146     /**
147      * Sets the businessObjectService attribute value.
148      * 
149      * @param businessObjectService The businessObjectService to set.
150      */
151     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
152         this.businessObjectService = businessObjectService;
153     }
154 
155 }