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