001/*
002 * (C) Copyright 2013 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 */
019package org.nuxeo.ecm.platform.routing.core.api.operation;
020
021import java.io.IOException;
022
023import org.apache.commons.logging.Log;
024import org.apache.commons.logging.LogFactory;
025import org.nuxeo.ecm.automation.core.Constants;
026import org.nuxeo.ecm.automation.core.annotations.Context;
027import org.nuxeo.ecm.automation.core.annotations.Operation;
028import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
029import org.nuxeo.ecm.automation.core.annotations.Param;
030import org.nuxeo.ecm.automation.core.util.DocumentHelper;
031import org.nuxeo.ecm.automation.core.util.Properties;
032import org.nuxeo.ecm.core.api.CoreSession;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.core.api.IdRef;
035import org.nuxeo.ecm.core.api.NuxeoException;
036import org.nuxeo.ecm.core.api.PropertyException;
037
038/**
039 * Set a list of variable on the workflow instance with the given id
040 *
041 * @since 5.7.2
042 */
043@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."
044        + "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."
045        + "The xpath is given by the prefix of the schema storing the variables (in the \"Workflow Variables\" tab) and the variable name."
046        + "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, "
047        + "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>")
048public class SetWorkflowVariablesOperation {
049
050    public static final String ID = "Workflow.SetWorkflowVariables";
051
052    private static Log log = LogFactory.getLog(SetWorkflowVariablesOperation.class);
053
054    @Context
055    protected CoreSession session;
056
057    @Param(name = "id", required = true)
058    protected String id;
059
060    @Param(name = "properties", required = true)
061    protected Properties properties;
062
063    @OperationMethod
064    public void run() {
065        DocumentModel workflowInstance = session.getDocument(new IdRef(id));
066        try {
067            DocumentHelper.setProperties(session, workflowInstance, properties);
068        } catch (PropertyException e) {
069            e.addInfo("Cannot set properties on workflow instance with the id: " + id);
070            throw e;
071        } catch (IOException e) {
072            throw new NuxeoException("Cannot set properties on workflow instance with the id: " + id, e);
073        }
074    }
075}