001/*
002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Mariana Cedica
016 */
017package org.nuxeo.ecm.platform.routing.core.api.operation;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021import org.nuxeo.ecm.automation.core.Constants;
022import org.nuxeo.ecm.automation.core.annotations.Context;
023import org.nuxeo.ecm.automation.core.annotations.Operation;
024import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
025import org.nuxeo.ecm.automation.core.annotations.Param;
026import org.nuxeo.ecm.core.api.CoreSession;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.DocumentNotFoundException;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
031import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEngineService;
032
033/**
034 * Cancels the workflow with the given id, where the id is the id of the document representing the workflow instance.
035 *
036 * @since 5.7.2
037 */
038@Operation(id = CancelWorkflowOperation.ID, category = Constants.CAT_WORKFLOW, label = "Cancel workflow", requires = Constants.WORKFLOW_CONTEXT, description = "Cancel the workflow with the given id, "
039        + "where the required id is the id of the document representing the workflow instance.", aliases = { "Context.CancelWorkflow" })
040public class CancelWorkflowOperation {
041
042    public static final String ID = "WorkflowInstance.Cancel";
043
044    private static final Log log = LogFactory.getLog(CancelWorkflowOperation.class);
045
046    @Context
047    protected CoreSession session;
048
049    @Param(name = "id")
050    protected String id;
051
052    @Context
053    protected DocumentRoutingEngineService documentRoutingEngineService;
054
055    @OperationMethod
056    public void cancelWorkflow() {
057        DocumentModel workflowInstance;
058        try {
059            workflowInstance = session.getDocument(new IdRef(id));
060        } catch (DocumentNotFoundException e) {
061            e.addInfo("Cannot get workflow instance with id: " + id);
062            throw e;
063        }
064        documentRoutingEngineService.cancel(workflowInstance.getAdapter(DocumentRoute.class), session);
065    }
066}