Search in sources :

Example 1 with GraphRowListModel

use of org.neo4j.ogm.model.GraphRowListModel in project neo4j-ogm by neo4j.

the class JsonGraphRowResponseTest method shouldParseDataInFilterGraphResponseCorrectly.

@Test
public void shouldParseDataInFilterGraphResponseCorrectly() throws IOException {
    when(entity.getContent()).thenReturn(filterQueryGraphRowResponse());
    try (Response<GraphRowListModel> rsp = new GraphRowsModelResponse(response)) {
        GraphRowListModel graphRowListModel = rsp.next();
        assertThat(graphRowListModel).isNotNull();
        List<GraphRowModel> graphRowModels = graphRowListModel.model();
        assertThat(graphRowModels).hasSize(8);
        GraphRowModel model = graphRowModels.get(0);
        GraphModel graph = model.getGraph();
        assertThat(graph.getNodes().iterator().next().getId()).isEqualTo(Long.valueOf(26));
        assertThat(graph.getRelationships()).isEmpty();
        Object[] rows = model.getRow();
        assertThat(rows.length).isEqualTo(2);
        Map row1 = (Map) ((List) rows[0]).get(0);
        assertThat(row1.get("name")).isEqualTo("GraphAware");
        assertThat(rows[1]).isEqualTo(26L);
    }
}
Also used : GraphRowModel(org.neo4j.ogm.model.GraphRowModel) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel) Map(java.util.Map) Test(org.junit.Test)

Example 2 with GraphRowListModel

use of org.neo4j.ogm.model.GraphRowListModel in project neo4j-ogm by neo4j.

the class GraphRowListModelMapper method map.

public <T> Iterable<T> map(Class<T> type, Response<GraphRowListModel> response) {
    Set<Long> idsOfResultEntities = new LinkedHashSet<>();
    Response<GraphModel> graphResponse = new Response<GraphModel>() {

        GraphRowListModel currentIteratedModel;

        int currentIndex = 0;

        @Override
        public GraphModel next() {
            if (currentIteratedModel == null) {
                currentIteratedModel = response.next();
                if (currentIteratedModel == null) {
                    return null;
                }
                currentIndex = 0;
            }
            List<GraphRowModel> listOfRowModels = currentIteratedModel.model();
            if (listOfRowModels.size() <= currentIndex) {
                currentIteratedModel = null;
                return next();
            }
            GraphRowModel graphRowModel = listOfRowModels.get(currentIndex++);
            Set<Long> idsInCurrentRow = Arrays.stream(graphRowModel.getRow()).filter(Number.class::isInstance).map(Number.class::cast).map(Number::longValue).collect(toSet());
            idsOfResultEntities.addAll(idsInCurrentRow);
            return graphRowModel.getGraph();
        }

        @Override
        public void close() {
            response.close();
        }

        @Override
        public String[] columns() {
            return response.columns();
        }
    };
    // although it looks like that the `idsOfResultEntities` will stay empty, they won't, trust us.
    BiFunction<GraphModel, Long, Boolean> includeModelObject = (graphModel, nativeId) -> idsOfResultEntities.contains(nativeId);
    return delegate.map(type, graphResponse, includeModelObject);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EntityInstantiator(org.neo4j.ogm.session.EntityInstantiator) Arrays(java.util.Arrays) List(java.util.List) GraphRowModel(org.neo4j.ogm.model.GraphRowModel) MetaData(org.neo4j.ogm.metadata.MetaData) BiFunction(java.util.function.BiFunction) Set(java.util.Set) Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel) Collectors(java.util.stream.Collectors) LinkedHashSet(java.util.LinkedHashSet) GraphRowModel(org.neo4j.ogm.model.GraphRowModel) Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) GraphRowListModel(org.neo4j.ogm.model.GraphRowListModel)

Example 3 with GraphRowListModel

use of org.neo4j.ogm.model.GraphRowListModel 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

GraphModel (org.neo4j.ogm.model.GraphModel)3 GraphRowListModel (org.neo4j.ogm.model.GraphRowListModel)3 GraphRowModel (org.neo4j.ogm.model.GraphRowModel)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 BiFunction (java.util.function.BiFunction)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)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 PagingAndSortingQuery (org.neo4j.ogm.cypher.query.PagingAndSortingQuery)1 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)1 MetaData (org.neo4j.ogm.metadata.MetaData)1 GraphModelRequest (org.neo4j.ogm.request.GraphModelRequest)1