Search in sources :

Example 6 with MappingContext

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

the class MergeWithPrimaryIndexTests method setUpMapper.

@Before
public void setUpMapper() {
    mappingContext = new MappingContext(mappingMetadata);
    this.mapper = new EntityGraphMapper(mappingMetadata, mappingContext);
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) Before(org.junit.Before)

Example 7 with MappingContext

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

the class CyclicStructureTest method mapAndCompile.

private static Compiler mapAndCompile(Neo4jSession session, Object object, int depth) {
    final MetaData metaData = session.metaData();
    final MappingContext mappingContext = new MappingContext(metaData);
    EntityMapper mapper = new EntityGraphMapper(metaData, mappingContext);
    CompileContext context = mapper.map(object, depth);
    Compiler compiler = context.getCompiler();
    compiler.useStatementFactory(new RowStatementFactory());
    return compiler;
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) EntityGraphMapper(org.neo4j.ogm.context.EntityGraphMapper) MetaData(org.neo4j.ogm.metadata.MetaData) RowStatementFactory(org.neo4j.ogm.session.request.RowStatementFactory) EntityMapper(org.neo4j.ogm.context.EntityMapper)

Example 8 with MappingContext

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

the class DeleteDelegate method deleteOneOrMoreObjects.

// TODO : this is being done in multiple requests at the moment, one per object. Why not put them in a single request?
private void deleteOneOrMoreObjects(List<?> objects, Set<Object> neighbours) {
    Set<Object> notified = new HashSet<>();
    if (session.eventsEnabled()) {
        for (Object affectedObject : neighbours) {
            if (!notified.contains(affectedObject)) {
                session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.PRE_SAVE));
                notified.add(affectedObject);
            }
        }
    }
    for (Object object : objects) {
        MetaData metaData = session.metaData();
        ClassInfo classInfo = metaData.classInfo(object);
        if (classInfo == null) {
            session.warn(object.getClass().getName() + " is not an instance of a persistable class");
        } else {
            MappingContext mappingContext = session.context();
            Long id = mappingContext.optionalNativeId(object).filter(possibleId -> possibleId >= 0).orElseGet(() -> {
                session.warn(String.format("Instance of class %s has to be reloaded to be deleted. This can happen if the session has " + "been cleared between loading and deleting or using an object from a different transaction.", object.getClass()));
                return classInfo.getPrimaryIndexOrIdReader().apply(object).map(primaryIndexOrId -> session.load(object.getClass(), (Serializable) primaryIndexOrId)).flatMap(reloadedObject -> mappingContext.optionalNativeId(reloadedObject)).orElse(-1L);
            });
            if (id >= 0) {
                Statement request = getDeleteStatement(object, id, classInfo);
                if (session.eventsEnabled() && !notified.contains(object)) {
                    session.notifyListeners(new PersistenceEvent(object, Event.TYPE.PRE_DELETE));
                    notified.add(object);
                }
                RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
                session.doInTransaction(() -> {
                    try (Response<RowModel> response = session.requestHandler().execute(query)) {
                        if (request.optimisticLockingConfig().isPresent()) {
                            List<RowModel> rowModels = response.toList();
                            session.optimisticLockingChecker().checkResultsCount(rowModels, request);
                        }
                        if (metaData.isRelationshipEntity(classInfo.name())) {
                            session.detachRelationshipEntity(id);
                        } else {
                            session.detachNodeEntity(id);
                        }
                        // enabled in the first place.
                        if (notified.contains(object)) {
                            session.notifyListeners(new PersistenceEvent(object, Event.TYPE.POST_DELETE));
                        }
                    }
                }, Transaction.Type.READ_WRITE);
            }
        }
    }
    if (session.eventsEnabled()) {
        for (Object affectedObject : neighbours) {
            if (notified.contains(affectedObject)) {
                session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.POST_SAVE));
            }
        }
    }
}
Also used : Arrays(java.util.Arrays) CypherQuery(org.neo4j.ogm.cypher.query.CypherQuery) Response(org.neo4j.ogm.response.Response) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappingContext(org.neo4j.ogm.context.MappingContext) RowModelRequest(org.neo4j.ogm.request.RowModelRequest) RelationshipDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.RelationshipDeleteStatements) ArrayList(java.util.ArrayList) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest) HashSet(java.util.HashSet) RowModel(org.neo4j.ogm.model.RowModel) Map(java.util.Map) Event(org.neo4j.ogm.session.event.Event) Transaction(org.neo4j.ogm.transaction.Transaction) PersistenceEvent(org.neo4j.ogm.session.event.PersistenceEvent) Result(org.neo4j.ogm.model.Result) DeleteStatements(org.neo4j.ogm.session.request.strategy.DeleteStatements) MetaData(org.neo4j.ogm.metadata.MetaData) Set(java.util.Set) NodeDeleteStatements(org.neo4j.ogm.session.request.strategy.impl.NodeDeleteStatements) Filter(org.neo4j.ogm.cypher.Filter) Statement(org.neo4j.ogm.request.Statement) Serializable(java.io.Serializable) List(java.util.List) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) Collections(java.util.Collections) Serializable(java.io.Serializable) Statement(org.neo4j.ogm.request.Statement) RowModelRequest(org.neo4j.ogm.request.RowModelRequest) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest) MappingContext(org.neo4j.ogm.context.MappingContext) PersistenceEvent(org.neo4j.ogm.session.event.PersistenceEvent) MetaData(org.neo4j.ogm.metadata.MetaData) RowModel(org.neo4j.ogm.model.RowModel) HashSet(java.util.HashSet) ClassInfo(org.neo4j.ogm.metadata.ClassInfo) DefaultRowModelRequest(org.neo4j.ogm.cypher.query.DefaultRowModelRequest)

Example 9 with MappingContext

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

the class ElectionTest method shouldAllowVoterToChangeHerMind.

@Test
public void shouldAllowVoterToChangeHerMind() {
    Candidate a = new Candidate("A");
    Candidate b = new Candidate("B");
    Voter v = new Voter("V");
    v.candidateVotedFor = b;
    session.save(a);
    session.save(v);
    MappingContext context = ((Neo4jSession) session).context();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    session.clear();
    a = session.load(Candidate.class, a.getId());
    v = session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(b.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    v.candidateVotedFor = a;
    session.save(v);
    session.clear();
    session.load(Candidate.class, b.getId());
    session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(a.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), null, Voter.class, Candidate.class))).isTrue();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isFalse();
    session.clear();
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) MappingContext(org.neo4j.ogm.context.MappingContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Example 10 with MappingContext

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

the class MergeWithPrimaryIndexTests method setUpTestDatabase.

@BeforeClass
public static void setUpTestDatabase() {
    mappingMetadata = new MetaData("org.neo4j.ogm.domain.autoindex.valid", "org.neo4j.ogm.domain.cineasts.annotated", "org.neo4j.ogm.domain.pizza");
    mappingContext = new MappingContext(mappingMetadata);
}
Also used : MappingContext(org.neo4j.ogm.context.MappingContext) BeforeClass(org.junit.BeforeClass)

Aggregations

MappingContext (org.neo4j.ogm.context.MappingContext)13 Test (org.junit.Test)5 MetaData (org.neo4j.ogm.metadata.MetaData)5 BeforeClass (org.junit.BeforeClass)4 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)4 EntityGraphMapper (org.neo4j.ogm.context.EntityGraphMapper)2 Album (org.neo4j.ogm.domain.music.Album)2 Artist (org.neo4j.ogm.domain.music.Artist)2 ClassInfo (org.neo4j.ogm.metadata.ClassInfo)2 Transaction (org.neo4j.ogm.transaction.Transaction)2 Serializable (java.io.Serializable)1 Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Before (org.junit.Before)1