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