001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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 * Contributors:
017 *     Mariana Cedica
018 */
019package org.nuxeo.ecm.platform.routing.core.api.operation;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.automation.core.annotations.Param;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.DocumentNotFoundException;
031import org.nuxeo.ecm.core.api.IdRef;
032import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
033import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEngineService;
034
035/**
036 * Cancels the workflow with the given id, where the id is the id of the document representing the workflow instance.
037 *
038 * @since 5.7.2
039 */
040@Operation(id = CancelWorkflowOperation.ID, category = Constants.CAT_WORKFLOW, label = "Cancel workflow", requires = Constants.WORKFLOW_CONTEXT, description = "Cancel the workflow with the given id, "
041        + "where the required id is the id of the document representing the workflow instance.", aliases = { "Context.CancelWorkflow" })
042public class CancelWorkflowOperation {
043
044    public static final String ID = "WorkflowInstance.Cancel";
045
046    private static final Log log = LogFactory.getLog(CancelWorkflowOperation.class);
047
048    @Context
049    protected CoreSession session;
050
051    @Param(name = "id")
052    protected String id;
053
054    @Context
055    protected DocumentRoutingEngineService documentRoutingEngineService;
056
057    @OperationMethod
058    public void cancelWorkflow() {
059        DocumentModel workflowInstance;
060        try {
061            workflowInstance = session.getDocument(new IdRef(id));
062        } catch (DocumentNotFoundException e) {
063            e.addInfo("Cannot get workflow instance with id: " + id);
064            throw e;
065        }
066        documentRoutingEngineService.cancel(workflowInstance.getAdapter(DocumentRoute.class), session);
067    }
068}