001/*
002 * Copyright (c) 2013 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 *     Mariana Cedica
011 */
012package org.nuxeo.ecm.platform.routing.core.api.operation;
013
014import java.io.IOException;
015
016import org.apache.commons.logging.Log;
017import org.apache.commons.logging.LogFactory;
018import org.nuxeo.ecm.automation.core.Constants;
019import org.nuxeo.ecm.automation.core.annotations.Context;
020import org.nuxeo.ecm.automation.core.annotations.Operation;
021import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
022import org.nuxeo.ecm.automation.core.annotations.Param;
023import org.nuxeo.ecm.automation.core.util.DocumentHelper;
024import org.nuxeo.ecm.automation.core.util.Properties;
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.IdRef;
028import org.nuxeo.ecm.core.api.NuxeoException;
029import org.nuxeo.ecm.core.api.PropertyException;
030
031/**
032 * Set a list of variable on the workflow instance with the given id
033 *
034 * @since 5.7.2
035 */
036@Operation(id = SetWorkflowVariablesOperation.ID, category = Constants.CAT_WORKFLOW, label = "Set workflow variables", requires = Constants.WORKFLOW_CONTEXT, description = "Set a list of variable on the workflow instance with the given id."
037        + "The variables are specified as <i>key=value</i> pairs separated by a new line. The key used for a variable is the property xpath."
038        + "The xpath is given by the prefix of the schema storing the variables (in the \"Workflow Variables\" tab) and the variable name."
039        + "To specify multi-line values you can use a \\ character followed by a new line. <p>Example:<pre>dc:title=The Document Title<br>dc:description=foo bar</pre>For updating a date, you will need to expose the value as ISO 8601 format, "
040        + "for instance : <p>Example:<pre>dc:title=The Document Title<br>dc:issued=@{org.nuxeo.ecm.core.schema.utils.DateParser.formatW3CDateTime(CurrentDate.date)}</pre><p>")
041public class SetWorkflowVariablesOperation {
042
043    public static final String ID = "Workflow.SetWorkflowVariables";
044
045    private static Log log = LogFactory.getLog(SetWorkflowVariablesOperation.class);
046
047    @Context
048    protected CoreSession session;
049
050    @Param(name = "id", required = true)
051    protected String id;
052
053    @Param(name = "properties", required = true)
054    protected Properties properties;
055
056    @OperationMethod
057    public void run() {
058        DocumentModel workflowInstance = session.getDocument(new IdRef(id));
059        try {
060            DocumentHelper.setProperties(session, workflowInstance, properties);
061        } catch (PropertyException e) {
062            e.addInfo("Cannot set properties on workflow instance with the id: " + id);
063            throw e;
064        } catch (IOException e) {
065            throw new NuxeoException("Cannot set properties on workflow instance with the id: " + id, e);
066        }
067    }
068}