예제 #1
0
파일: Trav.java 프로젝트: Analect/neo4j
  @Override
  protected Continuation exec(AppCommandParser parser, Session session, Output out)
      throws ShellException, RemoteException {
    assertCurrentIsNode(session);

    Node node = this.getCurrent(session).asNode();
    boolean caseInsensitiveFilters = parser.options().containsKey("i");
    boolean looseFilters = parser.options().containsKey("l");
    boolean quiet = parser.options().containsKey("q");

    // Order
    TraversalDescription description = Traversal.description();
    String order = parser.options().get("o");
    if (order != null) {
      description = description.order(parseOrder(order));
    }

    // Relationship types / expander
    String relationshipTypes = parser.options().get("r");
    if (relationshipTypes != null) {
      Map<String, Object> types = parseFilter(relationshipTypes, out);
      description =
          description.expand(
              toExpander(getServer().getDb(), null, types, caseInsensitiveFilters, looseFilters));
    }

    // Uniqueness
    String uniqueness = parser.options().get("u");
    if (uniqueness != null) {
      description = description.uniqueness(parseUniqueness(uniqueness));
    }

    // Depth limit
    String depthLimit = parser.options().get("d");
    if (depthLimit != null) {
      description = description.evaluator(toDepth(parseInt(depthLimit)));
    }

    String filterString = parser.options().get("f");
    Map<String, Object> filterMap = filterString != null ? parseFilter(filterString, out) : null;
    String commandToRun = parser.options().get("c");
    Collection<String> commandsToRun = new ArrayList<String>();
    if (commandToRun != null) {
      commandsToRun.addAll(Arrays.asList(commandToRun.split(Pattern.quote("&&"))));
    }
    for (Path path : description.traverse(node)) {
      boolean hit = false;
      if (filterMap == null) {
        hit = true;
      } else {
        Node endNode = path.endNode();
        Map<String, Boolean> matchPerFilterKey = new HashMap<String, Boolean>();
        for (String key : endNode.getPropertyKeys()) {
          for (Map.Entry<String, Object> filterEntry : filterMap.entrySet()) {
            String filterKey = filterEntry.getKey();
            if (matchPerFilterKey.containsKey(filterKey)) {
              continue;
            }

            if (matches(
                newPattern(filterKey, caseInsensitiveFilters),
                key,
                caseInsensitiveFilters,
                looseFilters)) {
              Object value = endNode.getProperty(key);
              String filterPattern =
                  filterEntry.getValue() != null ? filterEntry.getValue().toString() : null;
              if (matches(
                  newPattern(filterPattern, caseInsensitiveFilters),
                  value.toString(),
                  caseInsensitiveFilters,
                  looseFilters)) {
                matchPerFilterKey.put(filterKey, true);
              }
            }
          }
        }

        if (matchPerFilterKey.size() == filterMap.size()) {
          hit = true;
        }
      }
      if (hit) {
        if (commandsToRun.isEmpty()) {
          printPath(path, quiet, session, out);
        } else {
          printAndInterpretTemplateLines(
              commandsToRun,
              false,
              true,
              NodeOrRelationship.wrap(path.endNode()),
              getServer(),
              session,
              out);
        }
      }
    }
    return Continuation.INPUT_COMPLETE;
  }