private boolean isConnected(NodeOrRelationship current, TypedId newId) throws ShellException { if (current.isNode()) { Node currentNode = current.asNode(); for (Relationship rel : currentNode.getRelationships()) { if (newId.isNode()) { if (rel.getOtherNode(currentNode).getId() == newId.getId()) { return true; } } else { if (rel.getId() == newId.getId()) { return true; } } } } else { if (newId.isRelationship()) { return false; } Relationship relationship = current.asRelationship(); if (relationship.getStartNode().getId() == newId.getId() || relationship.getEndNode().getId() == newId.getId()) { return true; } } return false; }
@Override public List<String> completionCandidates(String partOfLine, Session session) { String lastWord = TextUtil.lastWordOrQuoteOf(partOfLine, false); if (lastWord.startsWith("-")) { return super.completionCandidates(partOfLine, session); } try { TreeSet<String> result = new TreeSet<String>(); NodeOrRelationship current = getCurrent(session); if (current.isNode()) { // TODO Check if -r is supplied Node node = current.asNode(); for (Node otherNode : RelationshipToNodeIterable.wrap(node.getRelationships(), node)) { long otherNodeId = otherNode.getId(); String title = findTitle(getServer(), session, otherNode); if (title != null) { if (!result.contains(title)) { maybeAddCompletionCandidate(result, title + "," + otherNodeId, lastWord); } } maybeAddCompletionCandidate(result, "" + otherNodeId, lastWord); } } else { maybeAddCompletionCandidate(result, START_ALIAS, lastWord); maybeAddCompletionCandidate(result, END_ALIAS, lastWord); Relationship rel = current.asRelationship(); maybeAddCompletionCandidate(result, "" + rel.getStartNode().getId(), lastWord); maybeAddCompletionCandidate(result, "" + rel.getEndNode().getId(), lastWord); } return new ArrayList<String>(result); } catch (ShellException e) { e.printStackTrace(); return super.completionCandidates(partOfLine, session); } }
@Override protected String exec(AppCommandParser parser, Session session, Output out) throws ShellException, RemoteException { List<TypedId> paths = readCurrentWorkingDir(session); NodeOrRelationship current = getCurrent(session); NodeOrRelationship newThing = null; if (parser.arguments().isEmpty()) { newThing = NodeOrRelationship.wrap(getServer().getDb().getReferenceNode()); paths.clear(); } else { String arg = parser.arguments().get(0); TypedId newId = current.getTypedId(); if (arg.equals("..")) { if (paths.size() > 0) { newId = paths.remove(paths.size() - 1); } } else if (arg.equals(".")) { } else if (arg.equals(START_ALIAS) || arg.equals(END_ALIAS)) { newId = getStartOrEnd(current, arg); paths.add(current.getTypedId()); } else { long suppliedId = -1; try { suppliedId = Long.parseLong(arg); } catch (NumberFormatException e) { suppliedId = findNodeWithTitle(current.asNode(), arg, session); if (suppliedId == -1) { throw new ShellException("No connected node with title '" + arg + "'"); } } newId = parser.options().containsKey("r") ? new TypedId(NodeOrRelationship.TYPE_RELATIONSHIP, suppliedId) : new TypedId(NodeOrRelationship.TYPE_NODE, suppliedId); if (newId.equals(current.getTypedId())) { throw new ShellException("Can't cd to where you stand"); } boolean absolute = parser.options().containsKey("a"); if (!absolute && !this.isConnected(current, newId)) { throw new ShellException( getDisplayName(getServer(), session, newId, false) + " isn't connected to the current primitive," + " use -a to force it to go there anyway"); } paths.add(current.getTypedId()); } newThing = this.getThingById(newId); } setCurrent(session, newThing); writeCurrentWorkingDir(paths, session); return null; }
/** * @param server the {@link GraphDatabaseShellServer} to run at. * @param session the {@link Session} used by the client. * @param thing the thing to get the name-representation for. * @return the display name for a {@link Node}. */ public static String getDisplayName( GraphDatabaseShellServer server, Session session, NodeOrRelationship thing, boolean checkForMe) throws ShellException { if (thing.isNode()) { return getDisplayName(server, session, thing.asNode(), checkForMe); } else { return getDisplayName(server, session, thing.asRelationship(), true, checkForMe); } }