KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > ObjectContextStateLog


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;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.apache.cayenne.graph.GraphChangeHandler;
31 import org.apache.cayenne.graph.GraphManager;
32
33 /**
34  * Tracks dirty Persistent objects.
35  *
36  * @since 1.2
37  * @author Andrus Adamchik
38  */

39 class ObjectContextStateLog implements GraphChangeHandler {
40
41     Set JavaDoc dirtyIds;
42     GraphManager graphManager;
43
44     ObjectContextStateLog(GraphManager graphManager) {
45         this.dirtyIds = new HashSet JavaDoc();
46         this.graphManager = graphManager;
47     }
48
49     void clear() {
50         dirtyIds = new HashSet JavaDoc();
51     }
52
53     /**
54      * Updates dirty objects state and clears dirty ids map.
55      */

56     void graphCommitted() {
57         Iterator JavaDoc it = dirtyIds.iterator();
58         while (it.hasNext()) {
59             Object JavaDoc node = graphManager.getNode(it.next());
60             if (node instanceof Persistent) {
61                 Persistent persistentNode = (Persistent) node;
62                 switch (persistentNode.getPersistenceState()) {
63                     case PersistenceState.MODIFIED:
64                     case PersistenceState.NEW:
65                         persistentNode.setPersistenceState(PersistenceState.COMMITTED);
66                         break;
67                     case PersistenceState.DELETED:
68                         persistentNode.setPersistenceState(PersistenceState.TRANSIENT);
69                         break;
70                 }
71             }
72         }
73
74         clear();
75     }
76
77     void graphReverted() {
78         Iterator JavaDoc it = dirtyIds.iterator();
79         while (it.hasNext()) {
80             Object JavaDoc node = graphManager.getNode(it.next());
81             if (node instanceof Persistent) {
82                 Persistent persistentNode = (Persistent) node;
83                 switch (persistentNode.getPersistenceState()) {
84                     case PersistenceState.MODIFIED:
85                     case PersistenceState.DELETED:
86                         persistentNode.setPersistenceState(PersistenceState.COMMITTED);
87                         break;
88                     case PersistenceState.NEW:
89                         persistentNode.setPersistenceState(PersistenceState.TRANSIENT);
90                         break;
91                 }
92             }
93         }
94
95         clear();
96     }
97
98     boolean hasChanges() {
99         return !dirtyIds.isEmpty();
100     }
101
102     Collection JavaDoc dirtyNodes() {
103         if (dirtyIds.isEmpty()) {
104             return Collections.EMPTY_SET;
105         }
106
107         List JavaDoc objects = new ArrayList JavaDoc(dirtyIds.size());
108         Iterator JavaDoc it = dirtyIds.iterator();
109         while (it.hasNext()) {
110             objects.add(graphManager.getNode(it.next()));
111         }
112
113         return objects;
114     }
115
116     Collection JavaDoc dirtyNodes(int state) {
117         if (dirtyIds.isEmpty()) {
118             return Collections.EMPTY_SET;
119         }
120
121         int size = dirtyIds.size();
122         List JavaDoc objects = new ArrayList JavaDoc(size > 50 ? size / 2 : size);
123         Iterator JavaDoc it = dirtyIds.iterator();
124         while (it.hasNext()) {
125             Persistent o = (Persistent) graphManager.getNode(it.next());
126
127             if (o.getPersistenceState() == state) {
128                 objects.add(o);
129             }
130         }
131
132         return objects;
133     }
134
135     void unregisterNode(Object JavaDoc nodeId) {
136         dirtyIds.remove(nodeId);
137     }
138
139     // *** GraphChangeHandler methods
140

141     public void nodeIdChanged(Object JavaDoc nodeId, Object JavaDoc newId) {
142         if (dirtyIds.remove(nodeId)) {
143             dirtyIds.add(newId);
144         }
145     }
146
147     public void nodeCreated(Object JavaDoc nodeId) {
148         dirtyIds.add(nodeId);
149     }
150
151     public void nodeRemoved(Object JavaDoc nodeId) {
152         dirtyIds.add(nodeId);
153     }
154
155     public void nodePropertyChanged(
156             Object JavaDoc nodeId,
157             String JavaDoc property,
158             Object JavaDoc oldValue,
159             Object JavaDoc newValue) {
160         dirtyIds.add(nodeId);
161     }
162
163     public void arcCreated(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
164         dirtyIds.add(nodeId);
165     }
166
167     public void arcDeleted(Object JavaDoc nodeId, Object JavaDoc targetNodeId, Object JavaDoc arcId) {
168         dirtyIds.add(nodeId);
169     }
170
171 }
172
Popular Tags