001: /*
002: * Copyright 2006-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.kra.budget.service.impl;
017:
018: import java.util.ArrayList;
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.kuali.core.service.BusinessObjectService;
025: import org.kuali.core.service.DateTimeService;
026: import org.kuali.kfs.KFSPropertyConstants;
027: import org.kuali.module.kra.budget.bo.BudgetPeriod;
028: import org.kuali.module.kra.budget.service.BudgetPeriodService;
029: import org.springframework.transaction.annotation.Transactional;
030:
031: @Transactional
032: public class BudgetPeriodServiceImpl implements BudgetPeriodService {
033:
034: private DateTimeService dateTimeService;
035: private BusinessObjectService businessObjectService;
036:
037: /**
038: * @return Returns the dateTimeService.
039: */
040: public DateTimeService getDateTimeService() {
041: return dateTimeService;
042: }
043:
044: /**
045: * @param dateTimeService The dateTimeService to set.
046: */
047: public void setDateTimeService(DateTimeService dateTimeService) {
048: this .dateTimeService = dateTimeService;
049: }
050:
051: public void setBusinessObjectService(
052: BusinessObjectService businessObjectService) {
053: this .businessObjectService = businessObjectService;
054: }
055:
056: /*
057: * (non-Javadoc)
058: *
059: * @see org.kuali.service.BudgetPeriodService#getBudgetPeriod(java.lang.Long, java.lang.Integer)
060: */
061: public BudgetPeriod getBudgetPeriod(String documentNumber,
062: Integer budgetPeriodSequenceNumber) {
063: return (BudgetPeriod) businessObjectService
064: .retrieve(new BudgetPeriod(documentNumber,
065: budgetPeriodSequenceNumber));
066: }
067:
068: public BudgetPeriod getFirstBudgetPeriod(String documentNumber) {
069: Map fieldValues = new HashMap();
070: fieldValues.put(KFSPropertyConstants.DOCUMENT_NUMBER,
071: documentNumber);
072: List budgetPeriodList = new ArrayList(businessObjectService
073: .findMatchingOrderBy(BudgetPeriod.class, fieldValues,
074: KFSPropertyConstants.BUDGET_PERIOD_BEGIN_DATE,
075: true));
076: return (BudgetPeriod) budgetPeriodList.get(0);
077: }
078:
079: public int getPeriodIndex(Integer budgetPeriodSequenceNumber,
080: List budgetPeriodList) {
081: int periodIndexNumber = -1;
082: Iterator budgetPeriodListIter = budgetPeriodList.iterator();
083:
084: for (int i = 0; budgetPeriodListIter.hasNext(); i++) {
085: BudgetPeriod budgetPeriod = (BudgetPeriod) budgetPeriodListIter
086: .next();
087:
088: if (budgetPeriod.getBudgetPeriodSequenceNumber().equals(
089: budgetPeriodSequenceNumber)) {
090: periodIndexNumber = i;
091: break;
092: }
093: }
094:
095: return periodIndexNumber;
096: }
097:
098: public int getPeriodsRange(Integer budgetPeriodSequenceNumberA,
099: Integer budgetPeriodSequenceNumberB, List budgetPeriodList) {
100: int periodIndexNumberA = getPeriodIndex(
101: budgetPeriodSequenceNumberA, budgetPeriodList);
102: int periodIndexNumberB = getPeriodIndex(
103: budgetPeriodSequenceNumberB, budgetPeriodList);
104:
105: int periodsRange = -1;
106:
107: if (periodIndexNumberA != -1 || periodIndexNumberB != -1) {
108: periodsRange = periodIndexNumberB - periodIndexNumberA;
109: }
110:
111: return periodsRange;
112: }
113:
114: public BudgetPeriod getPeriodAfterOffset(
115: Integer budgetPeriodSequenceNumber, int offset,
116: List budgetPeriodList) {
117: BudgetPeriod period = (BudgetPeriod) budgetPeriodList
118: .get(getPeriodIndex(budgetPeriodSequenceNumber,
119: budgetPeriodList)
120: + offset);
121:
122: return period;
123: }
124: }
|