1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
37  
38  public class AccountingPeriodServiceImpl implements AccountingPeriodService {
39      
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  
55  
56  
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  
66  
67  
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  
80  
81  
82  
83  
84  
85      @Override
86      @Cacheable(value=AccountingPeriod.CACHE_NAME, key="#p0+'-'+#p1")
87      public AccountingPeriod getByPeriod(String periodCode, Integer fiscalYear) {
88          
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  
98  
99  
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 
116 
117 
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 
133 
134 
135 
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 
147 
148 
149     @Override
150     public int compareAccountingPeriodsByDate(AccountingPeriod tweedleDee, AccountingPeriod tweedleDum) {
151         
152         
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         
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 }