001    /**
002     * Copyright 2005-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kew.impl.group;
017    
018    import java.util.ArrayList;
019    import java.util.Collection;
020    import java.util.Iterator;
021    import java.util.List;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
025    import org.kuali.rice.core.api.exception.RiceRuntimeException;
026    import org.kuali.rice.kew.actionitem.ActionItem;
027    import org.kuali.rice.kew.actionlist.service.ActionListService;
028    import org.kuali.rice.kew.actionrequest.ActionRequestValue;
029    import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
030    import org.kuali.rice.kew.api.WorkflowRuntimeException;
031    import org.kuali.rice.kew.api.group.GroupMembershipChangeQueue;
032    import org.kuali.rice.kew.service.KEWServiceLocator;
033    import org.kuali.rice.kew.api.KewApiConstants;
034    import org.kuali.rice.kim.api.group.Group;
035    import org.kuali.rice.kim.api.identity.principal.Principal;
036    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
037    
038    /**
039     * Executes the updating of {@link ActionItem}s for a {@link Group} when
040     * the membership of a group changes.  This keeps users' Action Lists
041     * in-sync with their group membership.  Allowing their Action List to
042     * be updated for requests routed to groups that they are either added to
043     * or removed from.
044     *
045     * @see ActionItem
046     * @see Group 
047     * 
048     * @author Kuali Rice Team (rice.collab@kuali.org)
049     */
050    public class GroupMembershipChangeQueueImpl implements GroupMembershipChangeQueue {
051        
052        /**
053         * @see org.kuali.rice.kew.api.group.GroupMembershipChangeQueue#notifyMembershipChange(java.lang.String, java.lang.String, java.lang.String)
054         */
055        @Override
056        public void notifyMembershipChange(String operation, String groupId, String principalId) {
057            if (StringUtils.isBlank(operation)) {
058                            throw new RiceIllegalArgumentException("operation was blank or null");
059                    }
060    
061            if (StringUtils.isBlank(groupId)) {
062                            throw new RiceIllegalArgumentException("groupId was blank or null");
063                    }
064    
065            if (StringUtils.isBlank(principalId)) {
066                            throw new RiceIllegalArgumentException("principalId was blank or null");
067                    }
068    
069            Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
070            if (principal == null) {
071                throw new RiceRuntimeException("Could not locate the user for the given principal id '" + principalId + "'");
072            }
073            Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
074            if (group == null) {
075                throw new RiceRuntimeException("Could not locate the group with the given id '" + groupId + "'");
076            }
077            if (KewApiConstants.GroupMembershipChangeOperations.ADDED.equalsIgnoreCase(operation)) {
078                updateActionListForUserAddedToGroup(principalId, groupId);
079            } else if (KewApiConstants.GroupMembershipChangeOperations.REMOVED.equalsIgnoreCase(operation)) {
080                updateActionListForUserRemovedFromGroup(principalId, groupId);
081            } else {
082                throw new WorkflowRuntimeException("Did not understand requested group membership change operation '" + operation + "'");
083            }
084        }
085    
086        
087        /**
088         * Update the user's Action List to reflect their addition to the given Workgroup.
089         */
090        private void updateActionListForUserAddedToGroup(String principalId, String groupId) {
091            List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>();
092            List<String> parentGroupIds = KimApiServiceLocator.getGroupService().getParentGroupIds(groupId);
093            List<String> allGroupsToCheck = new ArrayList<String>();
094            allGroupsToCheck.add(0, groupId);
095            allGroupsToCheck.addAll(parentGroupIds);
096            for (String groupToCheckId : allGroupsToCheck) {
097                actionRequests.addAll(getActionRequestService().findActivatedByGroup(groupToCheckId));
098            }
099            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    }