private static String makePath(List<TypedId> paths) { StringBuffer buffer = new StringBuffer(); for (TypedId typedId : paths) { if (buffer.length() > 0) { buffer.append(","); } buffer.append(typedId.toString()); } return buffer.length() > 0 ? buffer.toString() : null; }
@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; }
protected static NodeOrRelationship getThingById(GraphDatabaseShellServer server, TypedId typedId) throws ShellException { NodeOrRelationship result = null; if (typedId.isNode()) { try { result = NodeOrRelationship.wrap(server.getDb().getNodeById(typedId.getId())); } catch (NotFoundException e) { throw new ShellException("Node " + typedId.getId() + " not found"); } } else { try { result = NodeOrRelationship.wrap(server.getDb().getRelationshipById(typedId.getId())); } catch (NotFoundException e) { throw new ShellException("Relationship " + typedId.getId() + " not found"); } } return result; }
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; }