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.RemoteException;
022import org.nuxeo.ecm.automation.client.Session;
023import org.nuxeo.ecm.automation.client.model.Blob;
024import org.nuxeo.ecm.automation.client.model.Blobs;
025import org.nuxeo.ecm.automation.client.model.Document;
026import org.nuxeo.ecm.automation.client.model.Documents;
027import org.nuxeo.ecm.automation.client.model.FileBlob;
028import org.nuxeo.ecm.automation.client.model.OperationDocumentation;
029import org.nuxeo.shell.Shell;
030import org.nuxeo.shell.ShellException;
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 */
037public class OperationCommand implements Runnable {
038
039    public static final String ATTR_VOID = "-void";
040
041    public static final String ATTR_SCHEMAS = "-schemas";
042
043    public static final String ATTR_CTX = "-ctx";
044
045    protected Session session;
046
047    protected Shell shell;
048
049    protected OperationDocumentation op;
050
051    protected OperationRequest request;
052
053    protected OperationCommandType type;
054
055    public OperationCommand() {
056    }
057
058    public void init(OperationCommandType type, Shell shell, OperationDocumentation op) {
059        try {
060            this.type = type;
061            this.shell = shell;
062            this.session = shell.getContextObject(Session.class);
063            this.op = op;
064            this.request = session.newRequest(op.id);
065        } catch (Exception e) {
066            throw new ShellException(e);
067        }
068    }
069
070    public void run() {
071        try {
072            if (request.getInput() == null) {
073                if (type.hasDocumentInput()) {
074                    request.setInput(shell.getContextObject(RemoteContext.class).getDocument());
075                }
076            }
077            Object result = request.execute();
078            if (result instanceof Document) {
079                Cat.print(shell.getConsole(), (Document) result);
080            } else if (result instanceof Documents) {
081                for (Document doc : (Documents) result) {
082                    shell.getConsole().println(doc.getPath() + " - " + doc.getTitle());
083                }
084            } else if (result instanceof FileBlob) {
085                shell.getConsole().println(((FileBlob) result).getFile().getAbsolutePath());
086            } else if (result instanceof Blobs) {
087                for (Blob blob : (Blobs) result) {
088                    shell.getConsole().println(((FileBlob) blob).getFile().getAbsolutePath());
089                }
090            }
091        } catch (RemoteException e) {
092            throw new ShellException(e.getStatus() + " - " + e.getMessage(), e);
093        } catch (Exception e) {
094            throw new ShellException(e);
095        }
096    }
097
098    public void setParam(String name, Object value) {
099        if (ATTR_SCHEMAS.equals(name)) {
100            request.setHeader(Constants.HEADER_NX_SCHEMAS, (String) value);
101        } else if (ATTR_VOID.equals(name)) {
102            request.setHeader(Constants.HEADER_NX_VOIDOP, (String) value); // TODO
103        } else if (ATTR_CTX.equals(name)) {
104            for (String v : StringUtils.split(value.toString(), ',', true)) {
105                String[] pair = StringUtils.split(v.toString(), '=', true);
106                request.setContextProperty(pair[0], pair[1]);
107            }
108        } else {
109            if (value instanceof String) {
110                value = value.toString().replace("\\n", "\n");
111                request.set(name, value);
112            } else {
113                request.set(name, value);
114            }
115        }
116    }
117
118}