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 org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.ecm.automation.AutomationService;
025import org.nuxeo.ecm.automation.OperationContext;
026import org.nuxeo.ecm.automation.OperationException;
027import org.nuxeo.ecm.automation.core.Constants;
028import org.nuxeo.ecm.automation.core.annotations.Context;
029import org.nuxeo.ecm.automation.core.annotations.Operation;
030import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
031import org.nuxeo.ecm.automation.core.annotations.Param;
032import org.nuxeo.ecm.automation.core.util.Properties;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.NuxeoException;
035import org.nuxeo.runtime.transaction.TransactionHelper;
036
037/**
038 * @deprecated since 6.0. Use instead {@link RunDocumentChain} with ID 'Context.RunDocumentOperation'. Operation to run
039 *             an operation chain in a separate transaction. The existing transaction is committed before running the
040 *             new transaction.
041 * @since 5.6
042 */
043@Deprecated
044@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")
045public class RunInNewTransaction {
046
047    public static final String ID = "Context.RunDocumentOperationInNewTx";
048
049    private static final Log log = LogFactory.getLog(RunInNewTransaction.class);
050
051    @Context
052    protected OperationContext ctx;
053
054    @Context
055    protected AutomationService service;
056
057    @Context
058    protected CoreSession session;
059
060    @Param(name = "id")
061    protected String chainId;
062
063    @Param(name = "isolate", required = false, values = "false")
064    protected boolean isolate = false;
065
066    @Param(name = "rollbackGlobalOnError", required = false, values = "false")
067    protected boolean rollbackGlobalOnError = false;
068
069    @Param(name = "parameters", description = "Accessible in the subcontext ChainParameters. For instance, @{ChainParameters['parameterKey']}.", required = false)
070    protected Properties chainParameters = new Properties();
071
072    @Param(name = "timeout", required = false)
073    protected Integer timeout = Integer.valueOf(60);
074
075    @OperationMethod
076    public void run() throws OperationException {
077        // if the current transaction was already marked for rollback, do nothing
078        if (TransactionHelper.isTransactionMarkedRollback()) {
079            return;
080        }
081        try {
082            TransactionHelper.runInNewTransaction(() -> {
083                try (OperationContext subctx = ctx.getSubContext(isolate)) {
084                    try {
085                        service.run(subctx, chainId, chainParameters);
086                    } catch (OperationException e) {
087                        if (rollbackGlobalOnError) {
088                            throw new NuxeoException(e);
089                        } else {
090                            // just log, no rethrow
091                            log.error("Error while executing operation " + chainId, e);
092                        }
093                    }
094                } catch (OperationException e) {
095                    throw new NuxeoException(e);
096                }
097            });
098        } catch (NuxeoException e) {
099            if (e.getCause() instanceof OperationException) {
100                throw (OperationException) e.getCause();
101            }
102            throw e;
103        }
104    }
105
106}