Search in sources :

Example 1 with EntityInterceptor

use of dev.morphia.EntityInterceptor in project morphia by mongodb.

the class EntityModel method callGlobalInterceptors.

private void callGlobalInterceptors(Class<? extends Annotation> event, Object entity, Document document, Datastore datastore) {
    for (EntityInterceptor ei : datastore.getMapper().getInterceptors()) {
        Sofia.logCallingInterceptorMethod(event.getSimpleName(), ei);
        if (event.equals(PreLoad.class)) {
            ei.preLoad(entity, document, mapper);
            ei.preLoad(entity, document, datastore);
        } else if (event.equals(PostLoad.class)) {
            ei.postLoad(entity, document, mapper);
            ei.postLoad(entity, document, datastore);
        } else if (event.equals(PrePersist.class)) {
            ei.prePersist(entity, document, mapper);
            ei.prePersist(entity, document, datastore);
        } else if (event.equals(PostPersist.class)) {
            ei.postPersist(entity, document, mapper);
            ei.postPersist(entity, document, datastore);
        }
    }
}
Also used : PostLoad(dev.morphia.annotations.PostLoad) EntityInterceptor(dev.morphia.EntityInterceptor) PostPersist(dev.morphia.annotations.PostPersist)

Example 2 with EntityInterceptor

use of dev.morphia.EntityInterceptor in project morphia by MorphiaOrg.

the class EntityModel method callGlobalInterceptors.

private void callGlobalInterceptors(Class<? extends Annotation> event, Object entity, Document document, Datastore datastore) {
    for (EntityInterceptor ei : datastore.getMapper().getInterceptors()) {
        Sofia.logCallingInterceptorMethod(event.getSimpleName(), ei);
        if (event.equals(PreLoad.class)) {
            ei.preLoad(entity, document, mapper);
            ei.preLoad(entity, document, datastore);
        } else if (event.equals(PostLoad.class)) {
            ei.postLoad(entity, document, mapper);
            ei.postLoad(entity, document, datastore);
        } else if (event.equals(PrePersist.class)) {
            ei.prePersist(entity, document, mapper);
            ei.prePersist(entity, document, datastore);
        } else if (event.equals(PostPersist.class)) {
            ei.postPersist(entity, document, mapper);
            ei.postPersist(entity, document, datastore);
        }
    }
}
Also used : PostLoad(dev.morphia.annotations.PostLoad) EntityInterceptor(dev.morphia.EntityInterceptor) PostPersist(dev.morphia.annotations.PostPersist)

Example 3 with EntityInterceptor

use of dev.morphia.EntityInterceptor in project morphia by MorphiaOrg.

the class DocumentReaderTest method testNestedByteArrayWithInterceptor.

@Test
public void testNestedByteArrayWithInterceptor() {
    HasByteArray hasByteArray = new HasByteArray();
    hasByteArray.data = new byte[] { 1, 2, 3 };
    HasNestedByteArray hasNestedByteArray = new HasNestedByteArray();
    hasNestedByteArray.nested = hasByteArray;
    getDs().save(hasNestedByteArray);
    EntityInterceptor interceptor = new EntityInterceptor() {
    };
    try {
        getDs().getMapper().addInterceptor(interceptor);
        Document first = getDs().getCollection(HasNestedByteArray.class).withDocumentClass(Document.class).find().first();
        fromDocument(HasNestedByteArray.class, first);
    } finally {
        getDs().getMapper().getInterceptors().remove(interceptor);
    }
}
Also used : EntityInterceptor(dev.morphia.EntityInterceptor) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 4 with EntityInterceptor

use of dev.morphia.EntityInterceptor in project morphia by mongodb.

the class DocumentReaderTest method testNestedByteArrayWithInterceptor.

@Test
public void testNestedByteArrayWithInterceptor() {
    HasByteArray hasByteArray = new HasByteArray();
    hasByteArray.data = new byte[] { 1, 2, 3 };
    HasNestedByteArray hasNestedByteArray = new HasNestedByteArray();
    hasNestedByteArray.nested = hasByteArray;
    getDs().save(hasNestedByteArray);
    EntityInterceptor interceptor = new EntityInterceptor() {
    };
    try {
        getDs().getMapper().addInterceptor(interceptor);
        Document first = getDs().getCollection(HasNestedByteArray.class).withDocumentClass(Document.class).find().first();
        fromDocument(HasNestedByteArray.class, first);
    } finally {
        getDs().getMapper().getInterceptors().remove(interceptor);
    }
}
Also used : EntityInterceptor(dev.morphia.EntityInterceptor) Document(org.bson.Document) Test(org.testng.annotations.Test)

Aggregations

EntityInterceptor (dev.morphia.EntityInterceptor)4 PostLoad (dev.morphia.annotations.PostLoad)2 PostPersist (dev.morphia.annotations.PostPersist)2 Document (org.bson.Document)2 Test (org.testng.annotations.Test)2