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 java.util.Collection;
019    import java.util.Collections;
020    import java.util.HashMap;
021    import java.util.List;
022    import java.util.Map;
023    
024    import org.kuali.kfs.gl.Constant;
025    import org.kuali.kfs.gl.businessobject.inquiry.InquirableFinancialDocument;
026    import org.kuali.kfs.module.ld.businessobject.LaborLedgerPendingEntry;
027    import org.kuali.kfs.module.ld.businessobject.inquiry.AbstractLaborInquirableImpl;
028    import org.kuali.kfs.module.ld.businessobject.inquiry.LedgerPendingEntryInquirableImpl;
029    import org.kuali.kfs.module.ld.businessobject.inquiry.PositionDataDetailsInquirableImpl;
030    import org.kuali.kfs.module.ld.service.LaborInquiryOptionsService;
031    import org.kuali.kfs.module.ld.service.LaborLedgerPendingEntryService;
032    import org.kuali.kfs.sys.KFSConstants;
033    import org.kuali.kfs.sys.KFSPropertyConstants;
034    import org.kuali.rice.kns.bo.BusinessObject;
035    import org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl;
036    import org.kuali.rice.kns.lookup.CollectionIncomplete;
037    import org.kuali.rice.kns.lookup.HtmlData;
038    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
039    import org.kuali.rice.kns.util.BeanPropertyComparator;
040    
041    /**
042     * Helper Service for looking up instances of <code>{@link LaborLedgerPendingEntry}</code>
043     */
044    public class LaborPendingEntryLookupableHelperServiceImpl extends AbstractLookupableHelperServiceImpl {
045        private static org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(LaborPendingEntryLookupableHelperServiceImpl.class);
046    
047        private LaborLedgerPendingEntryService laborLedgerPendingEntryService;
048        private LaborInquiryOptionsService laborInquiryOptionsService;
049    
050        /**
051         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
052         *      java.lang.String)
053         */
054        @Override
055        public HtmlData getInquiryUrl(BusinessObject businessObject, String propertyName) {
056            if (KFSPropertyConstants.DOCUMENT_NUMBER.equals(propertyName) && businessObject instanceof LaborLedgerPendingEntry) {
057                LaborLedgerPendingEntry pendingEntry = (LaborLedgerPendingEntry) businessObject;
058                return new AnchorHtmlData(new InquirableFinancialDocument().getInquirableDocumentUrl(pendingEntry), KFSConstants.EMPTY_STRING);
059            }
060            else if (KFSPropertyConstants.POSITION_NUMBER.equals(propertyName)) {
061                LaborLedgerPendingEntry pendingEntry = (LaborLedgerPendingEntry) businessObject;
062                AbstractLaborInquirableImpl positionDataDetailsInquirable = new PositionDataDetailsInquirableImpl();
063    
064                Map<String, String> fieldValues = new HashMap<String, String>();
065                fieldValues.put(propertyName, pendingEntry.getPositionNumber());
066    
067                BusinessObject positionData = positionDataDetailsInquirable.getBusinessObject(fieldValues);
068    
069                return positionData == null ? new AnchorHtmlData(KFSConstants.EMPTY_STRING, KFSConstants.EMPTY_STRING) : positionDataDetailsInquirable.getInquiryUrl(positionData, propertyName);
070            }
071            return (new LedgerPendingEntryInquirableImpl()).getInquiryUrl(businessObject, propertyName);
072        }
073    
074        /**
075         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getSearchResults(java.util.Map)
076         */
077        @Override
078        public List getSearchResults(Map fieldValues) {
079            setBackLocation((String) fieldValues.get(KFSConstants.BACK_LOCATION));
080            setDocFormKey((String) fieldValues.get(KFSConstants.DOC_FORM_KEY));
081    
082            // determine if only approved pending entries need to be returned
083            String pendingEntryOption = laborInquiryOptionsService.getSelectedPendingEntryOption(fieldValues);
084            boolean isApprovedPendingSelected = Constant.APPROVED_PENDING_ENTRY.equals(pendingEntryOption) ? true : false;
085    
086            Collection<LaborLedgerPendingEntry> searchResults = laborLedgerPendingEntryService.findPendingEntries(fieldValues, isApprovedPendingSelected);
087            Long resultSize = searchResults == null ? 0 : new Long(searchResults.size());
088    
089            return this.buildSearchResultList(searchResults, resultSize);
090        }
091    
092        /**
093         * build the serach result list from the given collection and the number of all qualified search results
094         * 
095         * @param searchResultsCollection the given search results, which may be a subset of the qualified search results
096         * @param actualSize the number of all qualified search results
097         * @return the serach result list with the given results and actual size
098         */
099        protected List buildSearchResultList(Collection searchResultsCollection, Long actualSize) {
100            CollectionIncomplete results = new CollectionIncomplete(searchResultsCollection, actualSize);
101    
102            // sort list if default sort column given
103            List searchResults = (List) results;
104            List defaultSortColumns = getDefaultSortColumns();
105            if (defaultSortColumns.size() > 0) {
106                Collections.sort(results, new BeanPropertyComparator(defaultSortColumns, true));
107            }
108            return searchResults;
109        }
110    
111        /**
112         * Sets the laborLedgerPendingEntryService attribute value.
113         * 
114         * @param laborLedgerPendingEntryService The laborLedgerPendingEntryService to set.
115         */
116        public void setLaborLedgerPendingEntryService(LaborLedgerPendingEntryService laborLedgerPendingEntryService) {
117            this.laborLedgerPendingEntryService = laborLedgerPendingEntryService;
118        }
119    
120        /**
121         * Sets the laborInquiryOptionsService attribute value.
122         * 
123         * @param laborInquiryOptionsService The laborInquiryOptionsService to set.
124         */
125        public void setLaborInquiryOptionsService(LaborInquiryOptionsService laborInquiryOptionsService) {
126            this.laborInquiryOptionsService = laborInquiryOptionsService;
127        }
128    }