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.sql.Date;
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.businessobject.Entry;
27  import org.kuali.ole.gl.businessobject.inquiry.EntryInquirableImpl;
28  import org.kuali.ole.gl.businessobject.inquiry.InquirableFinancialDocument;
29  import org.kuali.ole.gl.service.EntryService;
30  import org.kuali.ole.gl.service.ScrubberValidator;
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.ole.sys.businessobject.UniversityDate;
36  import org.kuali.ole.sys.context.SpringContext;
37  import org.kuali.ole.sys.service.UniversityDateService;
38  import org.kuali.rice.core.api.datetime.DateTimeService;
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.util.GlobalVariables;
44  import org.kuali.rice.krad.util.KRADConstants;
45  
46  /**
47   * An extension of KualiLookupableImpl to support entry lookups
48   */
49  public class EntryLookupableHelperServiceImpl extends AbstractGeneralLedgerLookupableHelperServiceImpl {
50      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(EntryLookupableHelperServiceImpl.class);
51  
52      private ScrubberValidator scrubberValidator;
53      private EntryService entryService;
54  
55      /**
56       * Validate the university fiscal year that has been queried on
57       *
58       * @param fieldValues the queried fields
59       * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
60       */
61      @Override
62      public void validateSearchParameters(Map fieldValues) {
63          super.validateSearchParameters(fieldValues);
64  
65          String valueFiscalYear = (String) fieldValues.get("universityFiscalYear");
66          if (!StringUtils.isEmpty(valueFiscalYear)) {
67              try {
68                  int year = Integer.parseInt(valueFiscalYear);
69              }
70              catch (NumberFormatException e) {
71                  GlobalVariables.getMessageMap().putError("universityFiscalYear", OLEKeyConstants.ERROR_CUSTOM, new String[] { "Fiscal Year must be a four-digit number" });
72                  throw new ValidationException("errors in search criteria");
73              }
74          }
75  
76          if (!allRequiredsForAccountSearch(fieldValues) && !allRequiredsForDocumentSearch(fieldValues)) {
77              GlobalVariables.getMessageMap().putError("universityFiscalYear", OLEKeyConstants.ERROR_GL_LOOKUP_ENTRY_NON_MATCHING_REQUIRED_FIELDS, new String[] {});
78              throw new ValidationException("errors in search criteria");
79          }
80      }
81  
82      /**
83       * Determines if all the required values for an account based search are present - fiscal year, chart, account number, and fiscal period code
84       * @param fieldValues field values to check
85       * @return true if all the account-based required search fields are present; false otherwise
86       */
87      protected boolean allRequiredsForAccountSearch(Map fieldValues) {
88          final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
89          final String chartOfAccountsCode = (String)fieldValues.get("chartOfAccountsCode");
90          final String accountNumber = (String)fieldValues.get("accountNumber");
91          final String fiscalPeriodCode = (String)fieldValues.get("universityFiscalPeriodCode");
92          return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(chartOfAccountsCode) && !StringUtils.isBlank(accountNumber) && !StringUtils.isBlank(fiscalPeriodCode);
93      }
94  
95      /**
96       * Determines if all the required values for an document based search are present - fiscal year and document number
97       * @param fieldValues field values to check
98       * @return true if all the document-based required search fields are present; false otherwise
99       */
100     protected boolean allRequiredsForDocumentSearch(Map fieldValues) {
101         final String fiscalYearAsString = (String)fieldValues.get("universityFiscalYear");
102         final String documentNumber = (String)fieldValues.get("documentNumber");
103         return !StringUtils.isBlank(fiscalYearAsString) && !StringUtils.isBlank(documentNumber);
104     }
105 
106     /**
107      * Returns the url for any drill down links within the lookup
108      * @param bo the business object with a property being drilled down on
109      * @param propertyName the name of the property being drilled down on
110      * @return a String with the URL of the property
111      * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject, java.lang.String)
112      */
113     @Override
114     public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
115         if (OLEPropertyConstants.DOCUMENT_NUMBER.equals(propertyName)) {
116             if (businessObject instanceof Entry) {
117                 Entry entry = (Entry) businessObject;
118                 return new AnchorHtmlData(new InquirableFinancialDocument().getInquirableDocumentUrl(entry), KRADConstants.EMPTY_STRING, "view entry "+entry.toString());
119             }
120         }
121         return (new EntryInquirableImpl()).getInquiryUrl(businessObject, propertyName);
122     }
123 
124     /**
125      * Generates the list of search results for this inquiry
126      * @param fieldValues the field values of the query to carry out
127      * @return List the search results returned by the lookup
128      * @see org.kuali.rice.kns.lookup.Lookupable#getSearchResults(java.util.Map)
129      */
130     @Override
131     public List getSearchResults(Map fieldValues) {
132         setBackLocation((String) fieldValues.get(OLEConstants.BACK_LOCATION));
133         setDocFormKey((String) fieldValues.get(OLEConstants.DOC_FORM_KEY));
134 
135         // get the pending entry option. This method must be prior to the get search results
136         String pendingEntryOption = this.getSelectedPendingEntryOption(fieldValues);
137 
138         String debitCreditOption  = this.getDebitCreditOption(fieldValues);
139 
140         // get the search result collection
141         Collection searchResultsCollection = getLookupService().findCollectionBySearch(getBusinessObjectClass(), fieldValues);
142 
143         // update search results according to the selected pending entry option
144         updateByPendingLedgerEntry(searchResultsCollection, fieldValues, pendingEntryOption, false, false);
145 
146         // get the actual size of all qualified search results
147         Long actualSize = new Long(entryService.getEntryRecordCount(fieldValues));
148 
149         return this.buildSearchResultList(searchResultsCollection, actualSize);
150     }
151 
152     /**
153      * Updates pending entries before their results are included in the lookup results
154      *
155      * @param entryCollection a collection of balance entries
156      * @param fieldValues the map containing the search fields and values
157      * @param isApproved flag whether the approved entries or all entries will be processed
158      * @param isConsolidated flag whether the results are consolidated or not
159      * @param isCostShareExcluded flag whether the user selects to see the results with cost share subaccount
160      * @see org.kuali.module.gl.web.lookupable.AbstractGLLookupableImpl#updateEntryCollection(java.util.Collection, java.util.Map,
161      *      boolean, boolean, boolean)
162      */
163     @Override
164     protected void updateEntryCollection(Collection entryCollection, Map fieldValues, boolean isApproved, boolean isConsolidated, boolean isCostShareInclusive) {
165         LOG.debug("updateEntryCollection started");
166 
167         // convert the field names of balance object into corresponding ones of pending entry object
168         Map pendingEntryFieldValues = BusinessObjectFieldConverter.convertToTransactionFieldValues(fieldValues);
169 
170         // go through the pending entries to update the balance collection
171         Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService().findPendingLedgerEntriesForEntry(pendingEntryFieldValues, isApproved);
172 
173         String pendingOption = isApproved ? Constant.APPROVED_PENDING_ENTRY : Constant.ALL_PENDING_ENTRY;
174         UniversityDate today = SpringContext.getBean(UniversityDateService.class).getCurrentUniversityDate();
175         String currentFiscalPeriodCode = today.getUniversityFiscalAccountingPeriod();
176         Integer currentFiscalYear = today.getUniversityFiscalYear();
177         Date postDate = SpringContext.getBean(DateTimeService.class).getCurrentSqlDate();
178 
179         while (pendingEntryIterator.hasNext()) {
180             GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator.next();
181 
182             // Gotta circumvent date checks in the scrubberValidator. They totally kill performance.
183             if (pendingEntry.getUniversityFiscalYear() == null) {
184                 pendingEntry.setUniversityFiscalYear(currentFiscalYear);
185             }
186 
187             if (pendingEntry.getUniversityFiscalPeriodCode() == null) {
188                 pendingEntry.setUniversityFiscalPeriodCode(currentFiscalPeriodCode);
189             }
190 
191             scrubberValidator.validateForInquiry(pendingEntry);
192 
193             Entry entry = new Entry(pendingEntry, postDate);
194 
195             String approvedCode = pendingEntry.getFinancialDocumentApprovedCode();
196             String description = Constant.DocumentApprovedCode.getDescription(approvedCode);
197             entry.getDummyBusinessObject().setPendingEntryOption(description);
198 
199             entryCollection.add(entry);
200         }
201     }
202 
203     /**
204      * Sets the scrubberValidator attribute value.
205      *
206      * @param scrubberValidator The scrubberValidator to set.
207      */
208     public void setScrubberValidator(ScrubberValidator scrubberValidator) {
209         this.scrubberValidator = scrubberValidator;
210     }
211 
212     /**
213      * Sets the entryService attribute value.
214      *
215      * @param entryService The entryService to set.
216      */
217     public void setEntryService(EntryService entryService) {
218         this.entryService = entryService;
219     }
220 }