Search in sources :

Example 11 with ClassInfo

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

the class IdentityMap method getSnapshotOf.

/**
 * Returns the snapshot for the given id. The snapshot contains the corresponding entity's dynamic labels and properties
 * as stored during initial load of the entity.
 *
 * @param entity   the entity whos snapshot should be retrieved
 * @param entityId the native id of the entity
 * @return A snapshot of dynamic labels and properties or an empty optional.
 */
Optional<EntitySnapshot> getSnapshotOf(Object entity, Long entityId) {
    EntitySnapshot entitySnapshot;
    ClassInfo classInfo = metaData.classInfo(entity);
    if (metaData.isRelationshipEntity(classInfo.name())) {
        entitySnapshot = this.snapshotsOfRelationshipEntities.get(entityId);
    } else {
        entitySnapshot = this.snapshotsOfNodeEntities.get(entityId);
    }
    return Optional.ofNullable(entitySnapshot);
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 12 with ClassInfo

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

the class MappingContext method replaceRelationshipEntity.

public void replaceRelationshipEntity(Object entity, Long id) {
    relationshipEntityRegister.remove(id);
    ClassInfo classInfo = metaData.classInfo(entity);
    if (classInfo.hasPrimaryIndexField()) {
        final Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
        LabelPrimaryId labelPrimaryId = LabelPrimaryId.of(classInfo, primaryIndexValue);
        primaryIdToRelationship.remove(labelPrimaryId);
        primaryIdToNativeId.remove(labelPrimaryId);
    }
    addRelationshipEntity(entity, id);
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 13 with ClassInfo

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

the class MappingContext method addRelationshipEntity.

public Object addRelationshipEntity(Object relationshipEntity, Long id) {
    if (relationshipEntityRegister.putIfAbsent(id, relationshipEntity) == null) {
        relationshipEntity = relationshipEntityRegister.get(id);
        remember(relationshipEntity, id);
        ClassInfo classInfo = metaData.classInfo(relationshipEntity);
        if (classInfo.hasPrimaryIndexField()) {
            final Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(relationshipEntity);
            LabelPrimaryId labelPrimaryId = LabelPrimaryId.of(classInfo, primaryIndexValue);
            primaryIdToRelationship.put(labelPrimaryId, relationshipEntity);
            primaryIdToNativeId.put(labelPrimaryId, id);
        }
    }
    return relationshipEntity;
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 14 with ClassInfo

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

the class MappingContext method addNodeEntity.

/**
 * Adds an entity to the MappingContext.
 *
 * @param entity The object to add.
 * @param id
 * @return The object added, never null.
 */
public Object addNodeEntity(Object entity, Long id) {
    ClassInfo classInfo = metaData.classInfo(entity);
    if (!nodeEntityRegister.containsKey(id)) {
        nodeEntityRegister.put(id, entity);
        final Object primaryIndexValue = classInfo.readPrimaryIndexValueOf(entity);
        if (primaryIndexValue != null) {
            LabelPrimaryId key = LabelPrimaryId.of(classInfo, primaryIndexValue);
            primaryIndexNodeRegister.putIfAbsent(key, entity);
            primaryIdToNativeId.put(key, id);
        }
        remember(entity, id);
    }
    return entity;
}
Also used : ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Example 15 with ClassInfo

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

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