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.adapter;
022
023import java.util.List;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.POST;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.core.Response;
030import javax.ws.rs.core.Response.Status;
031
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
036import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
037import org.nuxeo.ecm.platform.routing.core.io.WorkflowRequest;
038import org.nuxeo.ecm.platform.task.Task;
039import org.nuxeo.ecm.webengine.model.WebAdapter;
040import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
041import org.nuxeo.runtime.api.Framework;
042
043/**
044 * @since 7.2
045 */
046@WebAdapter(name = WorkflowAdapter.NAME, type = "workflowAdapter")
047public class WorkflowAdapter extends DefaultAdapter {
048
049    public static final String NAME = "workflow";
050
051    @POST
052    public Response doPost(WorkflowRequest routingRequest) {
053        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
054        CoreSession session = doc.getCoreSession();
055        String workflowModelName = routingRequest.getWorkflowModelName();
056        DocumentRoutingService documentRoutingService = Framework.getService(DocumentRoutingService.class);
057        if (documentRoutingService.canCreateInstance(session, List.of(doc.getId()), workflowModelName)) {
058            String workflowInstanceId = documentRoutingService.createNewInstance(workflowModelName,
059                    List.of(doc.getId()), routingRequest.getVariables(), session, true);
060            DocumentModel result = session.getDocument(new IdRef(workflowInstanceId));
061            DocumentRoute route = result.getAdapter(DocumentRoute.class);
062            return Response.ok(route).status(Status.CREATED).build();
063        } else {
064            return Response.status(Status.BAD_REQUEST).build();
065        }
066    }
067
068    @GET
069    public List<DocumentRoute> doGet() {
070        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
071        return Framework.getService(DocumentRoutingService.class)
072                        .getDocumentRelatedWorkflows(doc, getContext().getCoreSession());
073    }
074
075    @GET
076    @Path("{workflowInstanceId}/task")
077    public List<Task> doGetTasks(@PathParam("workflowInstanceId") String workflowInstanceId) {
078        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
079        return Framework.getService(DocumentRoutingService.class)
080                        .getTasks(doc, null, workflowInstanceId, null, getContext().getCoreSession());
081    }
082
083}