001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kew.actions;
017
018import org.kuali.rice.kew.actionitem.ActionItem;
019import org.kuali.rice.kew.actionlist.service.ActionListService;
020import org.kuali.rice.kew.actionrequest.ActionRequestValue;
021import org.kuali.rice.kew.actiontaken.ActionTakenValue;
022import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
023import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
024import org.kuali.rice.kew.service.KEWServiceLocator;
025import org.kuali.rice.kew.api.KewApiConstants;
026import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
027import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.List;
032
033
034
035/**
036 * Removes all workgroup action items for a document from everyone's action list except the person
037 * who took the workgroup authority
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 *
041 */
042public class TakeWorkgroupAuthority extends ActionTakenEvent {
043    
044    private String groupId;
045    
046    /**
047     * @param routeHeader
048     * @param principal
049     */
050    public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
051        super(KewApiConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal);
052    }
053
054    /**
055     * @param routeHeader
056     * @param principal
057     * @param annotation
058     * @param groupId
059     */
060    public TakeWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String groupId) {
061        super(KewApiConstants.ACTION_TAKEN_TAKE_WORKGROUP_AUTHORITY_CD, routeHeader, principal, annotation);
062        this.groupId = groupId;
063    }
064
065    /* (non-Javadoc)
066     * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
067     */
068    @Override
069    public String validateActionRules() {
070        if  ( (groupId != null) && (!KimApiServiceLocator.getGroupService().isMemberOfGroup(getPrincipal().getPrincipalId(), groupId))) {
071            return (getPrincipal().getPrincipalName() + " not a member of workgroup " + groupId);
072        }
073        return "";
074    }
075
076    @Override
077    public String validateActionRules(List<ActionRequestValue> actionRequests) {
078        return validateActionRules();
079    }
080    
081    public void recordAction() throws InvalidActionTakenException {
082
083        String errorMessage = validateActionRules();
084        if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
085            throw new InvalidActionTakenException(errorMessage);
086        }
087//        if (! workgroup.hasMember(getUser())) {
088//            throw new InvalidActionTakenException(getUser().getPrincipalName() + " not a member of workgroup " + workgroup.getDisplayName());
089//        }
090
091        List<ActionRequestValue> documentRequests = getActionRequestService().findPendingByDoc(getDocumentId());
092        List<ActionRequestValue> workgroupRequests = new ArrayList<ActionRequestValue>();
093        for (ActionRequestValue actionRequest : documentRequests)
094        {
095            if (actionRequest.isGroupRequest() && actionRequest.getGroup().getId().equals(groupId))
096            {
097                workgroupRequests.add(actionRequest);
098            }
099        }
100
101        ActionTakenValue actionTaken = saveActionTaken(findDelegatorForActionRequests(workgroupRequests));
102        notifyActionTaken(actionTaken);
103
104        ActionListService actionListService = KEWServiceLocator.getActionListService();
105        Collection<ActionItem> actionItems = actionListService.findByDocumentId(getDocumentId());
106        for (ActionItem actionItem : actionItems)
107        {
108            //delete all requests for this workgroup on this document not to this user
109            if (actionItem.isWorkgroupItem() && actionItem.getGroupId().equals(groupId) &&
110                    !actionItem.getPrincipalId().equals(getPrincipal().getPrincipalId()))
111            {
112                actionListService.deleteActionItem(actionItem);
113            }
114        }
115    }
116}