Search in sources :

Example 11 with PagingAndSortingQuery

use of org.neo4j.ogm.cypher.query.PagingAndSortingQuery in project neo4j-ogm by neo4j.

the class RelationshipQueryStatements method findByType.

@Override
public PagingAndSortingQuery findByType(String type, Filters parameters, int depth) {
    if (depth > 0) {
        FilteredQuery query = FilteredQueryBuilder.buildRelationshipQuery(type, parameters);
        String matchClause = query.statement() + " WITH DISTINCT(r0) as r0,startnode(r0) AS n, endnode(r0) AS m";
        String returnClause = loadClauseBuilder.build("r0", type, depth);
        return new PagingAndSortingQuery(matchClause, returnClause, query.parameters(), true, true, "r0");
    } else {
        throw new InvalidDepthException("Cannot load a relationship entity with depth 0 i.e. no start or end node");
    }
}
Also used : InvalidDepthException(org.neo4j.ogm.exception.core.InvalidDepthException) FilteredQuery(org.neo4j.ogm.session.request.FilteredQuery) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery)

Example 12 with PagingAndSortingQuery

use of org.neo4j.ogm.cypher.query.PagingAndSortingQuery in project neo4j-ogm by neo4j.

the class LoadByIdsDelegate method loadAll.

public <T, ID extends Serializable> Collection<T> loadAll(Class<T> type, Collection<ID> ids, SortOrder sortOrder, Pagination pagination, int depth) {
    Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
    if (!labelsOrType.isPresent()) {
        LOG.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
        return Collections.emptyList();
    }
    QueryStatements<ID> queryStatements = session.queryStatementsFor(type, depth);
    ClassInfo classInfo = session.metaData().classInfo(type.getName());
    PagingAndSortingQuery qry = queryStatements.findAllByType(labelsOrType.get(), convertIfNeeded(classInfo, ids), depth).setSortOrder(sortOrder).setPagination(pagination);
    GraphModelRequest request = new DefaultGraphModelRequest(qry.getStatement(), qry.getParameters());
    return session.doInTransaction(() -> {
        try (Response<GraphModel> response = session.requestHandler().execute(request)) {
            Iterable<T> mapped = new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
            if (sortOrder.sortClauses().isEmpty()) {
                return sortResultsByIds(type, ids, mapped);
            }
            Set<T> results = new LinkedHashSet<>();
            for (T entity : mapped) {
                if (includeMappedEntity(ids, entity)) {
                    results.add(entity);
                }
            }
            return results;
        }
    }, Transaction.Type.READ_ONLY);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphRowModelMapper(org.neo4j.ogm.context.GraphRowModelMapper) GraphModelRequest(org.neo4j.ogm.request.GraphModelRequest) DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphModel(org.neo4j.ogm.model.GraphModel) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 13 with PagingAndSortingQuery

use of org.neo4j.ogm.cypher.query.PagingAndSortingQuery in project neo4j-ogm by neo4j.

the class LoadByTypeDelegate method loadAll.

/**
 * Loads all objects of a given {@code type}. The {@code type} is used to determine the Neo4j label. If no such label
 * can be determined, a warning is logged and an immutable, empty list is returned to prevent queries without label
 * that potentially can retrieve all objects in the database if no {@link Filters filters} are given.
 *
 * @param type       The type of objects to load.
 * @param filters    Additional filters to reduce the number of objects loaded, may be null or empty.
 * @param sortOrder  Sort order to be passed on to the database
 * @param pagination Pagination if required
 * @param depth      Depth of relationships to load
 * @param <T>        Returned type
 * @return A list of objects with the requested type
 */
public <T> Collection<T> loadAll(Class<T> type, Filters filters, SortOrder sortOrder, Pagination pagination, int depth) {
    Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
    if (!labelsOrType.isPresent()) {
        LOG.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
        return Collections.emptyList();
    }
    QueryStatements queryStatements = session.queryStatementsFor(type, depth);
    SortOrder sortOrderWithResolvedProperties = sortOrderWithResolvedProperties(type, sortOrder);
    PagingAndSortingQuery query;
    if (filters == null || filters.isEmpty()) {
        query = queryStatements.findByType(labelsOrType.get(), depth);
    } else {
        resolvePropertyAnnotations(type, filters);
        query = queryStatements.findByType(labelsOrType.get(), filters, depth);
    }
    query.setSortOrder(sortOrderWithResolvedProperties).setPagination(pagination);
    return session.doInTransaction(() -> {
        if (query.needsRowResult()) {
            DefaultGraphRowListModelRequest graphRowListModelRequest = new DefaultGraphRowListModelRequest(query.getStatement(), query.getParameters());
            try (Response<GraphRowListModel> response = session.requestHandler().execute(graphRowListModelRequest)) {
                return (Collection<T>) new GraphRowListModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
            }
        } else {
            GraphModelRequest request = new DefaultGraphModelRequest(query.getStatement(), query.getParameters());
            try (Response<GraphModel> response = session.requestHandler().execute(request)) {
                return (Collection<T>) new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
            }
        }
    }, Transaction.Type.READ_WRITE);
}
Also used : DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphRowModelMapper(org.neo4j.ogm.context.GraphRowModelMapper) QueryStatements(org.neo4j.ogm.session.request.strategy.QueryStatements) SortOrder(org.neo4j.ogm.cypher.query.SortOrder) DefaultGraphRowListModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphRowListModelRequest) GraphModelRequest(org.neo4j.ogm.request.GraphModelRequest) DefaultGraphModelRequest(org.neo4j.ogm.cypher.query.DefaultGraphModelRequest) GraphRowListModelMapper(org.neo4j.ogm.context.GraphRowListModelMapper) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel) Collection(java.util.Collection) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery)

Example 14 with PagingAndSortingQuery

use of org.neo4j.ogm.cypher.query.PagingAndSortingQuery in project neo4j-ogm by neo4j.

the class RelationshipEntityQuerySortingTest method testDefaultMultipleSortOrders.

@Test
public void testDefaultMultipleSortOrders() {
    String cypher = "MATCH ()-[r0:`ORBITS`]-()  WITH DISTINCT(r0) as r0,startnode(r0) AS n, " + "endnode(r0) AS m ORDER BY r0.distance,r0.aphelion " + "MATCH p1 = (n)-[*0..3]-() WITH r0, COLLECT(DISTINCT p1) AS startPaths, m " + "MATCH p2 = (m)-[*0..3]-() WITH r0, startPaths, COLLECT(DISTINCT p2) AS endPaths " + "WITH r0,startPaths + endPaths  AS paths UNWIND paths AS p RETURN DISTINCT p, ID(r0)";
    PagingAndSortingQuery orbitsQuery = query.findByType("ORBITS", 3);
    check(cypher, orbitsQuery.setSortOrder(sortOrder.add("distance", "aphelion")).getStatement());
    check(cypher, orbitsQuery.setSortOrder(new SortOrder("distance", "aphelion")).getStatement());
    check(cypher, orbitsQuery.setSortOrder(new SortOrder().asc("distance", "aphelion")).getStatement());
}
Also used : SortOrder(org.neo4j.ogm.cypher.query.SortOrder) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) Test(org.junit.Test)

Example 15 with PagingAndSortingQuery

use of org.neo4j.ogm.cypher.query.PagingAndSortingQuery in project neo4j-ogm by neo4j.

the class NodeEntityQueryPagingTest method testFindByTypeAndOffset.

@Test
public void testFindByTypeAndOffset() {
    Pagination pagination = new Pagination(1, 5);
    pagination.setOffset(3);
    PagingAndSortingQuery query = queryStatements.findByType("Raptor", 1).setPagination(pagination);
    assertThat(query.getStatement()).isEqualTo("MATCH (n:`Raptor`) WITH n SKIP 3 LIMIT 5 MATCH p=(n)-[*0..1]-(m) RETURN p, ID(n)");
}
Also used : Pagination(org.neo4j.ogm.cypher.query.Pagination) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) Test(org.junit.Test)

Aggregations

PagingAndSortingQuery (org.neo4j.ogm.cypher.query.PagingAndSortingQuery)19 Test (org.junit.Test)14 DefaultGraphModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphModelRequest)4 GraphRowModelMapper (org.neo4j.ogm.context.GraphRowModelMapper)3 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)3 GraphModel (org.neo4j.ogm.model.GraphModel)3 GraphModelRequest (org.neo4j.ogm.request.GraphModelRequest)3 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 FilteredQuery (org.neo4j.ogm.session.request.FilteredQuery)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 GraphRowListModelMapper (org.neo4j.ogm.context.GraphRowListModelMapper)1 Filter (org.neo4j.ogm.cypher.Filter)1 Filters (org.neo4j.ogm.cypher.Filters)1 DefaultGraphRowListModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphRowListModelRequest)1 Pagination (org.neo4j.ogm.cypher.query.Pagination)1 InvalidDepthException (org.neo4j.ogm.exception.core.InvalidDepthException)1 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)1 GraphRowListModel (org.neo4j.ogm.model.GraphRowListModel)1