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());
}
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);
}
}
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());
}
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());
}
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;
}
}
Aggregations