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 java.util.HashMap; 023import java.util.Map; 024 025import org.nuxeo.ecm.automation.AutomationService; 026import org.nuxeo.ecm.automation.OperationChain; 027import org.nuxeo.ecm.automation.OperationContext; 028import org.nuxeo.ecm.automation.OperationException; 029import org.nuxeo.ecm.automation.OperationParameters; 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.jsf.OperationHelper; 036 037/** 038 * Execute an operation within the Seam context (doing automatically the needed init and cleanup) 039 * 040 * @author Tiry (tdelprat@nuxeo.com) 041 */ 042@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" }) 043public class RunOperationInSeam { 044 045 public static final String ID = "WebUI.RunOperationInSeam"; 046 047 @Context 048 protected OperationContext ctx; 049 050 @Param(name = "conversationId", required = false) 051 protected String conversationId; 052 053 @Context 054 protected AutomationService service; 055 056 @Param(name = "id") 057 protected String chainId; 058 059 @Param(name = "isolate", required = false, values = "false") 060 protected boolean isolate = false; 061 062 @OperationMethod 063 public Object run() throws OperationException { 064 065 Map<String, Object> vars = isolate ? new HashMap<String, Object>(ctx.getVars()) : ctx.getVars(); 066 067 OperationContext subctx = new OperationContext(ctx.getCoreSession(), vars); 068 subctx.setInput(ctx.getInput()); 069 if (!OperationHelper.isSeamContextAvailable()) { 070 SeamOperationFilter.handleBeforeRun(ctx, conversationId); 071 try { 072 return runChain(subctx, vars); 073 } finally { 074 SeamOperationFilter.handleAfterRun(ctx, conversationId); 075 } 076 } else { 077 return runChain(subctx, vars); 078 } 079 } 080 081 protected Object runChain(OperationContext subctx, Map<String, Object> vars) throws OperationException { 082 if (chainId.startsWith("Chain.")) { 083 return service.run(subctx, chainId.substring(6)); 084 } else { 085 OperationChain chain = new OperationChain("operation"); 086 OperationParameters oparams = new OperationParameters(chainId, vars); 087 chain.add(oparams); 088 return service.run(subctx, chain); 089 } 090 } 091}