Search in sources :

Example 11 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataDomainCallbacksIT method testPostPersist.

@Test
public void testPostPersist() {
    LifecycleCallbackRegistry registry = resolver.getCallbackRegistry();
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPostPersisted());
    registry.addCallback(LifecycleEvent.POST_PERSIST, Artist.class, "postPersistCallback");
    MockCallingBackListener listener2 = new MockCallingBackListener() {

        @Override
        public void publicCallback(Object entity) {
            super.publicCallback(entity);
            assertFalse(((Persistent) entity).getObjectId().isTemporary());
        }
    };
    registry.addListener(LifecycleEvent.POST_PERSIST, Artist.class, listener2, "publicCallback");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("XX");
    context.commitChanges();
    assertFalse(a1.isPostPersisted());
    assertTrue(a2.isPostPersisted());
    assertSame(a2, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) Persistent(org.apache.cayenne.Persistent) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 12 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataContextEJBQLQueryIT method testSelectAggregatePostLoadCallback.

/**
 * CAY-899: Checks that aggregate results do not cause callbacks execution.
 */
@Test
public void testSelectAggregatePostLoadCallback() throws Exception {
    createFourArtistsTwoPaintings();
    LifecycleCallbackRegistry existingCallbacks = context.getEntityResolver().getCallbackRegistry();
    LifecycleCallbackRegistry testCallbacks = new LifecycleCallbackRegistry(context.getEntityResolver());
    DataContextEJBQLQueryCallback listener = new DataContextEJBQLQueryCallback();
    testCallbacks.addDefaultListener(LifecycleEvent.POST_LOAD, listener, "postLoad");
    context.getEntityResolver().setCallbackRegistry(testCallbacks);
    try {
        String ejbql = "select count(p), count(distinct p.estimatedPrice), max(p.estimatedPrice), sum(p.estimatedPrice) from Painting p";
        EJBQLQuery query = new EJBQLQuery(ejbql);
        List<?> data = context.performQuery(query);
        assertFalse(listener.postLoad);
        assertEquals(1, data.size());
        assertTrue(data.get(0) instanceof Object[]);
    } finally {
        context.getEntityResolver().setCallbackRegistry(existingCallbacks);
    }
}
Also used : EJBQLQuery(org.apache.cayenne.query.EJBQLQuery) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 13 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataContextCallbacksIT method testPostAddCallbacks.

@Test
public void testPostAddCallbacks() {
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    // no callbacks
    Artist a1 = context.newObject(Artist.class);
    assertNotNull(a1);
    assertFalse(a1.isPostAdded());
    registry.addCallback(LifecycleEvent.POST_ADD, Artist.class, "postAddCallback");
    Artist a2 = context.newObject(Artist.class);
    assertNotNull(a2);
    assertTrue(a2.isPostAdded());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.POST_ADD, Artist.class, listener2, "publicCallback");
    Artist a3 = context.newObject(Artist.class);
    assertNotNull(a3);
    assertTrue(a3.isPostAdded());
    assertSame(a3, listener2.getPublicCalledbackEntity());
    Painting p3 = context.newObject(Painting.class);
    assertNotNull(p3);
    assertFalse(p3.isPostAdded());
    assertSame(a3, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 14 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class DataContextCallbacksIT method testPreRemoveCallbacks.

@Test
public void testPreRemoveCallbacks() {
    LifecycleCallbackRegistry registry = runtime.getDataDomain().getEntityResolver().getCallbackRegistry();
    // no callbacks
    Artist a1 = context.newObject(Artist.class);
    a1.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a1);
    assertFalse(a1.isPostAdded());
    assertFalse(a1.isPreRemoved());
    registry.addCallback(LifecycleEvent.PRE_REMOVE, Artist.class, "preRemoveCallback");
    Artist a2 = context.newObject(Artist.class);
    a2.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a2);
    assertFalse(a2.isPostAdded());
    assertTrue(a2.isPreRemoved());
    MockCallingBackListener listener2 = new MockCallingBackListener();
    registry.addListener(LifecycleEvent.PRE_REMOVE, Artist.class, listener2, "publicCallback");
    Artist a3 = context.newObject(Artist.class);
    a3.setArtistName("XX");
    context.commitChanges();
    context.deleteObjects(a3);
    assertFalse(a3.isPostAdded());
    assertTrue(a3.isPreRemoved());
    assertSame(a3, listener2.getPublicCalledbackEntity());
    Painting p3 = context.newObject(Painting.class);
    p3.setPaintingTitle("XX");
    context.commitChanges();
    context.deleteObjects(p3);
    assertFalse(p3.isPostAdded());
    assertFalse(p3.isPreRemoved());
    assertSame(a3, listener2.getPublicCalledbackEntity());
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 15 with LifecycleCallbackRegistry

use of org.apache.cayenne.reflect.LifecycleCallbackRegistry in project cayenne by apache.

the class EntityResolver method initCallbacks.

/**
 * Compiles internal callback registry.
 */
synchronized void initCallbacks() {
    if (callbackRegistry == null) {
        LifecycleCallbackRegistry callbackRegistry = new LifecycleCallbackRegistry(this);
        // load entity callbacks
        for (ObjEntity entity : getObjEntities()) {
            Class<?> entityClass = entity.getJavaClass();
            CallbackDescriptor[] callbacks = entity.getCallbackMap().getCallbacks();
            for (CallbackDescriptor callback : callbacks) {
                for (String method : callback.getCallbackMethods()) {
                    callbackRegistry.addCallback(callback.getCallbackType(), entityClass, method);
                }
            }
        }
        this.callbackRegistry = callbackRegistry;
    }
}
Also used : LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry)

Aggregations

LifecycleCallbackRegistry (org.apache.cayenne.reflect.LifecycleCallbackRegistry)27 Test (org.junit.Test)26 Artist (org.apache.cayenne.testdo.testmap.Artist)15 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)7 Painting (org.apache.cayenne.testdo.testmap.Painting)6 SelectQuery (org.apache.cayenne.query.SelectQuery)4 ClientChannelServerDiffsListener1 (org.apache.cayenne.remote.ClientChannelServerDiffsListener1)4 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)2 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)2 ArrayList (java.util.ArrayList)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DataContext (org.apache.cayenne.access.DataContext)1 MockEventManager (org.apache.cayenne.event.MockEventManager)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 RefreshQuery (org.apache.cayenne.query.RefreshQuery)1 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)1 MtTable1 (org.apache.cayenne.testdo.mt.MtTable1)1