001/*
002 * Copyright (c) 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     mcedica
011 */
012package org.nuxeo.ecm.platform.routing.api.operation;
013
014import java.util.List;
015
016import org.apache.commons.logging.Log;
017import org.apache.commons.logging.LogFactory;
018import org.nuxeo.ecm.automation.OperationContext;
019import org.nuxeo.ecm.automation.core.Constants;
020import org.nuxeo.ecm.automation.core.annotations.Context;
021import org.nuxeo.ecm.automation.core.annotations.Operation;
022import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
023import org.nuxeo.ecm.automation.core.annotations.Param;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoException;
027import org.nuxeo.ecm.core.api.propertiesmapping.PropertiesMappingService;
028import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService;
029import org.nuxeo.ecm.platform.task.Task;
030
031/**
032 * Applies the mapping passed as parameter on the input task document. The sourceDoc in the mapping is the input
033 * document in the workflow.
034 *
035 * @since 5.6
036 */
037@Operation(id = MapPropertiesOnTaskOperation.ID, category = Constants.CAT_WORKFLOW, label = "Apply mapping on input task doc", requires = Constants.WORKFLOW_CONTEXT, description = "Applies the mapping passed in parameter on the task document. "
038        + "The sourceDoc in the mapping is the input document in the workflow. The operation throws a NuxeoException if the input document is not a Task.", aliases = { "Context.ApplyMappingOnTask" })
039public class MapPropertiesOnTaskOperation {
040
041    public static final String ID = "Task.ApplyDocumentMapping";
042
043    private static Log log = LogFactory.getLog(MapPropertiesOnTaskOperation.class);
044
045    @Context
046    protected CoreSession session;
047
048    @Context
049    protected OperationContext ctx;
050
051    @Param(name = "mappingName", required = true)
052    protected String mappingName;
053
054    @Context
055    protected DocumentRoutingService routing;
056
057    @Context
058    protected PropertiesMappingService mappingService;
059
060    @OperationMethod
061    public DocumentModel run(DocumentModel taskDoc) {
062        Task task = taskDoc.getAdapter(Task.class);
063        if (task == null) {
064            throw new NuxeoException("Input document is not a Task");
065        }
066        List<DocumentModel> docs = routing.getWorkflowInputDocuments(session, task);
067        if (docs.size() == 0) {
068            throw new NuxeoException("Can not fetch the input documents in the related workflow instance");
069        }
070        if (docs.size() > 1) {
071            log.warn("Using as mapping source only the first document in the input documents in the workflow");
072        }
073        mappingService.mapProperties(session, docs.get(0), taskDoc, mappingName);
074        return taskDoc;
075    }
076}