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