1 19 20 package org.apache.cayenne.access; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.cayenne.LifecycleListener; 27 import org.apache.cayenne.PersistenceState; 28 import org.apache.cayenne.Persistent; 29 import org.apache.cayenne.access.event.DataContextEvent; 30 import org.apache.cayenne.access.event.DataContextTransactionEventListener; 31 import org.apache.cayenne.access.event.DataObjectTransactionEventListener; 32 import org.apache.cayenne.event.EventManager; 33 34 42 class DataContextFlushEventHandler implements DataContextTransactionEventListener { 43 44 List objectsToNotify; 45 46 DataContext originatingContext; 47 48 DataContextFlushEventHandler(DataContext originatingContext) { 49 this.originatingContext = originatingContext; 50 this.objectsToNotify = new ArrayList (); 51 52 54 Iterator it = originatingContext.getObjectStore().getObjectIterator(); 55 while (it.hasNext()) { 56 Persistent object = (Persistent) it.next(); 57 if (object instanceof DataObjectTransactionEventListener) { 58 switch (object.getPersistenceState()) { 59 case PersistenceState.NEW: 60 case PersistenceState.MODIFIED: 61 case PersistenceState.DELETED: 62 this.objectsToNotify.add(object); 63 break; 64 } 65 } 66 } 67 } 68 69 void registerForDataContextEvents() { 70 EventManager eventManager = originatingContext.getEventManager(); 71 eventManager.addListener( 72 this, 73 "dataContextWillCommit", 74 DataContextEvent.class, 75 DataContext.WILL_COMMIT, 76 originatingContext); 77 eventManager.addListener( 78 this, 79 "dataContextDidCommit", 80 DataContextEvent.class, 81 DataContext.DID_COMMIT, 82 originatingContext); 83 eventManager.addListener( 84 this, 85 "dataContextDidRollback", 86 DataContextEvent.class, 87 DataContext.DID_ROLLBACK, 88 originatingContext); 89 } 90 91 void unregisterFromDataContextEvents() { 92 EventManager eventManager = originatingContext.getEventManager(); 93 eventManager.removeListener(this, DataContext.WILL_COMMIT); 94 eventManager.removeListener(this, DataContext.DID_COMMIT); 95 eventManager.removeListener(this, DataContext.DID_ROLLBACK); 96 } 97 98 public void dataContextWillCommit(DataContextEvent event) { 99 Iterator iter = objectsToNotify.iterator(); 100 while (iter.hasNext()) { 101 ((DataObjectTransactionEventListener) iter.next()).willCommit(event); 102 } 103 } 104 105 public void dataContextDidCommit(DataContextEvent event) { 106 Iterator iter = objectsToNotify.iterator(); 107 while (iter.hasNext()) { 108 ((DataObjectTransactionEventListener) iter.next()).didCommit(event); 109 } 110 } 111 112 public void dataContextDidRollback(DataContextEvent event) { 113 } 115 } 116 | Popular Tags |