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.model.DocRef;
020import org.nuxeo.ecm.automation.client.model.Document;
021import org.nuxeo.ecm.automation.client.model.Documents;
022import org.nuxeo.shell.Argument;
023import org.nuxeo.shell.Command;
024import org.nuxeo.shell.Context;
025import org.nuxeo.shell.Parameter;
026import org.nuxeo.shell.ShellConsole;
027import org.nuxeo.shell.ShellException;
028import org.nuxeo.shell.automation.DocRefCompletor;
029import org.nuxeo.shell.automation.DocumentHelper;
030import org.nuxeo.shell.automation.RemoteContext;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035@Command(name = "getrel", help = "Get realtions between two documents")
036public class GetRelations implements Runnable {
037
038    @Context
039    protected RemoteContext ctx;
040
041    @Parameter(name = "-out", hasValue = false, help = "Is the document the relation subject? This flag is by default on true.")
042    protected boolean outgoing = true;
043
044    @Parameter(name = "-in", hasValue = false, help = "Is the document the relation object?")
045    protected boolean ingoing;
046
047    @Parameter(name = "-predicate", hasValue = true, help = "The relation predicate - requested.")
048    protected String predicate;
049
050    @Parameter(name = "-graphName", hasValue = true, help = "The graph name - optional.")
051    protected String graphName;
052
053    @Argument(name = "doc", index = 0, required = false, completor = DocRefCompletor.class, help = "The document involved in the relation")
054    protected String path;
055
056    public void run() {
057        if (predicate == null) {
058            throw new ShellException("Relation predicate is required!");
059        }
060        if (ingoing) {
061            outgoing = false;
062        }
063        ShellConsole console = ctx.getShell().getConsole();
064        DocRef docRef = ctx.resolveRef(path);
065        try {
066            Documents docs = (Documents) ctx.getDocumentService().getRelations(docRef, predicate, outgoing, graphName);
067            for (Document doc : docs) {
068                DocumentHelper.printPath(console, doc);
069            }
070        } catch (Exception e) {
071            throw new ShellException(e);
072        }
073    }
074}