Search in sources :

Example 1 with GraphModelRequest

use of org.neo4j.ogm.request.GraphModelRequest in project neo4j-ogm by neo4j.

the class StrictQueryingTest method shouldUseOnlyOneLabelForStandardEntity.

// GH-651
@Test
public void shouldUseOnlyOneLabelForStandardEntity() {
    LoadByIdsDelegate delegate = new LoadByIdsDelegate(neo4jSession);
    delegate.loadAll(SomeEntity.class, Arrays.asList(1L, 2L));
    ArgumentCaptor<GraphModelRequest> argumentCaptor = ArgumentCaptor.forClass(GraphModelRequest.class);
    verify(request).execute(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue().getStatement()).isEqualTo("MATCH (n:`SomeEntity`) WHERE ID(n) IN $ids WITH n MATCH p=(n)-[*0..1]-(m) RETURN p");
}
Also used : GraphModelRequest(org.neo4j.ogm.request.GraphModelRequest) LoadByIdsDelegate(org.neo4j.ogm.session.delegates.LoadByIdsDelegate) Test(org.junit.Test)

Example 2 with GraphModelRequest

use of org.neo4j.ogm.request.GraphModelRequest in project neo4j-ogm by neo4j.

the class StrictQueryingTest method shouldUseOnlyOneLabelForOneRelationshipEntity.

// GH-651
@Test
public void shouldUseOnlyOneLabelForOneRelationshipEntity() {
    LoadOneDelegate delegate = new LoadOneDelegate(neo4jSession);
    delegate.load(SomeRelationshipEntity.class, 1L);
    ArgumentCaptor<GraphModelRequest> argumentCaptor = ArgumentCaptor.forClass(GraphModelRequest.class);
    verify(request).execute(argumentCaptor.capture());
    assertThat(argumentCaptor.getValue().getStatement()).startsWith("MATCH ()-[r0:`SOME_RELATIONSHIP`]->() WHERE ID(r0)=$id");
}
Also used : GraphModelRequest(org.neo4j.ogm.request.GraphModelRequest) LoadOneDelegate(org.neo4j.ogm.session.delegates.LoadOneDelegate) Test(org.junit.Test)

Example 3 with GraphModelRequest

use of org.neo4j.ogm.request.GraphModelRequest in project neo4j-ogm by neo4j.

the class LoadOneDelegate method load.

public <T, ID extends Serializable> T load(Class<T> type, ID id, int depth) {
    ClassInfo classInfo = session.metaData().classInfo(type.getName());
    if (classInfo == null) {
        throw new IllegalArgumentException(type + " is not a managed entity.");
    }
    final FieldInfo primaryIndexField = classInfo.primaryIndexField();
    if (primaryIndexField != null && !primaryIndexField.isTypeOf(id.getClass())) {
        throw new IllegalArgumentException("Supplied id does not match primary index type on supplied class " + type.getName());
    }
    if (primaryIndexField == null && !(id instanceof Long)) {
        throw new IllegalArgumentException("Supplied id must be of type Long (native graph id) when supplied class " + "does not have primary id " + type.getName());
    }
    Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
    if (!labelsOrType.isPresent()) {
        logger.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 null;
    }
    QueryStatements<ID> queryStatements = session.queryStatementsFor(type, depth);
    PagingAndSortingQuery qry = queryStatements.findOneByType(labelsOrType.get(), convertIfNeeded(classInfo, id), depth);
    GraphModelRequest request = new DefaultGraphModelRequest(qry.getStatement(), qry.getParameters());
    return session.doInTransaction(() -> {
        try (Response<GraphModel> response = session.requestHandler().execute(request)) {
            new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
            return lookup(type, id);
        }
    }, Transaction.Type.READ_ONLY);
}
Also used : 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) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) PagingAndSortingQuery(org.neo4j.ogm.cypher.query.PagingAndSortingQuery) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 4 with GraphModelRequest

use of org.neo4j.ogm.request.GraphModelRequest 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 5 with GraphModelRequest

use of org.neo4j.ogm.request.GraphModelRequest 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

GraphModelRequest (org.neo4j.ogm.request.GraphModelRequest)11 Test (org.junit.Test)8 GraphRowModelMapper (org.neo4j.ogm.context.GraphRowModelMapper)3 DefaultGraphModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphModelRequest)3 PagingAndSortingQuery (org.neo4j.ogm.cypher.query.PagingAndSortingQuery)3 GraphModel (org.neo4j.ogm.model.GraphModel)3 LoadByIdsDelegate (org.neo4j.ogm.session.delegates.LoadByIdsDelegate)3 LoadOneDelegate (org.neo4j.ogm.session.delegates.LoadOneDelegate)3 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 LoadByTypeDelegate (org.neo4j.ogm.session.delegates.LoadByTypeDelegate)2 Collection (java.util.Collection)1 LinkedHashSet (java.util.LinkedHashSet)1 GraphRowListModelMapper (org.neo4j.ogm.context.GraphRowListModelMapper)1 DefaultGraphRowListModelRequest (org.neo4j.ogm.cypher.query.DefaultGraphRowListModelRequest)1 SortOrder (org.neo4j.ogm.cypher.query.SortOrder)1 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)1 GraphRowListModel (org.neo4j.ogm.model.GraphRowListModel)1 QueryStatements (org.neo4j.ogm.session.request.strategy.QueryStatements)1