Search in sources :

Example 6 with GraphModel

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

use of org.neo4j.ogm.model.GraphModel 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 8 with GraphModel

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

the class RestModelMapper method map.

public RestStatisticsModel map(Response<RestModel> response) {
    List<ResultRowBuilder> resultRowBuilders = new ArrayList<>();
    Response<GraphModel> graphModelResponse = new Response<GraphModel>() {

        @Override
        public GraphModel next() {
            RestModel model = response.next();
            if (model == null) {
                return null;
            }
            ResultRowBuilder resultRowBuilder = new ResultRowBuilder(RestModelMapper.this::getEntityOrNodeModel, mappingContext::getRelationshipEntity);
            resultRowBuilders.add(resultRowBuilder);
            model.getRow().forEach(resultRowBuilder::handle);
            return resultRowBuilder.buildGraphModel();
        }

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

        @Override
        public String[] columns() {
            return response.columns();
        }
    };
    // Run the actual mapping
    delegate.map(Object.class, graphModelResponse);
    // Recreate the original structure
    RestStatisticsModel restStatisticsModel = new RestStatisticsModel();
    response.getStatistics().ifPresent(restStatisticsModel::setStatistics);
    restStatisticsModel.setResult(resultRowBuilders.stream().map(ResultRowBuilder::finish).collect(Collectors.toList()));
    return restStatisticsModel;
}
Also used : Response(org.neo4j.ogm.response.Response) GraphModel(org.neo4j.ogm.model.GraphModel) DefaultGraphModel(org.neo4j.ogm.response.model.DefaultGraphModel) RestModel(org.neo4j.ogm.model.RestModel)

Example 9 with GraphModel

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

the class JsonGraphResponseTest method shouldParseDataInLoadByIdsGraphResponseCorrectly.

@Test
public void shouldParseDataInLoadByIdsGraphResponseCorrectly() throws IOException {
    when(entity.getContent()).thenReturn(loadByIdsGraphResults());
    try (Response<GraphModel> rsp = new GraphModelResponse(response)) {
        GraphModel graphModel = rsp.next();
        assertThat(graphModel).isNotNull();
        Collection<Node> nodes = graphModel.getNodes();
        assertThat(nodes).hasSize(1);
        assertThat(nodes.iterator().next().getPropertyList().get(0).getValue()).isEqualTo("adam");
        assertThat(graphModel.getRelationships()).isEmpty();
        graphModel = rsp.next();
        assertThat(graphModel).isNotNull();
        nodes = graphModel.getNodes();
        assertThat(nodes).hasSize(2);
        Iterator<Node> nodeIterator = nodes.iterator();
        // skip adam
        nodeIterator.next();
        assertThat(nodeIterator.next().getPropertyList().get(0).getValue()).isEqualTo("GraphAware");
        assertThat(graphModel.getRelationships()).hasSize(1);
        assertThat(graphModel.getRelationships().iterator().next().getType()).isEqualTo("EMPLOYED_BY");
        for (int i = 0; i < 4; i++) {
            assertThat(rsp.next()).isNotNull();
        }
        assertThat(rsp.next()).isNull();
    }
}
Also used : GraphModel(org.neo4j.ogm.model.GraphModel) Node(org.neo4j.ogm.model.Node) Test(org.junit.Test)

Aggregations

GraphModel (org.neo4j.ogm.model.GraphModel)9 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)4 Response (org.neo4j.ogm.response.Response)4 BiFunction (java.util.function.BiFunction)3 Collectors (java.util.stream.Collectors)3 GraphRowModelMapper (org.neo4j.ogm.context.GraphRowModelMapper)3 DefaultGraphModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphModelRequest)3 PagingAndSortingQuery (org.neo4j.ogm.cypher.query.PagingAndSortingQuery)3 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)3 MetaData (org.neo4j.ogm.metadata.MetaData)3 GraphRowListModel (org.neo4j.ogm.model.GraphRowListModel)3 Node (org.neo4j.ogm.model.Node)3 GraphModelRequest (org.neo4j.ogm.request.GraphModelRequest)3 EntityInstantiator (org.neo4j.ogm.session.EntityInstantiator)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 java.util (java.util)2 LinkedHashSet (java.util.LinkedHashSet)2 Consumer (java.util.function.Consumer)2 Predicate (java.util.function.Predicate)2 Test (org.junit.Test)2