001/* 002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 */ 018package org.nuxeo.ecm.automation.seam.operations; 019 020import java.util.HashMap; 021import java.util.Map; 022 023import org.nuxeo.ecm.automation.AutomationService; 024import org.nuxeo.ecm.automation.OperationChain; 025import org.nuxeo.ecm.automation.OperationContext; 026import org.nuxeo.ecm.automation.OperationException; 027import org.nuxeo.ecm.automation.OperationParameters; 028import org.nuxeo.ecm.automation.core.Constants; 029import org.nuxeo.ecm.automation.core.annotations.Context; 030import org.nuxeo.ecm.automation.core.annotations.Operation; 031import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 032import org.nuxeo.ecm.automation.core.annotations.Param; 033import org.nuxeo.ecm.automation.jsf.OperationHelper; 034 035/** 036 * Execute an operation within the Seam context (doing automatically the needed init and cleanup) 037 * 038 * @author Tiry (tdelprat@nuxeo.com) 039 */ 040@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" }) 041public class RunOperationInSeam { 042 043 public static final String ID = "WebUI.RunOperationInSeam"; 044 045 @Context 046 protected OperationContext ctx; 047 048 @Param(name = "conversationId", required = false) 049 protected String conversationId; 050 051 @Context 052 protected AutomationService service; 053 054 @Param(name = "id") 055 protected String chainId; 056 057 @Param(name = "isolate", required = false, values = "false") 058 protected boolean isolate = false; 059 060 @OperationMethod 061 public Object run() throws OperationException { 062 063 Map<String, Object> vars = isolate ? new HashMap<String, Object>(ctx.getVars()) : ctx.getVars(); 064 065 OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars); 066 subctx.setInput(ctx.getInput()); 067 if (!OperationHelper.isSeamContextAvailable()) { 068 SeamOperationFilter.handleBeforeRun(ctx, conversationId); 069 try { 070 return runChain(subctx, vars); 071 } finally { 072 SeamOperationFilter.handleAfterRun(ctx, conversationId); 073 } 074 } else { 075 return runChain(subctx, vars); 076 } 077 } 078 079 protected Object runChain(OperationContext subctx, Map<String, Object> vars) throws OperationException { 080 if (chainId.startsWith("Chain.")) { 081 return service.run(subctx, chainId.substring(6)); 082 } else { 083 OperationChain chain = new OperationChain("operation"); 084 OperationParameters oparams = new OperationParameters(chainId, vars); 085 chain.add(oparams); 086 return service.run(subctx, chain); 087 } 088 } 089}