Search in sources :

Example 1 with StoreLifecycleListener

use of javax.jdo.listener.StoreLifecycleListener 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 StoreLifecycleListener

use of javax.jdo.listener.StoreLifecycleListener in project tests by datanucleus.

the class AttachDetachTest method testAttachCleanCollectionWithNonPCElements.

/**
 * Test of attaching an object with a collection that has no changed elements and
 * where the elements of the collection are non-PC.
 */
public void testAttachCleanCollectionWithNonPCElements() {
    try {
        Object objectId = null;
        ClassWithNonPCCollection obj1 = new ClassWithNonPCCollection();
        PersistenceManager pm = newPM();
        Transaction tx = pm.currentTransaction();
        try {
            // test detach and attach
            tx.begin();
            obj1.getElements().add("elem1");
            obj1.getElements().add("elem2");
            pm.makePersistent(obj1);
            tx.commit();
            objectId = pm.getObjectId(obj1);
            // detach
            tx.begin();
            obj1 = (ClassWithNonPCCollection) pm.getObjectById(objectId, true);
            pm.getFetchPlan().addGroup("detach");
            obj1 = (ClassWithNonPCCollection) pm.detachCopy(obj1);
            tx.commit();
            // add a storeLifeCycleListener to check what gets stored
            final Collection<Object> storedElements = new ArrayList<>();
            pm.addInstanceLifecycleListener(new StoreLifecycleListener() {

                public void preStore(InstanceLifecycleEvent event) {
                }

                public void postStore(InstanceLifecycleEvent event) {
                    storedElements.add(event.getSource());
                }
            });
            // attach the unchanged Object
            tx.begin();
            obj1 = (ClassWithNonPCCollection) pm.makePersistent(obj1);
            tx.commit();
            // test that nothing has been stored
            assertTrue(storedElements.isEmpty());
        } catch (Exception e) {
            LOG.error("Exception in test", e);
            fail(e.toString());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(ClassWithNonPCCollection.class);
    }
}
Also used : Transaction(javax.jdo.Transaction) StoreLifecycleListener(javax.jdo.listener.StoreLifecycleListener) JDOPersistenceManager(org.datanucleus.api.jdo.JDOPersistenceManager) PersistenceManager(javax.jdo.PersistenceManager) ArrayList(java.util.ArrayList) ClassWithNonPCCollection(org.datanucleus.samples.detach.ClassWithNonPCCollection) JDODetachedFieldAccessException(javax.jdo.JDODetachedFieldAccessException) JDOUserException(javax.jdo.JDOUserException) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) InstanceLifecycleEvent(javax.jdo.listener.InstanceLifecycleEvent)

Aggregations

StoreLifecycleListener (javax.jdo.listener.StoreLifecycleListener)2 ArrayList (java.util.ArrayList)1 JDODetachedFieldAccessException (javax.jdo.JDODetachedFieldAccessException)1 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)1 JDOUserCallbackException (javax.jdo.JDOUserCallbackException)1 JDOUserException (javax.jdo.JDOUserException)1 PersistenceManager (javax.jdo.PersistenceManager)1 Transaction (javax.jdo.Transaction)1 InstanceLifecycleEvent (javax.jdo.listener.InstanceLifecycleEvent)1 StoreCallback (javax.jdo.listener.StoreCallback)1 ExecutionContext (org.datanucleus.ExecutionContext)1 JDOPersistenceManager (org.datanucleus.api.jdo.JDOPersistenceManager)1 ClassWithNonPCCollection (org.datanucleus.samples.detach.ClassWithNonPCCollection)1 ObjectProvider (org.datanucleus.state.ObjectProvider)1