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.gl.web.lookupable;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.kuali.core.bo.BusinessObject;
025: import org.kuali.core.util.KualiDecimal;
026: import org.kuali.kfs.KFSConstants;
027: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
028: import org.kuali.kfs.context.SpringContext;
029: import org.kuali.module.financial.service.UniversityDateService;
030: import org.kuali.module.gl.batch.poster.BalanceCalculator;
031: import org.kuali.module.gl.bo.Balance;
032: import org.kuali.module.gl.bo.CashBalance;
033: import org.kuali.module.gl.bo.UniversityDate;
034: import org.kuali.module.gl.service.BalanceService;
035: import org.kuali.module.gl.util.BusinessObjectFieldConverter;
036: import org.kuali.module.gl.util.OJBUtility;
037: import org.kuali.module.gl.web.Constant;
038: import org.kuali.module.gl.web.inquirable.CashBalanceInquirableImpl;
039: import org.springframework.transaction.annotation.Transactional;
040:
041: /**
042: * An extension of KualiLookupableImpl to support cash lookups
043: */
044: @Transactional
045: public class CashBalanceLookupableHelperServiceImpl extends
046: AbstractGLLookupableHelperServiceImpl {
047: private BalanceCalculator postBalance;
048: private BalanceService balanceService;
049:
050: /**
051: * Returns the URL for inquiries on fields returned in the lookup
052: * @param bo the business object the field to inquiry on is in
053: * @param propertyName the name of the property that an inquiry url is being asked of
054: * @return the String of the url
055: * @see org.kuali.core.lookup.Lookupable#getInquiryUrl(org.kuali.core.bo.BusinessObject, java.lang.String)
056: */
057: @Override
058: public String getInquiryUrl(BusinessObject bo, String propertyName) {
059: return (new CashBalanceInquirableImpl()).getInquiryUrl(bo,
060: propertyName);
061: }
062:
063: /**
064: * Generates a list of results for this inquiry
065: * @param fieldValues the field values that the user entered for this inquiry
066: * @return a List of results
067: * @see org.kuali.core.lookup.Lookupable#getSearchResults(java.util.Map)
068: */
069: @Override
070: public List getSearchResults(Map fieldValues) {
071: setBackLocation((String) fieldValues
072: .get(KFSConstants.BACK_LOCATION));
073: setDocFormKey((String) fieldValues
074: .get(KFSConstants.DOC_FORM_KEY));
075:
076: // get the pending entry option. This method must be prior to the get search results
077: String pendingEntryOption = getSelectedPendingEntryOption(fieldValues);
078:
079: // get the consolidation option
080: boolean isConsolidated = isConsolidationSelected(fieldValues);
081:
082: // get the search result collection
083: Iterator cashBalanceIterator = balanceService.findCashBalance(
084: fieldValues, isConsolidated);
085: Collection searchResultsCollection = this
086: .buildCashBalanceCollection(cashBalanceIterator,
087: isConsolidated);
088:
089: // update search results according to the selected pending entry option
090: updateByPendingLedgerEntry(searchResultsCollection,
091: fieldValues, pendingEntryOption, isConsolidated, false);
092:
093: // get the actual size of all qualified search results
094: Integer recordCount = balanceService.getCashBalanceRecordCount(
095: fieldValues, isConsolidated);
096: Long actualSize = OJBUtility.getResultActualSize(
097: searchResultsCollection, recordCount, fieldValues,
098: new Balance());
099:
100: return this .buildSearchResultList(searchResultsCollection,
101: actualSize);
102: }
103:
104: /**
105: * This method builds the cash balance collection based on the input iterator
106: *
107: * @param iterator the iterator of search results of avaiable cash balance
108: * @return the cash balance collection
109: */
110: private Collection buildCashBalanceCollection(Iterator iterator,
111: boolean isConsolidated) {
112: Collection balanceCollection = new ArrayList();
113:
114: while (iterator.hasNext()) {
115: Object cashBalance = iterator.next();
116:
117: if (cashBalance.getClass().isArray()) {
118: int i = 0;
119: Object[] array = (Object[]) cashBalance;
120: Balance balance = new CashBalance();
121:
122: balance.setUniversityFiscalYear(new Integer(array[i++]
123: .toString()));
124: balance.setChartOfAccountsCode(array[i++].toString());
125: balance.setAccountNumber(array[i++].toString());
126:
127: String subAccountNumber = isConsolidated ? Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER
128: : array[i++].toString();
129: balance.setSubAccountNumber(subAccountNumber);
130:
131: balance.setBalanceTypeCode(array[i++].toString());
132: balance.setObjectCode(array[i++].toString());
133:
134: String subObjectCode = isConsolidated ? Constant.CONSOLIDATED_SUB_OBJECT_CODE
135: : array[i++].toString();
136: balance.setSubObjectCode(subObjectCode);
137:
138: String objectTypeCode = isConsolidated ? Constant.CONSOLIDATED_OBJECT_TYPE_CODE
139: : array[i++].toString();
140: balance.setObjectTypeCode(objectTypeCode);
141:
142: KualiDecimal annualAmount = new KualiDecimal(array[i++]
143: .toString());
144: balance.setAccountLineAnnualBalanceAmount(annualAmount);
145:
146: KualiDecimal beginningAmount = new KualiDecimal(
147: array[i++].toString());
148: balance.setBeginningBalanceLineAmount(beginningAmount);
149:
150: KualiDecimal CGBeginningAmount = new KualiDecimal(
151: array[i].toString());
152: balance
153: .setContractsGrantsBeginningBalanceAmount(CGBeginningAmount);
154:
155: KualiDecimal totalAvailableAmount = this
156: .getTotalAvailableCashAmount(balance);
157: balance.getDummyBusinessObject().setGenericAmount(
158: totalAvailableAmount);
159:
160: balanceCollection.add(balance);
161: }
162: }
163: return balanceCollection;
164: }
165:
166: /**
167: * Allows an updating of pending entry records before they are applied to the inquiry results
168: *
169: * @param entryCollection a collection of balance entries
170: * @param fieldValues the map containing the search fields and values
171: * @param isApproved flag whether the approved entries or all entries will be processed
172: * @param isConsolidated flag whether the results are consolidated or not
173: * @param isCostShareExcluded flag whether the user selects to see the results with cost share subaccount
174: * @see org.kuali.module.gl.web.lookupable.AbstractGLLookupableImpl#updateEntryCollection(java.util.Collection, java.util.Map,
175: * boolean, boolean, boolean)
176: */
177: @Override
178: protected void updateEntryCollection(Collection entryCollection,
179: Map fieldValues, boolean isApproved,
180: boolean isConsolidated, boolean isCostShareInclusive) {
181:
182: // convert the field names of balance object into corresponding ones of pending entry object
183: Map pendingEntryFieldValues = BusinessObjectFieldConverter
184: .convertToTransactionFieldValues(fieldValues);
185:
186: UniversityDate today = SpringContext.getBean(
187: UniversityDateService.class).getCurrentUniversityDate();
188: String currentFiscalPeriodCode = today
189: .getUniversityFiscalAccountingPeriod();
190: Integer currentFiscalYear = today.getUniversityFiscalYear();
191:
192: // use the pending entry to update the input entry collection
193: Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService()
194: .findPendingLedgerEntriesForCashBalance(
195: pendingEntryFieldValues, isApproved);
196: while (pendingEntryIterator.hasNext()) {
197: GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator
198: .next();
199:
200: // Fix the fiscal period/year if they are null
201: // Don't want to use the GLPE service.fillInFiscalPeriodYear. It totally kills performance.
202: // generalLedgerPendingEntryService.fillInFiscalPeriodYear(pendingEntry);
203:
204: if (pendingEntry.getUniversityFiscalYear() == null) {
205: pendingEntry.setUniversityFiscalYear(currentFiscalYear);
206: }
207:
208: if (pendingEntry.getUniversityFiscalPeriodCode() == null) {
209: pendingEntry
210: .setUniversityFiscalPeriodCode(currentFiscalPeriodCode);
211: }
212:
213: // if consolidated, change the following fields into the default values for consolidation
214: if (isConsolidated) {
215: pendingEntry
216: .setSubAccountNumber(Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER);
217: pendingEntry
218: .setFinancialSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
219: pendingEntry
220: .setFinancialObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
221: }
222: Balance balance = postBalance.findBalance(entryCollection,
223: pendingEntry);
224: postBalance.updateBalance(pendingEntry, balance);
225:
226: KualiDecimal totalAvailableAmount = this
227: .getTotalAvailableCashAmount(balance);
228: balance.getDummyBusinessObject().setGenericAmount(
229: totalAvailableAmount);
230: }
231: }
232:
233: // calculate the total available cash amont of the given balance record
234: private KualiDecimal getTotalAvailableCashAmount(Balance balance) {
235: KualiDecimal annualAmount = balance
236: .getAccountLineAnnualBalanceAmount();
237: KualiDecimal beginningAmount = balance
238: .getBeginningBalanceLineAmount();
239: KualiDecimal CGBeginningAmount = balance
240: .getContractsGrantsBeginningBalanceAmount();
241:
242: KualiDecimal totalAvailableAmount = annualAmount
243: .add(beginningAmount);
244: totalAvailableAmount = totalAvailableAmount
245: .add(CGBeginningAmount);
246:
247: return totalAvailableAmount;
248: }
249:
250: /**
251: * Sets the postBalance attribute value.
252: *
253: * @param postBalance The postBalance to set.
254: */
255: public void setPostBalance(BalanceCalculator postBalance) {
256: this .postBalance = postBalance;
257: }
258:
259: /**
260: * Sets the balanceService attribute value.
261: *
262: * @param balanceService The balanceService to set.
263: */
264: public void setBalanceService(BalanceService balanceService) {
265: this.balanceService = balanceService;
266: }
267:
268: }
|