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 *     Mariana Cedica
016 */
017
018package org.nuxeo.ecm.platform.routing.dm.operation;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.List;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.nuxeo.ecm.automation.OperationContext;
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.automation.core.annotations.Param;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.IdRef;
034import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
035import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
036import org.nuxeo.ecm.platform.routing.dm.adapter.RoutingTask;
037import org.nuxeo.ecm.platform.routing.dm.api.RoutingTaskConstants;
038import org.nuxeo.ecm.platform.task.TaskComment;
039
040/***
041 * Set the current running step as <document.routing.step> context variable.
042 *
043 * @author mcedica
044 * @since 5.6
045 * @deprecated since 5.9.2 - Use only routes of type 'graph'
046 */
047@Deprecated
048@Operation(id = SetCurrentRunningStepFromTask.ID, category = DocumentRoutingConstants.OPERATION_CATEGORY_ROUTING_NAME, label = "Set Current Step from Task", description = "Set the current running step as <document.routing.step> context variable. The comments from the task can be mapped to originating step.", addToStudio = false)
049public class SetCurrentRunningStepFromTask extends AbstractTaskStepOperation {
050
051    public final static String ID = "Document.Routing.SetRunningStepFromTask";
052
053    private static final Log log = LogFactory.getLog(SetCurrentRunningStepFromTask.class);
054
055    @Context
056    protected OperationContext context;
057
058    @Context
059    protected CoreSession session;
060
061    @Param(name = "mappingComments", required = false)
062    protected Boolean mappingComments = false;
063
064    @OperationMethod
065    public void setStepDocument() {
066        String stepDocumentId = getRoutingStepDocumentId(context);
067        DocumentModel docStep = session.getDocument(new IdRef(stepDocumentId));
068        if (mappingComments) {
069            mappCommentsFromTaskToStep(session, docStep);
070        }
071        context.put(DocumentRoutingConstants.OPERATION_STEP_DOCUMENT_KEY, docStep.getAdapter(DocumentRouteStep.class));
072    }
073
074    protected void mappCommentsFromTaskToStep(CoreSession session, DocumentModel docStep) {
075        List<String> comments = new ArrayList<String>();
076
077        RoutingTask task = getRoutingTask(context);
078        if (task == null) {
079            log.error("No task found on the operation context");
080            return;
081        }
082        List<TaskComment> taskComments = task.getComments();
083        for (TaskComment taskComment : taskComments) {
084            StringBuilder commentBuilder = new StringBuilder();
085            commentBuilder.append(taskComment.getAuthor());
086            commentBuilder.append(" : ");
087            commentBuilder.append(taskComment.getText());
088            comments.add(commentBuilder.toString());
089        }
090        if (docStep.hasFacet(RoutingTaskConstants.TASK_STEP_FACET_NAME)) {
091            docStep.setPropertyValue(RoutingTaskConstants.TASK_STEP_COMMENTS_PROPERTY_NAME, (Serializable) comments);
092            session.saveDocument(docStep);
093        }
094    }
095}