Coverage Report - org.kuali.rice.kim.service.impl.GroupInternalServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupInternalServiceImpl
0%
0/35
0%
0/12
2
GroupInternalServiceImpl$MembersDiff
0%
0/6
N/A
2
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.service.impl;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashSet;
 20  
 import java.util.List;
 21  
 import java.util.Set;
 22  
 
 23  
 import javax.xml.namespace.QName;
 24  
 
 25  
 import org.apache.commons.collections.ListUtils;
 26  
 import org.kuali.rice.kew.exception.WorkflowRuntimeException;
 27  
 import org.kuali.rice.kew.messaging.MessageServiceNames;
 28  
 import org.kuali.rice.kew.util.KEWConstants;
 29  
 import org.kuali.rice.kew.workgroup.WorkgroupMembershipChangeProcessor;
 30  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 31  
 import org.kuali.rice.kim.impl.group.GroupBo;
 32  
 import org.kuali.rice.kim.api.group.GroupService;
 33  
 import org.kuali.rice.kim.service.GroupInternalService;
 34  
 import org.kuali.rice.kns.service.BusinessObjectService;
 35  
 import org.kuali.rice.kns.service.KNSServiceLocator;
 36  
 import org.kuali.rice.ksb.api.KsbApiServiceLocator;
 37  
 import org.kuali.rice.ksb.messaging.service.KSBXMLService;
 38  
 
 39  
 /**
 40  
  * Concrete Implementation of {@link GroupInternalService}
 41  
  *
 42  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 43  
  *
 44  
  */
 45  0
 public class GroupInternalServiceImpl implements GroupInternalService {
 46  
     protected BusinessObjectService getBusinessObjectService() {
 47  0
             return KNSServiceLocator.getBusinessObjectService();
 48  
     }
 49  
 
 50  
 
 51  
     public GroupService getGroupService(){
 52  0
             return KimApiServiceLocator.getGroupService();
 53  
     }
 54  
 
 55  
     public GroupBo saveWorkgroup(GroupBo group) {
 56  0
             GroupService ims = getGroupService();
 57  0
             List<String> oldIds = ims.getMemberPrincipalIds(group.getId());
 58  0
         group = (GroupBo)getBusinessObjectService().save( group );
 59  0
         List<String> newIds = ims.getMemberPrincipalIds(group.getId());
 60  0
         updateForWorkgroupChange(group.getId(), oldIds, newIds);
 61  0
         return group;
 62  
     }
 63  
 
 64  
     public void updateForWorkgroupChange(String groupId,
 65  
                     List<String> oldPrincipalIds, List<String> newPrincipalIds) {
 66  0
         MembersDiff membersDiff = getMembersDiff(oldPrincipalIds, newPrincipalIds);
 67  0
         for (String removedPrincipalId : membersDiff.getRemovedPrincipalIds()) {
 68  0
                 updateForUserRemovedFromGroup(removedPrincipalId, groupId);
 69  
         }
 70  0
         for (String addedPrincipalId : membersDiff.getAddedPrincipalIds()) {
 71  0
                 updateForUserAddedToGroup(addedPrincipalId, groupId);
 72  
         }
 73  0
     }
 74  
 
 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
             KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KsbApiServiceLocator.getMessageHelper()
 80  
             .getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
 81  
             try {
 82  0
                 workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
 83  
                         .getMemberAddedMessageContents(principalId, groupId));
 84  0
             } catch (Exception e) {
 85  0
                 throw new WorkflowRuntimeException(e);
 86  0
             }
 87  
             }
 88  0
     }
 89  
 
 90  
     public void updateForUserRemovedFromGroup(String principalId, String groupId) {
 91  
         // first verify that the user is no longer a member of the workgroup
 92  0
             if(!getGroupService().isMemberOfGroup(principalId, groupId))
 93  
             {
 94  0
             KSBXMLService workgroupMembershipChangeProcessor = (KSBXMLService) KsbApiServiceLocator.getMessageHelper()
 95  
             .getServiceAsynchronously(new QName(KEWConstants.KEW_MODULE_NAMESPACE, MessageServiceNames.WORKGROUP_MEMBERSHIP_CHANGE_SERVICE));
 96  
             try {
 97  0
                 workgroupMembershipChangeProcessor.invoke(WorkgroupMembershipChangeProcessor
 98  
                         .getMemberRemovedMessageContents(principalId, groupId));
 99  0
             } catch (Exception e) {
 100  0
                 throw new WorkflowRuntimeException(e);
 101  0
             }
 102  
             }
 103  
 
 104  0
     }
 105  
 
 106  
     private MembersDiff getMembersDiff(List<String> oldMemberPrincipalIds, List<String> newMemberPrincipalIds) {
 107  
 
 108  
             // ListUtils does not check the null case.  Which can happen when adding a new group
 109  
             // so, if they're null make them empty lists.
 110  0
             if(oldMemberPrincipalIds == null) oldMemberPrincipalIds = new ArrayList<String>();
 111  0
             if(newMemberPrincipalIds == null) newMemberPrincipalIds = new ArrayList<String>();
 112  
 
 113  0
         Set<String> addedPrincipalIds = new HashSet<String>(ListUtils.subtract(newMemberPrincipalIds, oldMemberPrincipalIds));
 114  0
         Set<String> removedPrincipalIds = new HashSet<String>(ListUtils.subtract(oldMemberPrincipalIds, newMemberPrincipalIds));
 115  0
         return new MembersDiff(addedPrincipalIds, removedPrincipalIds);
 116  
     }
 117  
 
 118  0
     private class MembersDiff {
 119  
         private final Set<String> addedPrincipalIds;
 120  
 
 121  
         private final Set<String> removedPrincipalIds;
 122  
 
 123  0
         public MembersDiff(Set<String> addedPrincipalIds, Set<String>removedPrincipalIds) {
 124  0
             this.addedPrincipalIds = addedPrincipalIds;
 125  0
             this.removedPrincipalIds = removedPrincipalIds;
 126  0
         }
 127  
 
 128  
         public Set<String> getAddedPrincipalIds() {
 129  0
             return addedPrincipalIds;
 130  
         }
 131  
 
 132  
         public Set<String> getRemovedPrincipalIds() {
 133  0
             return removedPrincipalIds;
 134  
         }
 135  
     }
 136  
 
 137  
 }