001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.ecm.restapi.server.jaxrs.routing.adapter;
020
021import java.util.Arrays;
022import java.util.List;
023
024import javax.ws.rs.GET;
025import javax.ws.rs.POST;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.core.Response;
029import javax.ws.rs.core.Response.Status;
030
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.IdRef;
033import org.nuxeo.ecm.platform.routing.api.DocumentRoute;
034import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
035import org.nuxeo.ecm.platform.task.Task;
036import org.nuxeo.ecm.restapi.server.jaxrs.routing.model.WorkflowRequest;
037import org.nuxeo.ecm.webengine.model.WebAdapter;
038import org.nuxeo.ecm.webengine.model.impl.DefaultAdapter;
039import org.nuxeo.runtime.api.Framework;
040
041/**
042 * @since 7.2
043 */
044@WebAdapter(name = WorkflowAdapter.NAME, type = "workflowAdapter")
045public class WorkflowAdapter extends DefaultAdapter {
046
047    public static final String NAME = "workflow";
048
049    @POST
050    public Response doPost(WorkflowRequest routingRequest) {
051        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
052        final String workflowInstanceId = Framework.getLocalService(DocumentRoutingService.class).createNewInstance(
053                routingRequest.getWorkflowModelName(), Arrays.asList(new String[] { doc.getId() }), ctx.getCoreSession(),
054                true);
055        DocumentModel result = getContext().getCoreSession().getDocument(new IdRef(workflowInstanceId));
056        return Response.ok(result).status(Status.CREATED).build();
057    }
058
059    @GET
060    public List<DocumentRoute> doGet() {
061        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
062        return Framework.getLocalService(DocumentRoutingService.class).getDocumentRelatedWorkflows(doc,
063                getContext().getCoreSession());
064    }
065
066    @GET
067    @Path("{workflowInstanceId}/task")
068    public List<Task> doGetTasks(@PathParam("workflowInstanceId") String workflowInstanceId) {
069        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
070        return Framework.getLocalService(DocumentRoutingService.class).getTasks(doc,
071                null , workflowInstanceId, null, getContext().getCoreSession());
072    }
073
074}