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