001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.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/ecl2.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.kfs.module.ld.businessobject.lookup;
017    
018    import static org.kuali.kfs.module.ld.LaborConstants.BalanceInquiries.BALANCE_TYPE_AC_AND_A21;
019    
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.HashMap;
023    import java.util.List;
024    import java.util.Map;
025    
026    import org.apache.commons.lang.StringUtils;
027    import org.kuali.kfs.gl.businessobject.inquiry.EntryInquirableImpl;
028    import org.kuali.kfs.gl.businessobject.inquiry.InquirableFinancialDocument;
029    import org.kuali.kfs.module.ld.businessobject.LedgerEntry;
030    import org.kuali.kfs.module.ld.businessobject.inquiry.AbstractLaborInquirableImpl;
031    import org.kuali.kfs.module.ld.businessobject.inquiry.PositionDataDetailsInquirableImpl;
032    import org.kuali.kfs.module.ld.service.LaborInquiryOptionsService;
033    import org.kuali.kfs.sys.KFSConstants;
034    import org.kuali.kfs.sys.KFSPropertyConstants;
035    import org.kuali.rice.kns.bo.BusinessObject;
036    import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
037    import org.kuali.rice.kns.lookup.CollectionIncomplete;
038    import org.kuali.rice.kns.lookup.HtmlData;
039    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
040    import org.kuali.rice.kns.util.BeanPropertyComparator;
041    
042    /**
043     * The class is the front-end for all Ledger Entry inquiry processing.
044     * 
045     * @see org.kuali.kfs.module.ld.businessobject.LedgerEntry
046     */
047    public class LedgerEntryLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
048        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(LedgerEntryLookupableHelperServiceImpl.class);
049    
050        private LaborInquiryOptionsService laborInquiryOptionsService;
051    
052        /**
053         * @see org.kuali.rice.kns.lookup.Lookupable#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject, java.lang.String)
054         */
055        @Override
056        public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
057            if (KFSPropertyConstants.DOCUMENT_NUMBER.equals(propertyName)) {
058                if (businessObject instanceof LedgerEntry) {
059                    LedgerEntry entry = (LedgerEntry) businessObject;
060                    return new AnchorHtmlData(new InquirableFinancialDocument().getInquirableDocumentUrl(entry), KFSConstants.EMPTY_STRING, "view ledger entry");
061                }
062            }
063            else if (KFSPropertyConstants.POSITION_NUMBER.equals(propertyName)) {
064                LedgerEntry entry = (LedgerEntry) businessObject;
065                AbstractLaborInquirableImpl positionDataDetailsInquirable = new PositionDataDetailsInquirableImpl();
066    
067                Map<String, String> fieldValues = new HashMap<String, String>();
068                fieldValues.put(propertyName, entry.getPositionNumber());
069    
070                BusinessObject positionData = positionDataDetailsInquirable.getBusinessObject(fieldValues);
071    
072                return positionData == null ? new AnchorHtmlData(KFSConstants.EMPTY_STRING, KFSConstants.EMPTY_STRING) : positionDataDetailsInquirable.getInquiryUrl(positionData, propertyName);
073            }
074            return (new EntryInquirableImpl()).getInquiryUrl(businessObject, propertyName);
075        }
076    
077        /**
078         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getSearchResults(java.util.Map)
079         */
080        @Override
081        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
082            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
083            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
084    
085            // get the pending entry option. This method must be prior to the get search results
086            String pendingEntryOption = laborInquiryOptionsService.getSelectedPendingEntryOption(fieldValues);
087    
088            // get the input balance type code
089            String balanceTypeCode = fieldValues.get(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE);
090            boolean isA21Balance = StringUtils.isNotEmpty(balanceTypeCode) && BALANCE_TYPE_AC_AND_A21.equals(balanceTypeCode.trim());
091    
092            if (isA21Balance) {
093                fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_ACTUAL);
094            }
095    
096            Collection<LedgerEntry> ledgerEntries = getLookupService().findCollectionBySearch(LedgerEntry.class, fieldValues);
097            laborInquiryOptionsService.updateLedgerEntryByPendingLedgerEntry(ledgerEntries, fieldValues, pendingEntryOption);
098    
099            // add the ledger entries into the search results if the searching balance type code is A21
100            if (isA21Balance) {
101                fieldValues.put(KFSPropertyConstants.FINANCIAL_BALANCE_TYPE_CODE, KFSConstants.BALANCE_TYPE_A21);
102                Collection<LedgerEntry> effortLedgerEntries = getLookupService().findCollectionBySearch(LedgerEntry.class, fieldValues);
103                laborInquiryOptionsService.updateLedgerEntryByPendingLedgerEntry(effortLedgerEntries, fieldValues, pendingEntryOption);
104    
105                ledgerEntries.addAll(effortLedgerEntries);
106            }
107    
108            // get the actual size of all qualified search results
109            Long actualSize = new Long(ledgerEntries.size());
110            return this.buildSearchResultList(ledgerEntries, actualSize);
111        }
112    
113        /**
114         * build the serach result list from the given collection and the number of all qualified search results
115         * 
116         * @param searchResultsCollection the given search results, which may be a subset of the qualified search results
117         * @param actualSize the number of all qualified search results
118         * @return the serach result list with the given results and actual size
119         */
120        protected List buildSearchResultList(Collection searchResultsCollection, Long actualSize) {
121            CollectionIncomplete results = new CollectionIncomplete(searchResultsCollection, actualSize);
122    
123            // sort list if default sort column given
124            List searchResults = (List) results;
125            List defaultSortColumns = getDefaultSortColumns();
126            if (defaultSortColumns.size() > 0) {
127                Collections.sort(results, new BeanPropertyComparator(defaultSortColumns, true));
128            }
129            return searchResults;
130        }
131    
132        /**
133         * Sets the laborInquiryOptionsService attribute value.
134         * 
135         * @param laborInquiryOptionsService The laborInquiryOptionsService to set.
136         */
137        public void setLaborInquiryOptionsService(LaborInquiryOptionsService laborInquiryOptionsService) {
138            this.laborInquiryOptionsService = laborInquiryOptionsService;
139        }
140    }