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