Search in sources :

Example 1 with Node

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;
}
Also used : EntityAccessManager(org.neo4j.ogm.metadata.reflect.EntityAccessManager) EndNode(org.neo4j.ogm.annotation.EndNode) EntityInstantiator(org.neo4j.ogm.session.EntityInstantiator) java.util(java.util) CompositeAttributeConverter(org.neo4j.ogm.typeconversion.CompositeAttributeConverter) BiFunction(java.util.function.BiFunction) MethodInfo(org.neo4j.ogm.metadata.MethodInfo) LoggerFactory(org.slf4j.LoggerFactory) Property(org.neo4j.ogm.model.Property) Response(org.neo4j.ogm.response.Response) Direction(org.neo4j.ogm.annotation.Relationship.Direction) GraphModel(org.neo4j.ogm.model.GraphModel) Edge(org.neo4j.ogm.model.Edge) PropertyModel(org.neo4j.ogm.response.model.PropertyModel) Node(org.neo4j.ogm.model.Node) EntityUtils(org.neo4j.ogm.utils.EntityUtils) EntityFactory(org.neo4j.ogm.metadata.reflect.EntityFactory) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) MetaData(org.neo4j.ogm.metadata.MetaData) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(java.util.function.Consumer) StartNode(org.neo4j.ogm.annotation.StartNode) DescriptorMappings(org.neo4j.ogm.metadata.DescriptorMappings) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) MappingException(org.neo4j.ogm.exception.core.MappingException) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) GraphModel(org.neo4j.ogm.model.GraphModel)

Example 2 with Node

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;
}
Also used : Statement(org.neo4j.ogm.request.Statement) Node(org.neo4j.ogm.model.Node) ExistingNodeStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.ExistingNodeStatementBuilder)

Example 3 with Node

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;
}
Also used : EndNode(org.neo4j.ogm.annotation.EndNode) Node(org.neo4j.ogm.model.Node) StartNode(org.neo4j.ogm.annotation.StartNode) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 4 with Node

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;
}
Also used : Statement(org.neo4j.ogm.request.Statement) Node(org.neo4j.ogm.model.Node) NewNodeStatementBuilder(org.neo4j.ogm.cypher.compiler.builders.statement.NewNodeStatementBuilder)

Example 5 with Node

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);
}
Also used : List(java.util.List) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) CypherStatementBuilder(org.neo4j.ogm.cypher.compiler.CypherStatementBuilder) Map(java.util.Map) Set(java.util.Set) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Node(org.neo4j.ogm.model.Node) Statement(org.neo4j.ogm.request.Statement) StatementFactory(org.neo4j.ogm.request.StatementFactory) HashMap(java.util.HashMap) Node(org.neo4j.ogm.model.Node) OptimisticLockingConfig(org.neo4j.ogm.request.OptimisticLockingConfig) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Node (org.neo4j.ogm.model.Node)7 Statement (org.neo4j.ogm.request.Statement)4 Collectors (java.util.stream.Collectors)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 EndNode (org.neo4j.ogm.annotation.EndNode)2 StartNode (org.neo4j.ogm.annotation.StartNode)2 CypherStatementBuilder (org.neo4j.ogm.cypher.compiler.CypherStatementBuilder)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 GraphModel (org.neo4j.ogm.model.GraphModel)2 OptimisticLockingConfig (org.neo4j.ogm.request.OptimisticLockingConfig)2 StatementFactory (org.neo4j.ogm.request.StatementFactory)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 java.util (java.util)1 Arrays (java.util.Arrays)1 BiFunction (java.util.function.BiFunction)1 Consumer (java.util.function.Consumer)1 Predicate (java.util.function.Predicate)1