Coverage Report - org.kuali.rice.kew.impl.group.GroupMembershipChangeQueueImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
GroupMembershipChangeQueueImpl
0%
0/47
0%
0/26
4.8
 
 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.kew.impl.group;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collection;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import org.apache.commons.lang.StringUtils;
 24  
 import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
 25  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 26  
 import org.kuali.rice.kew.actionitem.ActionItem;
 27  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 28  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 29  
 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
 30  
 import org.kuali.rice.kew.api.WorkflowRuntimeException;
 31  
 import org.kuali.rice.kew.api.group.GroupMembershipChangeQueue;
 32  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 33  
 import org.kuali.rice.kew.api.KewApiConstants;
 34  
 import org.kuali.rice.kim.api.group.Group;
 35  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 36  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 37  
 
 38  
 /**
 39  
  * Executes the updating of {@link ActionItem}s for a {@link Group} when
 40  
  * the membership of a group changes.  This keeps users' Action Lists
 41  
  * in-sync with their group membership.  Allowing their Action List to
 42  
  * be updated for requests routed to groups that they are either added to
 43  
  * or removed from.
 44  
  *
 45  
  * @see ActionItem
 46  
  * @see Group 
 47  
  * 
 48  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 49  
  */
 50  0
 public class GroupMembershipChangeQueueImpl implements GroupMembershipChangeQueue {
 51  
     
 52  
     /**
 53  
      * @see org.kuali.rice.kew.api.group.GroupMembershipChangeQueue#notifyMembershipChange(java.lang.String, java.lang.String, java.lang.String)
 54  
      */
 55  
     @Override
 56  
     public void notifyMembershipChange(String operation, String groupId, String principalId) {
 57  0
         if (StringUtils.isBlank(operation)) {
 58  0
                         throw new RiceIllegalArgumentException("operation was blank or null");
 59  
                 }
 60  
 
 61  0
         if (StringUtils.isBlank(groupId)) {
 62  0
                         throw new RiceIllegalArgumentException("groupId was blank or null");
 63  
                 }
 64  
 
 65  0
         if (StringUtils.isBlank(principalId)) {
 66  0
                         throw new RiceIllegalArgumentException("principalId was blank or null");
 67  
                 }
 68  
 
 69  0
         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
 70  0
         if (principal == null) {
 71  0
             throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
 72  
         }
 73  0
         Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
 74  0
         if (group == null) {
 75  0
             throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
 76  
         }
 77  0
         if (KewApiConstants.GroupMembershipChangeOperations.ADDED.equalsIgnoreCase(operation)) {
 78  0
             updateActionListForUserAddedToGroup(principalId, groupId);
 79  0
         } else if (KewApiConstants.GroupMembershipChangeOperations.REMOVED.equalsIgnoreCase(operation)) {
 80  0
             updateActionListForUserRemovedFromGroup(principalId, groupId);
 81  
         } else {
 82  0
             throw new WorkflowRuntimeException("Did not understand requested group membership change operation '" + operation + "'");
 83  
         }
 84  0
     }
 85  
 
 86  
     
 87  
     /**
 88  
      * Update the user's Action List to reflect their addition to the given Workgroup.
 89  
      */
 90  
     private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
 91  0
         List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
 92  0
         List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
 93  0
         List<String> allGroupsToCheck = new ArrayList<String>();
 94  0
         allGroupsToCheck.add(0, groupId);
 95  0
         allGroupsToCheck.addAll(parentGroupIds);
 96  0
         for (String groupToCheckId : allGroupsToCheck) {
 97  0
             actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
 98  
         }
 99  0
         for (ActionRequestValue request : actionRequests) {
 100  0
             ActionItem item = getActionListService().createActionItemForActionRequest(request);
 101  0
             item.setPrincipalId(principalId);
 102  0
             getActionListService().saveActionItem(item);
 103  0
         }
 104  0
     }
 105  
     
 106  
     private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
 107  0
         List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
 108  0
         List<String> allGroupsToCheck = new ArrayList<String>();
 109  0
         allGroupsToCheck.add(0, groupId);
 110  0
         allGroupsToCheck.addAll(parentGroupIds);
 111  0
         Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
 112  0
         for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
 113  0
             ActionItem item = itemIt.next();
 114  0
             if (item.isWorkgroupItem()) {
 115  0
                 for (String groupIdToCheck : allGroupsToCheck) {
 116  0
                     if (item.getGroupId().equals(groupIdToCheck)) {
 117  0
                         getActionListService().deleteActionItem(item);
 118  
                     }
 119  
                 }
 120  
             }
 121  0
         }
 122  0
     } 
 123  
     
 124  
     public ActionRequestService getActionRequestService() {
 125  0
         return KEWServiceLocator.getActionRequestService();
 126  
     }
 127  
     
 128  
     public ActionListService getActionListService() {
 129  0
         return KEWServiceLocator.getActionListService();
 130  
     }
 131  
 }