View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ld.businessobject.lookup;
20  
21  import static org.kuali.kfs.module.ld.LaborConstants.BalanceInquiries.BALANCE_TYPE_AC_AND_A21;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.commons.lang.StringUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.kuali.kfs.coa.service.BalanceTypeService;
35  import org.kuali.kfs.gl.Constant;
36  import org.kuali.kfs.gl.OJBUtility;
37  import org.kuali.kfs.gl.businessobject.lookup.BalanceLookupableHelperServiceImpl;
38  import org.kuali.kfs.integration.ld.businessobject.inquiry.AbstractPositionDataDetailsInquirableImpl;
39  import org.kuali.kfs.module.ld.businessobject.LedgerBalance;
40  import org.kuali.kfs.module.ld.businessobject.inquiry.LedgerBalanceInquirableImpl;
41  import org.kuali.kfs.module.ld.businessobject.inquiry.PositionDataDetailsInquirableImpl;
42  import org.kuali.kfs.module.ld.service.LaborInquiryOptionsService;
43  import org.kuali.kfs.module.ld.service.LaborLedgerBalanceService;
44  import org.kuali.kfs.module.ld.util.ConsolidationUtil;
45  import org.kuali.kfs.sys.KFSConstants;
46  import org.kuali.kfs.sys.KFSPropertyConstants;
47  import org.kuali.rice.core.api.search.SearchOperator;
48  import org.kuali.rice.core.api.util.type.KualiDecimal;
49  import org.kuali.rice.kns.lookup.HtmlData;
50  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
51  import org.kuali.rice.krad.bo.BusinessObject;
52  import org.kuali.rice.krad.lookup.CollectionIncomplete;
53  import org.kuali.rice.krad.util.BeanPropertyComparator;
54  import org.kuali.rice.krad.util.GlobalVariables;
55  
56  /**
57   * Service implementation of LedgerBalanceLookupableHelperService. The class is the front-end for all Ledger balance inquiry
58   * processing.
59   */
60  public class LedgerBalanceLookupableHelperServiceImpl extends BalanceLookupableHelperServiceImpl {
61      private static final Log LOG = LogFactory.getLog(LedgerBalanceLookupableHelperServiceImpl.class);
62  
63      private LaborLedgerBalanceService balanceService;
64      private LaborInquiryOptionsService laborInquiryOptionsService;
65      protected BalanceTypeService balanceTypService;
66  
67      /**
68       * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.krad.bo.BusinessObject, java.lang.String)
69       */
70      @Override
71      public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
72          if (KFSPropertyConstants.POSITION_NUMBER.equals(propertyName)) {
73              LedgerBalance balance = (LedgerBalance) bo;
74              AbstractPositionDataDetailsInquirableImpl positionDataDetailsInquirable = new PositionDataDetailsInquirableImpl();
75  
76              Map<String, String> fieldValues = new HashMap<String, String>();
77              fieldValues.put(propertyName, balance.getPositionNumber());
78  
79              BusinessObject positionData = positionDataDetailsInquirable.getBusinessObject(fieldValues);
80  
81              return positionData == null ? new AnchorHtmlData(KFSConstants.EMPTY_STRING, KFSConstants.EMPTY_STRING) : positionDataDetailsInquirable.getInquiryUrl(positionData, propertyName);
82          }
83          return (new LedgerBalanceInquirableImpl()).getInquiryUrl(bo, propertyName);
84      }
85  
86      /**
87       * @see org.kuali.rice.kns.lookup.Lookupable#getSearchResults(java.util.Map)
88       */
89      @Override
90      public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
91          String wildCards = "";
92          for (SearchOperator op : SearchOperator.QUERY_CHARACTERS) {
93              wildCards += op.op();
94          }
95  
96          if (wildCards.indexOf(fieldValues.get(KFSPropertyConstants.EMPLID).toString().trim()) != -1) {
97              // StringUtils.indexOfAny(fieldValues.get(KFSPropertyConstants.EMPLID).toString().trim(), KFSConstants.QUERY_CHARACTERS)
98              // != 0) {
99              List emptySearchResults = new ArrayList();
100             Long actualCountIfTruncated = new Long(0);
101             GlobalVariables.getMessageMap().putError(KFSPropertyConstants.EMPLID, KFSConstants.WILDCARD_NOT_ALLOWED_ON_FIELD, "Employee ID field ");
102             return new CollectionIncomplete(emptySearchResults, actualCountIfTruncated);
103         }
104 
105         setBackLocation(fieldValues.get(KFSConstants.BACK_LOCATION));
106         setDocFormKey(fieldValues.get(KFSConstants.DOC_FORM_KEY));
107 
108         // get the pending entry option. This method must be prior to the get search results
109         String pendingEntryOption = laborInquiryOptionsService.getSelectedPendingEntryOption(fieldValues);
110 
111         // get the cgBeginningBalanceExcludeOption
112         boolean isCgBeginningBalanceExcluded = laborInquiryOptionsService.isCgBeginningBalanceOnlyExcluded(fieldValues);
113 
114         // test if the consolidation option is selected or not
115         boolean isConsolidated = laborInquiryOptionsService.isConsolidationSelected(fieldValues);
116 
117         // get Amount View Option and determine if the results has to be accumulated
118         String amountViewOption = getSelectedAmountViewOption(fieldValues);
119         boolean isAccumulated = amountViewOption.equals(Constant.ACCUMULATE);
120 
121         // get the input balance type code
122         String balanceTypeCode = fieldValues.get(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE).toString();
123         boolean isA21Balance = StringUtils.isNotEmpty(balanceTypeCode) && BALANCE_TYPE_AC_AND_A21.equals(balanceTypeCode.trim());
124 
125         // get the ledger balances with actual balance type code
126         if (isA21Balance) {
127             fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_ACTUAL);
128         }
129         Integer recordCountForActualBalance = balanceService.getBalanceRecordCount(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues), false);
130         Iterator actualBalanceIterator = balanceService.findBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues), false);
131         Collection searchResultsCollection = buildBalanceCollection(actualBalanceIterator, isConsolidated, pendingEntryOption);
132         laborInquiryOptionsService.updateLedgerBalanceByPendingLedgerEntry(searchResultsCollection, fieldValues, pendingEntryOption, isConsolidated);
133 
134         // get the search result collection
135         Integer recordCountForEffortBalance = 0;
136         if (isA21Balance) {
137             fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_A21);
138             recordCountForEffortBalance = balanceService.getBalanceRecordCount(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues), false);
139 
140             Iterator effortBalanceIterator = balanceService.findBalance(fieldValues, isConsolidated, getEncumbranceBalanceTypes(fieldValues), false);
141             Collection effortBalances = buildBalanceCollection(effortBalanceIterator, isConsolidated, pendingEntryOption);
142             laborInquiryOptionsService.updateLedgerBalanceByPendingLedgerEntry(effortBalances, fieldValues, pendingEntryOption, isConsolidated);
143 
144             List<String> consolidationKeyList = LedgerBalance.getPrimaryKeyList();
145             searchResultsCollection = ConsolidationUtil.consolidateA2Balances(searchResultsCollection, effortBalances, BALANCE_TYPE_AC_AND_A21, consolidationKeyList);
146         }
147 
148         // filter out rows with all amounts zero except CG beginning balance if cgBeginningBalanceExcludeOption is checked.
149         // Note: this has to be done before accumulate, because accumulate adds up CG amount into monthly amounts.
150         if (isCgBeginningBalanceExcluded) {
151             searchResultsCollection = filterOutCGBeginningBalanceOnlyRows(searchResultsCollection);
152         }
153 
154         // perform the accumulation of the amounts
155         accumulate(searchResultsCollection, isAccumulated);
156 
157         // get the actual size of all qualified search results
158         Integer recordCount = recordCountForActualBalance + recordCountForEffortBalance;
159         Long actualSize = OJBUtility.getResultActualSize(searchResultsCollection, recordCount, fieldValues, new LedgerBalance());
160 
161         return this.buildSearchResultList(searchResultsCollection, actualSize);
162     }
163 
164     /**
165      * Filter out rows with all amounts zero except CG beginning balance from the given searchResultsCollection.
166      */
167     protected Collection<LedgerBalance> filterOutCGBeginningBalanceOnlyRows(Collection<LedgerBalance> searchResultsCollection) {
168         Collection<LedgerBalance> filteredSearchResults = new ArrayList<LedgerBalance>();
169         for (LedgerBalance balance: searchResultsCollection) {
170             if (!balance.isCGBeginningBalanceOnly()) {
171                 filteredSearchResults.add(balance);
172             }
173         }
174         return filteredSearchResults;
175     }
176 
177     /**
178      * This method builds the balance collection based on the input iterator
179      *
180      * @param iterator the iterator of search results of balance
181      * @param isConsolidated determine if the consolidated result is desired
182      * @param pendingEntryOption the given pending entry option that can be no, approved or all
183      * @return the balance collection
184      */
185     protected Collection buildBalanceCollection(Iterator iterator, boolean isConsolidated, String pendingEntryOption) {
186         Collection balanceCollection = null;
187 
188         if (isConsolidated) {
189             balanceCollection = buildConsolidatedBalanceCollection(iterator, pendingEntryOption);
190         }
191         else {
192             balanceCollection = buildDetailedBalanceCollection(iterator, pendingEntryOption);
193         }
194         return balanceCollection;
195     }
196 
197     /**
198      * This method builds the balance collection with consolidation option from an iterator
199      *
200      * @param iterator
201      * @param pendingEntryOption the selected pending entry option
202      * @return the consolidated balance collection
203      */
204     protected Collection buildConsolidatedBalanceCollection(Iterator iterator, String pendingEntryOption) {
205         Collection balanceCollection = new ArrayList();
206 
207         while (iterator.hasNext()) {
208             Object collectionEntry = iterator.next();
209 
210             if (collectionEntry.getClass().isArray()) {
211                 int i = 0;
212                 Object[] array = (Object[]) collectionEntry;
213                 LedgerBalance balance = new LedgerBalance();
214 
215                 if (LedgerBalance.class.isAssignableFrom(getBusinessObjectClass())) {
216                     try {
217                         balance = (LedgerBalance) getBusinessObjectClass().newInstance();
218                     }
219                     catch (Exception e) {
220                         LOG.warn("Using " + LedgerBalance.class + " for results because I couldn't instantiate the " + getBusinessObjectClass());
221                     }
222                 }
223                 else {
224                     LOG.warn("Using " + LedgerBalance.class + " for results because I couldn't instantiate the " + getBusinessObjectClass());
225                 }
226 
227                 balance.setUniversityFiscalYear(new Integer(array[i++].toString()));
228                 balance.setChartOfAccountsCode(array[i++].toString());
229                 balance.setAccountNumber(array[i++].toString());
230 
231                 String subAccountNumber = Constant.CONSOLIDATED_SUB_ACCOUNT_NUMBER;
232                 balance.setSubAccountNumber(subAccountNumber);
233 
234                 balance.setBalanceTypeCode(array[i++].toString());
235                 balance.setFinancialObjectCode(array[i++].toString());
236 
237                 balance.setEmplid(array[i++].toString());
238                 balance.setPositionNumber(array[i++].toString());
239 
240                 balance.setFinancialSubObjectCode(Constant.CONSOLIDATED_SUB_OBJECT_CODE);
241                 balance.setFinancialObjectTypeCode(Constant.CONSOLIDATED_OBJECT_TYPE_CODE);
242 
243                 balance.setAccountLineAnnualBalanceAmount(new KualiDecimal(array[i++].toString()));
244                 balance.setBeginningBalanceLineAmount(new KualiDecimal(array[i++].toString()));
245                 balance.setContractsGrantsBeginningBalanceAmount(new KualiDecimal(array[i++].toString()));
246 
247                 balance.setMonth1Amount(new KualiDecimal(array[i++].toString()));
248                 balance.setMonth2Amount(new KualiDecimal(array[i++].toString()));
249                 balance.setMonth3Amount(new KualiDecimal(array[i++].toString()));
250                 balance.setMonth4Amount(new KualiDecimal(array[i++].toString()));
251                 balance.setMonth5Amount(new KualiDecimal(array[i++].toString()));
252                 balance.setMonth6Amount(new KualiDecimal(array[i++].toString()));
253                 balance.setMonth7Amount(new KualiDecimal(array[i++].toString()));
254                 balance.setMonth8Amount(new KualiDecimal(array[i++].toString()));
255                 balance.setMonth9Amount(new KualiDecimal(array[i++].toString()));
256 
257                 balance.setMonth10Amount(new KualiDecimal(array[i++].toString()));
258                 balance.setMonth11Amount(new KualiDecimal(array[i++].toString()));
259                 balance.setMonth12Amount(new KualiDecimal(array[i++].toString()));
260                 balance.setMonth13Amount(new KualiDecimal(array[i].toString()));
261 
262                 balance.getDummyBusinessObject().setPendingEntryOption(pendingEntryOption);
263                 balance.getDummyBusinessObject().setConsolidationOption(Constant.CONSOLIDATION);
264 
265                 balanceCollection.add(balance);
266             }
267         }
268         return balanceCollection;
269     }
270 
271     /**
272      * This method builds the balance collection with detail option from an iterator
273      *
274      * @param iterator the balance iterator
275      * @param pendingEntryOption the selected pending entry option
276      * @return the detailed balance collection
277      */
278     protected Collection buildDetailedBalanceCollection(Iterator iterator, String pendingEntryOption) {
279         Collection balanceCollection = new ArrayList();
280 
281         while (iterator.hasNext()) {
282             LedgerBalance copyBalance = (LedgerBalance) (iterator.next());
283 
284             LedgerBalance balance = new LedgerBalance();
285             if (LedgerBalance.class.isAssignableFrom(getBusinessObjectClass())) {
286                 try {
287                     balance = (LedgerBalance) getBusinessObjectClass().newInstance();
288                 }
289                 catch (Exception e) {
290                     LOG.warn("Using " + LedgerBalance.class + " for results because I couldn't instantiate the " + getBusinessObjectClass());
291                 }
292             }
293             else {
294                 LOG.warn("Using " + LedgerBalance.class + " for results because I couldn't instantiate the " + getBusinessObjectClass());
295             }
296 
297             balance.setUniversityFiscalYear(copyBalance.getUniversityFiscalYear());
298             balance.setChartOfAccountsCode(copyBalance.getChartOfAccountsCode());
299             balance.setAccountNumber(copyBalance.getAccountNumber());
300             balance.setSubAccountNumber(copyBalance.getSubAccountNumber());
301             balance.setBalanceTypeCode(copyBalance.getBalanceTypeCode());
302             balance.setFinancialObjectCode(copyBalance.getFinancialObjectCode());
303             balance.setEmplid(copyBalance.getEmplid());
304             balance.setObjectId(copyBalance.getObjectId());
305             balance.setPositionNumber(copyBalance.getPositionNumber());
306             balance.setFinancialSubObjectCode(copyBalance.getFinancialSubObjectCode());
307             balance.setFinancialObjectTypeCode(copyBalance.getFinancialObjectTypeCode());
308             balance.setAccountLineAnnualBalanceAmount(copyBalance.getAccountLineAnnualBalanceAmount());
309             balance.setBeginningBalanceLineAmount(copyBalance.getBeginningBalanceLineAmount());
310             balance.setContractsGrantsBeginningBalanceAmount(copyBalance.getContractsGrantsBeginningBalanceAmount());
311             balance.setMonth1Amount(copyBalance.getMonth1Amount());
312             balance.setMonth2Amount(copyBalance.getMonth2Amount());
313             balance.setMonth3Amount(copyBalance.getMonth3Amount());
314             balance.setMonth4Amount(copyBalance.getMonth4Amount());
315             balance.setMonth5Amount(copyBalance.getMonth5Amount());
316             balance.setMonth6Amount(copyBalance.getMonth6Amount());
317             balance.setMonth7Amount(copyBalance.getMonth7Amount());
318             balance.setMonth8Amount(copyBalance.getMonth8Amount());
319             balance.setMonth9Amount(copyBalance.getMonth9Amount());
320             balance.setMonth10Amount(copyBalance.getMonth10Amount());
321             balance.setMonth11Amount(copyBalance.getMonth11Amount());
322             balance.setMonth12Amount(copyBalance.getMonth12Amount());
323             balance.setMonth13Amount(copyBalance.getMonth13Amount());
324 
325             balance.getDummyBusinessObject().setPendingEntryOption(pendingEntryOption);
326             balance.getDummyBusinessObject().setConsolidationOption(Constant.DETAIL);
327 
328             balanceCollection.add(balance);
329         }
330         return balanceCollection;
331     }
332 
333     /**
334      * build the serach result list from the given collection and the number of all qualified search results
335      *
336      * @param searchResultsCollection the given search results, which may be a subset of the qualified search results
337      * @param actualSize the number of all qualified search results
338      * @return the serach result list with the given results and actual size
339      */
340     @Override
341     protected List buildSearchResultList(Collection searchResultsCollection, Long actualSize) {
342         CollectionIncomplete results = new CollectionIncomplete(searchResultsCollection, actualSize);
343 
344         // sort list if default sort column given
345         List searchResults = results;
346         List defaultSortColumns = getDefaultSortColumns();
347         if (defaultSortColumns.size() > 0) {
348             Collections.sort(results, new BeanPropertyComparator(defaultSortColumns, true));
349         }
350         return searchResults;
351     }
352 
353     /**
354      * Gets a list of encumbrance balance types.
355      *
356      * @param fieldValues
357      * @return a list of encumbrance balance types.
358      */
359     protected List<String> getEncumbranceBalanceTypes(Map<String, String> fieldValues) {
360         List<String> encumbranceBalanceTypes = new ArrayList<String>();
361 
362         if (fieldValues.containsKey(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR)) {
363             // parse the university fiscal year since it's a required field from the lookups
364             String universityFiscalYearStr = fieldValues.get(KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR);
365             Integer universityFiscalYear = new Integer(universityFiscalYearStr);
366             encumbranceBalanceTypes = balanceTypService.getEncumbranceBalanceTypes(universityFiscalYear);
367         }
368 
369         return encumbranceBalanceTypes;
370     }
371 
372     /**
373      * Sets the laborInquiryOptionsService attribute value.
374      *
375      * @param laborInquiryOptionsService The laborInquiryOptionsService to set.
376      */
377     public void setLaborInquiryOptionsService(LaborInquiryOptionsService laborInquiryOptionsService) {
378         this.laborInquiryOptionsService = laborInquiryOptionsService;
379     }
380 
381     /**
382      * Sets the balanceService attribute value.
383      *
384      * @param balanceService The balanceService to set.
385      */
386     public void setBalanceService(LaborLedgerBalanceService balanceService) {
387         this.balanceService = balanceService;
388     }
389 
390     /**
391      * Gets the balanceService attribute.
392      *
393      * @return Returns the balanceService.
394      */
395     public LaborLedgerBalanceService getBalanceService() {
396         return balanceService;
397     }
398 
399     /**
400      * Sets the balanceTypService.
401      *
402      * @param balanceTypService
403      */
404     public void setBalanceTypService(BalanceTypeService balanceTypService) {
405         this.balanceTypService = balanceTypService;
406     }
407 }