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