use of org.neo4j.ogm.model.GraphModel in project neo4j-ogm by neo4j.
the class GraphEntityMapper method map.
/**
* @param type the type of the entities to return
* @param graphModelResponse The response of graph models to work on
* @param additionalNodeFilter An optional filter to exclude entities based on some nodes from the result
* @param <T> The type of the class of the entities to return
* @return The list of entities represented by the list of graph models.
*/
<T> List<T> map(Class<T> type, Response<GraphModel> graphModelResponse, BiFunction<GraphModel, Long, Boolean> additionalNodeFilter) {
// Those are the ids of all mapped nodes.
Set<Long> mappedNodeIds = new LinkedHashSet<>();
Set<Long> returnedNodeIds = new LinkedHashSet<>();
Set<Long> mappedRelationshipIds = new LinkedHashSet<>();
Set<Long> returnedRelationshipIds = new LinkedHashSet<>();
// Execute mapping for each individual model
Consumer<GraphModel> mapContentOfIndividualModel = graphModel -> mapContentOf(graphModel, additionalNodeFilter, returnedNodeIds, mappedRelationshipIds, returnedRelationshipIds, mappedNodeIds);
GraphModel graphModel = null;
while ((graphModel = graphModelResponse.next()) != null) {
mapContentOfIndividualModel.accept(graphModel);
}
graphModelResponse.close();
// Execute postload after all models and only for new ids
executePostLoad(mappedNodeIds, mappedRelationshipIds);
// Collect result
Predicate<Object> entityPresentAndCompatible = entity -> entity != null && type.isAssignableFrom(entity.getClass());
List<T> results = returnedNodeIds.stream().map(mappingContext::getNodeEntity).filter(entityPresentAndCompatible).map(type::cast).collect(toList());
// only look for REs if no node entities were found
if (results.isEmpty()) {
results = returnedRelationshipIds.stream().map(mappingContext::getRelationshipEntity).filter(entityPresentAndCompatible).map(type::cast).collect(toList());
}
return results;
}
use of org.neo4j.ogm.model.GraphModel 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);
}
use of org.neo4j.ogm.model.GraphModel 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);
}
}
use of org.neo4j.ogm.model.GraphModel in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapContentOf.
private void mapContentOf(GraphModel graphModel, BiFunction<GraphModel, Long, Boolean> additionalNodeFilter, Set<Long> returnedNodeIds, Set<Long> mappedRelationshipIds, Set<Long> returnedRelationshipIds, Set<Long> mappedNodeIds) {
Predicate<Long> includeInResult = id -> additionalNodeFilter.apply(graphModel, id);
try {
Set<Long> newNodeIds = mapNodes(graphModel);
returnedNodeIds.addAll(newNodeIds.stream().filter(includeInResult).collect(toList()));
mappedNodeIds.addAll(newNodeIds);
newNodeIds = mapRelationships(graphModel);
returnedRelationshipIds.addAll(newNodeIds.stream().filter(includeInResult).collect(toList()));
mappedRelationshipIds.addAll(newNodeIds);
} catch (MappingException e) {
throw e;
} catch (Exception e) {
throw new MappingException("Error mapping GraphModel", e);
}
}
use of org.neo4j.ogm.model.GraphModel 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);
}
Aggregations