KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > intercept > SyncCallbackProcessor


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 package org.apache.cayenne.intercept;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.apache.cayenne.DataChannel;
27 import org.apache.cayenne.LifecycleListener;
28 import org.apache.cayenne.graph.GraphChangeHandler;
29 import org.apache.cayenne.graph.GraphDiff;
30 import org.apache.cayenne.graph.GraphManager;
31
32 class SyncCallbackProcessor implements GraphChangeHandler {
33
34     final DataChannelCallbackInterceptor interceptor;
35     private GraphManager graphManager;
36     Collection JavaDoc updated;
37     Collection JavaDoc persisted;
38     Collection JavaDoc removed;
39     private Set JavaDoc seenIds;
40
41     SyncCallbackProcessor(DataChannelCallbackInterceptor interceptor,
42             GraphManager graphManager, GraphDiff changes) {
43         this.interceptor = interceptor;
44         this.seenIds = new HashSet JavaDoc();
45         this.graphManager = graphManager;
46         changes.apply(this);
47     }
48
49     void applyPreCommit(int syncType) {
50         switch (syncType) {
51             case DataChannel.FLUSH_CASCADE_SYNC:
52             case DataChannel.FLUSH_NOCASCADE_SYNC:
53                 apply(LifecycleListener.PRE_UPDATE, updated);
54
55                 if (interceptor.isContextCallbacksEnabled()) {
56                     apply(LifecycleListener.PRE_PERSIST, persisted);
57                     apply(LifecycleListener.PRE_REMOVE, removed);
58                 }
59         }
60     }
61
62     void applyPostCommit(int syncType) {
63         switch (syncType) {
64             case DataChannel.FLUSH_CASCADE_SYNC:
65             case DataChannel.FLUSH_NOCASCADE_SYNC:
66                 apply(LifecycleListener.POST_UPDATE, updated);
67                 apply(LifecycleListener.POST_REMOVE, removed);
68                 apply(LifecycleListener.POST_PERSIST, persisted);
69                 break;
70             case DataChannel.ROLLBACK_CASCADE_SYNC:
71                 apply(LifecycleListener.POST_LOAD, updated);
72                 apply(LifecycleListener.POST_LOAD, removed);
73         }
74     }
75
76     void apply(int callbackType, Collection JavaDoc objects) {
77         if (objects != null) {
78             interceptor.getCallbackRegistry().performCallbacks(callbackType, objects);
79         }
80     }
81
82     public void nodeCreated(Object JavaDoc nodeId) {
83         if (seenIds.add(nodeId)) {
84
85             Object JavaDoc node = graphManager.getNode(nodeId);
86             if (node != null) {
87
88                 if (persisted == null) {
89                     persisted = new ArrayList JavaDoc();
90                 }
91
92                 persisted.add(node);
93             }
94         }
95     }
96
97     public void nodeRemoved(Object JavaDoc nodeId) {
98         if (seenIds.add(nodeId)) {
99
100             Object JavaDoc node = graphManager.getNode(nodeId);
101             if (node != null) {
102
103                 if (removed == null) {
104                     removed = new ArrayList JavaDoc();
105                 }
106
107                 removed.add(node);
108             }
109         }
110     }
111
112     public void arcCreated(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
113         // TODO: andrus, 9/21/2006 - should we register to-many relationship updates?
114
nodeUpdated(nodeId);
115     }
116
117     public void arcDeleted(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
118         // TODO: andrus, 9/21/2006 - should we register to-many relationship updates?
119
nodeUpdated(nodeId);
120     }
121
122     public void nodeIdChanged(Object JavaDoc nodeId, Object JavaDoc newId) {
123     }
124
125     public void nodePropertyChanged(
126             Object JavaDoc nodeId,
127             String JavaDoc property,
128             Object JavaDoc oldValue,
129             Object JavaDoc newValue) {
130         nodeUpdated(nodeId);
131     }
132
133     private void nodeUpdated(Object JavaDoc nodeId) {
134         if (seenIds.add(nodeId)) {
135
136             Object JavaDoc node = graphManager.getNode(nodeId);
137             if (node != null) {
138
139                 if (updated == null) {
140                     updated = new ArrayList JavaDoc();
141                 }
142
143                 updated.add(node);
144             }
145         }
146     }
147 }
Popular Tags