use of org.neo4j.ogm.model.Node 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.Node in project neo4j-ogm by neo4j.
the class MultiStatementCypherCompiler method updateNodesStatements.
@Override
public List<Statement> updateNodesStatements() {
assertStatementFactoryExists();
Map<String, Set<Node>> existingNodesByLabels = groupNodesByLabel(existingNodeBuilders.values());
List<Statement> statements = new ArrayList<>(existingNodesByLabels.size());
for (Set<Node> nodeModels : existingNodesByLabels.values()) {
ExistingNodeStatementBuilder existingNodeBuilder = new ExistingNodeStatementBuilder(nodeModels, statementFactory);
statements.add(existingNodeBuilder.build());
}
return statements;
}
use of org.neo4j.ogm.model.Node in project neo4j-ogm by neo4j.
the class GraphEntityMapper method mapNodes.
private Set<Long> mapNodes(GraphModel graphModel) {
Set<Long> mappedNodeIds = new LinkedHashSet<>();
for (Node node : graphModel.getNodes()) {
Object entity = mappingContext.getNodeEntity(node.getId());
if (entity == null) {
ClassInfo clsi = metadata.resolve(node.getLabels());
if (clsi == null) {
logger.debug("Could not find a class to map for labels " + Arrays.toString(node.getLabels()));
continue;
}
Map<String, Object> allProps = new HashMap<>(toMap(node.getPropertyList()));
getCompositeProperties(node.getPropertyList(), clsi).forEach((k, v) -> {
allProps.put(k.getName(), v);
});
entity = entityFactory.newObject(clsi.getUnderlyingClass(), allProps);
EntityUtils.setIdentity(entity, node.getId(), metadata);
setProperties(node.getPropertyList(), entity);
setLabels(node, entity);
mappingContext.addNodeEntity(entity, node.getId());
}
mappedNodeIds.add(node.getId());
}
return mappedNodeIds;
}
use of org.neo4j.ogm.model.Node in project neo4j-ogm by neo4j.
the class MultiStatementCypherCompiler method createNodesStatements.
public List<Statement> createNodesStatements() {
assertStatementFactoryExists();
Map<String, Set<Node>> newNodesByLabels = groupNodesByLabel(newNodeBuilders);
List<Statement> statements = new ArrayList<>(newNodesByLabels.size());
for (Set<Node> nodeModels : newNodesByLabels.values()) {
NewNodeStatementBuilder newNodeBuilder = new NewNodeStatementBuilder(nodeModels, statementFactory);
statements.add(newNodeBuilder.build());
}
return statements;
}
use of org.neo4j.ogm.model.Node in project neo4j-ogm by neo4j.
the class ExistingNodeStatementBuilder method build.
@Override
public Statement build() {
final Map<String, Object> parameters = new HashMap<>();
final StringBuilder queryBuilder = new StringBuilder();
if (existingNodes != null && existingNodes.size() > 0) {
Node firstNode = existingNodes.iterator().next();
queryBuilder.append("UNWIND $rows as row MATCH (n) WHERE ID(n)=row.nodeId ");
if (firstNode.hasVersionProperty()) {
queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstNode, "n"));
}
Set<String> previousDynamicLabels = firstNode.getPreviousDynamicLabels();
for (String label : previousDynamicLabels) {
queryBuilder.append(String.format(" REMOVE n:`%s` ", label));
}
queryBuilder.append(firstNode.createPropertyRemovalFragment("n"));
queryBuilder.append("SET n");
for (String label : firstNode.getLabels()) {
queryBuilder.append(":`").append(label).append("`");
}
queryBuilder.append(" SET n += row.props RETURN row.nodeId as ref, ID(n) as id, $type as type");
List<Map> rows = existingNodes.stream().map(node -> node.toRow("nodeId")).collect(toList());
parameters.put("type", "node");
parameters.put("rows", rows);
if (firstNode.hasVersionProperty()) {
OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
}
}
return statementFactory.statement(queryBuilder.toString(), parameters);
}
Aggregations