Search in sources :

Example 1 with MissingOperatorException

use of org.neo4j.ogm.exception.core.MissingOperatorException in project neo4j-ogm by neo4j.

the class NodeQueryBuilder method build.

public FilteredQuery build() {
    if (!built) {
        int i = 0;
        for (Filter filter : filters) {
            if (i != 0 && filter.getBooleanOperator().equals(BooleanOperator.NONE)) {
                throw new MissingOperatorException("BooleanOperator missing for filter with property name " + filter.getPropertyName() + ". Only the first filter may not specify the BooleanOperator.");
            }
            if (filter.isNested()) {
                appendNestedFilter(filter);
                hasRelationshipMatch = true;
            } else if (filter.isDeepNested()) {
                appendDeepNestedFilter(filter);
                hasRelationshipMatch = true;
            } else if (principalClause != null) {
                // If the filter is not nested, it belongs to the node we're returning
                principalClause().append(filter);
            }
            parameters.putAll(filter.parameters());
            i++;
        }
        built = true;
    }
    return new FilteredQuery(toCypher(), parameters);
}
Also used : MissingOperatorException(org.neo4j.ogm.exception.core.MissingOperatorException) Filter(org.neo4j.ogm.cypher.Filter)

Example 2 with MissingOperatorException

use of org.neo4j.ogm.exception.core.MissingOperatorException in project neo4j-ogm by neo4j.

the class FilteredQueryBuilder method constructRelationshipQuery.

private static StringBuilder constructRelationshipQuery(String type, Iterable<Filter> filters, Map<String, Object> properties) {
    // Filters are created in 3 steps: For deep nested filter, the improved version
    // NodeQueryBuilder is used. For the others the old approach still applies.
    FiltersAtStartNode outgoingDeepNestedFilters = new FiltersAtStartNode();
    FiltersAtStartNode incomingDeepNestedFilters = new FiltersAtStartNode();
    FiltersAtStartNode outgoingFilters = new FiltersAtStartNode();
    FiltersAtStartNode incomingFilters = new FiltersAtStartNode();
    List<Filter> relationshipFilters = new ArrayList<>();
    Direction initialDirection = null;
    for (Filter filter : filters) {
        if (filter.isNested() || filter.isDeepNested()) {
            if (filter.isDeepNested()) {
                List<Filter.NestedPathSegment> nestedPath = filter.getNestedPath();
                Filter.NestedPathSegment firstNestedPathSegment = nestedPath.get(0);
                filter.setOwnerEntityType(firstNestedPathSegment.getPropertyType());
                FiltersAtStartNode target;
                if (Relationship.Direction.OUTGOING == firstNestedPathSegment.getRelationshipDirection()) {
                    target = outgoingDeepNestedFilters;
                } else {
                    target = incomingDeepNestedFilters;
                }
                Filter.NestedPathSegment[] newPath = new Filter.NestedPathSegment[nestedPath.size() - 1];
                if (nestedPath.size() > 1) {
                    // The first element will represent the owning entity, so we need to get rid of it.
                    nestedPath.subList(1, nestedPath.size()).toArray(newPath);
                } else {
                    // The list of deep nested filters need an anchor only for relationships with one
                    // nested segments.
                    target.startNodeLabel = firstNestedPathSegment.getNestedEntityTypeLabel();
                }
                filter.setNestedPath(newPath);
                target.content.add(filter);
                if (initialDirection == null) {
                    initialDirection = firstNestedPathSegment.getRelationshipDirection();
                }
            } else {
                FiltersAtStartNode target;
                Direction relationshipDirection = filter.getRelationshipDirection();
                if (Relationship.OUTGOING == relationshipDirection) {
                    target = outgoingFilters;
                } else {
                    target = incomingFilters;
                }
                if (initialDirection == null) {
                    initialDirection = filter.getRelationshipDirection();
                }
                addFilterToList(target, filter);
            }
        } else {
            if (relationshipFilters.size() == 0) {
                filter.setBooleanOperator(BooleanOperator.NONE);
            } else {
                if (filter.getBooleanOperator().equals(BooleanOperator.NONE)) {
                    throw new MissingOperatorException("BooleanOperator missing for filter with property name " + filter.getPropertyName());
                }
            }
            relationshipFilters.add(filter);
        }
    }
    StringBuilder query = new StringBuilder();
    boolean outgoingDeepNested = !outgoingDeepNestedFilters.content.isEmpty();
    if (outgoingDeepNested) {
        NodeQueryBuilder nqb = new NodeQueryBuilder(outgoingDeepNestedFilters.startNodeLabel, outgoingDeepNestedFilters.content, "n");
        FilteredQuery filteredQuery = nqb.build();
        query.append(filteredQuery.statement()).append(" ");
        properties.putAll(filteredQuery.parameters());
    }
    if (!incomingDeepNestedFilters.content.isEmpty()) {
        NodeQueryBuilder nqb = new NodeQueryBuilder(incomingDeepNestedFilters.startNodeLabel, incomingDeepNestedFilters.content, outgoingDeepNested ? "m" : "n");
        FilteredQuery filteredQuery = nqb.build();
        query.append(filteredQuery.statement()).append(" ");
        if (outgoingDeepNested) {
            query.append(", n ");
        }
        properties.putAll(filteredQuery.parameters());
    }
    createNodeMatchSubquery(properties, outgoingFilters, query, "n");
    createNodeMatchSubquery(properties, incomingFilters, query, "m");
    createRelationSubquery(type, properties, relationshipFilters, query, initialDirection);
    return query;
}
Also used : ArrayList(java.util.ArrayList) Direction(org.neo4j.ogm.annotation.Relationship.Direction) MissingOperatorException(org.neo4j.ogm.exception.core.MissingOperatorException) Filter(org.neo4j.ogm.cypher.Filter)

Aggregations

Filter (org.neo4j.ogm.cypher.Filter)2 MissingOperatorException (org.neo4j.ogm.exception.core.MissingOperatorException)2 ArrayList (java.util.ArrayList)1 Direction (org.neo4j.ogm.annotation.Relationship.Direction)1