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.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   * Performs custom population of accounting periods records for a new year being created in the fiscal year maker process
33   */
34  public class AccountingPeriodFiscalYearMakerImpl extends FiscalYearMakerImpl {
35      
36      public AccountingPeriodFiscalYearMakerImpl() {
37          super();
38          
39          super.setAllowOverrideTargetYear(false);
40      }
41  
42      /**
43       * Updates the year on the fiscal period name and sets status to open for next year records
44       * 
45       * @see org.kuali.ole.coa.batch.dataaccess.impl.FiscalYearMakerHelperImpl#changeForNewYear(java.lang.Integer,
46       *      org.kuali.rice.krad.bo.PersistableBusinessObject)
47       */
48      @Override
49      public void changeForNewYear(Integer baseFiscalYear, FiscalYearBasedBusinessObject currentRecord) {
50          super.changeForNewYear(baseFiscalYear, currentRecord);
51  
52          AccountingPeriod accountingPeriod = (AccountingPeriod) currentRecord;
53  
54          // update fiscal period name which contains the fiscal year
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          // replace 4 digit year in name if found, else replace 2 digit
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          // increment period end date by one year
78          accountingPeriod.setUniversityFiscalPeriodEndDate(addYearToDate(accountingPeriod.getUniversityFiscalPeriodEndDate()));
79  
80          // set status to closed
81          accountingPeriod.setActive(false);
82      }
83  
84      /**
85       * Retrieves all Accounting Period records for the first copied fiscal year and make active
86       * 
87       * @see org.kuali.ole.coa.batch.dataaccess.impl.FiscalYearMakerHelperImpl#performCustomProcessing(java.lang.Integer)
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      * Adds one year to the given date
103      * 
104      * @param inDate date to increment
105      * @return Date incoming date plus one year
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      * this routine is provided to update string fields which contain two-digit years that need to be updated for display. it is
119      * very specific, but it's necessary. "two-digit year" means the two numeric characters preceded by a non-numeric character.
120      * 
121      * @param newYear
122      * @param oldYear
123      * @param currentString
124      * @return the updated string for a two digit year
125      */
126     protected String updateTwoDigitYear(String newYear, String oldYear, String currentString) {
127         // group 1 is the bounded by the outermost set of parentheses
128         // group 2 is the first inner set
129         // group 3 is the second inner set--a two-digit year at the beginning of the line
130         String regExpString = "(([^0-9]{1}" + oldYear + ")|^(" + oldYear + "))";
131         Pattern pattern = Pattern.compile(regExpString);
132         Matcher matcher = pattern.matcher(currentString);
133 
134         // start looking for a match
135         boolean matched = matcher.find();
136         if (!matched) {
137             // just return if nothing is found
138             return currentString;
139         }
140 
141         // we found something
142         // we have to process it
143         String returnString = currentString;
144         StringBuffer outString = new StringBuffer();
145         // is there a match at the beginning of the line (a match with group 3)?
146         if (matcher.group(3) != null) {
147             // there is a two-digit-year string at the beginning of the line
148             // we want to replace it
149             matcher.appendReplacement(outString, newYear);
150             // find the next match if there is one
151             matched = matcher.find();
152         }
153 
154         while (matched) {
155             // the new string will no longer match with group 3
156             // if there is still a match, it will be with group 2
157             // now we have to prefix the new year string with the same
158             // non-numeric character as the next match (hyphen, space, whatever)
159             String newYearString = matcher.group(2).substring(0, 1) + newYear;
160             matcher.appendReplacement(outString, newYearString);
161             matched = matcher.find();
162         }
163 
164         // dump whatever detritus is left into the new string
165         matcher.appendTail(outString);
166 
167         return outString.toString();
168     }
169 
170 }