Coverage Report - org.kuali.rice.kim.impl.group.GroupServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupServiceImpl
81%
120/148
60%
59/98
4.476
 
 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  
 import org.apache.commons.collections.CollectionUtils;
 20  
 import org.apache.commons.lang.StringUtils;
 21  
 import org.kuali.rice.core.api.criteria.QueryByCriteria;
 22  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 23  
 import org.kuali.rice.core.api.mo.common.Attributes;
 24  
 import org.kuali.rice.kim.api.group.Group;
 25  
 import org.kuali.rice.kim.api.group.GroupMember;
 26  
 import org.kuali.rice.kim.api.group.GroupQueryResults;
 27  
 import org.kuali.rice.kim.api.group.GroupService;
 28  
 import org.kuali.rice.kim.util.KIMPropertyConstants;
 29  
 import org.kuali.rice.kim.util.KimConstants;
 30  
 
 31  
 import java.util.ArrayList;
 32  
 import java.util.Collection;
 33  
 import java.util.Collections;
 34  
 import java.util.HashMap;
 35  
 import java.util.HashSet;
 36  
 import java.util.List;
 37  
 import java.util.Map;
 38  
 import java.util.Set;
 39  
 
 40  
 
 41  20
 public class GroupServiceImpl extends GroupServiceBase implements GroupService {
 42  
 
 43  
     @Override
 44  
     public List<Group> getGroupsForPrincipal(String principalId) throws RiceIllegalArgumentException {
 45  1
         if ( StringUtils.isEmpty(principalId) ) {
 46  0
                          throw new RiceIllegalArgumentException("principalId is blank");
 47  
                 }
 48  1
         return getGroupsForPrincipalByNamespace( principalId, null );
 49  
     }
 50  
 
 51  
     @Override
 52  
     public List<Group> getGroupsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException {
 53  4
         if ( StringUtils.isEmpty(principalId) ) {
 54  0
                          throw new RiceIllegalArgumentException("principalId is blank");
 55  
                 }
 56  4
         Collection<Group> directGroups = getDirectGroupsForPrincipal( principalId, namespaceCode );
 57  4
                 Set<Group> groups = new HashSet<Group>();
 58  4
         groups.addAll(directGroups);
 59  4
                 for ( Group group : directGroups ) {
 60  4
                         groups.add( group );
 61  4
                         groups.addAll( getParentGroups( group.getId() ) );
 62  
                 }
 63  4
                 return new ArrayList<Group>( groups );
 64  
     }
 65  
 
 66  
     @Override
 67  
     public List<String> findGroupIds(final QueryByCriteria queryByCriteria) {
 68  1
         GroupQueryResults results = this.findGroups(queryByCriteria);
 69  1
         List<String> result = new ArrayList<String>();
 70  
 
 71  1
         for (Group group : results.getResults()) {
 72  3
             result.add(group.getId());
 73  
         }
 74  
 
 75  1
         return result;
 76  
     }
 77  
 
 78  
     /*@Override
 79  
     public List<Group> lookupGroups(Map<String, String> searchCriteria) {
 80  
         List<GroupBo> groupBos = groupDao.getGroups(searchCriteria);
 81  
         List<Group> groups = toGroupList(groupBos);
 82  
         if (groups == null) {
 83  
             return Collections.emptyList();
 84  
         }
 85  
         return groups;
 86  
     }*/
 87  
 
 88  
     @Override
 89  
     public boolean isDirectMemberOfGroup(String principalId, String groupId) throws RiceIllegalArgumentException {
 90  2
         if ( StringUtils.isEmpty(groupId) ) {
 91  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 92  
                 }
 93  2
         if ( StringUtils.isEmpty(principalId) ) {
 94  0
                         throw new RiceIllegalArgumentException("principalId is blank");
 95  
                 }
 96  2
                 Map<String,String> criteria = new HashMap<String,String>();
 97  2
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
 98  2
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
 99  2
                 criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId);
 100  
 
 101  2
                 Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria);
 102  2
                 for ( GroupMemberBo gm : groupMembers ) {
 103  1
                         if ( gm.isActive() ) {
 104  1
                                 return true;
 105  
                         }
 106  
                 }
 107  1
                 return false;
 108  
     }
 109  
 
 110  
     @Override
 111  
     public List<String> getGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException {
 112  1
         if ( StringUtils.isEmpty(principalId) ) {
 113  0
                         throw new RiceIllegalArgumentException("principalId is blank");
 114  
                 }
 115  1
         return getGroupIdsForPrincipalByNamespace(principalId, null);
 116  
     }
 117  
 
 118  
     @Override
 119  
     public List<String> getGroupIdsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException {
 120  2
         if ( StringUtils.isEmpty(principalId) ) {
 121  0
                          throw new RiceIllegalArgumentException("principalId is blank");
 122  
                 }
 123  2
         List<String> result = new ArrayList<String>();
 124  
 
 125  2
         if (principalId != null) {
 126  2
             List<Group> groupList = getGroupsForPrincipalByNamespace(principalId, namespaceCode);
 127  
 
 128  2
             for (Group group : groupList) {
 129  2
                 result.add(group.getId());
 130  
             }
 131  
         }
 132  
 
 133  2
         return result;
 134  
     }
 135  
 
 136  
     @Override
 137  
     public List<String> getDirectGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException {
 138  1
         if ( StringUtils.isEmpty(principalId) ) {
 139  0
                         throw new RiceIllegalArgumentException("principalId is blank");
 140  
                 }
 141  1
         List<String> result = new ArrayList<String>();
 142  
 
 143  1
         if (principalId != null) {
 144  1
                 Collection<Group> groupList = getDirectGroupsForPrincipal(principalId);
 145  
 
 146  1
             for (Group g : groupList) {
 147  1
                 result.add(g.getId());
 148  
             }
 149  
         }
 150  
 
 151  1
         return result;
 152  
     }
 153  
 
 154  
     @Override
 155  
     public List<String> getMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException {
 156  1
         if ( StringUtils.isEmpty(groupId) ) {
 157  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 158  
                 }
 159  1
                 Set<String> visitedGroupIds = new HashSet<String>();
 160  1
                 return getMemberPrincipalIdsInternal(groupId, visitedGroupIds);
 161  
     }
 162  
 
 163  
     @Override
 164  
     public List<String> getDirectMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException {
 165  1
         if ( StringUtils.isEmpty(groupId) ) {
 166  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 167  
                 }
 168  
         //Group group = getGroup(groupId);
 169  1
         return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
 170  
     }
 171  
 
 172  
     @Override
 173  
     public List<String> getMemberGroupIds(String groupId) throws RiceIllegalArgumentException {
 174  
         /*if ( StringUtils.isEmpty(groupId) ) {
 175  
                         throw new RiceIllegalArgumentException("groupId is blank");
 176  
                 }
 177  
                 Set<String> visitedGroupIds = new HashSet<String>();
 178  
                 return getMemberIdsInternalByType(groupId, visitedGroupIds, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE);
 179  
 */
 180  
 
 181  1
         if ( StringUtils.isEmpty(groupId) ) {
 182  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 183  
                 }
 184  1
                 List<GroupBo> groups = getMemberGroupBos( groupId );
 185  1
                 ArrayList<String> groupIds = new ArrayList<String>( groups.size() );
 186  1
                 for ( GroupBo group : groups ) {
 187  1
                         if ( group.isActive() ) {
 188  1
                                 groupIds.add( group.getId() );
 189  
                         }
 190  
                 }
 191  1
                 return groupIds;
 192  
     }
 193  
 
 194  
 
 195  
         protected List<GroupBo> getMemberGroupBos(String groupId) {
 196  1
                 if ( groupId == null ) {
 197  0
                         return Collections.emptyList();
 198  
                 }
 199  1
                 Set<GroupBo> groups = new HashSet<GroupBo>();
 200  
 
 201  1
                 GroupBo group = getGroupBo(groupId);
 202  1
                 getMemberGroupsInternal(group, groups);
 203  
 
 204  1
                 return new ArrayList<GroupBo>(groups);
 205  
         }
 206  
 
 207  
     protected void getMemberGroupsInternal( GroupBo group, Set<GroupBo> groups ) {
 208  2
                 if ( group == null ) {
 209  0
                         return;
 210  
                 }
 211  2
                 List<String> groupIds = group.getMemberGroupIds();
 212  
 
 213  2
                 for (String id : groupIds) {
 214  1
                         GroupBo memberGroup = getGroupBo(id);
 215  
                         // if we've already seen that group, don't recurse into it
 216  1
                         if ( memberGroup.isActive() && !groups.contains( memberGroup ) ) {
 217  1
                                 groups.add(memberGroup);
 218  1
                                 getMemberGroupsInternal(memberGroup,groups);
 219  
                         }
 220  1
                 }
 221  
 
 222  2
         }
 223  
 
 224  
     @Override
 225  
     public List<String> getDirectMemberGroupIds(String groupId) {
 226  1
         if ( groupId == null ) {
 227  0
                         return Collections.emptyList();
 228  
                 }
 229  
         //Group group = getGroup(groupId);
 230  1
         return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE);
 231  
     }
 232  
 
 233  
     @Override
 234  
     public List<String> getParentGroupIds(String groupId) throws RiceIllegalArgumentException {
 235  1
         if ( StringUtils.isEmpty(groupId) ) {
 236  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 237  
                 }
 238  1
         List<String> result = new ArrayList<String>();
 239  1
         if (groupId != null) {
 240  1
             List<Group> groupList = getParentGroups(groupId);
 241  
 
 242  1
             for (Group group : groupList) {
 243  1
                 result.add(group.getId());
 244  
             }
 245  
         }
 246  
 
 247  1
         return result;
 248  
     }
 249  
 
 250  
     @Override
 251  
     public List<String> getDirectParentGroupIds(String groupId) throws RiceIllegalArgumentException {
 252  1
         if ( StringUtils.isEmpty(groupId) ) {
 253  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 254  
                 }
 255  1
         List<String> result = new ArrayList<String>();
 256  1
         if (groupId != null) {
 257  1
             Map<String,Group> groupList = getDirectParentGroups(groupId);
 258  1
             result.addAll(groupList.keySet());
 259  
         }
 260  
 
 261  1
         return result;
 262  
     }
 263  
 
 264  
     @Override
 265  
     public Attributes getAttributes(String groupId) throws RiceIllegalArgumentException {
 266  0
         if ( StringUtils.isEmpty(groupId) ) {
 267  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 268  
                 }
 269  
 
 270  0
         if (groupId == null) {
 271  0
             return Attributes.empty();
 272  
         }
 273  
 
 274  0
         Group group = getGroup(groupId);
 275  0
         if (group != null) {
 276  0
             return group.getAttributes();
 277  
         }
 278  0
         return null;
 279  
     }
 280  
 
 281  
     @Override
 282  
     public List<GroupMember> getMembers(List<String> groupIds) {
 283  1
         if (CollectionUtils.isEmpty(groupIds)) {
 284  0
             throw new RiceIllegalArgumentException("groupIds is empty");
 285  
                 }
 286  
 
 287  
         //TODO: PRIME example of something for new Criteria API
 288  1
         List<GroupMember> groupMembers = new ArrayList<GroupMember>();
 289  1
         for (String groupId : groupIds) {
 290  2
               groupMembers.addAll(getMembersOfGroup(groupId));
 291  
         }
 292  1
         return groupMembers;
 293  
     }
 294  
 
 295  
    /* @Override
 296  
     public Collection<Person> getPersonMembersOfGroup(final String groupId) {
 297  
         return null;  //To change body of implemented methods use File | Settings | File Templates.
 298  
     }
 299  
 
 300  
     @Override
 301  
     public Collection<Group> getGroupMembersOfGroup(@WebParam(name = "groupId") final String groupId) {
 302  
         return null;  //To change body of implemented methods use File | Settings | File Templates.
 303  
     }*/
 304  
 
 305  
 
 306  
         protected List<Group> getParentGroups(String groupId) throws RiceIllegalArgumentException {
 307  5
                 if ( StringUtils.isEmpty(groupId) ) {
 308  0
                         throw new RiceIllegalArgumentException("groupId is blank");
 309  
                 }
 310  5
                 Set<Group> groups = new HashSet<Group>();
 311  5
                 getParentGroupsInternal( groupId, groups );
 312  5
                 return new ArrayList<Group>( groups );
 313  
         }
 314  
 
 315  
     protected List<String> getMemberPrincipalIdsInternal(String groupId, Set<String> visitedGroupIds) {
 316  2
                 if ( groupId == null ) {
 317  0
                         return Collections.emptyList();
 318  
                 }
 319  2
                 Set<String> ids = new HashSet<String>();
 320  2
                 GroupBo group = getGroupBo(groupId);
 321  2
                 if ( group == null || !group.isActive()) {
 322  0
                         return Collections.emptyList();
 323  
                 }
 324  
 
 325  
         //List<String> memberIds = getMemberIdsByType(group, memberType);
 326  
         //List<GroupMember> members = new ArrayList<GroupMember>(getMembersOfGroup(group.getId()));
 327  2
                 ids.addAll( group.getMemberPrincipalIds());
 328  2
                 visitedGroupIds.add(group.getId());
 329  
 
 330  2
                 for (String memberGroupId : group.getMemberGroupIds()) {
 331  1
                         if (!visitedGroupIds.contains(memberGroupId)){
 332  1
                                 ids.addAll(getMemberPrincipalIdsInternal(memberGroupId, visitedGroupIds));
 333  
                         }
 334  
                 }
 335  
 
 336  2
                 return new ArrayList<String>(ids);
 337  
         }
 338  
 
 339  
     protected Collection<Group> getDirectGroupsForPrincipal( String principalId ) {
 340  1
                 return getDirectGroupsForPrincipal( principalId, null );
 341  
         }
 342  
 
 343  
     @SuppressWarnings("unchecked")
 344  
         protected Collection<Group> getDirectGroupsForPrincipal( String principalId, String namespaceCode ) {
 345  5
                 if ( principalId == null ) {
 346  0
                         return Collections.emptyList();
 347  
                 }
 348  5
                 Map<String,Object> criteria = new HashMap<String,Object>();
 349  5
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId);
 350  5
                 criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
 351  5
                 Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria);
 352  5
                 Set<String> groupIds = new HashSet<String>( groupMembers.size() );
 353  
                 // only return the active members
 354  5
                 for ( GroupMemberBo gm : groupMembers ) {
 355  5
                         if ( gm.isActive() ) {
 356  5
                                 groupIds.add( gm.getGroupId() );
 357  
                         }
 358  
                 }
 359  
                 // pull all the group information for the matching members
 360  5
                 Map<String,Group> groups = getGroups(groupIds);
 361  5
                 List<Group> result = new ArrayList<Group>( groups.size() );
 362  
                 // filter by namespace if necessary
 363  5
                 for ( Group group : groups.values() ) {
 364  5
                         if ( group.isActive() ) {
 365  5
                                 if ( StringUtils.isBlank(namespaceCode) || StringUtils.equals(namespaceCode, group.getNamespaceCode() ) ) {
 366  5
                                         result.add(group);
 367  
                                 }
 368  
                         }
 369  
                 }
 370  5
                 return result;
 371  
         }
 372  
 }