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.sql.Date;
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.apache.commons.lang.StringUtils;
025: import org.kuali.core.bo.BusinessObject;
026: import org.kuali.core.exceptions.ValidationException;
027: import org.kuali.core.service.DateTimeService;
028: import org.kuali.core.util.GlobalVariables;
029: import org.kuali.kfs.KFSConstants;
030: import org.kuali.kfs.KFSKeyConstants;
031: import org.kuali.kfs.KFSPropertyConstants;
032: import org.kuali.kfs.bo.GeneralLedgerPendingEntry;
033: import org.kuali.kfs.context.SpringContext;
034: import org.kuali.module.financial.service.UniversityDateService;
035: import org.kuali.module.gl.bo.Entry;
036: import org.kuali.module.gl.bo.UniversityDate;
037: import org.kuali.module.gl.service.EntryService;
038: import org.kuali.module.gl.service.ScrubberValidator;
039: import org.kuali.module.gl.util.BusinessObjectFieldConverter;
040: import org.kuali.module.gl.web.Constant;
041: import org.kuali.module.gl.web.inquirable.EntryInquirableImpl;
042: import org.kuali.module.gl.web.inquirable.InquirableFinancialDocument;
043: import org.springframework.transaction.annotation.Transactional;
044:
045: /**
046: * An extension of KualiLookupableImpl to support entry lookups
047: */
048: @Transactional
049: public class EntryLookupableHelperServiceImpl extends
050: AbstractGLLookupableHelperServiceImpl {
051: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
052: .getLogger(EntryLookupableHelperServiceImpl.class);
053:
054: private ScrubberValidator scrubberValidator;
055: private EntryService entryService;
056:
057: /**
058: * Validate the university fiscal year that has been queried on
059: *
060: * @param fieldValues the queried fields
061: * @see org.kuali.core.lookup.AbstractLookupableHelperServiceImpl#validateSearchParameters(java.util.Map)
062: */
063: @Override
064: public void validateSearchParameters(Map fieldValues) {
065: super .validateSearchParameters(fieldValues);
066:
067: String valueFiscalYear = (String) fieldValues
068: .get("universityFiscalYear");
069: if (!StringUtils.isEmpty(valueFiscalYear)) {
070: try {
071: int year = Integer.parseInt(valueFiscalYear);
072: } catch (NumberFormatException e) {
073: GlobalVariables
074: .getErrorMap()
075: .putError(
076: "universityFiscalYear",
077: KFSKeyConstants.ERROR_CUSTOM,
078: new String[] { "Fiscal Year must be a four-digit number" });
079: throw new ValidationException(
080: "errors in search criteria");
081: }
082: }
083: }
084:
085: /**
086: * Returns the url for any drill down links within the lookup
087: * @param bo the business object with a property being drilled down on
088: * @param propertyName the name of the property being drilled down on
089: * @return a String with the URL of the property
090: * @see org.kuali.core.lookup.Lookupable#getInquiryUrl(org.kuali.core.bo.BusinessObject, java.lang.String)
091: */
092: @Override
093: public String getInquiryUrl(BusinessObject businessObject,
094: String propertyName) {
095: if (KFSPropertyConstants.DOCUMENT_NUMBER.equals(propertyName)) {
096: if (businessObject instanceof Entry) {
097: Entry entry = (Entry) businessObject;
098: return new InquirableFinancialDocument()
099: .getInquirableDocumentUrl(entry);
100: }
101: }
102: return (new EntryInquirableImpl()).getInquiryUrl(
103: businessObject, propertyName);
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: @Override
113: public List getSearchResults(Map fieldValues) {
114: setBackLocation((String) fieldValues
115: .get(KFSConstants.BACK_LOCATION));
116: setDocFormKey((String) fieldValues
117: .get(KFSConstants.DOC_FORM_KEY));
118:
119: // get the pending entry option. This method must be prior to the get search results
120: String pendingEntryOption = this
121: .getSelectedPendingEntryOption(fieldValues);
122:
123: // get the search result collection
124: Collection searchResultsCollection = getLookupService()
125: .findCollectionBySearch(getBusinessObjectClass(),
126: fieldValues);
127:
128: // update search results according to the selected pending entry option
129: updateByPendingLedgerEntry(searchResultsCollection,
130: fieldValues, pendingEntryOption, false, false);
131:
132: // get the actual size of all qualified search results
133: Long actualSize = new Long(entryService
134: .getEntryRecordCount(fieldValues));
135:
136: return this .buildSearchResultList(searchResultsCollection,
137: 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,
153: Map fieldValues, boolean isApproved,
154: boolean isConsolidated, boolean isCostShareInclusive) {
155: LOG.debug("updateEntryCollection started");
156:
157: // convert the field names of balance object into corresponding ones of pending entry object
158: Map pendingEntryFieldValues = BusinessObjectFieldConverter
159: .convertToTransactionFieldValues(fieldValues);
160:
161: // go through the pending entries to update the balance collection
162: Iterator pendingEntryIterator = getGeneralLedgerPendingEntryService()
163: .findPendingLedgerEntriesForEntry(
164: pendingEntryFieldValues, isApproved);
165:
166: String pendingOption = isApproved ? Constant.APPROVED_PENDING_ENTRY
167: : Constant.ALL_PENDING_ENTRY;
168: UniversityDate today = SpringContext.getBean(
169: UniversityDateService.class).getCurrentUniversityDate();
170: String currentFiscalPeriodCode = today
171: .getUniversityFiscalAccountingPeriod();
172: Integer currentFiscalYear = today.getUniversityFiscalYear();
173: Date postDate = SpringContext.getBean(DateTimeService.class)
174: .getCurrentSqlDate();
175:
176: while (pendingEntryIterator.hasNext()) {
177: GeneralLedgerPendingEntry pendingEntry = (GeneralLedgerPendingEntry) pendingEntryIterator
178: .next();
179:
180: // Gotta circumvent date checks in the scrubberValidator. They totally kill performance.
181: if (pendingEntry.getUniversityFiscalYear() == null) {
182: pendingEntry.setUniversityFiscalYear(currentFiscalYear);
183: }
184:
185: if (pendingEntry.getUniversityFiscalPeriodCode() == null) {
186: pendingEntry
187: .setUniversityFiscalPeriodCode(currentFiscalPeriodCode);
188: }
189:
190: scrubberValidator.validateForInquiry(pendingEntry);
191: entryCollection.add(new Entry(pendingEntry, postDate));
192: }
193: }
194:
195: /**
196: * Sets the scrubberValidator attribute value.
197: *
198: * @param scrubberValidator The scrubberValidator to set.
199: */
200: public void setScrubberValidator(ScrubberValidator scrubberValidator) {
201: this .scrubberValidator = scrubberValidator;
202: }
203:
204: /**
205: * Sets the entryService attribute value.
206: *
207: * @param entryService The entryService to set.
208: */
209: public void setEntryService(EntryService entryService) {
210: this.entryService = entryService;
211: }
212: }
|