Search in sources :

Example 6 with FieldInfo

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

the class GraphEntityMapper method mapOneToMany.

/**
 * Map many values to an instance based on the relationship type.
 * See DATAGRAPH-637
 *
 * @param instance         the instance to map values onto
 * @param valueType        the type of each value
 * @param values           the values to map
 * @param relationshipType the relationship type associated with these values
 */
private void mapOneToMany(Object instance, Class<?> valueType, Object values, String relationshipType, Direction relationshipDirection) {
    ClassInfo classInfo = metadata.classInfo(instance);
    FieldInfo writer = EntityAccessManager.getIterableField(classInfo, valueType, relationshipType, relationshipDirection);
    if (writer != null) {
        if (writer.type().isArray() || Iterable.class.isAssignableFrom(writer.type())) {
            FieldInfo reader = EntityAccessManager.getIterableField(classInfo, valueType, relationshipType, relationshipDirection);
            Object currentValues;
            if (reader != null) {
                currentValues = reader.read(instance);
                if (writer.type().isArray()) {
                    values = EntityAccessManager.merge(writer.type(), values, (Object[]) currentValues, valueType);
                } else {
                    values = EntityAccessManager.merge(writer.type(), values, (Collection) currentValues, valueType);
                }
            }
        }
        writer.write(instance, values);
        return;
    }
    // this is not necessarily an error. but we can't tell.
    logger.debug("Unable to map iterable of type: {} onto property of {}", valueType, classInfo.name());
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 7 with FieldInfo

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

the class GraphEntityMapper method setLabels.

private void setLabels(Node nodeModel, Object instance) {
    ClassInfo classInfo = metadata.classInfo(instance);
    FieldInfo labelFieldInfo = classInfo.labelFieldOrNull();
    if (labelFieldInfo != null) {
        Collection<String> staticLabels = classInfo.staticLabels();
        Set<String> dynamicLabels = new HashSet<>();
        for (String label : nodeModel.getLabels()) {
            if (!staticLabels.contains(label)) {
                dynamicLabels.add(label);
            }
        }
        writeProperty(classInfo, instance, PropertyModel.with(labelFieldInfo.getName(), dynamicLabels));
    }
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 8 with FieldInfo

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

the class IdentityMap method hash.

private static long hash(Object object, ClassInfo classInfo) {
    List<FieldInfo> hashFields = new ArrayList<>(classInfo.propertyFields());
    if (classInfo.labelFieldOrNull() != null) {
        hashFields.add(classInfo.labelFieldOrNull());
    }
    long hash = SEED;
    for (FieldInfo fieldInfo : hashFields) {
        Object value = fieldInfo.read(object);
        if (value != null) {
            if (value.getClass().isArray()) {
                hash = hash * 31L + hashArray(value);
            } else {
                hash = hash * 31L + value.hashCode();
            }
        }
    }
    return hash;
}
Also used : ArrayList(java.util.ArrayList) FieldInfo(org.neo4j.ogm.metadata.FieldInfo)

Example 9 with FieldInfo

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

the class MappingContext method optionalNativeId.

/**
 * This method does not trigger a possible {@link IdStrategy} while accessing the identiy- or primary index field.
 *
 * @param entity The entity in question
 * @return An optional native id.
 */
public Optional<Long> optionalNativeId(Object entity) {
    ClassInfo classInfo = metaData.classInfo(entity);
    if (classInfo == null) {
        throw new IllegalArgumentException("Class " + entity.getClass() + " is not a valid entity class. " + "Please check the entity mapping.");
    }
    if (classInfo.hasIdentityField()) {
        FieldInfo identityField = classInfo.identityField();
        Object value = identityField.readProperty(entity);
        return Optional.ofNullable((Long) value);
    } else {
        Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
        return Optional.ofNullable(primaryIndexValue).map(v -> primaryIdToNativeId.get(LabelPrimaryId.of(classInfo, v)));
    }
}
Also used : FieldInfo(org.neo4j.ogm.metadata.FieldInfo) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 10 with FieldInfo

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

the class MappingContext method deregisterDependentRelationshipEntity.

/**
 * Deregister a relationship entity if it has either start or end node equal to the supplied startOrEndEntity
 *
 * @param startOrEndEntity the entity that might be the start or end node of a relationship entity
 */
private void deregisterDependentRelationshipEntity(Object startOrEndEntity) {
    Iterator<Long> relationshipEntityIdIterator = relationshipEntityRegister.keySet().iterator();
    while (relationshipEntityIdIterator.hasNext()) {
        Long relationshipEntityId = relationshipEntityIdIterator.next();
        Object relationshipEntity = relationshipEntityRegister.get(relationshipEntityId);
        final ClassInfo classInfo = metaData.classInfo(relationshipEntity);
        FieldInfo startNodeReader = classInfo.getStartNodeReader();
        FieldInfo endNodeReader = classInfo.getEndNodeReader();
        if (startOrEndEntity == startNodeReader.read(relationshipEntity) || startOrEndEntity == endNodeReader.read(relationshipEntity)) {
            relationshipEntityIdIterator.remove();
        }
    }
}
Also used : 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