001/*
002 * (C) Copyright 2006-2007 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 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.automation.seam.operations;
021
022import org.nuxeo.ecm.automation.AutomationService;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.OperationException;
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.jsf.OperationHelper;
031
032/**
033 * Execute an operation within the Seam context (doing automatically the needed init and cleanup)
034 *
035 * @author Tiry (tdelprat@nuxeo.com)
036 */
037@Operation(id = RunOperationInSeam.ID, category = Constants.CAT_UI, label = "Run operation in Seam Context", description = "Initialize a Seam context (including Conversation if needed) and runs an Operation", aliases = { "Seam.RunOperation" })
038public class RunOperationInSeam {
039
040    public static final String ID = "WebUI.RunOperationInSeam";
041
042    @Context
043    protected OperationContext ctx;
044
045    @Param(name = "conversationId", required = false)
046    protected String conversationId;
047
048    @Context
049    protected AutomationService service;
050
051    @Param(name = "id")
052    protected String chainId;
053
054    @Param(name = "isolate", required = false, values = "false")
055    protected boolean isolate = false;
056
057    @OperationMethod
058    public Object run() throws OperationException {
059        try (OperationContext subctx = ctx.getSubContext(isolate)) {
060            if (!OperationHelper.isSeamContextAvailable()) {
061                SeamOperationFilter.handleBeforeRun(ctx, conversationId);
062                try {
063                    return runChain(subctx);
064                } finally {
065                    SeamOperationFilter.handleAfterRun(ctx, conversationId);
066                }
067            } else {
068                return runChain(subctx);
069            }
070        }
071    }
072
073    protected Object runChain(OperationContext subctx) throws OperationException {
074        return service.run(subctx, chainId.startsWith("Chain.") ? chainId.substring(6) : chainId);
075    }
076}