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 org.kuali.rice.core.api.exception.RiceRuntimeException;
 20  
 import org.kuali.rice.kew.actionitem.ActionItem;
 21  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 22  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 23  
 import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
 24  
 import org.kuali.rice.kew.exception.WorkflowException;
 25  
 import org.kuali.rice.kew.messaging.ParameterTranslator;
 26  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 27  
 import org.kuali.rice.kim.api.group.Group;
 28  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 29  
 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
 30  
 import org.kuali.rice.ksb.messaging.service.KSBXMLService;
 31  
 
 32  
 import java.util.ArrayList;
 33  
 import java.util.Collection;
 34  
 import java.util.Iterator;
 35  
 import java.util.List;
 36  
 
 37  
 
 38  
 /**
 39  
  * Executes the updating of {@link ActionItem}s for a {@link Workgroup} when
 40  
  * the membership of a Workgroup 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 Workgroup
 47  
  *
 48  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 49  
  */
 50  0
 public class WorkgroupMembershipChangeProcessor implements KSBXMLService {
 51  
 
 52  
         private static final String ADDED_OPERATION = "ADDED";
 53  
         private static final String REMOVED_OPERATION = "REMOVED";
 54  
 
 55  
     public ActionRequestService getActionRequestService() {
 56  0
         return (ActionRequestService) KEWServiceLocator.getActionRequestService();
 57  
     }
 58  
     
 59  
     public ActionListService getActionListService() {
 60  0
             return (ActionListService) KEWServiceLocator.getActionListService();
 61  
     }
 62  
     
 63  
         public void invoke(String contents) throws Exception {
 64  0
                 ParameterTranslator translator = new ParameterTranslator(contents);
 65  0
                 String[] parameters = translator.getParameters();
 66  0
                 if (parameters.length != 3) {
 67  0
                         throw new IllegalArgumentException("The Workgroup Membership Change Processor requires four parameters.");
 68  
                 }
 69  0
                 String operation = parameters[0];
 70  0
                 String principalId = parameters[1];
 71  0
                 String groupId = parameters[2];
 72  0
                 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
 73  0
                 if (principal == null) {
 74  0
                         throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
 75  
                 }
 76  0
                 Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
 77  0
                 if (group == null) {
 78  0
                         throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
 79  
                 }
 80  0
                 if (ADDED_OPERATION.equals(operation)) {
 81  0
                         updateActionListForUserAddedToGroup(principalId, groupId);
 82  0
                 } else if (REMOVED_OPERATION.equals(operation)) {
 83  0
                         updateActionListForUserRemovedFromGroup(principalId, groupId);
 84  
                 } else {
 85  0
                         throw new WorkflowException("Did not understand requested group membership change operation '" + operation + "'");
 86  
                 }
 87  0
         }
 88  
 
 89  
         public static String getMemberAddedMessageContents(String principalId, String groupId) {
 90  0
                 return getMessageContents(principalId, groupId, ADDED_OPERATION);
 91  
     }
 92  
 
 93  
         public static String getMemberRemovedMessageContents(String principalId, String groupId) {
 94  0
                 return getMessageContents(principalId, groupId, REMOVED_OPERATION);
 95  
         }
 96  
 
 97  
         public static String getMessageContents(String principalId, String groupId, String operation) {
 98  0
                 ParameterTranslator translator = new ParameterTranslator();
 99  0
                 translator.addParameter(operation);
 100  0
                 translator.addParameter(principalId);
 101  0
                 translator.addParameter(groupId);
 102  0
                 return translator.getUntranslatedString();
 103  
         }
 104  
 
 105  
             
 106  
     
 107  
     /**
 108  
      * Update the user's Action List to reflect their addition to the given Workgroup.
 109  
      */
 110  
     private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
 111  0
                 List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
 112  0
                 List<String> allGroupsToCheck = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
 113  0
         allGroupsToCheck.add(0, groupId);
 114  0
         for (String groupToCheckId : allGroupsToCheck) {
 115  0
             actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
 116  
         }
 117  0
         for (Iterator requestIt = actionRequests.iterator(); requestIt.hasNext();) {
 118  0
             ActionRequestValue request = (ActionRequestValue) requestIt.next();
 119  0
             ActionItem item = getActionListService().createActionItemForActionRequest(request);
 120  0
             item.setPrincipalId(principalId);
 121  0
             getActionListService().saveActionItem(item);
 122  0
         }
 123  0
     }
 124  
     
 125  
     private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
 126  0
                 List<String> allGroupsToCheck = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
 127  0
         allGroupsToCheck.add(0, groupId);
 128  0
         Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
 129  0
                 for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
 130  0
                 ActionItem item = itemIt.next();
 131  0
                 if (item.isWorkgroupItem()) {
 132  0
                         for (String groupIdToCheck : allGroupsToCheck) {
 133  0
                                 if (item.getGroupId().equals(groupIdToCheck)) {
 134  0
                                         getActionListService().deleteActionItem(item);
 135  
                                 }
 136  
                         }
 137  
                 }
 138  0
         }
 139  0
     }    
 140  
 }