Search in sources :

Example 6 with SortOrder

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

the class LoadCapabilityTest method shouldMaintainSortOrderWhenLoadingByIds.

// GH-302
@Test
public void shouldMaintainSortOrderWhenLoadingByIds() {
    Artist led = new Artist("Led Zeppelin");
    session.save(led);
    Artist bonJovi = new Artist("Bon Jovi");
    session.save(bonJovi);
    List<Long> ids = Arrays.asList(beatlesId, led.getId(), bonJovi.getId());
    SortOrder sortOrder = new SortOrder();
    sortOrder.add(SortOrder.Direction.ASC, "name");
    Iterable<Artist> artists = session.loadAll(Artist.class, ids, sortOrder);
    assertThat(artists).isNotNull();
    List<String> artistNames = new ArrayList<>();
    for (Artist artist : artists) {
        artistNames.add(artist.getName());
    }
    assertThat(artistNames.get(0)).isEqualTo("Bon Jovi");
    assertThat(artistNames.get(1)).isEqualTo("Led Zeppelin");
    assertThat(artistNames.get(2)).isEqualTo("The Beatles");
    sortOrder = new SortOrder();
    sortOrder.add(SortOrder.Direction.DESC, "name");
    artists = session.loadAll(Artist.class, ids, sortOrder);
    assertThat(artists).isNotNull();
    artistNames = new ArrayList<>();
    for (Artist artist : artists) {
        artistNames.add(artist.getName());
    }
    assertThat(artistNames.get(0)).isEqualTo("The Beatles");
    assertThat(artistNames.get(1)).isEqualTo("Led Zeppelin");
    assertThat(artistNames.get(2)).isEqualTo("Bon Jovi");
}
Also used : Artist(org.neo4j.ogm.domain.music.Artist) ArrayList(java.util.ArrayList) SortOrder(org.neo4j.ogm.cypher.query.SortOrder) Test(org.junit.Test)

Example 7 with SortOrder

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

the class RelationshipEntityQuerySortingTest method testMultipleSortOrders.

@Test
public void testMultipleSortOrders() {
    String cypher = "MATCH ()-[r0:`ORBITS`]-()  WITH DISTINCT(r0) as r0,startnode(r0) AS n, " + "endnode(r0) AS m ORDER BY r0.distance DESC,r0.aphelion DESC " + "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(DESC, "distance", "aphelion")).getStatement());
    check(cypher, orbitsQuery.setSortOrder(new SortOrder(DESC, "distance", "aphelion")).getStatement());
    check(cypher, orbitsQuery.setSortOrder(new SortOrder().desc("distance", "aphelion")).getStatement());
}
Also used : SortOrder(org.neo4j.ogm.cypher.query.SortOrder) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) Test(org.junit.Test)

Example 8 with SortOrder

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

the class NodeEntityQuerySortingTest method setUp.

@Before
public void setUp() {
    sortOrder = new SortOrder();
    filters = new Filters();
}
Also used : Filters(org.neo4j.ogm.cypher.Filters) SortOrder(org.neo4j.ogm.cypher.query.SortOrder) Before(org.junit.Before)

Example 9 with SortOrder

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

the class NodeEntityQuerySortingTest method testDefaultMultipleSortOrders.

@Test
public void testDefaultMultipleSortOrders() {
    String cypher = "MATCH (n:`Raptor`) WITH n ORDER BY n.age,n.name RETURN n";
    check(cypher, query.findByType("Raptor", 0).setSortOrder(sortOrder.add("age", "name")).getStatement());
    check(cypher, query.findByType("Raptor", 0).setSortOrder(new SortOrder("age", "name")).getStatement());
    check(cypher, query.findByType("Raptor", 0).setSortOrder(new SortOrder().asc("age", "name")).getStatement());
}
Also used : SortOrder(org.neo4j.ogm.cypher.query.SortOrder) Test(org.junit.Test)

Example 10 with SortOrder

use of org.neo4j.ogm.cypher.query.SortOrder 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)

Aggregations

SortOrder (org.neo4j.ogm.cypher.query.SortOrder)19 Test (org.junit.Test)13 Filters (org.neo4j.ogm.cypher.Filters)4 Pagination (org.neo4j.ogm.cypher.query.Pagination)4 PagingAndSortingQuery (org.neo4j.ogm.cypher.query.PagingAndSortingQuery)3 Before (org.junit.Before)2 Filter (org.neo4j.ogm.cypher.Filter)2 User (org.neo4j.ogm.domain.cineasts.annotated.User)2 Artist (org.neo4j.ogm.domain.music.Artist)2 Satellite (org.neo4j.ogm.domain.satellites.Satellite)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 GraphRowListModelMapper (org.neo4j.ogm.context.GraphRowListModelMapper)1 GraphRowModelMapper (org.neo4j.ogm.context.GraphRowModelMapper)1 DefaultGraphModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphModelRequest)1 DefaultGraphRowListModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphRowListModelRequest)1 Movie (org.neo4j.ogm.domain.cineasts.annotated.Movie)1 Rating (org.neo4j.ogm.domain.cineasts.annotated.Rating)1 User (org.neo4j.ogm.domain.gh368.User)1 Album (org.neo4j.ogm.domain.music.Album)1