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 * mcedica 018 */ 019package org.nuxeo.ecm.platform.routing.api.operation; 020 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.automation.core.annotations.Param; 031import org.nuxeo.ecm.core.api.CoreSession; 032import org.nuxeo.ecm.core.api.DocumentModel; 033import org.nuxeo.ecm.core.api.NuxeoException; 034import org.nuxeo.ecm.core.api.propertiesmapping.PropertiesMappingService; 035import org.nuxeo.ecm.platform.routing.api.DocumentRoutingService; 036import org.nuxeo.ecm.platform.task.Task; 037 038/** 039 * Applies the mapping passed as parameter on the input task document. The sourceDoc in the mapping is the input 040 * document in the workflow. 041 * 042 * @since 5.6 043 */ 044@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. " 045 + "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" }) 046public class MapPropertiesOnTaskOperation { 047 048 public static final String ID = "Task.ApplyDocumentMapping"; 049 050 private static Log log = LogFactory.getLog(MapPropertiesOnTaskOperation.class); 051 052 @Context 053 protected CoreSession session; 054 055 @Context 056 protected OperationContext ctx; 057 058 @Param(name = "mappingName", required = true) 059 protected String mappingName; 060 061 @Context 062 protected DocumentRoutingService routing; 063 064 @Context 065 protected PropertiesMappingService mappingService; 066 067 @OperationMethod 068 public DocumentModel run(DocumentModel taskDoc) { 069 Task task = taskDoc.getAdapter(Task.class); 070 if (task == null) { 071 throw new NuxeoException("Input document is not a Task"); 072 } 073 List<DocumentModel> docs = routing.getWorkflowInputDocuments(session, task); 074 if (docs.size() == 0) { 075 throw new NuxeoException("Can not fetch the input documents in the related workflow instance"); 076 } 077 if (docs.size() > 1) { 078 log.warn("Using as mapping source only the first document in the input documents in the workflow"); 079 } 080 mappingService.mapProperties(session, docs.get(0), taskDoc, mappingName); 081 return taskDoc; 082 } 083}