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