View Javadoc

1   /**
2    * Copyright 2005-2013 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.kim.impl.role.RoleBo;
28  import org.kuali.rice.kim.impl.role.RoleDao;
29  import org.kuali.rice.kim.impl.services.KimImplServiceLocator;
30  import org.kuali.rice.krad.lookup.LookupableImpl;
31  import org.kuali.rice.krad.util.KRADConstants;
32  import org.kuali.rice.krad.web.form.LookupForm;
33  
34  import java.sql.Timestamp;
35  import java.util.ArrayList;
36  import java.util.Calendar;
37  import java.util.HashMap;
38  import java.util.List;
39  import java.util.Map;
40  
41  import static org.kuali.rice.core.api.criteria.PredicateFactory.*;
42  import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
43  import static org.kuali.rice.core.api.criteria.PredicateFactory.lessThan;
44  
45  /**
46   * Custom lookupable for the {@link GroupBo} lookup to call the group service for searching
47   *
48   * @author Kuali Rice Team (rice.collab@kuali.org)
49   */
50  public class GroupLookupableImpl extends LookupableImpl {
51      private static final long serialVersionUID = -3149952849854425077L;
52  
53      /**
54       * Translates any search criteria on principal name to member id and active data, then calls group service to
55       * retrieve matching groups
56       *
57       * @return List<GroupBo>
58       */
59      @Override
60      protected List<?> getSearchResults(LookupForm form, Map<String, String> searchCriteria, boolean unbounded) {
61          Map<String, String> criteriaMap = new HashMap<String, String>(searchCriteria);
62          QueryByCriteria.Builder criteria = QueryByCriteria.Builder.create();
63  
64          if (!criteriaMap.isEmpty()) {
65              List<Predicate> predicates = new ArrayList<Predicate>();
66              //principalId doesn't exist on 'Group'.  Lets do this predicate conversion separately
67              if (StringUtils.isNotBlank(criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME))) {
68                  String principalId = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(
69                          criteriaMap.get(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME)).getPrincipalId();
70                  Timestamp currentTime = new Timestamp(Calendar.getInstance().getTimeInMillis());
71                  predicates.add(and(equal("members.memberId", principalId), equal("members.typeCode",
72                          KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode()), and(or(isNull(
73                          "members.activeFromDateValue"), greaterThanOrEqual("members.activeFromDateValue", currentTime)),
74                          or(isNull("members.activeToDateValue"), lessThan("members.activeToDateValue", currentTime)))));
75  
76              }
77              criteriaMap.remove(KimConstants.UniqueKeyConstants.PRINCIPAL_NAME);
78  
79              predicates.add(PredicateUtils.convertMapToPredicate(criteriaMap));
80              criteria.setPredicates(and(predicates.toArray(new Predicate[predicates.size()])));
81          }
82  
83          GroupQueryResults groupResults = getGroupService().findGroups(criteria.build());
84          List<Group> groups = groupResults.getResults();
85  
86          //have to convert back to Bos :(
87          List<GroupBo> groupBos = new ArrayList<GroupBo>(groups.size());
88          for (Group group : groups) {
89              groupBos.add(GroupBo.from(group));
90          }
91  
92          return groupBos;
93      }
94  
95      public GroupService getGroupService() {
96          return KimApiServiceLocator.getGroupService();
97      }
98  }