Esempio n. 1
0
  @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;
  }
Esempio n. 2
0
 protected static void setCurrent(Session session, NodeOrRelationship current) {
   session.set(CURRENT_KEY, current.getTypedId().toString());
 }
Esempio n. 3
0
 public static boolean isCurrent(Session session, NodeOrRelationship thing) {
   String currentThing = (String) session.get(CURRENT_KEY);
   return currentThing != null && currentThing.equals(thing.getTypedId().toString());
 }