1 56 package org.objectstyle.cayenne.access.util; 57 58 import java.util.ArrayList ; 59 import java.util.Arrays ; 60 import java.util.Collection ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 64 import org.apache.log4j.Level; 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.access.DataContext; 67 import org.objectstyle.cayenne.access.event.DataContextEvent; 68 import org.objectstyle.cayenne.access.event.DataContextTransactionEventListener; 69 import org.objectstyle.cayenne.access.event.DataObjectTransactionEventListener; 70 import org.objectstyle.cayenne.event.EventManager; 71 import org.objectstyle.cayenne.query.Query; 72 import org.objectstyle.cayenne.util.Util; 73 74 81 public class ContextCommitObserver 82 extends DefaultOperationObserver 83 implements DataContextTransactionEventListener { 84 85 protected List updObjects; 86 protected List delObjects; 87 protected List insObjects; 88 protected List objectsToNotify; 89 90 protected DataContext context; 91 92 public ContextCommitObserver( 93 Level logLevel, 94 DataContext context, 95 List insObjects, 96 List updObjects, 97 List delObjects) { 98 99 super.setLoggingLevel(logLevel); 100 101 this.context = context; 102 this.insObjects = insObjects; 103 this.updObjects = updObjects; 104 this.delObjects = delObjects; 105 this.objectsToNotify = new ArrayList (); 106 107 Iterator collIter = 112 (Arrays.asList(new List [] { delObjects, updObjects, insObjects })) 113 .iterator(); 114 while (collIter.hasNext()) { 115 Iterator objIter = ((Collection ) collIter.next()).iterator(); 116 while (objIter.hasNext()) { 117 Object element = objIter.next(); 118 if (element instanceof DataObjectTransactionEventListener) { 119 this.objectsToNotify.add(element); 120 } 121 } 122 } 123 } 124 125 public void nextQueryException(Query query, Exception ex) { 126 super.nextQueryException(query, ex); 127 throw new CayenneRuntimeException( 128 "Raising from query exception.", 129 Util.unwindException(ex)); 130 } 131 132 public void nextGlobalException(Exception ex) { 133 super.nextGlobalException(ex); 134 throw new CayenneRuntimeException( 135 "Raising from underlyingQueryEngine exception.", 136 Util.unwindException(ex)); 137 } 138 139 public void registerForDataContextEvents() { 140 EventManager mgr = EventManager.getDefaultManager(); 141 mgr.addListener( 142 this, 143 "dataContextWillCommit", 144 DataContextEvent.class, 145 DataContext.WILL_COMMIT, 146 this.context); 147 mgr.addListener( 148 this, 149 "dataContextDidCommit", 150 DataContextEvent.class, 151 DataContext.DID_COMMIT, 152 this.context); 153 mgr.addListener( 154 this, 155 "dataContextDidRollback", 156 DataContextEvent.class, 157 DataContext.DID_ROLLBACK, 158 this.context); 159 } 160 161 public void unregisterFromDataContextEvents() { 162 EventManager mgr = EventManager.getDefaultManager(); 163 mgr.removeListener(this, DataContext.WILL_COMMIT); 164 mgr.removeListener(this, DataContext.DID_COMMIT); 165 mgr.removeListener(this, DataContext.DID_ROLLBACK); 166 } 167 168 public void dataContextWillCommit(DataContextEvent event) { 169 Iterator iter = objectsToNotify.iterator(); 170 while (iter.hasNext()) { 171 ((DataObjectTransactionEventListener) iter.next()).willCommit( 172 event); 173 } 174 } 175 176 public void dataContextDidCommit(DataContextEvent event) { 177 Iterator iter = objectsToNotify.iterator(); 178 while (iter.hasNext()) { 179 ((DataObjectTransactionEventListener) iter.next()).didCommit(event); 180 } 181 } 182 183 public void dataContextDidRollback(DataContextEvent event) { 184 } 186 } 187 | Popular Tags |