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