001/*
002 * (C) Copyright 2006-2014 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 *    Florent Guillaume
019 */
020package org.nuxeo.ecm.automation.core.operations.execution;
021
022import java.util.HashMap;
023import java.util.Map;
024
025import org.apache.commons.logging.Log;
026import org.apache.commons.logging.LogFactory;
027import org.nuxeo.ecm.automation.AutomationService;
028import org.nuxeo.ecm.automation.OperationContext;
029import org.nuxeo.ecm.automation.OperationException;
030import org.nuxeo.ecm.automation.core.Constants;
031import org.nuxeo.ecm.automation.core.annotations.Context;
032import org.nuxeo.ecm.automation.core.annotations.Operation;
033import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
034import org.nuxeo.ecm.automation.core.annotations.Param;
035import org.nuxeo.ecm.automation.core.util.Properties;
036import org.nuxeo.ecm.core.api.CoreSession;
037import org.nuxeo.ecm.core.api.DocumentModel;
038import org.nuxeo.runtime.transaction.TransactionHelper;
039
040/**
041 * @deprecated since 6.0. Use instead {@link RunDocumentChain} with ID 'Context.RunDocumentOperation'. Operation to run
042 *             an operation chain in a separate transaction. The existing transaction is committed before running the
043 *             new transaction.
044 * @since 5.6
045 */
046@Deprecated
047@Operation(id = RunInNewTransaction.ID, category = Constants.CAT_SUBCHAIN_EXECUTION, label = "Run Document Chain in new Tx", description = "Run an operation chain in a separate tx. The 'parameters' injected are accessible in the subcontext ChainParameters. For instance, @{ChainParameters['parameterKey']}.", deprecatedSince = "6.0")
048public class RunInNewTransaction {
049
050    public static final String ID = "Context.RunDocumentOperationInNewTx";
051
052    private static final Log log = LogFactory.getLog(RunInNewTransaction.class);
053
054    @Context
055    protected OperationContext ctx;
056
057    @Context
058    protected AutomationService service;
059
060    @Context
061    protected CoreSession session;
062
063    @Param(name = "id")
064    protected String chainId;
065
066    @Param(name = "isolate", required = false, values = "false")
067    protected boolean isolate = false;
068
069    @Param(name = "rollbackGlobalOnError", required = false, values = "false")
070    protected boolean rollbackGlobalOnError = false;
071
072    @Param(name = "parameters", description = "Accessible in the subcontext ChainParameters. For instance, @{ChainParameters['parameterKey']}.", required = false)
073    protected Properties chainParameters;
074
075    @Param(name = "timeout", required = false)
076    protected Integer timeout = Integer.valueOf(60);
077
078    @OperationMethod
079    public void run() throws OperationException {
080        // if the current transaction was already marked for rollback, do nothing
081        if (TransactionHelper.isTransactionMarkedRollback()) {
082            return;
083        }
084        // commit the current transaction
085        TransactionHelper.commitOrRollbackTransaction();
086
087        Map<String, Object> vars = isolate ? new HashMap<String, Object>(ctx.getVars()) : ctx.getVars();
088
089        int to = timeout == null ? 0 : timeout.intValue();
090
091        TransactionHelper.startTransaction(to);
092        boolean ok = false;
093        try {
094            OperationContext subctx = new OperationContext(session, vars);
095            subctx.setInput(ctx.getInput());
096            service.run(subctx, chainId, (Map) chainParameters);
097            ok = true;
098        } catch (OperationException e) {
099            if (rollbackGlobalOnError) {
100                throw e;
101            } else {
102                // just log, no rethrow
103                log.error("Error while executing operation " + chainId, e);
104            }
105        } finally {
106            if (!ok) {
107                // will be logged by Automation framework
108                TransactionHelper.setTransactionRollbackOnly();
109            }
110            TransactionHelper.commitOrRollbackTransaction();
111            // caller expects a transaction to be started
112            TransactionHelper.startTransaction();
113        }
114
115        // reconnect documents in the context
116        if (!isolate) {
117            for (String varName : vars.keySet()) {
118                if (!ctx.getVars().containsKey(varName)) {
119                    ctx.put(varName, vars.get(varName));
120                } else {
121                    Object value = vars.get(varName);
122                    if (session != null && value != null && value instanceof DocumentModel) {
123                        ctx.getVars().put(varName, session.getDocument(((DocumentModel) value).getRef()));
124                    } else {
125                        ctx.getVars().put(varName, value);
126                    }
127                }
128            }
129        }
130    }
131
132}