001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.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/ecl1.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.module.chart.service.impl;
017:
018: import java.sql.Date;
019: import java.util.Calendar;
020: import java.util.Collection;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025: import java.util.TreeSet;
026:
027: import org.kuali.core.service.BusinessObjectService;
028: import org.kuali.core.util.spring.Cached;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.KFSPropertyConstants;
031: import org.kuali.module.chart.bo.AccountingPeriod;
032: import org.kuali.module.chart.service.AccountingPeriodService;
033: import org.springframework.transaction.annotation.Transactional;
034:
035: /**
036: * This service implementation is the default implementation of the AccountingPeriod service that is delivered with Kuali.
037: */
038: @Transactional
039: public class AccountingPeriodServiceImpl implements
040: AccountingPeriodService {
041: // member data
042: private BusinessObjectService businessObjectService;
043:
044: protected static final Set _invalidPeriodCodes = new TreeSet();
045:
046: static {
047: _invalidPeriodCodes.add("13");
048: _invalidPeriodCodes.add("AB");
049: _invalidPeriodCodes.add("BB");
050: _invalidPeriodCodes.add("CB");
051: }
052:
053: /**
054: * The default implementation.
055: *
056: * @see org.kuali.module.chart.service.AccountingPeriodService#getAllAccountingPeriods()
057: */
058: public Collection getAllAccountingPeriods() {
059: return businessObjectService.findAll(AccountingPeriod.class);
060: }
061:
062: /**
063: * Implements by choosing only accounting periods that have a status that is open ("O").
064: *
065: * @see org.kuali.module.chart.service.AccountingPeriodService#getOpenAccountingPeriods()
066: */
067: @Cached
068: public Collection getOpenAccountingPeriods() {
069: HashMap map = new HashMap();
070: map.put(KFSConstants.ACCOUNTING_PERIOD_STATUS_CODE_FIELD,
071: KFSConstants.ACCOUNTING_PERIOD_STATUS_OPEN);
072:
073: return businessObjectService
074: .findMatchingOrderBy(
075: AccountingPeriod.class,
076: map,
077: KFSPropertyConstants.ACCTING_PERIOD_UNIV_FISCAL_PERIOD_END_DATE,
078: true);
079: }
080:
081: /**
082: * This method is a helper method to easily grab an accounting period by looking up it's period and fiscal year
083: *
084: * @param periodCode
085: * @param fiscalYear
086: * @return an accounting period
087: */
088: public AccountingPeriod getByPeriod(String periodCode,
089: Integer fiscalYear) {
090: // build up the hashmap to find the accounting period
091: Map keys = new HashMap();
092: keys.put("universityFiscalPeriodCode", periodCode);
093: keys.put("universityFiscalYear", fiscalYear);
094: AccountingPeriod acctPeriod = (AccountingPeriod) getBusinessObjectService()
095: .findByPrimaryKey(AccountingPeriod.class, keys);
096: return acctPeriod;
097: }
098:
099: /**
100: * This method is a helper method to get the current period.
101: *
102: * @see org.kuali.module.chart.service.AccountingPeriodService#getByDate(java.sql.Date)
103: */
104: public AccountingPeriod getByDate(Date date) {
105: // first we need to figure out the last day for a given date
106: java.util.Date myDate = new java.util.Date(date.getTime());
107: Calendar cal = Calendar.getInstance();
108: cal.setTime(myDate);
109: // get the current month
110: int month = cal.get(Calendar.MONTH);
111: cal.set(Calendar.MONTH, month + 1);
112: cal.set(Calendar.DAY_OF_MONTH, 1);
113: // this should roll us back to the last day of the month for the previous month
114: cal.add(Calendar.DAY_OF_MONTH, -1);
115: Date lookupDate = new Date(cal.getTimeInMillis());
116: Map keys = new HashMap();
117: keys.put("universityFiscalPeriodEndDate", lookupDate);
118: Collection coll = getBusinessObjectService().findMatching(
119: AccountingPeriod.class, keys);
120: if (coll.size() == 1) {
121: Iterator iter = coll.iterator();
122: AccountingPeriod period = (AccountingPeriod) iter.next();
123: return period;
124: } else {
125: Iterator iter = coll.iterator();
126: while (iter.hasNext()) {
127: AccountingPeriod period = (AccountingPeriod) iter
128: .next();
129: if (!isInvalidPeriodCode(period)) {
130: return period;
131: }
132: }
133: }
134: return null;
135: }
136:
137: /**
138: *
139: * This checks to see if the period code is empty or invalid ("13", "AB", "BB", "CB")
140: * @param period
141: * @return
142: */
143: private boolean isInvalidPeriodCode(AccountingPeriod period) {
144: String periodCode = period.getUniversityFiscalPeriodCode();
145: if (periodCode == null) {
146: throw new IllegalArgumentException(
147: "invalid (null) universityFiscalPeriodCode ("
148: + periodCode + ")for" + period);
149: }
150: return _invalidPeriodCodes.contains(periodCode);
151: }
152:
153: /**
154: * @see org.kuali.module.chart.service.AccountingPeriodService#compareAccountingPeriodsByDate(org.kuali.module.chart.bo.AccountingPeriod,
155: * org.kuali.module.chart.bo.AccountingPeriod)
156: */
157: public int compareAccountingPeriodsByDate(
158: AccountingPeriod tweedleDee, AccountingPeriod tweedleDum) {
159: // note the lack of defensive programming here. If you send a null accounting
160: // period...then chances are, you deserve the NPE that you receive
161: Date tweedleDeeClose = tweedleDee
162: .getUniversityFiscalPeriodEndDate();
163: Date tweedleDumClose = tweedleDum
164: .getUniversityFiscalPeriodEndDate();
165:
166: return tweedleDeeClose.compareTo(tweedleDumClose);
167: }
168:
169: /**
170: * This method retrieves an instance of the businessObjectService.
171: *
172: * @return
173: */
174: public BusinessObjectService getBusinessObjectService() {
175: return businessObjectService;
176: }
177:
178: /**
179: * This method sets the instance of the businessObjectService.
180: *
181: * @param businessObjectService
182: */
183: public void setBusinessObjectService(
184: BusinessObjectService businessObjectService) {
185: this.businessObjectService = businessObjectService;
186: }
187: }
|