View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.impl.group;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.Predicate;
20  import org.kuali.rice.core.api.criteria.PredicateUtils;
21  import org.kuali.rice.core.api.criteria.QueryByCriteria;
22  import org.kuali.rice.kim.api.KimConstants;
23  import org.kuali.rice.kim.api.group.Group;
24  import org.kuali.rice.kim.api.group.GroupQueryResults;
25  import org.kuali.rice.kim.api.group.GroupService;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.krad.lookup.LookupableImpl;
28  import org.kuali.rice.krad.lookup.LookupForm;
29  
30  import java.sql.Timestamp;
31  import java.util.ArrayList;
32  import java.util.Calendar;
33  import java.util.Collection;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  
38  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
39  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
40  import static org.kuali.rice.core.api.criteria.PredicateFactory.lessThan;
41  
42  /**
43   * Custom lookupable for the {@link GroupBo} lookup to call the group service for searching
44   *
45   * @author Kuali Rice Team (rice.collab@kuali.org)
46   */
47  public class GroupLookupableImpl extends LookupableImpl {
48      private static final long serialVersionUID = -3149952849854425077L;
49  
50      /**
51       * Translates any search criteria on principal name to member id and active data, then calls group service to
52       * retrieve matching groups
53       *
54       * @return List<GroupBo>
55       */
56      @Override
57      protected Collection<?> executeSearch(Map<String, String> adjustedSearchCriteria,
58                  List<String> wildcardAsLiteralSearchCriteria, boolean bounded, Integer searchResultsLimit) {
59          Map<String, String> criteriaMap = new HashMap<String, String>(adjustedSearchCriteria);
60          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
61  
62          if (!criteriaMap.isEmpty()) {
63              List<Predicate> predicates = new ArrayList<Predicate>();
64              //principalId doesn't exist on 'Group'.  Lets do this predicate conversion separately
65              if (StringUtils.isNotBlank(criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME))) {
66                  String principalId = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(
67                          criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME)).getPrincipalId();
68                  Timestamp currentTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
69                  predicates.add(and(equal("members.memberId", principalId), equal("members.typeCode",
70                          KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode()), and(or(isNull(
71                          "members.activeFromDateValue"), greaterThanOrEqual("members.activeFromDateValue", currentTime)),
72                          or(isNull("members.activeToDateValue"), lessThan("members.activeToDateValue", currentTime)))));
73  
74              }
75              criteriaMap.remove(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME);
76  
77              predicates.add(PredicateUtils.convertMapToPredicate(criteriaMap));
78              criteria.setPredicates(and(predicates.toArray(new Predicate[predicates.size()])));
79          }
80  
81          GroupQueryResults groupResults = getGroupService().findGroups(criteria.build());
82          List<Group> groups = groupResults.getResults();
83  
84          //have to convert back to Bos :(
85          List<GroupBo> groupBos = new ArrayList<GroupBo>(groups.size());
86          for (Group group : groups) {
87              groupBos.add(GroupBo.from(group));
88          }
89  
90          return groupBos;
91      }
92  
93      public GroupService getGroupService() {
94          return KimApiServiceLocator.getGroupService();
95      }
96  }