001/*
002 * (C) Copyright 2006-2010 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 *     bstefanescu
018 */
019package org.nuxeo.shell.automation.cmds;
020
021import org.nuxeo.ecm.automation.client.Constants;
022import org.nuxeo.ecm.automation.client.OperationRequest;
023import org.nuxeo.ecm.automation.client.model.Document;
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 = "run", help = "Run a server automation chain that accepts a document or void input")
038public class RunChainWithDoc 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 = "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:'.")
056    protected String path;
057
058    public void run() {
059        try {
060            Document doc = ctx.resolveDocument(path);
061            OperationRequest request = ctx.getSession().newRequest(chain).setInput(doc);
062            if (ctxVars != null) {
063                for (String pair : ctxVars.split(sep)) {
064                    String[] ar = StringUtils.split(pair, '=', true);
065                    request.setContextProperty(ar[0], ar[1]);
066                }
067            }
068            if (isVoid) {
069                request.setHeader(Constants.HEADER_NX_VOIDOP, "true");
070            }
071            request.execute();
072
073        } catch (Exception e) {
074            throw new ShellException(e);
075        }
076    }
077
078}