View Javadoc
1   /*
2    * Copyright 2006 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.gl.businessobject.lookup;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.ole.gl.Constant;
26  import org.kuali.ole.gl.OJBUtility;
27  import org.kuali.ole.gl.batch.service.EncumbranceCalculator;
28  import org.kuali.ole.gl.businessobject.Encumbrance;
29  import org.kuali.ole.gl.businessobject.inquiry.EncumbranceInquirableImpl;
30  import org.kuali.ole.gl.service.EncumbranceService;
31  import org.kuali.ole.sys.OLEConstants;
32  import org.kuali.ole.sys.OLEKeyConstants;
33  import org.kuali.ole.sys.OLEPropertyConstants;
34  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
35  import org.kuali.rice.kns.lookup.HtmlData;
36  import org.kuali.rice.krad.bo.BusinessObject;
37  import org.kuali.rice.krad.exception.ValidationException;
38  import org.kuali.rice.krad.util.GlobalVariables;
39  
40  /**
41   * An extension of KualiLookupableImpl to support encumbrance lookups
42   */
43  public class EncumbranceLookupableHelperServiceImpl extends AbstractGeneralLedgerLookupableHelperServiceImpl {
44  
45      private EncumbranceCalculator postEncumbrance;
46      private EncumbranceService encumbranceService;
47  
48      /**
49       * Returns the url for any drill down links within the lookup
50       * @param bo the business object with a property being drilled down on
51       * @param propertyName the name of the property being drilled down on
52       * @return a String with the URL of the property
53       * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.krad.bo.BusinessObject, java.lang.String)
54       */
55      @Override
56      public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
57          return (new EncumbranceInquirableImpl()).getInquiryUrl(businessObject, propertyName);
58      }
59      
60      /**
61       * Validates the fiscal year searched for in the inquiry
62       * @param fieldValues the values of the query
63       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
64       */
65      @Override
66      public void validateSearchParameters(Map fieldValues) {
67          super.validateSearchParameters(fieldValues);
68  
69          String valueFiscalYear = (String) fieldValues.get(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR);
70          if (!StringUtils.isEmpty(valueFiscalYear)) {
71              try {
72                  int year = Integer.parseInt(valueFiscalYear);
73              }
74              catch (NumberFormatException e) {
75                  GlobalVariables.getMessageMap().putError(OLEPropertyConstants.UNIVERSITY_FISCAL_YEAR, OLEKeyConstants.ERROR_CUSTOM, new String[] { OLEKeyConstants.PendingEntryLookupableImpl.FISCAL_YEAR_FOUR_DIGIT });
76                  throw new ValidationException("errors in search criteria");
77              }
78          }
79          
80          if (!allRequiredsForAccountSearch(fieldValues) && !allRequiredsForDocumentSearch(fieldValues)) {
81              GlobalVariables.getMessageMap().putError("universityFiscalYear", OLEKeyConstants.ERROR_GL_LOOKUP_ENCUMBRANCE_NON_MATCHING_REQUIRED_FIELDS, new String[] {});
82              throw new ValidationException("errors in search criteria");
83          }
84      }
85      
86      /**
87       * Determines if all the required values for an account based search are present - fiscal year, chart, account number, and fiscal period code
88       * @param fieldValues field values to check
89       * @return true if all the account-based required search fields are present; false otherwise
90       */
91      protected boolean allRequiredsForAccountSearch(Map fieldValues) {
92          final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
93          final String chartOfAccountsCode = (String)fieldValues.get("chartOfAccountsCode");
94          final String accountNumber = (String)fieldValues.get("accountNumber");
95          return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(accountNumber);
96      }
97      
98      /**
99       * Determines if all the required values for an document based search are present - fiscal year and document number
100      * @param fieldValues field values to check
101      * @return true if all the document-based required search fields are present; false otherwise
102      */
103     protected boolean allRequiredsForDocumentSearch(Map fieldValues) {
104         final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
105         final String documentNumber = (String)fieldValues.get("documentNumber");
106         return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(documentNumber);
107     }
108 
109     /**
110      * Generates the list of search results for this inquiry
111      * @param fieldValues the field values of the query to carry out
112      * @return List the search results returned by the lookup
113      * @see org.kuali.rice.kns.lookup.Lookupable#getSearchResults(java.util.Map)
114      */
115     @Override
116     public List getSearchResults(Map fieldValues) {
117         setBackLocation((String) fieldValues.get(OLEConstants.BACK_LOCATION));
118         setDocFormKey((String) fieldValues.get(OLEConstants.DOC_FORM_KEY));
119 
120         // get the pending entry option. This method must be prior to the get search results
121         String pendingEntryOption = this.getSelectedPendingEntryOption(fieldValues);
122 
123         final String zeroEncumbranceOption = getSelectedZeroEncumbranceOption(fieldValues); // store in a temporary variable, because the method removes the key from the map
124         final boolean includeZeroEncumbrances = (StringUtils.isBlank(zeroEncumbranceOption) || zeroEncumbranceOption.equals(Constant.ZERO_ENCUMBRANCE_INCLUDE));
125 
126         // get the search result collection
127         Iterator encumbranceIterator = encumbranceService.findOpenEncumbrance(fieldValues, includeZeroEncumbrances);
128         Collection searchResultsCollection = this.buildEncumbranceCollection(encumbranceIterator);
129 
130         // update search results according to the selected pending entry option
131         updateByPendingLedgerEntry(searchResultsCollection, fieldValues, pendingEntryOption, false, false);
132 
133         // get the actual size of all qualified search results
134         Integer recordCount = encumbranceService.getOpenEncumbranceRecordCount(fieldValues, includeZeroEncumbrances);
135         Long actualSize = OJBUtility.getResultActualSize(searchResultsCollection, recordCount, fieldValues, new Encumbrance());
136 
137         return this.buildSearchResultList(searchResultsCollection, actualSize);
138     }
139 
140     /**
141      * Updates pending entries before their results are included in the lookup results
142      * 
143      * @param entryCollection a collection of balance entries
144      * @param fieldValues the map containing the search fields and values
145      * @param isApproved flag whether the approved entries or all entries will be processed
146      * @param isConsolidated flag whether the results are consolidated or not
147      * @param isCostShareExcluded flag whether the user selects to see the results with cost share subaccount
148      * @see org.kuali.module.gl.web.lookupable.AbstractGLLookupableImpl#updateEntryCollection(java.util.Collection, java.util.Map,
149      *      boolean, boolean, boolean)
150      */
151     @Override
152     protected void updateEntryCollection(Collection entryCollection, Map fieldValues, boolean isApproved, boolean isConsolidated, boolean isCostShareInclusive) {
153 
154         // convert the field names of balance object into corresponding ones of pending entry object
155         Map pendingEntryFieldValues = BusinessObjectFieldConverter.convertToTransactionFieldValues(fieldValues);
156 
157         // go through the pending entries to update the encumbrance collection
158         Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService().findPendingLedgerEntriesForEncumbrance(pendingEntryFieldValues, isApproved);
159         while (pendingEntryIterator.hasNext()) {
160             GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator.next();
161             Encumbrance encumbrance = postEncumbrance.findEncumbrance(entryCollection, pendingEntry);
162             postEncumbrance.updateEncumbrance(pendingEntry, encumbrance);
163         }
164     }
165 
166     /**
167      * go through the given iterator to get encumbrances and put them into a collection
168      * @param iterator an iterator of encumbrances
169      * @return a collection of those encumbrances
170      */
171     private Collection buildEncumbranceCollection(Iterator iterator) {
172         Collection encumbranceCollection = new ArrayList();
173 
174         while (iterator.hasNext()) {
175             Encumbrance encumrbance = (Encumbrance) iterator.next();
176             encumbranceCollection.add(encumrbance);
177         }
178         return encumbranceCollection;
179     }
180 
181     /**
182      * Method tests to see if the user selected to include or exclude zero encumbrances
183      * @param fieldValues the lookup field values
184      * @return the value of the zero encumbrance option
185      */
186     protected String getSelectedZeroEncumbranceOption(Map fieldValues) {
187         return (String)fieldValues.remove(Constant.ZERO_ENCUMBRANCE_OPTION);
188     }
189 
190     /**
191      * Sets the postEncumbrance attribute value.
192      * 
193      * @param postEncumbrance The postEncumbrance to set.
194      */
195     public void setPostEncumbrance(EncumbranceCalculator postEncumbrance) {
196         this.postEncumbrance = postEncumbrance;
197     }
198 
199     /**
200      * Sets the encumbranceService attribute value.
201      * 
202      * @param encumbranceService The encumbranceService to set.
203      */
204     public void setEncumbranceService(EncumbranceService encumbranceService) {
205         this.encumbranceService = encumbranceService;
206     }
207 }