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.bc.businessobject.lookup;
017    
018    import java.util.Collections;
019    import java.util.List;
020    import java.util.Map;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.kuali.kfs.sys.KFSConstants;
024    import org.kuali.rice.kns.bo.BusinessObject;
025    import org.kuali.rice.kns.lookup.HtmlData;
026    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
027    import org.kuali.rice.kns.lookup.HtmlData.AnchorHtmlData;
028    import org.kuali.rice.kns.util.BeanPropertyComparator;
029    import org.kuali.rice.kns.util.KNSConstants;
030    
031    /**
032     * Base lookupable helper service for budget selection lookups.
033     */
034    public class SelectLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
035    
036        /**
037         * Super impl clears out hidden values but we need to keep principalId hidden field in the criteria. 
038         * Overridding here so that the call to clear hiddens is not executed.
039         * 
040         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getSearchResults(java.util.Map)
041         */
042        @Override
043        public List<? extends BusinessObject> getSearchResults(Map<String, String> fieldValues) {
044            setBackLocation(fieldValues.get(KFSConstants.BACK_LOCATION));
045            setDocFormKey(fieldValues.get(KFSConstants.DOC_FORM_KEY));
046            setReferencesToRefresh(fieldValues.get(KFSConstants.REFERENCES_TO_REFRESH));
047    
048            List searchResults = (List) getLookupService().findCollectionBySearchHelper(getBusinessObjectClass(), fieldValues, false);
049    
050            // sort list if default sort column given
051            List defaultSortColumns = getDefaultSortColumns();
052            if (defaultSortColumns.size() > 0) {
053                Collections.sort(searchResults, new BeanPropertyComparator(getDefaultSortColumns(), true));
054            }
055    
056            return searchResults;
057        }
058    
059        
060        /**
061         * Since this lookupable is called by the budget lookup action, the context will be KFS, not Rice. So the generated inquiries
062         * will not have the Rice context (kr/) and be invalid. This override adds the Rice context to the inquiry Url to working
063         * around the issue.
064         * 
065         * @see org.kuali.rice.kns.lookup.AbstractLookupableHelperServiceImpl#getInquiryUrl(org.kuali.rice.kns.bo.BusinessObject,
066         *      java.lang.String)
067         */
068        @Override
069        public HtmlData getInquiryUrl(BusinessObject bo, String propertyName) {
070            AnchorHtmlData inquiryHref = (AnchorHtmlData)super.getInquiryUrl(bo, propertyName);
071            inquiryHref.setHref(StringUtils.replace(inquiryHref.getHref(), KNSConstants.INQUIRY_ACTION, KFSConstants.INQUIRY_ACTION));
072    
073            return inquiryHref;
074        }
075    }