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.module.gl.batch.poster.BalanceCalculator;
029: import org.kuali.module.gl.bo.Balance;
030: import org.kuali.module.gl.bo.TransientBalanceInquiryAttributes;
031: import org.kuali.module.gl.service.BalanceService;
032: import org.kuali.module.gl.util.BusinessObjectFieldConverter;
033: import org.kuali.module.gl.util.OJBUtility;
034: import org.kuali.module.gl.web.Constant;
035: import org.kuali.module.gl.web.inquirable.BalanceInquirableImpl;
036: import org.springframework.transaction.annotation.Transactional;
037:
038: /**
039: * An extension of KualiLookupableImpl to support balance lookups
040: */
041: @Transactional
042: public class BalanceLookupableHelperServiceImpl extends
043: AbstractGLLookupableHelperServiceImpl {
044: private BalanceCalculator postBalance;
045: private BalanceService balanceService;
046: private Map fieldValues;
047:
048: /**
049: * Returns the url for any drill down links within the lookup
050: * @param bo the business object with a property being drilled down on
051: * @param propertyName the name of the property being drilled down on
052: * @return a String with the URL of the property
053: * @see org.kuali.core.lookup.Lookupable#getInquiryUrl(org.kuali.core.bo.BusinessObject, java.lang.String)
054: */
055: @Override
056: public String getInquiryUrl(BusinessObject bo, String propertyName) {
057: return (new BalanceInquirableImpl()).getInquiryUrl(bo,
058: propertyName);
059: }
060:
061: /**
062: * Generates the list of search results for this inquiry
063: * @param fieldValues the field values of the query to carry out
064: * @return List the search results returned by the lookup
065: * @see org.kuali.core.lookup.Lookupable#getSearchResults(java.util.Map)
066: */
067: @Override
068: public List getSearchResults(Map fieldValues) {
069: setBackLocation((String) fieldValues
070: .get(KFSConstants.BACK_LOCATION));
071: setDocFormKey((String) fieldValues
072: .get(KFSConstants.DOC_FORM_KEY));
073:
074: // get the pending entry option. This method must be prior to the get search results
075: String pendingEntryOption = this
076: .getSelectedPendingEntryOption(fieldValues);
077:
078: // test if the consolidation option is selected or not
079: boolean isConsolidated = isConsolidationSelected(fieldValues);
080:
081: // get Amount View Option and determine if the results has to be accumulated
082: String amountViewOption = getSelectedAmountViewOption(fieldValues);
083: boolean isAccumulated = amountViewOption
084: .equals(Constant.ACCUMULATE);
085:
086: // get the search result collection
087: Iterator balanceIterator = balanceService.findBalance(
088: fieldValues, isConsolidated);
089: Collection searchResultsCollection = this
090: .buildBalanceCollection(balanceIterator,
091: isConsolidated, pendingEntryOption);
092:
093: // update search results according to the selected pending entry option
094: updateByPendingLedgerEntry(searchResultsCollection,
095: fieldValues, pendingEntryOption, isConsolidated, false);
096:
097: // perform the accumulation of the amounts
098: this .accumulate(searchResultsCollection, isAccumulated);
099:
100: // get the actual size of all qualified search results
101: Integer recordCount = balanceService.getBalanceRecordCount(
102: fieldValues, isConsolidated);
103: Long actualSize = OJBUtility.getResultActualSize(
104: searchResultsCollection, recordCount, fieldValues,
105: new Balance());
106:
107: return this .buildSearchResultList(searchResultsCollection,
108: actualSize);
109: }
110:
111: /**
112: * This method builds the balance collection based on the input iterator
113: *
114: * @param iterator the iterator of search results of balance
115: * @param isConsolidated determine if the consolidated result is desired
116: * @param pendingEntryOption the given pending entry option that can be no, approved or all
117: * @return the balance collection
118: */
119: private Collection buildBalanceCollection(Iterator iterator,
120: boolean isConsolidated, String pendingEntryOption) {
121: Collection balanceCollection = null;
122:
123: if (isConsolidated) {
124: balanceCollection = buildConsolidatedBalanceCollection(
125: iterator, pendingEntryOption);
126: } else {
127: balanceCollection = buildDetailedBalanceCollection(
128: iterator, pendingEntryOption);
129: }
130: return balanceCollection;
131: }
132:
133: /**
134: * This method builds the balance collection with consolidation option from an iterator
135: *
136: * @param iterator th iterator of balance results
137: * @param pendingEntryOption the selected pending entry option
138: * @return the consolidated balance collection
139: */
140: private Collection buildConsolidatedBalanceCollection(
141: Iterator iterator, String pendingEntryOption) {
142: Collection balanceCollection = new ArrayList();
143:
144: while (iterator.hasNext()) {
145: Object collectionEntry = iterator.next();
146:
147: if (collectionEntry.getClass().isArray()) {
148: int i = 0;
149: Object[] array = (Object[]) collectionEntry;
150: Balance balance = new Balance();
151:
152: balance.setUniversityFiscalYear(new Integer(array[i++]
153: .toString()));
154: balance.setChartOfAccountsCode(array[i++].toString());
155: balance.setAccountNumber(array[i++].toString());
156:
157: String subAccountNumber = Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER;
158: balance.setSubAccountNumber(subAccountNumber);
159:
160: balance.setBalanceTypeCode(array[i++].toString());
161: balance.setObjectCode(array[i++].toString());
162:
163: balance
164: .setSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
165: balance
166: .setObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
167:
168: balance
169: .setAccountLineAnnualBalanceAmount(new KualiDecimal(
170: array[i++].toString()));
171: balance.setBeginningBalanceLineAmount(new KualiDecimal(
172: array[i++].toString()));
173: balance
174: .setContractsGrantsBeginningBalanceAmount(new KualiDecimal(
175: array[i++].toString()));
176:
177: balance.setMonth1Amount(new KualiDecimal(array[i++]
178: .toString()));
179: balance.setMonth2Amount(new KualiDecimal(array[i++]
180: .toString()));
181: balance.setMonth3Amount(new KualiDecimal(array[i++]
182: .toString()));
183: balance.setMonth4Amount(new KualiDecimal(array[i++]
184: .toString()));
185: balance.setMonth5Amount(new KualiDecimal(array[i++]
186: .toString()));
187: balance.setMonth6Amount(new KualiDecimal(array[i++]
188: .toString()));
189: balance.setMonth7Amount(new KualiDecimal(array[i++]
190: .toString()));
191: balance.setMonth8Amount(new KualiDecimal(array[i++]
192: .toString()));
193: balance.setMonth9Amount(new KualiDecimal(array[i++]
194: .toString()));
195:
196: balance.setMonth10Amount(new KualiDecimal(array[i++]
197: .toString()));
198: balance.setMonth11Amount(new KualiDecimal(array[i++]
199: .toString()));
200: balance.setMonth12Amount(new KualiDecimal(array[i++]
201: .toString()));
202: balance.setMonth13Amount(new KualiDecimal(array[i]
203: .toString()));
204:
205: balance
206: .setDummyBusinessObject(new TransientBalanceInquiryAttributes());
207: balance.getDummyBusinessObject().setPendingEntryOption(
208: pendingEntryOption);
209:
210: balanceCollection.add(balance);
211: }
212: }
213: return balanceCollection;
214: }
215:
216: /**
217: * This method builds the balance collection with detail option from an iterator
218: *
219: * @param iterator the balance iterator
220: * @param pendingEntryOption the selected pending entry option
221: * @return the detailed balance collection
222: */
223: private Collection buildDetailedBalanceCollection(
224: Iterator iterator, String pendingEntryOption) {
225: Collection balanceCollection = new ArrayList();
226:
227: while (iterator.hasNext()) {
228: Balance balance = (Balance) (iterator.next());
229:
230: balance
231: .setDummyBusinessObject(new TransientBalanceInquiryAttributes());
232: balance.getDummyBusinessObject().setPendingEntryOption(
233: pendingEntryOption);
234:
235: balanceCollection.add(balance);
236: }
237: return balanceCollection;
238: }
239:
240: /**
241: * This method updates the balance collection with accumulated amounts if required (isAccumulated is true)
242: *
243: * @param balanceCollection the balance collection to be updated
244: * @param isAccumulated determine if the accumulated result is desired
245: */
246: private void accumulate(Collection balanceCollection,
247: boolean isAccumulated) {
248:
249: if (isAccumulated) {
250: for (Iterator iterator = balanceCollection.iterator(); iterator
251: .hasNext();) {
252: Balance balance = (Balance) (iterator.next());
253: accumulateByBalance(balance, isAccumulated);
254: }
255: }
256: }
257:
258: /**
259: * This method computes the accumulate amount of the given balance and updates its fields
260: *
261: * @param balance the given balance object
262: * @param isAccumulated determine if the accumulated result is desired
263: */
264: private void accumulateByBalance(Balance balance,
265: boolean isAccumulated) {
266:
267: KualiDecimal annualAmount = balance
268: .getAccountLineAnnualBalanceAmount();
269: KualiDecimal beginningAmount = balance
270: .getBeginningBalanceLineAmount();
271: KualiDecimal CGBeginningAmount = balance
272: .getContractsGrantsBeginningBalanceAmount();
273:
274: KualiDecimal month0Amount = beginningAmount
275: .add(CGBeginningAmount);
276: KualiDecimal month1Amount = balance.getMonth1Amount();
277: month1Amount = accumulateAmount(month1Amount, month0Amount,
278: isAccumulated);
279: balance.setMonth1Amount(month1Amount);
280:
281: KualiDecimal month2Amount = balance.getMonth2Amount();
282: month2Amount = accumulateAmount(month2Amount, month1Amount,
283: isAccumulated);
284: balance.setMonth2Amount(month2Amount);
285:
286: KualiDecimal month3Amount = balance.getMonth3Amount();
287: month3Amount = accumulateAmount(month3Amount, month2Amount,
288: isAccumulated);
289: balance.setMonth3Amount(month3Amount);
290:
291: KualiDecimal month4Amount = balance.getMonth4Amount();
292: month4Amount = accumulateAmount(month4Amount, month3Amount,
293: isAccumulated);
294: balance.setMonth4Amount(month4Amount);
295:
296: KualiDecimal month5Amount = balance.getMonth5Amount();
297: month5Amount = accumulateAmount(month5Amount, month4Amount,
298: isAccumulated);
299: balance.setMonth5Amount(month5Amount);
300:
301: KualiDecimal month6Amount = balance.getMonth6Amount();
302: month6Amount = accumulateAmount(month6Amount, month5Amount,
303: isAccumulated);
304: balance.setMonth6Amount(month6Amount);
305:
306: KualiDecimal month7Amount = balance.getMonth7Amount();
307: month7Amount = accumulateAmount(month7Amount, month6Amount,
308: isAccumulated);
309: balance.setMonth7Amount(month7Amount);
310:
311: KualiDecimal month8Amount = balance.getMonth8Amount();
312: month8Amount = accumulateAmount(month8Amount, month7Amount,
313: isAccumulated);
314: balance.setMonth8Amount(month8Amount);
315:
316: KualiDecimal month9Amount = balance.getMonth9Amount();
317: month9Amount = accumulateAmount(month9Amount, month8Amount,
318: isAccumulated);
319: balance.setMonth9Amount(month9Amount);
320:
321: KualiDecimal month10Amount = balance.getMonth10Amount();
322: month10Amount = accumulateAmount(month10Amount, month9Amount,
323: isAccumulated);
324: balance.setMonth10Amount(month10Amount);
325:
326: KualiDecimal month11Amount = balance.getMonth11Amount();
327: month11Amount = accumulateAmount(month11Amount, month10Amount,
328: isAccumulated);
329: balance.setMonth11Amount(month11Amount);
330:
331: KualiDecimal month12Amount = balance.getMonth12Amount();
332: month12Amount = accumulateAmount(month12Amount, month11Amount,
333: isAccumulated);
334: balance.setMonth12Amount(month12Amount);
335:
336: KualiDecimal month13Amount = balance.getMonth13Amount();
337: month13Amount = accumulateAmount(month13Amount, month12Amount,
338: isAccumulated);
339: balance.setMonth13Amount(month13Amount);
340: }
341:
342: /**
343: * This method converts the amount from String and adds it with the input addend
344: *
345: * @param stringAugend a String-type augend
346: * @param addend an addend
347: * @param isAccumulated determine if the accumulated result is desired
348: * @return the accumulated amount if accumulate option is selected; otherwise, the input amount itself
349: */
350: private KualiDecimal accumulateAmount(Object stringAugend,
351: KualiDecimal addend, boolean isAccumulated) {
352:
353: KualiDecimal augend = new KualiDecimal(stringAugend.toString());
354: if (isAccumulated) {
355: augend = augend.add(addend);
356: }
357: return augend;
358: }
359:
360: /**
361: * Updates pending entries before their results are included in the lookup results
362: *
363: * @param entryCollection a collection of balance entries
364: * @param fieldValues the map containing the search fields and values
365: * @param isApproved flag whether the approved entries or all entries will be processed
366: * @param isConsolidated flag whether the results are consolidated or not
367: * @param isCostShareExcluded flag whether the user selects to see the results with cost share subaccount
368: * @see org.kuali.module.gl.web.lookupable.AbstractGLLookupableImpl#updateEntryCollection(java.util.Collection, java.util.Map,
369: * boolean, boolean, boolean)
370: */
371: public void updateEntryCollection(Collection entryCollection,
372: Map fieldValues, boolean isApproved,
373: boolean isConsolidated, boolean isCostShareInclusive) {
374:
375: // convert the field names of balance object into corresponding ones of pending entry object
376: Map pendingEntryFieldValues = BusinessObjectFieldConverter
377: .convertToTransactionFieldValues(fieldValues);
378:
379: // go through the pending entries to update the balance collection
380: Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService()
381: .findPendingLedgerEntriesForBalance(
382: pendingEntryFieldValues, isApproved);
383: while (pendingEntryIterator.hasNext()) {
384: GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator
385: .next();
386:
387: // if consolidated, change the following fields into the default values for consolidation
388: if (isConsolidated) {
389: pendingEntry
390: .setSubAccountNumber(Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER);
391: pendingEntry
392: .setFinancialSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
393: pendingEntry
394: .setFinancialObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
395: }
396:
397: Balance balance = postBalance.findBalance(entryCollection,
398: pendingEntry);
399:
400: String pendingEntryOption = isApproved ? Constant.APPROVED_PENDING_ENTRY
401: : Constant.ALL_PENDING_ENTRY;
402: balance
403: .setDummyBusinessObject(new TransientBalanceInquiryAttributes());
404: balance.getDummyBusinessObject().setPendingEntryOption(
405: pendingEntryOption);
406:
407: postBalance.updateBalance(pendingEntry, balance);
408: }
409: }
410:
411: /**
412: * Sets the postBalance attribute value.
413: *
414: * @param postBalance The postBalance to set.
415: */
416: public void setPostBalance(BalanceCalculator postBalance) {
417: this .postBalance = postBalance;
418: }
419:
420: /**
421: * Sets the balanceService attribute value.
422: *
423: * @param balanceService The balanceService to set.
424: */
425: public void setBalanceService(BalanceService balanceService) {
426: this.balanceService = balanceService;
427: }
428:
429: }
|