Search in sources :

Example 1 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Rmnode method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    NodeOrRelationship node = null;
    if (parser.arguments().isEmpty()) {
        node = getCurrent(session);
    } else {
        long id = parseInt(parser.arguments().get(0));
        try {
            node = wrap(getNodeById(id));
        } catch (NotFoundException e) {
            throw new ShellException("No node " + id + " found");
        }
    }
    if (!node.isNode()) {
        out.println("Please select a node to delete");
        return Continuation.INPUT_COMPLETE;
    }
    boolean forceDeletion = parser.options().containsKey("f");
    if (forceDeletion) {
        for (Relationship relationship : node.asNode().getRelationships()) {
            out.println("Relationship " + getDisplayName(getServer(), session, relationship, true, false) + " deleted");
            relationship.delete();
        }
    }
    if (node.asNode().hasRelationship()) {
        throw new ShellException(getDisplayName(getServer(), session, node.asNode(), false) + " cannot be deleted because it still has relationships. Use -f to force deletion of its relationships");
    }
    node.asNode().delete();
    return Continuation.INPUT_COMPLETE;
}
Also used : Relationship(org.neo4j.graphdb.Relationship) NotFoundException(org.neo4j.graphdb.NotFoundException) ShellException(org.neo4j.shell.ShellException)

Example 2 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Rmrel method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    assertCurrentIsNode(session);
    if (parser.arguments().isEmpty()) {
        throw new ShellException("Must supply relationship id to delete as the first argument");
    }
    Node currentNode = this.getCurrent(session).asNode();
    Relationship rel = findRel(currentNode, Long.parseLong(parser.arguments().get(0)));
    rel.delete();
    Node otherNode = rel.getOtherNode(currentNode);
    boolean deleteOtherNodeIfEmpty = parser.options().containsKey("d");
    if (deleteOtherNodeIfEmpty && !otherNode.hasRelationship()) {
        out.println("Also deleted " + getDisplayName(getServer(), session, otherNode, false) + " due to it not having any relationships left");
        otherNode.delete();
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) ShellException(org.neo4j.shell.ShellException)

Example 3 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class TransactionProvidingApp method jsonToNeo4jPropertyValue.

private Object jsonToNeo4jPropertyValue(Object value) throws ShellException {
    try {
        if (value instanceof JSONArray) {
            JSONArray array = (JSONArray) value;
            Object firstItem = array.get(0);
            Object resultArray = Array.newInstance(firstItem.getClass(), array.length());
            for (int i = 0; i < array.length(); i++) {
                Array.set(resultArray, i, array.get(i));
            }
            return resultArray;
        }
        return value;
    } catch (JSONException e) {
        throw new ShellException(stackTraceAsString(e));
    }
}
Also used : JSONArray(org.neo4j.shell.util.json.JSONArray) JSONException(org.neo4j.shell.util.json.JSONException) ShellException(org.neo4j.shell.ShellException)

Example 4 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class IndexProviderShellApp method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException {
    boolean doCd = parser.options().containsKey("cd");
    boolean doLs = parser.options().containsKey("ls");
    boolean query = parser.options().containsKey("q");
    boolean get = parser.options().containsKey("g") || query || doCd || doLs;
    boolean index = parser.options().containsKey("i");
    boolean remove = parser.options().containsKey("r");
    boolean getConfig = parser.options().containsKey("get-config");
    boolean create = parser.options().containsKey("create");
    boolean setConfig = parser.options().containsKey("set-config");
    boolean delete = parser.options().containsKey("delete");
    boolean indexes = parser.options().containsKey("indexes");
    int count = boolCount(get, index, remove, getConfig, create, setConfig, delete, indexes);
    if (count != 1) {
        throw new ShellException("Supply one of: -g, -i, -r, --get-config, --set-config, --create, --delete, --indexes");
    }
    if (get) {
        String commandToRun = parser.options().get("c");
        Collection<String> commandsToRun = new ArrayList<String>();
        boolean specialCommand = false;
        if (doCd || doLs) {
            specialCommand = true;
            if (doCd) {
                commandsToRun.add("cd -a $i");
            } else if (doLs) {
                commandsToRun.add("ls $i");
            }
        } else if (commandToRun != null) {
            commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
        }
        if (getIndex(getIndexName(parser), getEntityType(parser), out) == null) {
            return Continuation.INPUT_COMPLETE;
        }
        IndexHits<PropertyContainer> result = query ? query(parser, out) : get(parser, out);
        try {
            for (PropertyContainer hit : result) {
                printAndInterpretTemplateLines(commandsToRun, false, !specialCommand, NodeOrRelationship.wrap(hit), getServer(), session, out);
            }
        } finally {
            result.close();
        }
    } else if (index) {
        index(parser, session, out);
    } else if (remove) {
        if (getIndex(getIndexName(parser), Node.class, out) == null) {
            return null;
        }
        remove(parser, session, out);
    } else if (getConfig) {
        displayConfig(parser, out);
    } else if (create) {
        createIndex(parser, out);
    } else if (setConfig) {
        setConfig(parser, out);
    } else if (delete) {
        deleteIndex(parser, out);
    }
    if (indexes) {
        listIndexes(out);
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : PropertyContainer(org.neo4j.graphdb.PropertyContainer) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) ShellException(org.neo4j.shell.ShellException)

Example 5 with ShellException

use of org.neo4j.shell.ShellException in project neo4j by neo4j.

the class Rm method exec.

@Override
protected Continuation exec(AppCommandParser parser, Session session, Output out) throws Exception {
    NodeOrRelationship thing = getCurrent(session);
    boolean forProperty = parser.options().containsKey("p");
    boolean forLabel = parser.options().containsKey("l");
    if (forProperty || !forLabel) {
        // Property
        if (parser.arguments().isEmpty()) {
            throw new ShellException("Must supply the property key or label name to " + "remove, like: rm title");
        }
        String key = parser.arguments().get(0);
        if (thing.removeProperty(key) == null) {
            out.println("Property '" + key + "' not found");
        }
    } else {
        Node node = thing.asNode();
        for (Label label : parseLabels(parser)) {
            node.removeLabel(label);
        }
    }
    return Continuation.INPUT_COMPLETE;
}
Also used : Node(org.neo4j.graphdb.Node) Label(org.neo4j.graphdb.Label) ShellException(org.neo4j.shell.ShellException)

Aggregations

ShellException (org.neo4j.shell.ShellException)33 RemoteException (java.rmi.RemoteException)12 Node (org.neo4j.graphdb.Node)6 Test (org.junit.Test)5 Relationship (org.neo4j.graphdb.Relationship)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 GraphDatabaseShellServer (org.neo4j.shell.kernel.GraphDatabaseShellServer)4 Serializable (java.io.Serializable)3 Kernel (org.neo4j.jmx.Kernel)3 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)3 Response (org.neo4j.shell.Response)3 Session (org.neo4j.shell.Session)3 ShellServer (org.neo4j.shell.ShellServer)3 JSONException (org.neo4j.shell.util.json.JSONException)3 ManagementFactory.getPlatformMBeanServer (java.lang.management.ManagementFactory.getPlatformMBeanServer)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 MBeanServer (javax.management.MBeanServer)2 Label (org.neo4j.graphdb.Label)2 ThreadToStatementContextBridge (org.neo4j.kernel.impl.core.ThreadToStatementContextBridge)2