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