001/*
002 * (C) Copyright 2014 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 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
018 *
019 */
020
021package org.nuxeo.ecm.restapi.server.jaxrs.routing;
022
023import java.util.List;
024
025import javax.ws.rs.DELETE;
026import javax.ws.rs.GET;
027import javax.ws.rs.POST;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030import javax.ws.rs.Produces;
031import javax.ws.rs.QueryParam;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.core.Response;
034import javax.ws.rs.core.Response.Status;
035
036import org.apache.commons.logging.Log;
037import org.apache.commons.logging.LogFactory;
038import org.nuxeo.ecm.core.api.CoreSession;
039import org.nuxeo.ecm.core.api.DocumentModel;
040import org.nuxeo.ecm.core.api.IdRef;
041import org.nuxeo.ecm.core.api.NuxeoException;
042import org.nuxeo.ecm.core.api.NuxeoPrincipal;
043import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
044import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
045import org.nuxeo.ecm.platform.routing.core.api.DocumentRoutingEngineService;
046import org.nuxeo.ecm.platform.routing.core.impl.jsongraph.JsonGraphRoute;
047import org.nuxeo.ecm.platform.routing.core.io.WorkflowRequest;
048import org.nuxeo.ecm.webengine.model.WebObject;
049import org.nuxeo.ecm.webengine.model.exceptions.WebSecurityException;
050import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
051import org.nuxeo.runtime.api.Framework;
052
053/**
054 * @since 7.2
055 */
056@WebObject(type = "workflow")
057@Produces(MediaType.APPLICATION_JSON)
058public class WorkflowObject extends DefaultObject {
059
060    private static Log log = LogFactory.getLog(WorkflowObject.class);
061
062    private DocumentRoutingService documentRoutingService;
063
064    @Override
065    protected void initialize(Object... args) {
066        documentRoutingService = Framework.getService(DocumentRoutingService.class);
067    }
068
069    @POST
070    public Response createWorkflowInstance(WorkflowRequest workflowRequest) {
071        CoreSession session = ctx.getCoreSession();
072        List<String> attachedDocumentIds = workflowRequest.getAttachedDocumentIds();
073        String workflowModelName = workflowRequest.getWorkflowModelName();
074        if (documentRoutingService.canCreateInstance(session, attachedDocumentIds, workflowModelName)) {
075            String workflowInstanceId = documentRoutingService.createNewInstance(workflowModelName,
076                    attachedDocumentIds, workflowRequest.getVariables(), session, true);
077            DocumentModel workflowInstance = session.getDocument(new IdRef(workflowInstanceId));
078            DocumentRoute route = workflowInstance.getAdapter(DocumentRoute.class);
079            return Response.ok(route).status(Status.CREATED).build();
080        } else {
081            return Response.status(Status.BAD_REQUEST).build();
082        }
083    }
084
085    @GET
086    @Path("{workflowInstanceId}")
087    public DocumentRoute getWorkflowInstance(@PathParam("workflowInstanceId") String workflowInstanceId) {
088        DocumentModel workflowInstance;
089        try {
090            workflowInstance = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId));
091            return workflowInstance.getAdapter(DocumentRoute.class);
092        } catch (NuxeoException e) {
093            e.addInfo("Can not get workflow instance with id: " + workflowInstanceId);
094            throw e;
095        }
096    }
097
098    @GET
099    @Path("{workflowInstanceId}/graph")
100    public JsonGraphRoute getWorkflowGraph(@PathParam("workflowInstanceId") String workflowInstanceId) {
101        try {
102            return new JsonGraphRoute(getContext().getCoreSession(), workflowInstanceId, getContext().getLocale());
103        } catch (NuxeoException e) {
104            e.addInfo("Can not get workflow instance graph with id: " + workflowInstanceId);
105            throw e;
106        }
107    }
108
109    @DELETE
110    @Path("{workflowInstanceId}")
111    public Response cancelWorkflowInstance(@PathParam("workflowInstanceId") String workflowInstanceId) {
112        DocumentModel workflowInstance;
113        DocumentRoute route;
114        try {
115            workflowInstance = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId));
116            route = workflowInstance.getAdapter(DocumentRoute.class);
117            checkCancelGuards(route);
118        } catch (NuxeoException e) {
119            e.addInfo("Can not get workflow instance with id: " + workflowInstanceId);
120            throw e;
121        }
122        Framework.getService(DocumentRoutingEngineService.class).cancel(route, getContext().getCoreSession());
123        return Response.ok().status(Status.NO_CONTENT).build();
124    }
125
126    protected void checkCancelGuards(DocumentRoute route) {
127        NuxeoPrincipal currentUser = getContext().getCoreSession().getPrincipal();
128        if (currentUser.isAdministrator() || currentUser.isMemberOf("powerusers")) {
129            return;
130        }
131        if (currentUser.getName().equals(route.getInitiator())) {
132            return;
133        }
134        throw new WebSecurityException("You don't have the permission to cancel this workflow");
135    }
136
137    @GET
138    public List<DocumentRoute> getRunningWorkflowInstancesLaunchedByCurrentUser(
139            @QueryParam("workflowModelName") String worflowModelName) {
140        return documentRoutingService.getRunningWorkflowInstancesLaunchedByCurrentUser(getContext().getCoreSession(),
141                worflowModelName);
142    }
143
144}