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.Collections;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.ole.gl.Constant;
27  import org.kuali.ole.gl.businessobject.inquiry.EntryInquirableImpl;
28  import org.kuali.ole.gl.businessobject.inquiry.InquirableFinancialDocument;
29  import org.kuali.ole.sys.OLEConstants;
30  import org.kuali.ole.sys.OLEKeyConstants;
31  import org.kuali.ole.sys.OLEPropertyConstants;
32  import org.kuali.ole.sys.businessobject.GeneralLedgerPendingEntry;
33  import org.kuali.ole.sys.businessobject.UniversityDate;
34  import org.kuali.ole.sys.context.SpringContext;
35  import org.kuali.ole.sys.service.GeneralLedgerPendingEntryService;
36  import org.kuali.ole.sys.service.UniversityDateService;
37  import org.kuali.ole.sys.service.impl.OleParameterConstants;
38  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
39  import org.kuali.rice.kns.lookup.HtmlData;
40  import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
41  import org.kuali.rice.krad.bo.BusinessObject;
42  import org.kuali.rice.krad.exception.ValidationException;
43  import org.kuali.rice.krad.lookup.CollectionIncomplete;
44  import org.kuali.rice.krad.util.BeanPropertyComparator;
45  import org.kuali.rice.krad.util.GlobalVariables;
46  import org.kuali.rice.krad.util.KRADConstants;
47  
48  /**
49   * An extension of KualiLookupableImpl to support balance lookups
50   */
51  public class PendingEntryLookupableHelperServiceImpl extends AbstractGeneralLedgerLookupableHelperServiceImpl {
52      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(PendingEntryLookupableHelperServiceImpl.class);
53  
54      private GeneralLedgerPendingEntryService generalLedgerPendingEntryService;
55      private ParameterService parameterService;
56  
57      private final static String UNIVERSITY_FISCAL_YEAR = "universityFiscalYear";
58      private final static String UNIVERSITY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
59  
60      /**
61       * Returns the url for any drill down links within the lookup (defers to its superclass unless it needs
62       * to get the url of the document that created this result pending entry)
63       * @param bo the business object with a property being drilled down on
64       * @param propertyName the name of the property being drilled down on
65       * @return a String with the URL of the property
66       * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.krad.bo.BusinessObject, java.lang.String)
67       */
68      @Override
69      public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
70          if (OLEPropertyConstants.DOCUMENT_NUMBER.equals(propertyName) && businessObject instanceof GeneralLedgerPendingEntry) {
71              GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) businessObject;
72              return new AnchorHtmlData(new InquirableFinancialDocument().getInquirableDocumentUrl(pendingEntry), KRADConstants.EMPTY_STRING, "view pending entry "+pendingEntry.toString());
73          }
74          return (new EntryInquirableImpl()).getInquiryUrl(businessObject, propertyName);
75          //return super.getInquiryUrl(businessObject, propertyName);
76      }
77  
78  
79      /**
80       * Validates the fiscal year searched for in the inquiry
81       * @param fieldValues the values of the query
82       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
83       */
84      @Override
85      public void validateSearchParameters(Map fieldValues) {
86          super.validateSearchParameters(fieldValues);
87  
88          String valueFiscalYear = (String) fieldValues.get(UNIVERSITY_FISCAL_YEAR);
89          if (!StringUtils.isEmpty(valueFiscalYear)) {
90              try {
91                  int year = Integer.parseInt(valueFiscalYear);
92              }
93              catch (NumberFormatException e) {
94                  GlobalVariables.getMessageMap().putError(UNIVERSITY_FISCAL_YEAR, OLEKeyConstants.ERROR_CUSTOM, new String[] { OLEKeyConstants.PendingEntryLookupableImpl.FISCAL_YEAR_FOUR_DIGIT });
95                  throw new ValidationException("errors in search criteria");
96              }
97          }
98  
99          if (!allRequiredsForAccountSearch(fieldValues) && !allRequiredsForDocumentSearch(fieldValues)) {
100             GlobalVariables.getMessageMap().putError("universityFiscalYear", OLEKeyConstants.ERROR_GL_LOOKUP_PENDING_ENTRY_NON_MATCHING_REQUIRED_FIELDS, new String[] {});
101             throw new ValidationException("errors in search criteria");
102         }
103     }
104 
105     /**
106      * Determines if all the required values for an account based search are present - fiscal year, chart, account number, and fiscal period code
107      * @param fieldValues field values to check
108      * @return true if all the account-based required search fields are present; false otherwise
109      */
110     protected boolean allRequiredsForAccountSearch(Map fieldValues) {
111         final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
112         final String chartOfAccountsCode = (String)fieldValues.get("chartOfAccountsCode");
113         final String accountNumber = (String)fieldValues.get("accountNumber");
114         final String fiscalPeriodCode = (String)fieldValues.get("universityFiscalPeriodCode");
115         return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(accountNumber) && !StringUtils.isBlank(fiscalPeriodCode);
116     }
117 
118     /**
119      * Determines if all the required values for an document based search are present - fiscal year and document number
120      * @param fieldValues field values to check
121      * @return true if all the document-based required search fields are present; false otherwise
122      */
123     protected boolean allRequiredsForDocumentSearch(Map fieldValues) {
124         final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
125         final String documentNumber = (String)fieldValues.get("documentNumber");
126         return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(documentNumber);
127     }
128 
129 
130     /**
131      * Generates the list of search results for this inquiry
132      * @param fieldValues the field values of the query to carry out
133      * @return List the search results returned by the lookup
134      * @see org.kuali.rice.kns.lookup.Lookupable#getSearchResults(java.util.Map)
135      */
136     @Override
137     public List getSearchResults(Map fieldValues) {
138         setBackLocation((String) fieldValues.get(OLEConstants.BACK_LOCATION));
139         setDocFormKey((String) fieldValues.get(OLEConstants.DOC_FORM_KEY));
140 
141         boolean isApproved = fieldValues.containsKey(Constant.PENDING_ENTRY_OPTION) && Constant.APPROVED_PENDING_ENTRY.equals(fieldValues.get(Constant.PENDING_ENTRY_OPTION));
142         Collection searchResultsCollection = generalLedgerPendingEntryService.findPendingEntries(fieldValues, isApproved);
143 
144         // sort list if default sort column given
145         List searchResults = (List) searchResultsCollection;
146         List defaultSortColumns = getDefaultSortColumns();
147         if (defaultSortColumns.size() > 0) {
148             Collections.sort(searchResults, new BeanPropertyComparator(defaultSortColumns, true));
149         }
150 
151         // get the result limit number from configuration
152         String limitConfig = parameterService.getParameterValueAsString(OleParameterConstants.NERVOUS_SYSTEM_LOOKUP.class, OLEConstants.LOOKUP_RESULTS_LIMIT_URL_KEY);
153         Integer limit = null;
154         if (limitConfig != null) {
155             limit = Integer.valueOf(limitConfig);
156         }
157 
158         Long collectionCount = new Long(searchResults.size());
159         Long actualCountIfTruncated = new Long(0);
160 
161         // If more than limit number of records were returned, removed
162         if (limit != null) {
163             if (collectionCount >= limit.intValue()) {
164                 actualCountIfTruncated = collectionCount;
165                 for (int i = collectionCount.intValue() - 1; i >= limit; i--) {
166                     searchResults.remove(i);
167                 }
168             }
169         }
170 
171         UniversityDate currentUniversityDate = SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate();
172         String currentFiscalPeriodCode = currentUniversityDate.getUniversityFiscalAccountingPeriod();
173         Integer currentFiscalYear = currentUniversityDate.getUniversityFiscalYear();
174 
175         String fiscalPeriodFromForm = null;
176         if (fieldValues.containsKey(UNIVERSITY_FISCAL_PERIOD_CODE)) {
177             fiscalPeriodFromForm = (String) fieldValues.get(UNIVERSITY_FISCAL_PERIOD_CODE);
178         }
179 
180         String fiscalYearFromForm = null;
181         if (fieldValues.containsKey(UNIVERSITY_FISCAL_YEAR)) {
182             fiscalYearFromForm = (String) fieldValues.get(UNIVERSITY_FISCAL_YEAR);
183         }
184         // Set null fy and ap to current values.
185         for (Iterator i = searchResults.iterator(); i.hasNext();) {
186             GeneralLedgerPendingEntry glpe = (GeneralLedgerPendingEntry) i.next();
187 
188             if (currentFiscalPeriodCode.equals(fiscalPeriodFromForm) && null == glpe.getUniversityFiscalPeriodCode()) {
189                 glpe.setUniversityFiscalPeriodCode(currentFiscalPeriodCode);
190             }
191 
192             if (currentFiscalYear.toString().equals(fiscalYearFromForm) && null == glpe.getUniversityFiscalYear()) {
193                 glpe.setUniversityFiscalYear(currentFiscalYear);
194             }
195         }
196 
197         return new CollectionIncomplete(searchResults, actualCountIfTruncated);
198     }
199 
200     /**
201      * This method builds the collection of search results without period codes and updates the results with current period code
202      *
203      * @param iterator the iterator of search results
204      * @param periodCode the current period code
205      * @return the collection of search results with updated period codes
206      */
207     private Collection buildSearchResults(Iterator iterator, String periodCode) {
208         Collection collection = new ArrayList();
209 
210         while (iterator.hasNext()) {
211             GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) iterator.next();
212             pendingEntry.setUniversityFiscalPeriodCode(periodCode);
213             collection.add(pendingEntry);
214         }
215 
216         return new CollectionIncomplete(collection, new Long(collection.size()));
217     }
218 
219 
220     @Override
221     protected void updateEntryCollection(Collection entryCollection, Map fieldValues, boolean isApproved, boolean isConsolidated, boolean isCostShareInclusive) {
222         // TODO Auto-generated method stub
223 
224     }
225 
226     /**
227      * Sets the generalLedgerPendingEntryService attribute value.
228      *
229      * @param generalLedgerPendingEntryService The generalLedgerPendingEntryService to set.
230      */
231     @Override
232     public void setGeneralLedgerPendingEntryService(GeneralLedgerPendingEntryService generalLedgerPendingEntryService) {
233         this.generalLedgerPendingEntryService = generalLedgerPendingEntryService;
234     }
235 
236     @Override
237     public void setParameterService(ParameterService parameterService) {
238         this.parameterService = parameterService;
239     }
240 
241 }