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;
020
021import java.util.List;
022import java.util.Map;
023
024import javax.ws.rs.Consumes;
025import javax.ws.rs.GET;
026import javax.ws.rs.PUT;
027import javax.ws.rs.Path;
028import javax.ws.rs.PathParam;
029import javax.ws.rs.Produces;
030import javax.ws.rs.QueryParam;
031import javax.ws.rs.core.MediaType;
032import javax.ws.rs.core.Response;
033import javax.ws.rs.core.Response.Status;
034
035import org.nuxeo.ecm.core.api.CoreSession;
036import org.nuxeo.ecm.core.api.DocumentModel;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
039import org.nuxeo.ecm.platform.task.Task;
040import org.nuxeo.ecm.platform.task.TaskConstants;
041import org.nuxeo.ecm.restapi.server.jaxrs.routing.model.TaskCompletionRequest;
042import org.nuxeo.ecm.webengine.model.WebObject;
043import org.nuxeo.ecm.webengine.model.impl.DefaultObject;
044import org.nuxeo.runtime.api.Framework;
045
046/**
047 * @since 7.2
048 */
049@WebObject(type = "task")
050@Produces(MediaType.APPLICATION_JSON)
051public class TaskObject extends DefaultObject {
052
053    public static final String BASE_QUERY = String.format(
054            "SELECT * FROM Document WHERE ecm:mixinType = '%s' AND ecm:currentLifeCycleState = '%s'",
055            TaskConstants.TASK_FACET_NAME, TaskConstants.TASK_OPENED_LIFE_CYCLE_STATE);
056
057    @PUT
058    @Path("{taskId}/reassign")
059    @Consumes
060    public Response reassignTask(@PathParam("taskId") String taskId, @QueryParam("actors") List<String> actors,
061            @QueryParam("comment") String comment) {
062        Framework.getLocalService(DocumentRoutingService.class).reassignTask(getContext().getCoreSession(), taskId,
063                actors, comment);
064        return Response.ok().status(Status.OK).build();
065    }
066
067    @PUT
068    @Path("{taskId}/delegate")
069    @Consumes
070    public Response delegateTask(@PathParam("taskId") String taskId, @QueryParam("delegatedActors") List<String> delegatedActors,
071            @QueryParam("comment") String comment) {
072        Framework.getLocalService(DocumentRoutingService.class).delegateTask(getContext().getCoreSession(), taskId,
073                delegatedActors, comment);
074        return Response.ok().status(Status.OK).build();
075    }
076
077    @PUT
078    @Path("{taskId}/{taskAction}")
079    @Consumes({ MediaType.APPLICATION_JSON, "application/json+nxentity" })
080    @Produces(MediaType.APPLICATION_JSON)
081    public Response completeTask(@PathParam("taskId") String taskId, @PathParam("taskAction") String action,
082            TaskCompletionRequest taskCompletionRequest) {
083        Map<String, Object> data = taskCompletionRequest.getDataMap();
084        CoreSession session = getContext().getCoreSession();
085        Framework.getLocalService(DocumentRoutingService.class).endTask(session,
086                session.getDocument(new IdRef(taskId)).getAdapter(Task.class), data, action);
087        Task completedTask = session.getDocument(new IdRef(taskId)).getAdapter(Task.class);
088        return Response.ok(completedTask).status(Status.OK).build();
089    }
090
091    @GET
092    public List<Task> getUserRelatedWorkflowTasks(@QueryParam("userId") String userId,
093            @QueryParam("workflowInstanceId") String workflowInstanceId,
094            @QueryParam("workflowModelName") String workflowModelName) {
095        return Framework.getService(DocumentRoutingService.class).getTasks(null, userId,
096                workflowInstanceId, workflowModelName, getContext().getCoreSession());
097    }
098
099    @GET
100    @Path("{taskId}")
101    public Task getTaskById(@PathParam("taskId") String taskId) {
102        DocumentModel docModel = getContext().getCoreSession().getDocument(new IdRef(taskId));
103        return docModel.getAdapter(Task.class);
104    }
105
106}