001/* 002 * (C) Copyright 2006-2010 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 * bstefanescu 016 */ 017package org.nuxeo.shell.automation.cmds; 018 019import org.nuxeo.ecm.automation.client.Constants; 020import org.nuxeo.ecm.automation.client.OperationRequest; 021import org.nuxeo.ecm.automation.client.model.Document; 022import org.nuxeo.shell.Argument; 023import org.nuxeo.shell.Command; 024import org.nuxeo.shell.Context; 025import org.nuxeo.shell.Parameter; 026import org.nuxeo.shell.ShellException; 027import org.nuxeo.shell.automation.ChainCompletor; 028import org.nuxeo.shell.automation.DocRefCompletor; 029import org.nuxeo.shell.automation.RemoteContext; 030import org.nuxeo.shell.utils.StringUtils; 031 032/** 033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 034 */ 035@Command(name = "run", help = "Run a server automation chain that accepts a document or void input") 036public class RunChainWithDoc implements Runnable { 037 038 @Context 039 protected RemoteContext ctx; 040 041 @Parameter(name = "-void", hasValue = false, help = "Use this to avoid having the server sending back the result.") 042 protected boolean isVoid; 043 044 @Parameter(name = "-ctx", hasValue = true, help = "Use this to set execution context variables. Syntax is: k1=v1,k1=v2") 045 protected String ctxVars; 046 047 @Parameter(name = "-s", hasValue = true, help = "Use this to change the separator used in context variables. THe default is ','") 048 protected String sep = ","; 049 050 @Argument(name = "chain", index = 0, required = true, completor = ChainCompletor.class, help = "The chain to run") 051 protected String chain; 052 053 @Argument(name = "doc", index = 1, required = false, completor = DocRefCompletor.class, help = "A reference to the new context document to use. To use UID references prefix them with 'doc:'.") 054 protected String path; 055 056 public void run() { 057 try { 058 Document doc = ctx.resolveDocument(path); 059 OperationRequest request = ctx.getSession().newRequest(chain).setInput(doc); 060 if (ctxVars != null) { 061 for (String pair : ctxVars.split(sep)) { 062 String[] ar = StringUtils.split(pair, '=', true); 063 request.setContextProperty(ar[0], ar[1]); 064 } 065 } 066 if (isVoid) { 067 request.setHeader(Constants.HEADER_NX_VOIDOP, "true"); 068 } 069 request.execute(); 070 071 } catch (Exception e) { 072 throw new ShellException(e); 073 } 074 } 075 076}