Search in sources :

Example 81 with FieldInfo

use of org.neo4j.ogm.metadata.FieldInfo 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 82 with FieldInfo

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

the class GraphEntityMapper method writeProperty.

private void writeProperty(ClassInfo classInfo, Object instance, Property<?, ?> property) {
    FieldInfo writer = classInfo.getFieldInfo(property.getKey().toString());
    if (writer == null) {
        logger.debug("Unable to find property: {} on class: {} for writing", property.getKey(), classInfo.name());
    } else {
        Object value = property.getValue();
        // merge iterable / arrays and co-erce to the correct attribute type
        if (writer.type().isArray() || Iterable.class.isAssignableFrom(writer.type())) {
            Class<?> paramType = writer.type();
            Class elementType = underlyingElementType(classInfo, property.getKey().toString());
            if (paramType.isArray()) {
                value = EntityAccessManager.merge(paramType, value, new Object[] {}, elementType);
            } else {
                value = EntityAccessManager.merge(paramType, value, Collections.emptyList(), elementType);
            }
        }
        writer.write(instance, value);
    }
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo)

Example 83 with FieldInfo

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

the class GraphEntityMapper method underlyingElementType.

private Class underlyingElementType(ClassInfo classInfo, String propertyName) {
    FieldInfo fieldInfo = fieldInfoForPropertyName(propertyName, classInfo);
    Class clazz = null;
    if (fieldInfo != null) {
        clazz = DescriptorMappings.getType(fieldInfo.getTypeDescriptor());
    }
    return clazz;
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo)

Example 84 with FieldInfo

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

the class ExecuteQueriesDelegate method countEntitiesOfType.

public long countEntitiesOfType(Class<?> entity) {
    ClassInfo classInfo = session.metaData().classInfo(entity.getName());
    if (classInfo == null) {
        return 0;
    }
    CypherQuery countStatement;
    if (classInfo.isRelationshipEntity()) {
        ClassInfo startNodeInfo = null;
        ClassInfo endNodeInfo = null;
        for (FieldInfo fieldInfo : classInfo.fieldsInfo().fields()) {
            if (fieldInfo.hasAnnotation(StartNode.class)) {
                startNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
            } else if (fieldInfo.hasAnnotation(EndNode.class)) {
                endNodeInfo = session.metaData().classInfo(DescriptorMappings.getType(fieldInfo.getTypeDescriptor()).getName());
            }
            if (endNodeInfo != null && startNodeInfo != null) {
                break;
            }
        }
        String start = startNodeInfo.neo4jName();
        String end = endNodeInfo.neo4jName();
        String type = classInfo.neo4jName();
        countStatement = new CountStatements().countEdges(start, type, end);
    } else {
        Collection<String> labels = classInfo.staticLabels();
        if (labels.isEmpty()) {
            return 0;
        }
        countStatement = new CountStatements().countNodes(labels);
    }
    return session.doInTransaction(() -> {
        try (Response<RowModel> response = session.requestHandler().execute((RowModelRequest) countStatement)) {
            RowModel queryResult = response.next();
            return queryResult == null ? 0 : ((Number) queryResult.getValues()[0]).longValue();
        }
    }, Transaction.Type.READ_ONLY);
}
Also used : EndNode(org.neo4j.ogm.annotation.EndNode) RowModel(org.neo4j.ogm.model.RowModel) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) CountStatements(org.neo4j.ogm.session.request.strategy.impl.CountStatements) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 85 with FieldInfo

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

the class LoadOneDelegate method lookup.

private <T, U> T lookup(Class<T> type, U id) {
    Object ref;
    ClassInfo typeInfo = session.metaData().classInfo(type.getName());
    FieldInfo primaryIndex = typeInfo.primaryIndexField();
    if (typeInfo.annotationsInfo().get(RelationshipEntity.class) == null) {
        if (primaryIndex == null) {
            ref = session.context().getNodeEntity((Long) id);
        } else {
            ref = session.context().getNodeEntityById(typeInfo, id);
        }
    } else {
        if (primaryIndex == null) {
            // Coercing to Long. identityField.convertedType() yields no parameterised type to call cast() with.
            // But we know this will always be Long.
            ref = session.context().getRelationshipEntity((Long) id);
        } else {
            ref = session.context().getRelationshipEntityById(typeInfo, id);
        }
    }
    try {
        return type.cast(ref);
    } catch (ClassCastException cce) {
        logger.warn("Could not cast entity {} for id {} to {}", ref, id, type);
        return null;
    }
}
Also used : RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Aggregations

FieldInfo (org.neo4j.ogm.metadata.FieldInfo)142 Test (org.junit.Test)102 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)100 ArrayList (java.util.ArrayList)12 Date (java.util.Date)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)5 Collection (java.util.Collection)4 Satellite (org.neo4j.ogm.domain.satellites.Satellite)4 MappingException (org.neo4j.ogm.exception.core.MappingException)4 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)3 Person (org.neo4j.ogm.domain.convertible.enums.Person)3 Member (org.neo4j.ogm.domain.forum.Member)3 Topic (org.neo4j.ogm.domain.forum.Topic)3 Post (org.neo4j.ogm.domain.forum.activity.Post)3 MetaData (org.neo4j.ogm.metadata.MetaData)3 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2