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.QueryParam;
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.routing.core.io.TaskCompletionRequest;
040import org.nuxeo.ecm.platform.task.Task;
041import org.nuxeo.ecm.platform.task.TaskConstants;
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")
050public class TaskObject extends DefaultObject {
051
052    public static final String BASE_QUERY = String.format(
053            "SELECT * FROM Document WHERE ecm:mixinType = '%s' AND ecm:currentLifeCycleState = '%s'",
054            TaskConstants.TASK_FACET_NAME, TaskConstants.TASK_OPENED_LIFE_CYCLE_STATE);
055
056    @PUT
057    @Path("{taskId}/reassign")
058    @Consumes
059    public Response reassignTask(@PathParam("taskId") String taskId, @QueryParam("actors") List<String> actors,
060            @QueryParam("comment") String comment) {
061        Framework.getService(DocumentRoutingService.class).reassignTask(getContext().getCoreSession(), taskId,
062                actors, comment);
063        return Response.ok().status(Status.OK).build();
064    }
065
066    @PUT
067    @Path("{taskId}/delegate")
068    public Response delegateTask(@PathParam("taskId") String taskId,
069            @QueryParam("delegatedActors") List<String> delegatedActors, @QueryParam("comment") String comment) {
070        Framework.getService(DocumentRoutingService.class).delegateTask(getContext().getCoreSession(), taskId,
071                delegatedActors, comment);
072        return Response.ok().status(Status.OK).build();
073    }
074
075    @PUT
076    @Path("{taskId}/{taskAction}")
077    public Response completeTask(@PathParam("taskId") String taskId, @PathParam("taskAction") String action,
078            TaskCompletionRequest taskCompletionRequest) {
079        Map<String, Object> data = taskCompletionRequest.getDataMap();
080        CoreSession session = getContext().getCoreSession();
081        Framework.getService(DocumentRoutingService.class).endTask(session,
082                session.getDocument(new IdRef(taskId)).getAdapter(Task.class), data, action);
083        Task completedTask = session.getDocument(new IdRef(taskId)).getAdapter(Task.class);
084        return Response.ok(completedTask).status(Status.OK).build();
085    }
086
087    @GET
088    public List<Task> getUserRelatedWorkflowTasks(@QueryParam("userId") String userId,
089            @QueryParam("workflowInstanceId") String workflowInstanceId,
090            @QueryParam("workflowModelName") String workflowModelName) {
091        return Framework.getService(DocumentRoutingService.class).getTasks(null, userId, workflowInstanceId,
092                workflowModelName, getContext().getCoreSession());
093    }
094
095    @GET
096    @Path("{taskId}")
097    public Task getTaskById(@PathParam("taskId") String taskId) {
098        DocumentModel docModel = getContext().getCoreSession().getDocument(new IdRef(taskId));
099        return docModel.getAdapter(Task.class);
100    }
101
102}