001/*
002 * (C) Copyright 2012 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 *     ldoguin
018 */
019
020package org.nuxeo.ecm.platform.routing.dm.operation;
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.automation.OperationContext;
028import org.nuxeo.ecm.automation.core.Constants;
029import org.nuxeo.ecm.automation.core.annotations.Context;
030import org.nuxeo.ecm.automation.core.annotations.Operation;
031import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.DocumentModelList;
035import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
036import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
037import org.nuxeo.ecm.platform.routing.dm.operation.CreateRoutingTask.OperationTaskVariableName;
038import org.nuxeo.ecm.platform.task.Task;
039import org.nuxeo.ecm.platform.task.TaskService;
040
041/**
042 * This operation removes all the tasks created when a given step is run
043 *
044 * @author mcedica
045 * @since 5.6
046 * @deprecated since 5.9.2 - Use only routes of type 'graph'
047 */
048@Deprecated
049@Operation(id = RemoveRoutingTask.ID, category = Constants.CAT_SERVICES, label = "remove task", since = "5.6", description = " This operation removes all the tasks created when a given step is run.", addToStudio = false)
050public class RemoveRoutingTask {
051
052    public static final String ID = "Workflow.RemoveRoutingTask";
053
054    private static final Log log = LogFactory.getLog(RemoveRoutingTask.class);
055
056    @Context
057    protected OperationContext ctx;
058
059    @Context
060    protected CoreSession coreSession;
061
062    @Context
063    protected TaskService taskService;
064
065    @OperationMethod
066    public void removeTasks() {
067        List<Task> tasks = fetchTasks();
068        for (Task task : tasks) {
069            taskService.deleteTask(coreSession, task.getId());
070        }
071    }
072
073    protected List<Task> fetchTasks() {
074        List<Task> tasks = new ArrayList<Task>();
075        DocumentModelList docList = (DocumentModelList) ctx.get(OperationTaskVariableName.taskDocuments.name());
076        if (docList != null) {
077            for (DocumentModel documentModel : docList) {
078                tasks.add(documentModel.getAdapter(Task.class));
079            }
080        }
081        if (tasks.isEmpty()) {
082            tasks = fetchTasksFromStep();
083        }
084        return tasks;
085    }
086
087    protected List<Task> fetchTasksFromStep() {
088        List<Task> tasks = new ArrayList<Task>();
089        DocumentRouteStep step = (DocumentRouteStep) ctx.get(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY);
090        DocumentModelList list = coreSession.query(String.format(
091                "Select * from TaskDoc WHERE nt:task_variables/*/key like '%s' AND nt:task_variables/*/value like '%s'",
092                DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, step.getDocument().getId()));
093        for (DocumentModel documentModel : list) {
094            tasks.add(documentModel.getAdapter(Task.class));
095        }
096        return tasks;
097    }
098}