001    /**
002     * Copyright 2005-2012 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.rice.kim.impl.group;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.criteria.Predicate;
020    import org.kuali.rice.core.api.criteria.PredicateUtils;
021    import org.kuali.rice.core.api.criteria.QueryByCriteria;
022    import org.kuali.rice.kim.api.KimConstants;
023    import org.kuali.rice.kim.api.group.Group;
024    import org.kuali.rice.kim.api.group.GroupQueryResults;
025    import org.kuali.rice.kim.api.group.GroupService;
026    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027    import org.kuali.rice.kim.impl.role.RoleBo;
028    import org.kuali.rice.kim.impl.role.RoleDao;
029    import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
030    import org.kuali.rice.krad.lookup.LookupableImpl;
031    import org.kuali.rice.krad.util.KRADConstants;
032    import org.kuali.rice.krad.web.form.LookupForm;
033    
034    import java.sql.Timestamp;
035    import java.util.ArrayList;
036    import java.util.Calendar;
037    import java.util.HashMap;
038    import java.util.List;
039    import java.util.Map;
040    
041    import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
042    import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
043    import static org.kuali.rice.core.api.criteria.PredicateFactory.lessThan;
044    
045    /**
046     * Custom lookupable for the {@link GroupBo} lookup to call the group service for searching
047     *
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class GroupLookupableImpl extends LookupableImpl {
051        private static final long serialVersionUID = -3149952849854425077L;
052    
053        /**
054         * Translates any search criteria on principal name to member id and active data, then calls group service to
055         * retrieve matching groups
056         *
057         * @return List<GroupBo>
058         */
059        @Override
060        protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
061            Map<String, String> criteriaMap = new HashMap<String, String>(searchCriteria);
062            QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
063    
064            if (!criteriaMap.isEmpty()) {
065                List<Predicate> predicates = new ArrayList<Predicate>();
066                //principalId doesn't exist on 'Group'.  Lets do this predicate conversion separately
067                if (StringUtils.isNotBlank(criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME))) {
068                    String principalId = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(
069                            criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME)).getPrincipalId();
070                    Timestamp currentTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
071                    predicates.add(and(equal("members.memberId", principalId), equal("members.typeCode",
072                            KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode()), and(or(isNull(
073                            "members.activeFromDateValue"), greaterThanOrEqual("members.activeFromDateValue", currentTime)),
074                            or(isNull("members.activeToDateValue"), lessThan("members.activeToDateValue", currentTime)))));
075    
076                }
077                criteriaMap.remove(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME);
078    
079                predicates.add(PredicateUtils.convertMapToPredicate(criteriaMap));
080                criteria.setPredicates(and(predicates.toArray(new Predicate[predicates.size()])));
081            }
082    
083            GroupQueryResults groupResults = getGroupService().findGroups(criteria.build());
084            List<Group> groups = groupResults.getResults();
085    
086            //have to convert back to Bos :(
087            List<GroupBo> groupBos = new ArrayList<GroupBo>(groups.size());
088            for (Group group : groups) {
089                groupBos.add(GroupBo.from(group));
090            }
091    
092            return groupBos;
093        }
094    
095        public GroupService getGroupService() {
096            return KimApiServiceLocator.getGroupService();
097        }
098    }