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