use of javax.jdo.JDOUserCallbackException in project datanucleus-api-jdo by datanucleus.
the class JDOCallbackHandler method preStore.
/**
* Callback before the object is stored.
* @param pc The Object
*/
public void preStore(Object pc) {
for (LifecycleListenerForClass listener : getListenersWorkingCopy()) {
if (listener.forClass(pc.getClass()) && listener.getListener() instanceof StoreLifecycleListener) {
ExecutionContext ec = nucleusCtx.getApiAdapter().getExecutionContext(pc);
String[] fieldNames = null;
// PRE_STORE will return the fields being stored (DataNucleus extension)
ObjectProvider op = ec.findObjectProvider(pc);
fieldNames = op.getDirtyFieldNames();
if (fieldNames == null) {
// Must be persisting so just return all loaded fields
fieldNames = op.getLoadedFieldNames();
}
((StoreLifecycleListener) listener.getListener()).preStore(new FieldInstanceLifecycleEvent(pc, InstanceLifecycleEvent.STORE, null, fieldNames));
}
}
if (pc instanceof StoreCallback) {
try {
((StoreCallback) pc).jdoPreStore();
} catch (Exception e) {
throw new JDOUserCallbackException(Localiser.msg("025001", "jdoPreStore"), e);
}
}
if (beanValidationHandler != null) {
ObjectProvider op = nucleusCtx.getApiAdapter().getExecutionContext(pc).findObjectProvider(pc);
if (!op.getLifecycleState().isNew()) {
// Don't fire this when persisting new since we will have done prePersist
beanValidationHandler.preStore(pc);
}
}
}
use of javax.jdo.JDOUserCallbackException in project tests by datanucleus.
the class PersistenceManagerTest method testInstanceCallbacks.
/**
* Tests the InstanceCallback interface methods
* @todo Add test for jdoPreClear()
*/
public void testInstanceCallbacks() {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
InstanceCallbackTester tester = new InstanceCallbackTester();
tester.setPersistentValue("value");
try {
tx.begin();
pm.makePersistent(tester);
tx.commit();
Query q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
tx.begin();
Collection c = (Collection) q.execute();
// ////////////////////////////////////////
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdPreStore()
assertNull(tester.getPersistentValue());
pm.refresh(tester);
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPostLoad using refresh for calling the post load in a persistent-dirty instance
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
tester.setPersistentValue("dirty pers");
tester.setTransientValue("dirty trans");
pm.refresh(tester);
assertNull(tester.getPersistentValue());
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPostLoad using ignoreCache = true on query
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
q.setIgnoreCache(true);
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdoPostLoad()
assertNotNull(tester.getTransientValue());
assertEquals(InstanceCallbackTester.POST_LOAD_VALUE, tester.getTransientValue());
} finally {
q.closeAll();
}
// ////////////////////////////////////////
// test jdoPreDelete
// ////////////////////////////////////////
q = pm.newQuery(pm.getExtent(InstanceCallbackTester.class, false));
c = (Collection) q.execute();
try {
assertEquals(1, c.size());
tester = (InstanceCallbackTester) c.iterator().next();
// test jdoPreDelete()
try {
tester.setTransientValue(null);
pm.deletePersistent(tester);
fail("Delete persistent didn't fail but should have");
} catch (JDOUserCallbackException e) {
// callback throws exception with PreDeleteException as the nested exception
}
} finally {
q.closeAll();
}
tx.commit();
} finally {
if (tx.isActive())
tx.rollback();
pm.close();
}
}
Aggregations