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.servlet.http.HttpServletRequest;
027import javax.ws.rs.Consumes;
028import javax.ws.rs.GET;
029import javax.ws.rs.PUT;
030import javax.ws.rs.Path;
031import javax.ws.rs.PathParam;
032import javax.ws.rs.QueryParam;
033import javax.ws.rs.core.Response;
034import javax.ws.rs.core.Response.Status;
035
036import org.apache.commons.lang3.StringUtils;
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.query.api.PageProviderDefinition;
041import org.nuxeo.ecm.platform.query.api.PageProviderService;
042import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
043import org.nuxeo.ecm.platform.routing.core.io.TaskCompletionRequest;
044import org.nuxeo.ecm.platform.task.Task;
045import org.nuxeo.ecm.platform.task.TaskConstants;
046import org.nuxeo.ecm.restapi.server.jaxrs.PaginableObject;
047import org.nuxeo.ecm.webengine.model.WebObject;
048import org.nuxeo.runtime.api.Framework;
049
050/**
051 * @since 7.2
052 */
053@WebObject(type = "task")
054public class TaskObject extends PaginableObject<Task> {
055
056    /**
057     * @since 11.1
058     */
059    public static final String PAGE_PROVIDER_NAME = "nuxeo_tasks_listing";
060
061    public static final String BASE_QUERY = String.format(
062            "SELECT * FROM Document WHERE ecm:mixinType = '%s' AND ecm:currentLifeCycleState = '%s'",
063            TaskConstants.TASK_FACET_NAME, TaskConstants.TASK_OPENED_LIFE_CYCLE_STATE);
064
065    protected String userId;
066
067    protected String workflowInstanceId;
068
069    /**
070     * For backward compatibility only. This parameter forces post-filtering and prevents paginated results.
071     * @deprecated since 11.1
072     */
073    @Deprecated
074    protected String workflowModelName;
075
076    @Override
077    protected void initialize(Object... args) {
078        super.initialize(args);
079        HttpServletRequest request = ctx.getRequest();
080        userId = request.getParameter("userId");
081        workflowInstanceId = request.getParameter("workflowInstanceId");
082        workflowModelName = request.getParameter("workflowModelName");
083    }
084
085    @PUT
086    @Path("{taskId}/reassign")
087    @Consumes
088    public Response reassignTask(@PathParam("taskId") String taskId, @QueryParam("actors") List<String> actors,
089            @QueryParam("comment") String comment) {
090        Framework.getService(DocumentRoutingService.class).reassignTask(getContext().getCoreSession(), taskId,
091                actors, comment);
092        return Response.ok().status(Status.OK).build();
093    }
094
095    @PUT
096    @Path("{taskId}/delegate")
097    public Response delegateTask(@PathParam("taskId") String taskId,
098            @QueryParam("delegatedActors") List<String> delegatedActors, @QueryParam("comment") String comment) {
099        Framework.getService(DocumentRoutingService.class).delegateTask(getContext().getCoreSession(), taskId,
100                delegatedActors, comment);
101        return Response.ok().status(Status.OK).build();
102    }
103
104    @PUT
105    @Path("{taskId}/{taskAction}")
106    public Response completeTask(@PathParam("taskId") String taskId, @PathParam("taskAction") String action,
107            TaskCompletionRequest taskCompletionRequest) {
108        Map<String, Object> data = taskCompletionRequest.getDataMap();
109        CoreSession session = getContext().getCoreSession();
110        Framework.getService(DocumentRoutingService.class).endTask(session,
111                session.getDocument(new IdRef(taskId)).getAdapter(Task.class), data, action);
112        Task completedTask = session.getDocument(new IdRef(taskId)).getAdapter(Task.class);
113        return Response.ok(completedTask).status(Status.OK).build();
114    }
115
116    @GET
117    public List<Task> getUserRelatedWorkflowTasks() {
118        if (StringUtils.isNotBlank(workflowModelName)) {
119            return Framework.getService(DocumentRoutingService.class).getTasks(null, userId, workflowInstanceId,
120                    workflowModelName, getContext().getCoreSession());
121        } else {
122            return getPaginableEntries();
123        }
124    }
125
126    @GET
127    @Path("{taskId}")
128    public Task getTaskById(@PathParam("taskId") String taskId) {
129        DocumentModel docModel = getContext().getCoreSession().getDocument(new IdRef(taskId));
130        return docModel.getAdapter(Task.class);
131    }
132
133    @Override
134    protected PageProviderDefinition getPageProviderDefinition() {
135        PageProviderService pageProviderService = Framework.getService(PageProviderService.class);
136        return pageProviderService.getPageProviderDefinition(PAGE_PROVIDER_NAME);
137    }
138
139    @Override
140    protected Object[] getParams() {
141        return new Object[] { userId, workflowInstanceId };
142    }
143
144}