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.endow.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.module.endow.EndowPropertyConstants;
024    import org.kuali.kfs.sys.context.SpringContext;
025    import org.kuali.rice.kim.bo.Person;
026    import org.kuali.rice.kim.bo.group.dto.GroupInfo;
027    import org.kuali.rice.kim.service.GroupService;
028    import org.kuali.rice.kim.service.KIMServiceLocator;
029    import org.kuali.rice.kim.service.PersonService;
030    import org.kuali.rice.kim.service.ResponsibilityService;
031    import org.kuali.rice.kns.bo.BusinessObject;
032    import org.kuali.rice.kns.lookup.KualiLookupableHelperServiceImpl;
033    
034    /**
035     * Allows custom handling of Proposals within the lookup framework.
036     */
037    public class TicklerLookupableHelperServiceImpl extends KualiLookupableHelperServiceImpl 
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        {
047            // Perform the lookup on the project director object first
048            if (!StringUtils.isBlank(fieldValues.get(EndowPropertyConstants.TICKLER_LOOKUP_USER_ID_FIELD))) {
049                Person person = getPersonService().getPersonByPrincipalName(fieldValues.get(EndowPropertyConstants.TICKLER_LOOKUP_USER_ID_FIELD));
050    
051                // if no project directors match, we can return an empty list right now
052                if (person == null) {
053                    return Collections.EMPTY_LIST;
054                }
055                
056                // place the universal ID into the fieldValues map and remove the dummy attribute
057                fieldValues.put(EndowPropertyConstants.TICKLER_LOOKUP_UNIVERSAL_USER_ID_FIELD, person.getPrincipalId());
058                fieldValues.remove(EndowPropertyConstants.TICKLER_LOOKUP_USER_ID_FIELD);
059            }
060    
061            // Perform the lookup on the Group 
062            if (!StringUtils.isBlank(fieldValues.get(EndowPropertyConstants.TICKLER_RECIPIENT_GROUPID))) 
063            {
064                // Place the Group ID into the fieldValues map and remove the dummy attribute
065                fieldValues.put(EndowPropertyConstants.TICKLER_LOOKUP_GROUP_NAME_FIELD, fieldValues.get(EndowPropertyConstants.TICKLER_RECIPIENT_GROUPID));
066                fieldValues.remove(EndowPropertyConstants.TICKLER_LOOKUP_GROUP_NAME_FIELD);
067                fieldValues.remove(EndowPropertyConstants.TICKLER_RECIPIENT_GROUPID);
068                
069            }
070            
071            return super.getSearchResultsHelper(fieldValues, unbounded);
072        }
073    
074        /**
075         * @return Returns the personService.
076         */
077        protected PersonService<Person> getPersonService() {
078            if(personService==null)
079                personService = SpringContext.getBean(PersonService.class);
080            return personService;
081        }
082    
083        private GroupService getGroupService()
084        {
085            return KIMServiceLocator.getGroupService();
086        }
087    }