001: /*
002: * Copyright 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.labor.web.lookupable;
017:
018: import static org.kuali.module.labor.LaborConstants.BalanceInquiries.BALANCE_TYPE_AC_AND_A21;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.commons.lang.StringUtils;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.kuali.core.bo.BusinessObject;
031: import org.kuali.core.lookup.AbstractLookupableHelperServiceImpl;
032: import org.kuali.core.lookup.CollectionIncomplete;
033: import org.kuali.core.util.BeanPropertyComparator;
034: import org.kuali.core.util.GlobalVariables;
035: import org.kuali.core.util.KualiDecimal;
036: import org.kuali.kfs.KFSConstants;
037: import org.kuali.kfs.KFSPropertyConstants;
038: import org.kuali.module.gl.util.OJBUtility;
039: import org.kuali.module.gl.web.Constant;
040: import org.kuali.module.labor.bo.LedgerBalance;
041: import org.kuali.module.labor.service.LaborInquiryOptionsService;
042: import org.kuali.module.labor.service.LaborLedgerBalanceService;
043: import org.kuali.module.labor.util.ConsolidationUtil;
044: import org.kuali.module.labor.web.inquirable.LedgerBalanceInquirableImpl;
045: import org.springframework.transaction.annotation.Transactional;
046:
047: /**
048: * Service implementation of LedgerBalanceLookupableHelperService. The class is the front-end for all Ledger balance inquiry
049: * processing.
050: */
051: @Transactional
052: public class LedgerBalanceLookupableHelperServiceImpl extends
053: AbstractLookupableHelperServiceImpl {
054: private static final Log LOG = LogFactory
055: .getLog(LedgerBalanceLookupableHelperServiceImpl.class);
056:
057: private LaborLedgerBalanceService balanceService;
058: private LaborInquiryOptionsService laborInquiryOptionsService;
059:
060: /**
061: * @see org.kuali.core.lookup.Lookupable#getInquiryUrl(org.kuali.core.bo.BusinessObject, java.lang.String)
062: */
063: @Override
064: public String getInquiryUrl(BusinessObject bo, String propertyName) {
065: return (new LedgerBalanceInquirableImpl()).getInquiryUrl(bo,
066: propertyName);
067: }
068:
069: /**
070: * @see org.kuali.core.lookup.Lookupable#getSearchResults(java.util.Map)
071: */
072: @Override
073: public List<? extends BusinessObject> getSearchResults(
074: Map<String, String> fieldValues) {
075: String wildCards = "";
076: for (int i = 0; i < KFSConstants.QUERY_CHARACTERS.length; i++) {
077: wildCards += KFSConstants.QUERY_CHARACTERS[i];
078: }
079:
080: if (wildCards.indexOf(fieldValues.get(
081: KFSPropertyConstants.EMPLID).toString().trim()) != -1) {
082: // StringUtils.indexOfAny(fieldValues.get(KFSPropertyConstants.EMPLID).toString().trim(), KFSConstants.QUERY_CHARACTERS)
083: // != 0) {
084: List emptySearchResults = new ArrayList();
085: Long actualCountIfTruncated = new Long(0);
086: GlobalVariables.getErrorMap().putError(
087: KFSPropertyConstants.EMPLID,
088: KFSConstants.WILDCARD_NOT_ALLOWED_ON_FIELD,
089: "Employee ID field ");
090: return new CollectionIncomplete(emptySearchResults,
091: actualCountIfTruncated);
092: }
093:
094: setBackLocation((String) fieldValues
095: .get(KFSConstants.BACK_LOCATION));
096: setDocFormKey((String) fieldValues
097: .get(KFSConstants.DOC_FORM_KEY));
098:
099: // get the pending entry option. This method must be prior to the get search results
100: String pendingEntryOption = laborInquiryOptionsService
101: .getSelectedPendingEntryOption(fieldValues);
102:
103: // test if the consolidation option is selected or not
104: boolean isConsolidated = laborInquiryOptionsService
105: .isConsolidationSelected(fieldValues);
106:
107: // get the input balance type code
108: String balanceTypeCode = fieldValues
109: .get(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
110: boolean isA21Balance = StringUtils.isNotEmpty(balanceTypeCode)
111: && BALANCE_TYPE_AC_AND_A21.equals(balanceTypeCode
112: .trim());
113:
114: // get the ledger balances with actual balance type code
115: if (isA21Balance) {
116: fieldValues.put(
117: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
118: KFSConstants.BALANCE_TYPE_ACTUAL);
119: }
120: Integer recordCountForActualBalance = balanceService
121: .getBalanceRecordCount(fieldValues, isConsolidated);
122: Iterator actualBalanceIterator = balanceService.findBalance(
123: fieldValues, isConsolidated);
124: Collection searchResultsCollection = buildBalanceCollection(
125: actualBalanceIterator, isConsolidated,
126: pendingEntryOption);
127: laborInquiryOptionsService
128: .updateLedgerBalanceByPendingLedgerEntry(
129: searchResultsCollection, fieldValues,
130: pendingEntryOption, isConsolidated);
131:
132: // get the search result collection
133: Integer recordCountForEffortBalance = 0;
134: if (isA21Balance) {
135: fieldValues.put(
136: KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE,
137: KFSConstants.BALANCE_TYPE_A21);
138: recordCountForEffortBalance = balanceService
139: .getBalanceRecordCount(fieldValues, isConsolidated);
140:
141: Iterator effortBalanceIterator = balanceService
142: .findBalance(fieldValues, isConsolidated);
143: Collection effortBalances = buildBalanceCollection(
144: effortBalanceIterator, isConsolidated,
145: pendingEntryOption);
146: laborInquiryOptionsService
147: .updateLedgerBalanceByPendingLedgerEntry(
148: effortBalances, fieldValues,
149: pendingEntryOption, isConsolidated);
150: searchResultsCollection = ConsolidationUtil
151: .consolidateA2Balances(searchResultsCollection,
152: effortBalances, BALANCE_TYPE_AC_AND_A21);
153: }
154:
155: // get the actual size of all qualified search results
156: Integer recordCount = recordCountForActualBalance
157: + recordCountForEffortBalance;
158: Long actualSize = OJBUtility.getResultActualSize(
159: searchResultsCollection, recordCount, fieldValues,
160: new LedgerBalance());
161:
162: return this .buildSearchResultList(searchResultsCollection,
163: actualSize);
164: }
165:
166: /**
167: * This method builds the balance collection based on the input iterator
168: *
169: * @param iterator the iterator of search results of balance
170: * @param isConsolidated determine if the consolidated result is desired
171: * @param pendingEntryOption the given pending entry option that can be no, approved or all
172: * @return the balance collection
173: */
174: protected Collection buildBalanceCollection(Iterator iterator,
175: boolean isConsolidated, String pendingEntryOption) {
176: Collection balanceCollection = null;
177:
178: if (isConsolidated) {
179: balanceCollection = buildConsolidatedBalanceCollection(
180: iterator, pendingEntryOption);
181: } else {
182: balanceCollection = buildDetailedBalanceCollection(
183: iterator, pendingEntryOption);
184: }
185: return balanceCollection;
186: }
187:
188: /**
189: * This method builds the balance collection with consolidation option from an iterator
190: *
191: * @param iterator
192: * @param pendingEntryOption the selected pending entry option
193: * @return the consolidated balance collection
194: */
195: protected Collection buildConsolidatedBalanceCollection(
196: Iterator iterator, String pendingEntryOption) {
197: Collection balanceCollection = new ArrayList();
198:
199: while (iterator.hasNext()) {
200: Object collectionEntry = iterator.next();
201:
202: if (collectionEntry.getClass().isArray()) {
203: int i = 0;
204: Object[] array = (Object[]) collectionEntry;
205: LedgerBalance balance = new LedgerBalance();
206:
207: if (LedgerBalance.class
208: .isAssignableFrom(getBusinessObjectClass())) {
209: try {
210: balance = (LedgerBalance) getBusinessObjectClass()
211: .newInstance();
212: } catch (Exception e) {
213: LOG
214: .warn("Using "
215: + LedgerBalance.class
216: + " for results because I couldn't instantiate the "
217: + getBusinessObjectClass());
218: }
219: } else {
220: LOG
221: .warn("Using "
222: + LedgerBalance.class
223: + " for results because I couldn't instantiate the "
224: + getBusinessObjectClass());
225: }
226:
227: balance.setUniversityFiscalYear(new Integer(array[i++]
228: .toString()));
229: balance.setChartOfAccountsCode(array[i++].toString());
230: balance.setAccountNumber(array[i++].toString());
231:
232: String subAccountNumber = Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER;
233: balance.setSubAccountNumber(subAccountNumber);
234:
235: balance.setBalanceTypeCode(array[i++].toString());
236: balance.setFinancialObjectCode(array[i++].toString());
237:
238: balance.setEmplid(array[i++].toString());
239: balance.setPositionNumber(array[i++].toString());
240:
241: balance
242: .setFinancialSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
243: balance
244: .setFinancialObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
245:
246: balance
247: .setAccountLineAnnualBalanceAmount(new KualiDecimal(
248: array[i++].toString()));
249: balance.setBeginningBalanceLineAmount(new KualiDecimal(
250: array[i++].toString()));
251: balance
252: .setContractsGrantsBeginningBalanceAmount(new KualiDecimal(
253: array[i++].toString()));
254:
255: balance.setMonth1Amount(new KualiDecimal(array[i++]
256: .toString()));
257: balance.setMonth2Amount(new KualiDecimal(array[i++]
258: .toString()));
259: balance.setMonth3Amount(new KualiDecimal(array[i++]
260: .toString()));
261: balance.setMonth4Amount(new KualiDecimal(array[i++]
262: .toString()));
263: balance.setMonth5Amount(new KualiDecimal(array[i++]
264: .toString()));
265: balance.setMonth6Amount(new KualiDecimal(array[i++]
266: .toString()));
267: balance.setMonth7Amount(new KualiDecimal(array[i++]
268: .toString()));
269: balance.setMonth8Amount(new KualiDecimal(array[i++]
270: .toString()));
271: balance.setMonth9Amount(new KualiDecimal(array[i++]
272: .toString()));
273:
274: balance.setMonth10Amount(new KualiDecimal(array[i++]
275: .toString()));
276: balance.setMonth11Amount(new KualiDecimal(array[i++]
277: .toString()));
278: balance.setMonth12Amount(new KualiDecimal(array[i++]
279: .toString()));
280: balance.setMonth13Amount(new KualiDecimal(array[i]
281: .toString()));
282:
283: balance.getDummyBusinessObject().setPendingEntryOption(
284: pendingEntryOption);
285: balance.getDummyBusinessObject()
286: .setConsolidationOption(Constant.CONSOLIDATION);
287:
288: balanceCollection.add(balance);
289: }
290: }
291: return balanceCollection;
292: }
293:
294: /**
295: * This method builds the balance collection with detail option from an iterator
296: *
297: * @param iterator the balance iterator
298: * @param pendingEntryOption the selected pending entry option
299: * @return the detailed balance collection
300: */
301: protected Collection buildDetailedBalanceCollection(
302: Iterator iterator, String pendingEntryOption) {
303: Collection balanceCollection = new ArrayList();
304:
305: while (iterator.hasNext()) {
306: LedgerBalance copyBalance = (LedgerBalance) (iterator
307: .next());
308:
309: LedgerBalance balance = new LedgerBalance();
310: if (LedgerBalance.class
311: .isAssignableFrom(getBusinessObjectClass())) {
312: try {
313: balance = (LedgerBalance) getBusinessObjectClass()
314: .newInstance();
315: } catch (Exception e) {
316: LOG
317: .warn("Using "
318: + LedgerBalance.class
319: + " for results because I couldn't instantiate the "
320: + getBusinessObjectClass());
321: }
322: } else {
323: LOG
324: .warn("Using "
325: + LedgerBalance.class
326: + " for results because I couldn't instantiate the "
327: + getBusinessObjectClass());
328: }
329:
330: balance.setUniversityFiscalYear(copyBalance
331: .getUniversityFiscalYear());
332: balance.setChartOfAccountsCode(copyBalance
333: .getChartOfAccountsCode());
334: balance.setAccountNumber(copyBalance.getAccountNumber());
335: balance.setSubAccountNumber(copyBalance
336: .getSubAccountNumber());
337: balance
338: .setBalanceTypeCode(copyBalance
339: .getBalanceTypeCode());
340: balance.setFinancialObjectCode(copyBalance
341: .getFinancialObjectCode());
342: balance.setEmplid(copyBalance.getEmplid());
343: balance.setObjectId(copyBalance.getObjectId());
344: balance.setPositionNumber(copyBalance.getPositionNumber());
345: balance.setFinancialSubObjectCode(copyBalance
346: .getFinancialSubObjectCode());
347: balance.setFinancialObjectTypeCode(copyBalance
348: .getFinancialObjectTypeCode());
349: balance.setAccountLineAnnualBalanceAmount(copyBalance
350: .getAccountLineAnnualBalanceAmount());
351: balance.setBeginningBalanceLineAmount(copyBalance
352: .getBeginningBalanceLineAmount());
353: balance
354: .setContractsGrantsBeginningBalanceAmount(copyBalance
355: .getContractsGrantsBeginningBalanceAmount());
356: balance.setMonth1Amount(copyBalance.getMonth1Amount());
357: balance.setMonth2Amount(copyBalance.getMonth2Amount());
358: balance.setMonth3Amount(copyBalance.getMonth3Amount());
359: balance.setMonth4Amount(copyBalance.getMonth4Amount());
360: balance.setMonth5Amount(copyBalance.getMonth5Amount());
361: balance.setMonth6Amount(copyBalance.getMonth6Amount());
362: balance.setMonth7Amount(copyBalance.getMonth7Amount());
363: balance.setMonth8Amount(copyBalance.getMonth8Amount());
364: balance.setMonth9Amount(copyBalance.getMonth9Amount());
365: balance.setMonth10Amount(copyBalance.getMonth10Amount());
366: balance.setMonth11Amount(copyBalance.getMonth11Amount());
367: balance.setMonth12Amount(copyBalance.getMonth12Amount());
368: balance.setMonth13Amount(copyBalance.getMonth13Amount());
369:
370: balance.getDummyBusinessObject().setPendingEntryOption(
371: pendingEntryOption);
372: balance.getDummyBusinessObject().setConsolidationOption(
373: Constant.DETAIL);
374:
375: balanceCollection.add(balance);
376: }
377: return balanceCollection;
378: }
379:
380: /**
381: * build the serach result list from the given collection and the number of all qualified search results
382: *
383: * @param searchResultsCollection the given search results, which may be a subset of the qualified search results
384: * @param actualSize the number of all qualified search results
385: * @return the serach result list with the given results and actual size
386: */
387: protected List buildSearchResultList(
388: Collection searchResultsCollection, Long actualSize) {
389: CollectionIncomplete results = new CollectionIncomplete(
390: searchResultsCollection, actualSize);
391:
392: // sort list if default sort column given
393: List searchResults = (List) results;
394: List defaultSortColumns = getDefaultSortColumns();
395: if (defaultSortColumns.size() > 0) {
396: Collections.sort(results, new BeanPropertyComparator(
397: defaultSortColumns, true));
398: }
399: return searchResults;
400: }
401:
402: /**
403: * Sets the laborInquiryOptionsService attribute value.
404: *
405: * @param laborInquiryOptionsService The laborInquiryOptionsService to set.
406: */
407: public void setLaborInquiryOptionsService(
408: LaborInquiryOptionsService laborInquiryOptionsService) {
409: this .laborInquiryOptionsService = laborInquiryOptionsService;
410: }
411:
412: /**
413: * Sets the balanceService attribute value.
414: *
415: * @param balanceService The balanceService to set.
416: */
417: public void setBalanceService(
418: LaborLedgerBalanceService balanceService) {
419: this .balanceService = balanceService;
420: }
421:
422: /**
423: * Gets the balanceService attribute.
424: *
425: * @return Returns the balanceService.
426: */
427: public LaborLedgerBalanceService getBalanceService() {
428: return balanceService;
429: }
430: }
|