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.text.SimpleDateFormat;
020import java.util.TreeSet;
021
022import jline.ANSIBuffer;
023
024import org.nuxeo.ecm.automation.client.model.Document;
025import org.nuxeo.ecm.automation.client.model.PropertiesHelper;
026import org.nuxeo.ecm.automation.client.model.PropertyMap;
027import org.nuxeo.shell.Argument;
028import org.nuxeo.shell.Command;
029import org.nuxeo.shell.Context;
030import org.nuxeo.shell.Parameter;
031import org.nuxeo.shell.Shell;
032import org.nuxeo.shell.ShellConsole;
033import org.nuxeo.shell.automation.DocRefCompletor;
034import org.nuxeo.shell.automation.RemoteContext;
035import org.nuxeo.shell.utils.StringUtils;
036
037/**
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040@Command(name = "cat", help = "Print document details")
041public class Cat implements Runnable {
042
043    @Context
044    protected RemoteContext ctx;
045
046    @Parameter(name = "-schemas", hasValue = true, help = "A filter of schemas to include in the document. Use * for all schemas.")
047    protected String schemas;
048
049    @Parameter(name = "-all", hasValue = false, help = "Include all schemas. The -schemas attribute will be ignored if used in conjunction with this one.")
050    protected boolean all;
051
052    @Argument(name = "doc", index = 0, required = false, completor = DocRefCompletor.class, help = "The document to print. To use UIDs as refences you should prefix them with 'doc:'")
053    protected String path;
054
055    public void run() {
056        ShellConsole console = ctx.getShell().getConsole();
057        if (all) {
058            schemas = "*";
059        }
060        Document doc = ctx.resolveDocument(path, schemas);
061        print(console, doc);
062    }
063
064    public static void print(ShellConsole console, Document doc) {
065        ANSIBuffer buf = Shell.get().newANSIBuffer();
066        buf.append(ShellConsole.CRLF);
067        buf.bold(doc.getType()).append(" -- ").append(doc.getTitle());
068        buf.append(ShellConsole.CRLF);
069        buf.append("\tUID: ").append(doc.getId());
070        buf.append(ShellConsole.CRLF);
071        buf.append("\tPath: ").append(doc.getPath());
072        buf.append(ShellConsole.CRLF);
073        buf.append("\tType: ").append(doc.getType());
074        buf.append(ShellConsole.CRLF);
075
076        if (doc.getLastModified() != null) {
077            buf.append("\tLast Modified: ").append(new SimpleDateFormat().format(doc.getLastModified()));
078            buf.append(ShellConsole.CRLF);
079        }
080        buf.append("\tState: ").append(doc.getState() == null ? "none" : doc.getState());
081        buf.append(ShellConsole.CRLF);
082        buf.append("\tLock: ").append(doc.getLock() == null ? "none" : doc.getLock());
083        buf.append(ShellConsole.CRLF);
084        buf.append(ShellConsole.CRLF);
085
086        String desc = doc.getString("dc:description");
087        if (desc != null && desc.length() > 0) {
088            buf.bold("DESCRIPTION");
089            buf.append(ShellConsole.CRLF);
090            for (String line : StringUtils.split(desc, '\n', true)) {
091                buf.append("\t").append(line).append(ShellConsole.CRLF);
092            }
093            buf.append(ShellConsole.CRLF);
094        }
095        PropertyMap props = doc.getProperties();
096        if (props != null && !props.isEmpty()) {
097            TreeSet<String> keys = new TreeSet<String>(props.getKeys());
098            buf.bold("PROPERTIES");
099            buf.append(ShellConsole.CRLF);
100            for (String key : keys) {
101                if ("dc:description".equals(key)) {
102                    continue;
103                }
104                Object obj = props.get(key);
105                buf.append("\t").append(key).append(" = ");
106                if (obj != null) {
107                    if (PropertiesHelper.isScalar(obj)) {
108                        buf.append(props.getString(key));
109                    } else {
110                        buf.append(obj.toString());
111                    }
112                }
113                buf.append(ShellConsole.CRLF);
114            }
115            buf.append(ShellConsole.CRLF);
116        }
117
118        console.println(buf.toString());
119
120    }
121}