View Javadoc

1   /**
2    * Copyright 2005-2012 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  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          if (StringUtils.isBlank(operation)) {
58  			throw new RiceIllegalArgumentException("operation was blank or null");
59  		}
60  
61          if (StringUtils.isBlank(groupId)) {
62  			throw new RiceIllegalArgumentException("groupId was blank or null");
63  		}
64  
65          if (StringUtils.isBlank(principalId)) {
66  			throw new RiceIllegalArgumentException("principalId was blank or null");
67  		}
68  
69          Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
70          if (principal == null) {
71              throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
72          }
73          Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
74          if (group == null) {
75              throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
76          }
77          if (KewApiConstants.GroupMembershipChangeOperations.ADDED.equalsIgnoreCase(operation)) {
78              updateActionListForUserAddedToGroup(principalId, groupId);
79          } else if (KewApiConstants.GroupMembershipChangeOperations.REMOVED.equalsIgnoreCase(operation)) {
80              updateActionListForUserRemovedFromGroup(principalId, groupId);
81          } else {
82              throw new WorkflowRuntimeException("Did not understand requested group membership change operation '" + operation + "'");
83          }
84      }
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          List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
92          List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
93          List<String> allGroupsToCheck = new ArrayList<String>();
94          allGroupsToCheck.add(0, groupId);
95          allGroupsToCheck.addAll(parentGroupIds);
96          for (String groupToCheckId : allGroupsToCheck) {
97              actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
98          }
99          for (ActionRequestValue request : actionRequests) {
100             ActionItem item = getActionListService().createActionItemForActionRequest(request);
101             item.setPrincipalId(principalId);
102             getActionListService().saveActionItem(item);
103         }
104     }
105     
106     private void updateActionListForUserRemovedFromGroup(String principalId, String groupId) {
107         List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
108         List<String> allGroupsToCheck = new ArrayList<String>();
109         allGroupsToCheck.add(0, groupId);
110         allGroupsToCheck.addAll(parentGroupIds);
111         Collection<ActionItem> actionItems = getActionListService().findByPrincipalId(principalId);
112         for (Iterator<ActionItem> itemIt = actionItems.iterator(); itemIt.hasNext();) {
113             ActionItem item = itemIt.next();
114             if (item.isWorkgroupItem()) {
115                 for (String groupIdToCheck : allGroupsToCheck) {
116                     if (item.getGroupId().equals(groupIdToCheck)) {
117                         getActionListService().deleteActionItem(item);
118                     }
119                 }
120             }
121         }
122     } 
123     
124     public ActionRequestService getActionRequestService() {
125         return KEWServiceLocator.getActionRequestService();
126     }
127     
128     public ActionListService getActionListService() {
129         return KEWServiceLocator.getActionListService();
130     }
131 }