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