Coverage Report - org.kuali.rice.kew.workgroup.WorkgroupMembershipChangeProcessor
 
Classes in this File Line Coverage Branch Coverage Complexity
WorkgroupMembershipChangeProcessor
0%
0/52
0%
0/22
2.875
 
 1  
 /*
 2  
  * Copyright 2005-2008 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.workgroup;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 
 24  
 import org.kuali.rice.core.api.exception.RiceRuntimeException;
 25  
 import org.kuali.rice.kew.actionitem.ActionItem;
 26  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 27  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 28  
 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
 29  
 import org.kuali.rice.kew.exception.WorkflowException;
 30  
 import org.kuali.rice.kew.messaging.ParameterTranslator;
 31  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 32  
 import org.kuali.rice.kim.api.entity.principal.Principal;
 33  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 34  
 import org.kuali.rice.kim.api.group.Group;
 35  
 
 36  
 import org.kuali.rice.ksb.messaging.service.KSBXMLService;
 37  
 
 38  
 
 39  
 /**
 40  
  * Executes the updating of {@link ActionItem}s for a {@link Workgroup} when
 41  
  * the membership of a Workgroup changes.  This keeps users' Action Lists
 42  
  * in-sync with their group membership.  Allowing their Action List to
 43  
  * be updated for requests routed to groups that they are either added to
 44  
  * or removed from.
 45  
  *
 46  
  * @see ActionItem
 47  
  * @see Workgroup
 48  
  *
 49  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 50  
  */
 51  0
 public class WorkgroupMembershipChangeProcessor implements KSBXMLService {
 52  
 
 53  
         private static final String ADDED_OPERATION = "ADDED";
 54  
         private static final String REMOVED_OPERATION = "REMOVED";
 55  
 
 56  
     public ActionRequestService getActionRequestService() {
 57  0
         return (ActionRequestService) KEWServiceLocator.getActionRequestService();
 58  
     }
 59  
     
 60  
     public ActionListService getActionListService() {
 61  0
             return (ActionListService) KEWServiceLocator.getActionListService();
 62  
     }
 63  
     
 64  
         public void invoke(String contents) throws Exception {
 65  0
                 ParameterTranslator translator = new ParameterTranslator(contents);
 66  0
                 String[] parameters = translator.getParameters();
 67  0
                 if (parameters.length != 3) {
 68  0
                         throw new IllegalArgumentException("The Workgroup Membership Change Processor requires four parameters.");
 69  
                 }
 70  0
                 String operation = parameters[0];
 71  0
                 String principalId = parameters[1];
 72  0
                 String groupId = parameters[2];
 73  0
                 Principal principal = KimApiServiceLocator.getIdentityManagementService().getPrincipal(principalId);
 74  0
                 if (principal == null) {
 75  0
                         throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
 76  
                 }
 77  0
                 Group group = KimApiServiceLocator.getIdentityManagementService().getGroup(groupId);
 78  0
                 if (group == null) {
 79  0
                         throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
 80  
                 }
 81  0
                 if (ADDED_OPERATION.equals(operation)) {
 82  0
                         updateActionListForUserAddedToGroup(principalId, groupId);
 83  0
                 } else if (REMOVED_OPERATION.equals(operation)) {
 84  0
                         updateActionListForUserRemovedFromGroup(principalId, groupId);
 85  
                 } else {
 86  0
                         throw new WorkflowException("Did not understand requested group membership change operation '" + operation + "'");
 87  
                 }
 88  0
         }
 89  
 
 90  
         public static String getMemberAddedMessageContents(String principalId, String groupId) {
 91  0
                 return getMessageContents(principalId, groupId, ADDED_OPERATION);
 92  
     }
 93  
 
 94  
         public static String getMemberRemovedMessageContents(String principalId, String groupId) {
 95  0
                 return getMessageContents(principalId, groupId, REMOVED_OPERATION);
 96  
         }
 97  
 
 98  
         public static String getMessageContents(String principalId, String groupId, String operation) {
 99  0
                 ParameterTranslator translator = new ParameterTranslator();
 100  0
                 translator.addParameter(operation);
 101  0
                 translator.addParameter(principalId);
 102  0
                 translator.addParameter(groupId);
 103  0
                 return translator.getUntranslatedString();
 104  
         }
 105  
 
 106  
             
 107  
     
 108  
     /**
 109  
      * Update the user's Action List to reflect their addition to the given Workgroup.
 110  
      */
 111  
     private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
 112  0
                 List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
 113  0
                 List<String> allGroupsToCheck = KimApiServiceLocator.getIdentityManagementService().getParentGroupIds(groupId);
 114  0
         allGroupsToCheck.add(0, groupId);
 115  0
         for (String groupToCheckId : allGroupsToCheck) {
 116  0
             actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
 117  
         }
 118  0
         for (Iterator requestIt = actionRequests.iterator(); requestIt.hasNext();) {
 119  0
             ActionRequestValue request = (ActionRequestValue) requestIt.next();
 120  0
             ActionItem item = getActionListService().createActionItemForActionRequest(request);
 121  0
             item.setPrincipalId(principalId);
 122  0
             getActionListService().saveActionItem(item);
 123  0
         }
 124  0
     }
 125  
     
 126  
     private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
 127  0
                 List<String> allGroupsToCheck = KimApiServiceLocator.getIdentityManagementService().getParentGroupIds(groupId);
 128  0
         allGroupsToCheck.add(0, groupId);
 129  0
         Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
 130  0
                 for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
 131  0
                 ActionItem item = itemIt.next();
 132  0
                 if (item.isWorkgroupItem()) {
 133  0
                         for (String groupIdToCheck : allGroupsToCheck) {
 134  0
                                 if (item.getGroupId().equals(groupIdToCheck)) {
 135  0
                                         getActionListService().deleteActionItem(item);
 136  
                                 }
 137  
                         }
 138  
                 }
 139  0
         }
 140  0
     }    
 141  
 }