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.actionrequest.ActionRequestValue;
020import org.kuali.rice.kew.api.action.ActionRequestStatus;
021import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
022import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
025import org.kuali.rice.kim.api.services.KimApiServiceLocator;
026
027import java.util.List;
028
029
030
031/**
032 * This is the inverse of the {@link TakeWorkgroupAuthority} action.  This puts the document back
033 * in all the peoples action lists that have the document routed to them.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 */
037public class ReleaseWorkgroupAuthority extends ActionTakenEvent {
038
039    private String groupId;
040    /**
041     * @param routeHeader
042     * @param principal
043     */
044    public ReleaseWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal) {
045        super(KewApiConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, routeHeader, principal);
046    }
047
048    /**
049     * @param routeHeader
050     * @param principal
051     * @param annotation
052     * @param groupId
053     */
054    public ReleaseWorkgroupAuthority(DocumentRouteHeaderValue routeHeader, PrincipalContract principal, String annotation, String groupId) {
055        super(KewApiConstants.ACTION_TAKEN_RELEASE_WORKGROUP_AUTHORITY_CD, routeHeader, principal, annotation);
056        this.groupId = groupId;
057    }
058
059    /* (non-Javadoc)
060     * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules()
061     */
062    @Override
063    public String validateActionRules() {
064        if (groupId == null) {
065            return "User cannot Release Workgroup Authority without a given workgroup";
066        } else {
067            return performReleaseWorkgroupAuthority(true);
068        }
069    }
070    
071    /**
072     * This overridden method ...
073     * 
074     * @see org.kuali.rice.kew.actions.ActionTakenEvent#validateActionRules(java.util.List)
075     */
076    @Override
077    public String validateActionRules(List<ActionRequestValue> actionRequests) {
078        return validateActionRules();
079    }
080
081    public void recordAction() throws InvalidActionTakenException {
082        String error = performReleaseWorkgroupAuthority(false);
083        if (!org.apache.commons.lang.StringUtils.isEmpty(error)) {
084            throw new InvalidActionTakenException(error);
085        }
086
087        queueDocumentProcessing();
088    }
089
090    private String performReleaseWorkgroupAuthority(boolean forValidationOnly) {
091        if (!KimApiServiceLocator.getGroupService().isMemberOfGroup(getPrincipal().getPrincipalId(), groupId)){
092            return (getPrincipal().getPrincipalName() + " not a member of workgroup " + groupId);
093        }
094
095        List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
096        //List groupRequestsToActivate = new ArrayList();//requests for this group that need action items
097        for (ActionRequestValue actionRequest : actionRequests)
098        {
099            //we left the group active from take authority action.  pending havent been normally activated yet
100            if (actionRequest.isGroupRequest() && actionRequest.isActive() && actionRequest.getGroupId().equals(groupId)) {
101                List<ActionItem> actionItems = actionRequest.getActionItems();
102                if (actionItems.size() == 1) {
103                    ActionItem actionItem = actionItems.get(0);
104                    if (! actionItem.getPrincipalId().equals(getPrincipal().getPrincipalId())) {
105                        return "User attempting to release workgroup authority did not take it.";
106                    } else if (!forValidationOnly)
107                    {
108                        actionRequest.setStatus(ActionRequestStatus.INITIALIZED.getCode());//to circumvent check in service during activation
109                        getActionRequestService().activateRequest(actionRequest);
110                    }
111                }
112            }
113        }
114        return "";
115    }
116}