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
021
022import java.util.List;
023import java.util.Map;
024
025import javax.ws.rs.GET;
026import javax.ws.rs.PUT;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.QueryParam;
030import javax.ws.rs.core.Response;
031import javax.ws.rs.core.Response.Status;
032
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.IdRef;
036import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
037import org.nuxeo.ecm.platform.task.Task;
038import org.nuxeo.ecm.restapi.server.jaxrs.routing.model.TaskCompletionRequest;
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 = TaskAdapter.NAME, type = "taskAdapter")
047public class TaskAdapter extends DefaultAdapter {
048
049    public static final String NAME = "task";
050
051    @PUT
052    @Path("{taskId}/{action}")
053    public Response completeTask(@PathParam("taskId") String taskId, @PathParam("action") String action,
054            TaskCompletionRequest taskCompletionRequest) {
055        Map<String, Object> data = taskCompletionRequest.getDataMap();
056        CoreSession session = getContext().getCoreSession();
057        Framework.getLocalService(DocumentRoutingService.class).endTask(session,
058                session.getDocument(new IdRef(taskId)).getAdapter(Task.class), data, action);
059        Task completedTask = session.getDocument(new IdRef(taskId)).getAdapter(Task.class);
060        return Response.ok(completedTask).status(Status.OK).build();
061    }
062
063    @GET
064    public List<Task> doGet(@QueryParam("userId") String userId,
065            @QueryParam("workflowInstanceId") String workflowInstanceId,
066            @QueryParam("workflowModelName") String workflowModelName) {
067        DocumentModel doc = getTarget().getAdapter(DocumentModel.class);
068        return Framework.getLocalService(DocumentRoutingService.class).getTasks(doc, userId, workflowInstanceId,
069                workflowModelName, getContext().getCoreSession());
070    }
071
072}