Search in sources :

Example 86 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class GraphEntityMapper method mapRelationshipEntity.

private void mapRelationshipEntity(List<Edge> oneToMany, Edge edge, Object source, Object target, ClassInfo relationshipEntityClassInfo) {
    logger.debug("Found relationship type: {} to map to RelationshipEntity: {}", edge.getType(), relationshipEntityClassInfo.name());
    // look to see if this relationship already exists in the mapping context.
    Object relationshipEntity = mappingContext.getRelationshipEntity(edge.getId());
    // do we know about it?
    if (relationshipEntity == null) {
        // no, create a new relationship entity
        relationshipEntity = createRelationshipEntity(edge, source, target);
    }
    // If the source has a writer for an outgoing relationship for the rel entity, then write the rel entity on the source if it's a scalar writer
    ClassInfo sourceInfo = metadata.classInfo(source);
    FieldInfo writer = getRelationalWriter(sourceInfo, edge.getType(), Direction.OUTGOING, relationshipEntity);
    if (writer == null) {
        logger.debug("No writer for {}", target);
    } else {
        if (writer.forScalar()) {
            writer.write(source, relationshipEntity);
            mappingContext.addRelationship(new MappedRelationship(edge.getStartNode(), edge.getType(), edge.getEndNode(), edge.getId(), source.getClass(), DescriptorMappings.getType(writer.getTypeDescriptor())));
        } else {
            oneToMany.add(edge);
        }
    }
    // If the target has a writer for an incoming relationship for the rel entity, then write the rel entity on the target if it's a scalar writer
    ClassInfo targetInfo = metadata.classInfo(target);
    writer = getRelationalWriter(targetInfo, edge.getType(), Direction.INCOMING, relationshipEntity);
    if (writer == null) {
        logger.debug("No writer for {}", target);
    } else {
        if (writer.forScalar()) {
            writer.write(target, relationshipEntity);
        } else {
            oneToMany.add(edge);
        }
    }
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 87 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class GraphEntityMapper method mapRelationships.

private Set<Long> mapRelationships(GraphModel graphModel) {
    Set<Long> mappedRelationshipIds = new HashSet<>();
    List<Edge> oneToMany = new ArrayList<>();
    for (Edge edge : graphModel.getRelationships()) {
        Object source = mappingContext.getNodeEntity(edge.getStartNode());
        Object target = mappingContext.getNodeEntity(edge.getEndNode());
        if (source == null || target == null) {
            String messageFormat = "Relationship {} cannot be fully hydrated because one or more required node entities have not been part of the result set.";
            logger.warn(messageFormat, edge);
        } else {
            mappedRelationshipIds.add(edge.getId());
            // check whether this edge should in fact be handled as a relationship entity
            ClassInfo relationshipEntityClassInfo = getRelationshipEntity(edge);
            if (relationshipEntityClassInfo != null) {
                mapRelationshipEntity(oneToMany, edge, source, target, relationshipEntityClassInfo);
            } else {
                oneToMany.add(edge);
            }
        }
    }
    if (!oneToMany.isEmpty()) {
        mapOneToMany(oneToMany);
    }
    return mappedRelationshipIds;
}
Also used : Edge(org.neo4j.ogm.model.Edge) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 88 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class GraphEntityMapper method setProperties.

private void setProperties(List<Property<String, Object>> propertyList, Object instance) {
    ClassInfo classInfo = metadata.classInfo(instance);
    getCompositeProperties(propertyList, classInfo).forEach((field, v) -> field.write(instance, v));
    for (Property<?, ?> property : propertyList) {
        writeProperty(classInfo, instance, property);
    }
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 89 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo in project neo4j-ogm by neo4j.

the class GraphEntityMapper method executePostLoad.

private void executePostLoad(Object instance) {
    ClassInfo classInfo = metadata.classInfo(instance);
    MethodInfo postLoadMethod = classInfo.postLoadMethodOrNull();
    if (postLoadMethod == null) {
        return;
    }
    try {
        postLoadMethod.invoke(instance);
    } catch (SecurityException e) {
        logger.warn("Cannot call PostLoad annotated method {} on class {}, " + "security manager denied access.", postLoadMethod.getMethod().getName(), classInfo.name(), e);
    } catch (IllegalAccessException | InvocationTargetException e) {
        logger.warn("Cannot call PostLoad annotated method {} on class {}. " + "Make sure it is public and has no arguments", postLoadMethod.getMethod().getName(), classInfo.name(), e);
    }
}
Also used : MethodInfo(org.neo4j.ogm.metadata.MethodInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 90 with ClassInfo

use of org.neo4j.ogm.metadata.ClassInfo 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)

Aggregations

ClassInfo (org.neo4j.ogm.metadata.ClassInfo)145 FieldInfo (org.neo4j.ogm.metadata.FieldInfo)100 Test (org.junit.Test)76 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)8 MetaData (org.neo4j.ogm.metadata.MetaData)8 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NodeBuilder (org.neo4j.ogm.cypher.compiler.NodeBuilder)5 MappingException (org.neo4j.ogm.exception.core.MappingException)5 RowModel (org.neo4j.ogm.model.RowModel)5 Collection (java.util.Collection)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Optional (java.util.Optional)4 CypherQuery (org.neo4j.ogm.cypher.query.CypherQuery)4 Member (org.neo4j.ogm.domain.forum.Member)4 Satellite (org.neo4j.ogm.domain.satellites.Satellite)4 Collections (java.util.Collections)3