View Javadoc
1   /*
2    * Copyright 2005 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.service.impl;
17  
18  import java.sql.Date;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.TreeSet;
24  
25  import org.kuali.ole.coa.businessobject.AccountingPeriod;
26  import org.kuali.ole.coa.service.AccountingPeriodService;
27  import org.kuali.ole.sys.OLEConstants;
28  import org.kuali.ole.sys.OLEPropertyConstants;
29  import org.kuali.ole.sys.businessobject.UniversityDate;
30  import org.kuali.rice.core.api.datetime.DateTimeService;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.springframework.cache.annotation.CacheEvict;
33  import org.springframework.cache.annotation.Cacheable;
34  
35  /**
36   * This service implementation is the default implementation of the AccountingPeriod service that is delivered with Kuali.
37   */
38  public class AccountingPeriodServiceImpl implements AccountingPeriodService {
39      // member data
40      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingPeriodServiceImpl.class);
41      protected BusinessObjectService businessObjectService;
42      protected DateTimeService dateTimeService;
43  
44      protected static final Set<String> _invalidPeriodCodes = new TreeSet<String>();
45  
46      static {
47          _invalidPeriodCodes.add("13");
48          _invalidPeriodCodes.add("AB");
49          _invalidPeriodCodes.add("BB");
50          _invalidPeriodCodes.add("CB");
51      }
52  
53      /**
54       * The default implementation.
55       * 
56       * @see org.kuali.ole.coa.service.AccountingPeriodService#getAllAccountingPeriods()
57       */
58      @Override
59      @Cacheable(value=AccountingPeriod.CACHE_NAME, key="'{getAllAccountingPeriods}'")
60      public Collection<AccountingPeriod> getAllAccountingPeriods() {
61          return businessObjectService.findAll(AccountingPeriod.class);
62      }
63  
64      /**
65       * Implements by choosing only accounting periods that are active.
66       * 
67       * @see org.kuali.ole.coa.service.AccountingPeriodService#getOpenAccountingPeriods()
68       */
69      @Override
70      @Cacheable(value=AccountingPeriod.CACHE_NAME, key="'{getOpenAccountingPeriods}'")
71      public Collection<AccountingPeriod> getOpenAccountingPeriods() {
72          HashMap<String,Object> map = new HashMap<String,Object>();
73          map.put(OLEConstants.ACCOUNTING_PERIOD_ACTIVE_INDICATOR_FIELD, Boolean.TRUE);
74  
75          return businessObjectService.findMatchingOrderBy(AccountingPeriod.class, map, OLEPropertyConstants.ACCTING_PERIOD_UNIV_FISCAL_PERIOD_END_DATE, true);
76      }
77  
78      /**
79       * This method is a helper method to easily grab an accounting period by looking up it's period and fiscal year
80       * 
81       * @param periodCode
82       * @param fiscalYear
83       * @return an accounting period
84       */
85      @Override
86      @Cacheable(value=AccountingPeriod.CACHE_NAME, key="#p0+'-'+#p1")
87      public AccountingPeriod getByPeriod(String periodCode, Integer fiscalYear) {
88          // build up the hashmap to find the accounting period
89          HashMap<String,Object> keys = new HashMap<String,Object>();
90          keys.put( OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, periodCode);
91          keys.put( OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, fiscalYear);
92          AccountingPeriod acctPeriod = businessObjectService.findByPrimaryKey(AccountingPeriod.class, keys);
93          return acctPeriod;
94      }
95  
96      /**
97       * This method allows for AccountingPeriod retrieval via String date.
98       * 
99       * @param String
100      */
101     @Override
102     public AccountingPeriod getByStringDate(String dateString) {
103         AccountingPeriod acctPeriod;
104         try {
105             acctPeriod = getByDate(dateTimeService.convertToSqlDate(dateString));
106         }
107         catch (Exception pe) {
108             LOG.error("AccountingPeriod getByStringDate unable to convert string " + dateString + " into date.", pe);
109             throw new RuntimeException("AccountingPeriod getByStringDate unable to convert string " + dateString + " into date.", pe);
110         }
111         return acctPeriod;
112     }
113 
114     /**
115      * This method is a helper method to get the current period.
116      * 
117      * @see org.kuali.ole.coa.service.AccountingPeriodService#getByDate(java.sql.Date)
118      */
119     @Override
120     @Cacheable(value=AccountingPeriod.CACHE_NAME, key="'date='+#p0")
121     public AccountingPeriod getByDate(Date date) {
122         Map<String,Object> primaryKeys = new HashMap<String, Object>();
123         primaryKeys.put(OLEPropertyConstants.UNIVERSITY_DATE, date);
124         UniversityDate universityDate = businessObjectService.findByPrimaryKey(UniversityDate.class, primaryKeys);
125         primaryKeys.clear();
126         primaryKeys.put(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, universityDate.getUniversityFiscalYear());
127         primaryKeys.put(OLEPropertyConstants.UNIVERSITY_FISCAL_PERIOD_CODE, universityDate.getUniversityFiscalAccountingPeriod());
128         return businessObjectService.findByPrimaryKey(AccountingPeriod.class, primaryKeys);
129     }
130 
131     /**
132      * This checks to see if the period code is empty or invalid ("13", "AB", "BB", "CB")
133      * 
134      * @param period
135      * @return
136      */
137     protected boolean isInvalidPeriodCode(AccountingPeriod period) {
138         String periodCode = period.getUniversityFiscalPeriodCode();
139         if (periodCode == null) {
140             throw new IllegalArgumentException("invalid (null) universityFiscalPeriodCode (" + periodCode + ")for" + period);
141         }
142         return _invalidPeriodCodes.contains(periodCode);
143     }
144 
145     /**
146      * @see org.kuali.ole.coa.service.AccountingPeriodService#compareAccountingPeriodsByDate(org.kuali.ole.coa.businessobject.AccountingPeriod,
147      *      org.kuali.ole.coa.businessobject.AccountingPeriod)
148      */
149     @Override
150     public int compareAccountingPeriodsByDate(AccountingPeriod tweedleDee, AccountingPeriod tweedleDum) {
151         // note the lack of defensive programming here. If you send a null accounting
152         // period...then chances are, you deserve the NPE that you receive
153         Date tweedleDeeClose = tweedleDee.getUniversityFiscalPeriodEndDate();
154         Date tweedleDumClose = tweedleDum.getUniversityFiscalPeriodEndDate();
155 
156         return tweedleDeeClose.compareTo(tweedleDumClose);
157     }
158 
159     @Override
160     @CacheEvict(value=AccountingPeriod.CACHE_NAME,allEntries=true)
161     public void clearCache() {
162         // nothing to do - annotation does it all
163     }
164 
165     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
166         this.businessObjectService = businessObjectService;
167     }
168     public void setDateTimeService(DateTimeService dateTimeService) {
169         this.dateTimeService = dateTimeService;
170     }
171 
172 
173 }