KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > DataContextFlushEventHandler


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.access;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
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 /**
35  * Handles DataContext events on domain flush.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  * @deprecated since 3.0M1 in favor of {@link LifecycleListener}. Will be removed in
40  * later 3.0 milestones.
41  */

42 class DataContextFlushEventHandler implements DataContextTransactionEventListener {
43
44     List JavaDoc objectsToNotify;
45
46     DataContext originatingContext;
47
48     DataContextFlushEventHandler(DataContext originatingContext) {
49         this.originatingContext = originatingContext;
50         this.objectsToNotify = new ArrayList JavaDoc();
51
52         // remember objects to notify of commit events
53

54         Iterator JavaDoc 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 JavaDoc iter = objectsToNotify.iterator();
100         while (iter.hasNext()) {
101             ((DataObjectTransactionEventListener) iter.next()).willCommit(event);
102         }
103     }
104
105     public void dataContextDidCommit(DataContextEvent event) {
106         Iterator JavaDoc iter = objectsToNotify.iterator();
107         while (iter.hasNext()) {
108             ((DataObjectTransactionEventListener) iter.next()).didCommit(event);
109         }
110     }
111
112     public void dataContextDidRollback(DataContextEvent event) {
113         // do nothing for now
114
}
115 }
116
Popular Tags