001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.coa.service.impl;
017
018 import java.sql.Date;
019 import java.util.Collection;
020 import java.util.HashMap;
021 import java.util.Map;
022 import java.util.Set;
023 import java.util.TreeSet;
024
025 import org.kuali.kfs.coa.businessobject.AccountingPeriod;
026 import org.kuali.kfs.coa.service.AccountingPeriodService;
027 import org.kuali.kfs.sys.KFSConstants;
028 import org.kuali.kfs.sys.KFSPropertyConstants;
029 import org.kuali.kfs.sys.businessobject.UniversityDate;
030 import org.kuali.rice.kns.service.BusinessObjectService;
031 import org.kuali.rice.kns.service.DateTimeService;
032 import org.kuali.rice.kns.util.spring.Cached;
033
034 /**
035 * This service implementation is the default implementation of the AccountingPeriod service that is delivered with Kuali.
036 */
037 public class AccountingPeriodServiceImpl implements AccountingPeriodService {
038 // member data
039 private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(AccountingPeriodServiceImpl.class);
040 private BusinessObjectService businessObjectService;
041 private DateTimeService dateTimeService;
042
043 protected static final Set _invalidPeriodCodes = new TreeSet();
044
045 static {
046 _invalidPeriodCodes.add("13");
047 _invalidPeriodCodes.add("AB");
048 _invalidPeriodCodes.add("BB");
049 _invalidPeriodCodes.add("CB");
050 }
051
052 /**
053 * The default implementation.
054 *
055 * @see org.kuali.kfs.coa.service.AccountingPeriodService#getAllAccountingPeriods()
056 */
057 public Collection getAllAccountingPeriods() {
058 return businessObjectService.findAll(AccountingPeriod.class);
059 }
060
061 /**
062 * Implements by choosing only accounting periods that are active.
063 *
064 * @see org.kuali.kfs.coa.service.AccountingPeriodService#getOpenAccountingPeriods()
065 */
066 @Cached
067 public Collection getOpenAccountingPeriods() {
068 HashMap map = new HashMap();
069 map.put(KFSConstants.ACCOUNTING_PERIOD_ACTIVE_INDICATOR_FIELD, Boolean.TRUE);
070
071 return businessObjectService.findMatchingOrderBy(AccountingPeriod.class, map, KFSPropertyConstants.ACCTING_PERIOD_UNIV_FISCAL_PERIOD_END_DATE, true);
072 }
073
074 /**
075 * This method is a helper method to easily grab an accounting period by looking up it's period and fiscal year
076 *
077 * @param periodCode
078 * @param fiscalYear
079 * @return an accounting period
080 */
081 @Cached
082 public AccountingPeriod getByPeriod(String periodCode, Integer fiscalYear) {
083 // build up the hashmap to find the accounting period
084 Map keys = new HashMap();
085 keys.put("universityFiscalPeriodCode", periodCode);
086 keys.put("universityFiscalYear", fiscalYear);
087 AccountingPeriod acctPeriod = (AccountingPeriod) getBusinessObjectService().findByPrimaryKey(AccountingPeriod.class, keys);
088 return acctPeriod;
089 }
090
091 /**
092 * This method allows for AccountingPeriod retrieval via String date.
093 *
094 * @param String
095 */
096 public AccountingPeriod getByStringDate(String dateString) {
097 AccountingPeriod acctPeriod;
098 try {
099 acctPeriod = getByDate(dateTimeService.convertToSqlDate(dateString));
100 }
101 catch (Exception pe) {
102 LOG.error("AccountingPeriod getByStringDate unable to convert string " + dateString + " into date.", pe);
103 throw new RuntimeException("AccountingPeriod getByStringDate unable to convert string " + dateString + " into date.", pe);
104 }
105 return acctPeriod;
106 }
107
108 /**
109 * This method is a helper method to get the current period.
110 *
111 * @see org.kuali.kfs.coa.service.AccountingPeriodService#getByDate(java.sql.Date)
112 */
113 @Cached
114 public AccountingPeriod getByDate(Date date) {
115 Map primaryKeys = new HashMap();
116 primaryKeys.put("universityDate", date);
117 UniversityDate universityDate = (UniversityDate) getBusinessObjectService().findByPrimaryKey(UniversityDate.class, primaryKeys);
118 primaryKeys.clear();
119 primaryKeys.put("universityFiscalYear", universityDate.getUniversityFiscalYear());
120 primaryKeys.put("universityFiscalPeriodCode", universityDate.getUniversityFiscalAccountingPeriod());
121 return (AccountingPeriod) getBusinessObjectService().findByPrimaryKey(AccountingPeriod.class, primaryKeys);
122 }
123
124 /**
125 * This checks to see if the period code is empty or invalid ("13", "AB", "BB", "CB")
126 *
127 * @param period
128 * @return
129 */
130 protected boolean isInvalidPeriodCode(AccountingPeriod period) {
131 String periodCode = period.getUniversityFiscalPeriodCode();
132 if (periodCode == null) {
133 throw new IllegalArgumentException("invalid (null) universityFiscalPeriodCode (" + periodCode + ")for" + period);
134 }
135 return _invalidPeriodCodes.contains(periodCode);
136 }
137
138 /**
139 * @see org.kuali.kfs.coa.service.AccountingPeriodService#compareAccountingPeriodsByDate(org.kuali.kfs.coa.businessobject.AccountingPeriod,
140 * org.kuali.kfs.coa.businessobject.AccountingPeriod)
141 */
142 public int compareAccountingPeriodsByDate(AccountingPeriod tweedleDee, AccountingPeriod tweedleDum) {
143 // note the lack of defensive programming here. If you send a null accounting
144 // period...then chances are, you deserve the NPE that you receive
145 Date tweedleDeeClose = tweedleDee.getUniversityFiscalPeriodEndDate();
146 Date tweedleDumClose = tweedleDum.getUniversityFiscalPeriodEndDate();
147
148 return tweedleDeeClose.compareTo(tweedleDumClose);
149 }
150
151 /**
152 * This method retrieves an instance of the businessObjectService.
153 *
154 * @return
155 */
156 public BusinessObjectService getBusinessObjectService() {
157 return businessObjectService;
158 }
159
160 /**
161 * This method sets the instance of the businessObjectService.
162 *
163 * @param businessObjectService
164 */
165 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
166 this.businessObjectService = businessObjectService;
167 }
168
169 /**
170 * Gets the dateTimeService attribute.
171 *
172 * @return Returns the dateTimeService.
173 */
174 public DateTimeService getDateTimeService() {
175 return dateTimeService;
176 }
177
178 /**
179 * Sets the dateTimeService attribute value.
180 *
181 * @param dateTimeService The dateTimeService to set.
182 */
183 public void setDateTimeService(DateTimeService dateTimeService) {
184 this.dateTimeService = dateTimeService;
185 }
186
187
188 }