Coverage Report - org.kuali.rice.kim.impl.group.GroupServiceBase
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupServiceBase
79%
72/91
58%
41/70
4.357
 
 1  
 /*
 2  
  * Copyright 2006-2011 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  
 
 17  
 package org.kuali.rice.kim.impl.group;
 18  
 
 19  
 
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 22  
 import org.kuali.rice.kim.api.group.Group;
 23  
 import org.kuali.rice.kim.api.group.GroupMember;
 24  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 25  
 import org.kuali.rice.kim.util.KimConstants;
 26  
 import org.kuali.rice.kns.service.BusinessObjectService;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Collection;
 30  
 import java.util.Collections;
 31  
 import java.util.HashMap;
 32  
 import java.util.HashSet;
 33  
 import java.util.List;
 34  
 import java.util.Map;
 35  
 import java.util.Set;
 36  
 
 37  20
 public abstract class GroupServiceBase {
 38  
     protected BusinessObjectService businessObjectService;
 39  
 
 40  
     //@Override
 41  
     public Group getGroup(String groupId) {
 42  14
                 return GroupBo.to(getGroupBo(groupId));
 43  
     }
 44  
 
 45  
     protected GroupBo getGroupBo(String groupId) {
 46  18
         if ( StringUtils.isEmpty(groupId) ) {
 47  0
                          throw new RiceIllegalArgumentException("groupId is blank");
 48  
                 }
 49  18
         return (GroupBo)businessObjectService.findBySinglePrimaryKey(GroupBo.class, groupId);
 50  
 
 51  
     }
 52  
 
 53  
     //@Override
 54  
         public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) {
 55  0
         if ( StringUtils.isEmpty(groupId) || StringUtils.isEmpty(groupMemberId) ) {
 56  0
                          throw new RiceIllegalArgumentException("groupMemberId or groupId is blank");
 57  
                 }
 58  
 
 59  0
         Set<String> visitedGroupIds = new HashSet<String>();
 60  0
                 return isMemberOfGroupInternal(groupMemberId, groupId, visitedGroupIds, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE);
 61  
         }
 62  
 
 63  
     //@Override
 64  
     public boolean isMemberOfGroup(String principalId, String groupId) {
 65  2
         if ( principalId == null || groupId == null ) {
 66  0
                         return false;
 67  
                 }
 68  2
                 Set<String> visitedGroupIds = new HashSet<String>();
 69  2
                 return isMemberOfGroupInternal(principalId, groupId, visitedGroupIds, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
 70  
     }
 71  
 
 72  
     /**
 73  
      * @see org.kuali.rice.kim.api.group.GroupService#getGroups(java.util.Collection)
 74  
      */
 75  
     //@Override
 76  
     public Map<String, Group> getGroups(Collection<String> groupIds) {
 77  
         //todo: This is a prime candidate for the new criteria builder
 78  
         //CriteriaBuilder<GroupBo> criteriaBuilder = CriteriaBuilder.newCriteriaBuilder(GroupBo.class);
 79  
         //criteriaBuilder.in("id", new ArrayList(groupIds));
 80  
         //QueryByCriteria.Builder<GroupBo> qbcBuilder = QueryByCriteria.Builder.create(GroupBo.class);
 81  
         //qbcBuilder.set
 82  
 
 83  12
         Map<String, Group> result = new HashMap<String, Group>();
 84  
 
 85  
         //todo: hopefully there is an efficient orm way to do this
 86  12
         for (String s : groupIds) {
 87  7
             Group group = getGroup(s);
 88  7
             if (group != null) {
 89  7
                 result.put(s, group);
 90  
             }
 91  7
         }
 92  
 
 93  12
         return result;
 94  
     }
 95  
 
 96  
     //@Override
 97  
     public Group getGroupByName(String namespaceCode, String groupName) {
 98  4
         if ( namespaceCode == null || groupName == null ) {
 99  0
                         return null;
 100  
                 }
 101  4
                 Map<String,String> criteria = new HashMap<String,String>();
 102  4
                 criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode);
 103  4
                 criteria.put(KimConstants.UniqueKeyConstants.GROUP_NAME, groupName);
 104  4
                 Collection<GroupBo> groups = businessObjectService.findMatching(GroupBo.class, criteria);
 105  4
                 if ( groups.size() > 0 ) {
 106  3
                         return GroupBo.to(groups.iterator().next());
 107  
                 }
 108  1
                 return null;
 109  
     }
 110  
 
 111  
     public boolean isMemberOfGroupInternal(String memberId, String groupId, Set<String> visitedGroupIds, String memberType) {
 112  3
                 if ( memberId == null || groupId == null ) {
 113  0
                         return false;
 114  
                 }
 115  
 
 116  
                 // when group traversal is not needed
 117  3
                 Group group = getGroup(groupId);
 118  3
                 if ( group == null || !group.isActive() ) {
 119  0
                         return false;
 120  
                 }
 121  
 
 122  3
         List<GroupMember> members = getMembersOfGroup(group.getId());
 123  
                 // check the immediate group
 124  3
                 for (String groupMemberId : getMemberIdsByType(members, memberType)) {
 125  3
                         if (groupMemberId.equals(memberId)) {
 126  1
                                 return true;
 127  
                         }
 128  
                 }
 129  
 
 130  
                 // check each contained group, returning as soon as a match is found
 131  2
                 for ( String memberGroupId : getMemberIdsByType(members, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE) ) {
 132  1
                         if (!visitedGroupIds.contains(memberGroupId)){
 133  1
                                 visitedGroupIds.add(memberGroupId);
 134  1
                                 if ( isMemberOfGroupInternal( memberId, memberGroupId, visitedGroupIds, memberType ) ) {
 135  0
                                         return true;
 136  
                                 }
 137  
                         }
 138  
                 }
 139  
 
 140  
                 // no match found, return false
 141  2
                 return false;
 142  
         }
 143  
 
 144  
     protected void getParentGroupsInternal( String groupId, Set<Group> groups ) {
 145  6
                 Map<String,Group> parentGroups = getDirectParentGroups( groupId );
 146  6
                 for ( Group group : parentGroups.values() ) {
 147  1
                         if ( !groups.contains( group ) ) {
 148  1
                                 groups.add( group );
 149  1
                                 getParentGroupsInternal( group.getId(), groups );
 150  
                         }
 151  
                 }
 152  6
         }
 153  
 
 154  
     protected Map<String,Group> getDirectParentGroups(String groupId) {
 155  7
                 if ( groupId == null ) {
 156  0
                         return Collections.emptyMap();
 157  
                 }
 158  7
                 Map<String,String> criteria = new HashMap<String,String>();
 159  7
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, groupId);
 160  7
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE);
 161  
 
 162  7
                 List<GroupMemberBo> groupMembers = (List<GroupMemberBo>)businessObjectService.findMatching(GroupMemberBo.class, criteria);
 163  7
                 Set<String> matchingGroupIds = new HashSet<String>();
 164  
                 // filter to active groups
 165  7
                 for ( GroupMemberBo gm : groupMembers ) {
 166  2
                         if ( gm.isActive() ) {
 167  2
                                 matchingGroupIds.add(gm.getGroupId());
 168  
                         }
 169  
                 }
 170  7
                 return getGroups(matchingGroupIds);
 171  
         }
 172  
 
 173  
     public List<GroupMember> getMembersOfGroup(String groupId) {
 174  7
         if (groupId == null) {
 175  0
             throw new RiceIllegalArgumentException("groupId is blank");
 176  
                 }
 177  7
         Map<String,String> criteria = new HashMap<String,String>();
 178  7
                 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
 179  
 
 180  7
                 Collection<GroupMemberBo> groupMembersBos = businessObjectService.findMatching(GroupMemberBo.class, criteria);
 181  7
         List<GroupMember> groupMembers = new ArrayList<GroupMember>();
 182  7
         for (GroupMemberBo groupBo : groupMembersBos) {
 183  16
             if (groupBo.isActive()){
 184  16
                 groupMembers.add(GroupMemberBo.to(groupBo));
 185  
             }
 186  
         }
 187  7
         return groupMembers;
 188  
     }
 189  
 
 190  
     /**
 191  
      * Sets the businessObjectService attribute value.
 192  
      *
 193  
      * @param businessObjectService The businessObjectService to set.
 194  
      */
 195  
     public void setBusinessObjectService(final BusinessObjectService businessObjectService) {
 196  18
         this.businessObjectService = businessObjectService;
 197  18
     }
 198  
 
 199  
     protected List<Group> toGroupList(List<GroupBo> groupBos) {
 200  2
         if (groupBos == null) {
 201  0
             return null;
 202  
         }
 203  2
         List<Group> groups = new ArrayList<Group>();
 204  2
         for (GroupBo bo : groupBos) {
 205  6
             groups.add(GroupBo.to(bo));
 206  
         }
 207  2
         return groups;
 208  
     }
 209  
 
 210  
     /*protected List<String> getMemberIdsByType(Group group, String memberType) {
 211  
         List<String> principalIds = new ArrayList<String>();
 212  
         if (group != null) {
 213  
             for (GroupMember member : getMembersOfGroup(group.getId())) {
 214  
                 if (member.getTypeCode().equals(memberType)) {
 215  
                     principalIds.add(member.getMemberId());
 216  
                 }
 217  
             }
 218  
         }
 219  
         return principalIds;
 220  
     }*/
 221  
 
 222  
     protected List<GroupMember> getMembersByType(Collection<GroupMember> members, String memberType) {
 223  0
         List<GroupMember> membersByType = new ArrayList<GroupMember>();
 224  0
         if (members != null) {
 225  0
             for (GroupMember member : members) {
 226  0
                 if (member.getTypeCode().equals(memberType)) {
 227  0
                     membersByType.add(member);
 228  
                 }
 229  
             }
 230  
         }
 231  0
         return membersByType;
 232  
     }
 233  
 
 234  
     protected List<String> getMemberIdsByType(Collection<GroupMember> members, String memberType) {
 235  7
         List<String> membersIds = new ArrayList<String>();
 236  7
         if (members != null) {
 237  7
             for (GroupMember member : members) {
 238  13
                 if (member.getTypeCode().equals(memberType)) {
 239  7
                     membersIds.add(member.getMemberId());
 240  
                 }
 241  
             }
 242  
         }
 243  7
         return membersIds;
 244  
     }
 245  
 }