Search in sources :

Example 1 with TransientRelationship

use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.

the class RequestExecutor method buildRegisteredTransientRelationshipIndex.

/**
 * Append {@link TransientRelationship} of {@link CompileContext} to an index.
 *
 * @param context the compile context
 * @return an index of {@link TransientRelationship}
 */
private Map<Long, TransientRelationship> buildRegisteredTransientRelationshipIndex(CompileContext context) {
    final Map<Long, TransientRelationship> transientRelationshipIndex = new HashMap<>();
    for (Object obj : context.registry()) {
        if (TransientRelationship.class.isAssignableFrom(obj.getClass())) {
            TransientRelationship transientRelationship = (TransientRelationship) obj;
            transientRelationshipIndex.put(transientRelationship.getRef(), transientRelationship);
        }
    }
    return transientRelationshipIndex;
}
Also used : HashMap(java.util.HashMap) TransientRelationship(org.neo4j.ogm.context.TransientRelationship)

Example 2 with TransientRelationship

use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.

the class RequestExecutor method updateRelationships.

/**
 * Update the mapping context with new relationships created in a request.
 *
 * @param context        the compile context
 * @param relRefMappings mapping of relationship reference used in the compile context and the relationship id from the database
 */
private void updateRelationships(CompileContext context, List<ReferenceMapping> relRefMappings) {
    final Map<Long, TransientRelationship> registeredTransientRelationshipIndex = buildRegisteredTransientRelationshipIndex(context);
    for (ReferenceMapping referenceMapping : relRefMappings) {
        if (registeredTransientRelationshipIndex.containsKey(referenceMapping.ref)) {
            TransientRelationship transientRelationship = registeredTransientRelationshipIndex.get(referenceMapping.ref);
            boolean isRelationshipEntity = session.context().getRelationshipEntity(referenceMapping.id) != null;
            MappedRelationship mappedRelationship = new MappedRelationship(context.getId(transientRelationship.getSrc()), transientRelationship.getRel(), context.getId(transientRelationship.getTgt()), isRelationshipEntity ? referenceMapping.id : null, transientRelationship.getSrcClass(), transientRelationship.getTgtClass());
            session.context().addRelationship(mappedRelationship);
        }
    }
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) TransientRelationship(org.neo4j.ogm.context.TransientRelationship)

Example 3 with TransientRelationship

use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.

the class RequestExecutor method updateNodeEntities.

/**
 * Update the mapping context with entity ids for new/existing nodes created or updated in the request.
 *
 * @param context           the compile context
 * @param entityRefMappings mapping of entity reference used in the compile context and the entity id from the database
 */
private void updateNodeEntities(CompileContext context, List<ReferenceMapping> entityRefMappings) {
    // Ensures the last saved version of existing nodes is current in the cache
    for (Object obj : context.registry()) {
        if (!(obj instanceof TransientRelationship)) {
            ClassInfo classInfo = session.metaData().classInfo(obj);
            if (!classInfo.isRelationshipEntity()) {
                session.context().optionalNativeId(obj).filter(id -> id >= 0).ifPresent(id -> {
                    LOGGER.debug("updating existing node id: {}, {}", id, obj);
                    registerEntity(session.context(), classInfo, id, obj);
                });
            }
        }
    }
    // Ensures newly created nodes are current in the cache and also assigns their new graph ids
    for (ReferenceMapping referenceMapping : entityRefMappings) {
        // by a reference mapping will always have identical 'ref' and 'id' values.
        if (!(referenceMapping.ref.equals(referenceMapping.id))) {
            Object newEntity = context.getNewObject(referenceMapping.ref);
            LOGGER.debug("creating new node id: {}, {}, {}", referenceMapping.ref, referenceMapping.id, newEntity);
            initialiseNewEntity(referenceMapping.id, newEntity);
        }
    }
}
Also used : MappedRelationship(org.neo4j.ogm.context.MappedRelationship) RelationshipEntity(org.neo4j.ogm.annotation.RelationshipEntity) AbstractTransaction(org.neo4j.ogm.transaction.AbstractTransaction) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Response(org.neo4j.ogm.response.Response) HashMap(java.util.HashMap) CompileContext(org.neo4j.ogm.cypher.compiler.CompileContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappingContext(org.neo4j.ogm.context.MappingContext) Statement(org.neo4j.ogm.request.Statement) ArrayList(java.util.ArrayList) List(java.util.List) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) Compiler(org.neo4j.ogm.cypher.compiler.Compiler) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Transaction(org.neo4j.ogm.transaction.Transaction) EntityUtils(org.neo4j.ogm.utils.EntityUtils) TransientRelationship(org.neo4j.ogm.context.TransientRelationship) TransientRelationship(org.neo4j.ogm.context.TransientRelationship) ClassInfo(org.neo4j.ogm.metadata.ClassInfo)

Aggregations

TransientRelationship (org.neo4j.ogm.context.TransientRelationship)3 HashMap (java.util.HashMap)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 RelationshipEntity (org.neo4j.ogm.annotation.RelationshipEntity)1 MappingContext (org.neo4j.ogm.context.MappingContext)1 CompileContext (org.neo4j.ogm.cypher.compiler.CompileContext)1 Compiler (org.neo4j.ogm.cypher.compiler.Compiler)1 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)1 RowModel (org.neo4j.ogm.model.RowModel)1 Statement (org.neo4j.ogm.request.Statement)1 Response (org.neo4j.ogm.response.Response)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 AbstractTransaction (org.neo4j.ogm.transaction.AbstractTransaction)1 Transaction (org.neo4j.ogm.transaction.Transaction)1 EntityUtils (org.neo4j.ogm.utils.EntityUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1