Coverage Report - org.kuali.rice.kim.impl.group.GroupInternalServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupInternalServiceImpl
0%
0/29
0%
0/14
1.7
GroupInternalServiceImpl$MembersDiff
0%
0/6
N/A
1.7
 
 1  
 /**
 2  
  * Copyright 2005-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  
 package org.kuali.rice.kim.impl.group;
 17  
 
 18  
 import org.apache.commons.collections.ListUtils;
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.kew.api.KewApiServiceLocator;
 21  
 import org.kuali.rice.kew.api.KewApiConstants;
 22  
 import org.kuali.rice.kim.api.group.GroupService;
 23  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 24  
 import org.kuali.rice.krad.service.BusinessObjectService;
 25  
 import org.kuali.rice.krad.service.KRADServiceLocator;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.Collections;
 29  
 import java.util.HashSet;
 30  
 import java.util.List;
 31  
 import java.util.Set;
 32  
 
 33  
 /**
 34  
  * Concrete Implementation of {@link GroupInternalService}
 35  
  *
 36  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 37  
  *
 38  
  */
 39  0
 public class GroupInternalServiceImpl implements GroupInternalService {
 40  
     protected BusinessObjectService getBusinessObjectService() {
 41  0
             return KRADServiceLocator.getBusinessObjectService();
 42  
     }
 43  
 
 44  
 
 45  
     public GroupService getGroupService(){
 46  0
             return KimApiServiceLocator.getGroupService();
 47  
     }
 48  
 
 49  
     @Override
 50  
     public GroupBo saveWorkgroup(GroupBo group) {
 51  0
             GroupService ims = getGroupService();
 52  0
         List<String> oldIds = Collections.emptyList();
 53  0
             if (StringUtils.isNotEmpty(group.getId())) {
 54  0
             oldIds = ims.getMemberPrincipalIds(group.getId());
 55  
         }
 56  0
         group = (GroupBo)getBusinessObjectService().save( group );
 57  0
         List<String> newIds = ims.getMemberPrincipalIds(group.getId());
 58  0
         updateForWorkgroupChange(group.getId(), oldIds, newIds);
 59  0
         return group;
 60  
     }
 61  
 
 62  
     @Override
 63  
     public void updateForWorkgroupChange(String groupId,
 64  
                     List<String> oldPrincipalIds, List<String> newPrincipalIds) {
 65  0
         MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
 66  0
         for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
 67  0
                 updateForUserRemovedFromGroup(removedPrincipalId, groupId);
 68  
         }
 69  0
         for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
 70  0
                 updateForUserAddedToGroup(addedPrincipalId, groupId);
 71  
         }
 72  0
     }
 73  
 
 74  
     @Override
 75  
     public void updateForUserAddedToGroup(String principalId, String groupId) {
 76  
         // first verify that the user is still a member of the workgroup
 77  0
             if(getGroupService().isMemberOfGroup(principalId, groupId))
 78  
             {
 79  0
                 KewApiServiceLocator.getGroupMembershipChangeQueue()
 80  
                     .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.ADDED, groupId, principalId);
 81  
             }
 82  0
     }
 83  
 
 84  
     @Override
 85  
     public void updateForUserRemovedFromGroup(String principalId, String groupId) {
 86  
         // first verify that the user is no longer a member of the workgroup
 87  0
             if(!getGroupService().isMemberOfGroup(principalId, groupId))
 88  
             {
 89  0
             KewApiServiceLocator.getGroupMembershipChangeQueue()
 90  
                 .notifyMembershipChange(KewApiConstants.GroupMembershipChangeOperations.REMOVED, groupId, principalId);
 91  
             }
 92  
 
 93  0
     }
 94  
 
 95  
     private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) {
 96  
 
 97  
             // ListUtils does not check the null case.  Which can happen when adding a new group
 98  
             // so, if they're null make them empty lists.
 99  0
             if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>();
 100  0
             if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>();
 101  
 
 102  0
         Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds));
 103  0
         Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds));
 104  0
         return new MembersDiff(addedPrincipalIds, removedPrincipalIds);
 105  
     }
 106  
 
 107  0
     private class MembersDiff {
 108  
         private final Set<String> addedPrincipalIds;
 109  
 
 110  
         private final Set<String> removedPrincipalIds;
 111  
 
 112  0
         public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) {
 113  0
             this.addedPrincipalIds = addedPrincipalIds;
 114  0
             this.removedPrincipalIds = removedPrincipalIds;
 115  0
         }
 116  
 
 117  
         public Set<String> getAddedPrincipalIds() {
 118  0
             return addedPrincipalIds;
 119  
         }
 120  
 
 121  
         public Set<String> getRemovedPrincipalIds() {
 122  0
             return removedPrincipalIds;
 123  
         }
 124  
     }
 125  
 
 126  
 }