001: /*
002: * Copyright 2006-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.gl.web.lookupable;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Collections;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.kuali.core.bo.BusinessObject;
027: import org.kuali.core.exceptions.ValidationException;
028: import org.kuali.core.lookup.AbstractLookupableHelperServiceImpl;
029: import org.kuali.core.lookup.CollectionIncomplete;
030: import org.kuali.core.util.BeanPropertyComparator;
031: import org.kuali.core.util.GlobalVariables;
032: import org.kuali.kfs.KFSConstants;
033: import org.kuali.kfs.KFSKeyConstants;
034: import org.kuali.kfs.KFSPropertyConstants;
035: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
036: import org.kuali.kfs.context.SpringContext;
037: import org.kuali.kfs.service.GeneralLedgerPendingEntryService;
038: import org.kuali.kfs.service.ParameterService;
039: import org.kuali.kfs.service.impl.ParameterConstants;
040: import org.kuali.module.financial.service.UniversityDateService;
041: import org.kuali.module.gl.bo.UniversityDate;
042: import org.kuali.module.gl.web.Constant;
043: import org.kuali.module.gl.web.inquirable.InquirableFinancialDocument;
044:
045: /**
046: * An extension of KualiLookupableImpl to support balance lookups
047: */
048: public class PendingEntryLookupableHelperServiceImpl extends
049: AbstractLookupableHelperServiceImpl {
050: private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(PendingEntryLookupableHelperServiceImpl.class);
052:
053: private GeneralLedgerPendingEntryService generalLedgerPendingEntryService;
054: private ParameterService parameterService;
055:
056: private final static String UNIVERSITY_FISCAL_YEAR = "universityFiscalYear";
057: private final static String UNIVERSITY_FISCAL_PERIOD_CODE = "universityFiscalPeriodCode";
058:
059: /**
060: * Returns the url for any drill down links within the lookup (defers to its superclass unless it needs
061: * to get the url of the document that created this result pending entry)
062: * @param bo the business object with a property being drilled down on
063: * @param propertyName the name of the property being drilled down on
064: * @return a String with the URL of the property
065: * @see org.kuali.core.lookup.Lookupable#getInquiryUrl(org.kuali.core.bo.BusinessObject, java.lang.String)
066: */
067: @Override
068: public String getInquiryUrl(BusinessObject businessObject,
069: String propertyName) {
070: if (KFSPropertyConstants.DOCUMENT_NUMBER.equals(propertyName)
071: && businessObject instanceof GeneralLedgerPendingEntry) {
072: GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) businessObject;
073: return new InquirableFinancialDocument()
074: .getInquirableDocumentUrl(pendingEntry);
075: }
076: return super .getInquiryUrl(businessObject, propertyName);
077: }
078:
079: /**
080: * Validates the fiscal year searched for in the inquiry
081: * @param fieldValues the values of the query
082: * @see org.kuali.core.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
083: */
084: @Override
085: public void validateSearchParameters(Map fieldValues) {
086: super .validateSearchParameters(fieldValues);
087:
088: String valueFiscalYear = (String) fieldValues
089: .get(UNIVERSITY_FISCAL_YEAR);
090: if (!StringUtils.isEmpty(valueFiscalYear)) {
091: try {
092: int year = Integer.parseInt(valueFiscalYear);
093: } catch (NumberFormatException e) {
094: GlobalVariables
095: .getErrorMap()
096: .putError(
097: UNIVERSITY_FISCAL_YEAR,
098: KFSKeyConstants.ERROR_CUSTOM,
099: new String[] { KFSKeyConstants.PendingEntryLookupableImpl.FISCAL_YEAR_FOUR_DIGIT });
100: throw new ValidationException(
101: "errors in search criteria");
102: }
103: }
104: }
105:
106: /**
107: * Generates the list of search results for this inquiry
108: * @param fieldValues the field values of the query to carry out
109: * @return List the search results returned by the lookup
110: * @see org.kuali.core.lookup.Lookupable#getSearchResults(java.util.Map)
111: */
112: public List getSearchResults(Map fieldValues) {
113: setBackLocation((String) fieldValues
114: .get(KFSConstants.BACK_LOCATION));
115: setDocFormKey((String) fieldValues
116: .get(KFSConstants.DOC_FORM_KEY));
117:
118: boolean isApproved = fieldValues
119: .containsKey(Constant.PENDING_ENTRY_OPTION)
120: && Constant.APPROVED_PENDING_ENTRY.equals(fieldValues
121: .get(Constant.PENDING_ENTRY_OPTION));
122: Collection searchResultsCollection = generalLedgerPendingEntryService
123: .findPendingEntries(fieldValues, isApproved);
124:
125: // sort list if default sort column given
126: List searchResults = (List) searchResultsCollection;
127: List defaultSortColumns = getDefaultSortColumns();
128: if (defaultSortColumns.size() > 0) {
129: Collections.sort(searchResults, new BeanPropertyComparator(
130: defaultSortColumns, true));
131: }
132:
133: // get the result limit number from configuration
134: String limitConfig = parameterService.getParameterValue(
135: ParameterConstants.NERVOUS_SYSTEM_LOOKUP.class,
136: KFSConstants.LOOKUP_RESULTS_LIMIT_URL_KEY);
137: Integer limit = null;
138: if (limitConfig != null) {
139: limit = Integer.valueOf(limitConfig);
140: }
141:
142: Long collectionCount = new Long(searchResults.size());
143: Long actualCountIfTruncated = new Long(0);
144:
145: // If more than limit number of records were returned, removed
146: if (limit != null) {
147: if (collectionCount >= limit.intValue()) {
148: actualCountIfTruncated = collectionCount;
149: for (int i = collectionCount.intValue() - 1; i >= limit; i--) {
150: searchResults.remove(i);
151: }
152: }
153: }
154:
155: UniversityDate currentUniversityDate = SpringContext.getBean(
156: UniversityDateService.class).getCurrentUniversityDate();
157: String currentFiscalPeriodCode = currentUniversityDate
158: .getUniversityFiscalAccountingPeriod();
159: Integer currentFiscalYear = currentUniversityDate
160: .getUniversityFiscalYear();
161:
162: String fiscalPeriodFromForm = null;
163: if (fieldValues.containsKey(UNIVERSITY_FISCAL_PERIOD_CODE)) {
164: fiscalPeriodFromForm = (String) fieldValues
165: .get(UNIVERSITY_FISCAL_PERIOD_CODE);
166: }
167:
168: String fiscalYearFromForm = null;
169: if (fieldValues.containsKey(UNIVERSITY_FISCAL_YEAR)) {
170: fiscalYearFromForm = (String) fieldValues
171: .get(UNIVERSITY_FISCAL_YEAR);
172: }
173: // Set null fy and ap to current values.
174: for (Iterator i = searchResults.iterator(); i.hasNext();) {
175: GeneralLedgerPendingEntry glpe = (GeneralLedgerPendingEntry) i
176: .next();
177:
178: if (currentFiscalPeriodCode.equals(fiscalPeriodFromForm)
179: && null == glpe.getUniversityFiscalPeriodCode()) {
180: glpe
181: .setUniversityFiscalPeriodCode(currentFiscalPeriodCode);
182: }
183:
184: if (currentFiscalYear.toString().equals(fiscalYearFromForm)
185: && null == glpe.getUniversityFiscalYear()) {
186: glpe.setUniversityFiscalYear(currentFiscalYear);
187: }
188: }
189:
190: return new CollectionIncomplete(searchResults,
191: actualCountIfTruncated);
192: }
193:
194: /**
195: * This method builds the collection of search results without period codes and updates the results with current period code
196: *
197: * @param iterator the iterator of search results
198: * @param periodCode the current period code
199: * @return the collection of search results with updated period codes
200: */
201: private Collection buildSearchResults(Iterator iterator,
202: String periodCode) {
203: Collection collection = new ArrayList();
204:
205: while (iterator.hasNext()) {
206: GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) iterator
207: .next();
208: pendingEntry.setUniversityFiscalPeriodCode(periodCode);
209: collection.add(pendingEntry);
210: }
211:
212: return new CollectionIncomplete(collection, new Long(collection
213: .size()));
214: }
215:
216: /**
217: * Sets the generalLedgerPendingEntryService attribute value.
218: *
219: * @param generalLedgerPendingEntryService The generalLedgerPendingEntryService to set.
220: */
221: public void setGeneralLedgerPendingEntryService(
222: GeneralLedgerPendingEntryService generalLedgerPendingEntryService) {
223: this .generalLedgerPendingEntryService = generalLedgerPendingEntryService;
224: }
225:
226: public void setParameterService(ParameterService parameterService) {
227: this.parameterService = parameterService;
228: }
229:
230: }
|