Search in sources :

Example 1 with JDOUserCallbackException

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);
        }
    }
}
Also used : JDOUserCallbackException(javax.jdo.JDOUserCallbackException) ExecutionContext(org.datanucleus.ExecutionContext) StoreLifecycleListener(javax.jdo.listener.StoreLifecycleListener) StoreCallback(javax.jdo.listener.StoreCallback) ObjectProvider(org.datanucleus.state.ObjectProvider) JDOUserCallbackException(javax.jdo.JDOUserCallbackException)

Example 2 with JDOUserCallbackException

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();
    }
}
Also used : JDOUserCallbackException(javax.jdo.JDOUserCallbackException) Transaction(javax.jdo.Transaction) Query(javax.jdo.Query) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) Collection(java.util.Collection) InstanceCallbackTester(org.datanucleus.samples.instancecallback.InstanceCallbackTester)

Aggregations

JDOUserCallbackException (javax.jdo.JDOUserCallbackException)2 Collection (java.util.Collection)1 PersistenceManager (javax.jdo.PersistenceManager)1 Query (javax.jdo.Query)1 Transaction (javax.jdo.Transaction)1 StoreCallback (javax.jdo.listener.StoreCallback)1 StoreLifecycleListener (javax.jdo.listener.StoreLifecycleListener)1 ExecutionContext (org.datanucleus.ExecutionContext)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 InstanceCallbackTester (org.datanucleus.samples.instancecallback.InstanceCallbackTester)1 ObjectProvider (org.datanucleus.state.ObjectProvider)1