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.Arrays;
024import java.util.List;
025
026import javax.ws.rs.GET;
027import javax.ws.rs.POST;
028import javax.ws.rs.Path;
029import javax.ws.rs.PathParam;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.Response.Status;
032
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        final String workflowInstanceId = Framework.getService(DocumentRoutingService.class).createNewInstance(
055                routingRequest.getWorkflowModelName(), Arrays.asList(new String[] { doc.getId() }), ctx.getCoreSession(),
056                true);
057        DocumentModel result = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId));
058        DocumentRoute route = result.getAdapter(DocumentRoute.class);
059        return Response.ok(route).status(Status.CREATED).build();
060    }
061
062    @GET
063    public List<DocumentRoute> doGet() {
064        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
065        return Framework.getService(DocumentRoutingService.class).getDocumentRelatedWorkflows(doc,
066                getContext().getCoreSession());
067    }
068
069    @GET
070    @Path("{workflowInstanceId}/task")
071    public List<Task> doGetTasks(@PathParam("workflowInstanceId") String workflowInstanceId) {
072        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
073        return Framework.getService(DocumentRoutingService.class).getTasks(doc,
074                null , workflowInstanceId, null, getContext().getCoreSession());
075    }
076
077}