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.cg.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.context.SpringContext;
024    import org.kuali.rice.kim.bo.Person;
025    import org.kuali.rice.kim.service.KIMServiceLocator;
026    import org.kuali.rice.kim.service.PersonService;
027    import org.kuali.rice.kim.service.ResponsibilityService;
028    import org.kuali.rice.kns.bo.BusinessObject;
029    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
030    
031    /**
032     * Allows custom handling of Proposals within the lookup framework.
033     */
034    public class ProposalLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl {
035    
036        private static final String LOOKUP_USER_ID_FIELD = "lookupPerson.principalName";
037        private static final String LOOKUP_UNIVERSAL_USER_ID_FIELD = "proposalProjectDirectors.principalId";
038    
039        private PersonService<Person> personService;
040    
041        /**
042         * @see org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl#getSearchResultsHelper(java.util.Map, boolean)
043         */
044        @Override
045        protected List<? extends BusinessObject> getSearchResultsHelper(Map<String, String> fieldValues, boolean unbounded) {
046            // perform the lookup on the project director object first
047            if (!StringUtils.isBlank(fieldValues.get(LOOKUP_USER_ID_FIELD))) {
048                Person person = getPersonService().getPersonByPrincipalName(fieldValues.get(LOOKUP_USER_ID_FIELD));
049    
050                // if no project directors match, we can return an empty list right now
051                if (person == null) {
052                    return Collections.EMPTY_LIST;
053                }
054                
055                // place the universal ID into the fieldValues map and remove the dummy attribute
056                fieldValues.put(LOOKUP_UNIVERSAL_USER_ID_FIELD, person.getPrincipalId());
057                fieldValues.remove(LOOKUP_USER_ID_FIELD);
058            }
059    
060            return super.getSearchResultsHelper(fieldValues, unbounded);
061        }
062    
063        /**
064         * @return Returns the personService.
065         */
066        protected PersonService<Person> getPersonService() {
067            if(personService==null)
068                personService = SpringContext.getBean(PersonService.class);
069            return personService;
070        }
071    
072    }