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 */
016package org.kuali.rice.kew.actions;
017
018import org.apache.log4j.MDC;
019import org.kuali.rice.kew.actionrequest.ActionRequestValue;
020import org.kuali.rice.kew.actiontaken.ActionTakenValue;
021import org.kuali.rice.kew.api.KewApiConstants;
022import org.kuali.rice.kew.api.exception.InvalidActionTakenException;
023import org.kuali.rice.kew.api.exception.WorkflowException;
024import org.kuali.rice.kew.engine.node.ProcessDefinitionBo;
025import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
026import org.kuali.rice.kew.service.KEWServiceLocator;
027import org.kuali.rice.kim.api.identity.principal.PrincipalContract;
028
029import java.sql.Timestamp;
030import java.util.List;
031
032
033/**
034 * //TODO KSENROLL-5346 DELETE THIS ONCE KRAD IS UPGRADED!!!
035 *
036 * Action that puts the document in routing. For zero node paths this fixes a bug where the document was never saved
037 * because it went straight to final skipping processed.
038 *
039 * @author Kuali Rice Team (rice.collab@kuali.org)
040 *
041 */
042@Deprecated
043//TODO KSENROLL-5346 DELETE THIS ONCE KRAD IS UPGRADED!!!
044public class RouteDocumentAction extends ActionTakenEvent {
045    private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RouteDocumentAction.class);
046
047    public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal) {
048        super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal);
049    }
050
051    public RouteDocumentAction(DocumentRouteHeaderValue rh, PrincipalContract principal, String annotation) {
052        super(KewApiConstants.ACTION_TAKEN_COMPLETED_CD, rh, principal, annotation);
053    }
054
055    /* (non-Javadoc)
056     * @see org.kuali.rice.kew.actions.ActionTakenEvent#getActionPerformedCode()
057     */
058    @Override
059    public String getActionPerformedCode() {
060        return KewApiConstants.ACTION_TAKEN_ROUTED_CD;
061    }
062
063    /* (non-Javadoc)
064     * @see org.kuali.rice.kew.actions.ActionTakenEvent#isActionCompatibleRequest(java.util.List)
065     */
066    @Override
067    public String validateActionRules() {
068        // check state before checking kim
069        if (!getRouteHeader().isValidActionToTake(getActionPerformedCode())) {
070            return "Document is not in a state to be routed";
071        }
072        if (! KEWServiceLocator.getDocumentTypePermissionService().canRoute(getPrincipal().getPrincipalId(), getRouteHeader())) {
073                return "User is not authorized to Route document";
074        }
075        return "";
076    }
077
078    @Override
079    public String validateActionRules(List<ActionRequestValue> actionRequests) {
080        return validateActionRules();
081    }
082    
083    /**
084     * Record the routing action. To route a document, it must be in the proper state. Previous requests and actions have no bearing on the outcome of this action, unless the
085     * @throws org.kuali.rice.kew.api.exception.InvalidActionTakenException
086     */
087    public void recordAction() throws InvalidActionTakenException {
088        MDC.put("docId", getRouteHeader().getDocumentId());
089        updateSearchableAttributesIfPossible();
090
091        LOG.debug("Routing document : " + annotation);
092
093        LOG.debug("Checking to see if the action is legal");
094        String errorMessage = validateActionRules();
095        if (!org.apache.commons.lang.StringUtils.isEmpty(errorMessage)) {
096            throw new InvalidActionTakenException(errorMessage);
097        }
098
099
100        // we want to check that the "RouteDocument" command is valid here, not the "Complete" command (which is in our Action's action taken code)
101        LOG.debug("Record the routing action");
102        ActionTakenValue actionTaken = saveActionTaken();
103
104        //TODO this will get all pending AR's even if they haven't been in an action list... This seems bad
105        List<ActionRequestValue> actionRequests = getActionRequestService().findPendingByDoc(getDocumentId());
106        LOG.debug("Deactivate all pending action requests");
107        // deactivate any requests for the user that routed the document.
108        for (ActionRequestValue actionRequest : actionRequests)
109        {
110            // requests generated to the user who is routing the document should be deactivated
111            if ((getPrincipal().getPrincipalId().equals(actionRequest.getPrincipalId())) && (actionRequest.isActive()))
112            {
113                getActionRequestService().deactivateRequest(actionTaken, actionRequest);
114            }
115            // requests generated by a save action should be deactivated
116            else if (KewApiConstants.SAVED_REQUEST_RESPONSIBILITY_ID.equals(actionRequest.getResponsibilityId()))
117            {
118                getActionRequestService().deactivateRequest(actionTaken, actionRequest);
119            }
120        }
121
122            notifyActionTaken(actionTaken);
123
124            try {
125                String oldStatus = getRouteHeader().getDocRouteStatus();
126                getRouteHeader().markDocumentEnroute();
127                
128                if (((ProcessDefinitionBo)getRouteHeader().getDocumentType().getProcesses().get(0)).getInitialRouteNode() == null) {
129                    getRouteHeader().setApprovedDate(new Timestamp(System.currentTimeMillis()));
130                    getRouteHeader().markDocumentProcessed();
131
132                    getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
133
134                    String newStatus = getRouteHeader().getDocRouteStatus();
135                    notifyStatusChange(newStatus, oldStatus);
136                    KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
137
138                    getRouteHeader().markDocumentFinalized();
139                }
140
141                getRouteHeader().setRoutedByUserWorkflowId(getPrincipal().getPrincipalId());
142
143                String newStatus = getRouteHeader().getDocRouteStatus();
144                notifyStatusChange(newStatus, oldStatus);
145                KEWServiceLocator.getRouteHeaderService().saveRouteHeader(getRouteHeader());
146            } catch (WorkflowException ex) {
147                LOG.warn(ex, ex);
148                    throw new InvalidActionTakenException(ex.getMessage());
149            }
150
151    }
152}