1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package net.wotonomy.access;
19
20 import java.util.Enumeration;
21
22 import net.wotonomy.control.EOEnterpriseObject;
23 import net.wotonomy.control.EOGlobalID;
24 import net.wotonomy.foundation.NSArray;
25 import net.wotonomy.foundation.NSDictionary;
26 import net.wotonomy.foundation.NSMutableArray;
27 import net.wotonomy.foundation.NSMutableDictionary;
28 import net.wotonomy.foundation.NSTimestamp;
29
30 /***
31 *
32 * @author ezamudio@nasoft.com
33 * @author $Author: cgruber $
34 * @version $Revision: 894 $
35 */
36 public class EODatabase {
37
38 protected EOAdaptor _adaptor;
39 protected NSMutableArray _models = new NSMutableArray();
40 protected NSMutableArray _contexts = new NSMutableArray();
41 protected NSMutableDictionary _resultCache = new NSMutableDictionary();
42 protected NSMutableDictionary _snapshots = new NSMutableDictionary();
43 protected NSTimestamp _timestamp;
44 protected static boolean _releaseUnrefSnapshots = true;
45
46 public EODatabase(EOAdaptor adaptor) {
47 super();
48 if (adaptor == null)
49 throw new IllegalArgumentException("Adaptor cannot be null.");
50 _adaptor = adaptor;
51 }
52
53 public EODatabase(EOModel model) {
54 super();
55 _adaptor = EOAdaptor.adaptorWithModel(model);
56 addModel(model);
57 }
58
59 public EOAdaptor adaptor() {
60 return _adaptor;
61 }
62
63 public void addModel(EOModel model) {
64 if (!addModelIfCompatible(model))
65 throw new IllegalArgumentException("Model is not compatible with this database.");
66 }
67
68 public void removeMode(EOModel model) {
69 _models.removeObject(model);
70 }
71
72 public boolean addModelIfCompatible(EOModel model) {
73 if (_models.containsObject(model))
74 return false;
75 if (model.adaptorName().equals(adaptor().name())) {
76 if (adaptor().canServiceModel(model)) {
77 _models.addObject(model);
78 return true;
79 }
80 }
81 return false;
82 }
83
84 public void decrementSnapshotCountForGlobalID(EOGlobalID gid) {
85 if (_releaseUnrefSnapshots) {
86 }
87 }
88
89 public void incrementSnapshotCountForGlobalID(EOGlobalID gid) {
90 if (_releaseUnrefSnapshots) {
91 }
92 }
93
94 public static void disableSnapshotRefCounting() {
95 _releaseUnrefSnapshots = false;
96 }
97
98 public EOEntity entityForObject(EOEnterpriseObject eo) {
99 String cname = eo.getClass().getName();
100 for (int i = 0; i < _models.count(); i++) {
101 EOModel m = (EOModel)_models.objectAtIndex(i);
102 NSArray ents = m.entities();
103 for (int j = 0; j < ents.count(); j++) {
104 EOEntity e = (EOEntity)ents.objectAtIndex(i);
105 if (e.className().equals(cname))
106 return e;
107 }
108 }
109 return null;
110 }
111
112 public EOEntity entityNamed(String name) {
113 for (int i = 0; i < _models.count(); i++) {
114 EOModel m = (EOModel)_models.objectAtIndex(i);
115 NSArray ents = m.entities();
116 for (int j = 0; j < ents.count(); j++) {
117 EOEntity e = (EOEntity)ents.objectAtIndex(i);
118 if (e.name().equals(name))
119 return e;
120 }
121 }
122 return null;
123 }
124
125 public void forgetAllSnapshots() {
126 _snapshots.removeAllObjects();
127 }
128
129 public void forgetSnapshotForGlobalID(EOGlobalID gid) {
130 _snapshots.removeObjectForKey(gid);
131 }
132
133 public void forgetSnapshotsForGlobalIDs(NSArray gids) {
134 for (int i = 0; i < gids.count(); i++)
135 forgetSnapshotForGlobalID((EOGlobalID)gids.objectAtIndex(i));
136 }
137
138 public void handleDroppedConnection() {
139 adaptor().handleDroppedConnection();
140 for (int i = 0; i < _contexts.count(); i++) {
141 EODatabaseContext c = (EODatabaseContext)_contexts.objectAtIndex(i);
142 c.handleDroppedConnection();
143 }
144 }
145
146 public void invalidateResultCache() {
147 _resultCache.removeAllObjects();
148 }
149
150 public void invalidateResultCacheForEntityNamed(String name) {
151 _resultCache.removeObjectForKey(name);
152 }
153
154 public NSArray models() {
155 return new NSArray(_models);
156 }
157
158 public void recordSnapshotForGlobalID(NSDictionary snap, EOGlobalID gid) {
159 _snapshots.setObjectForKey(snap, gid);
160 }
161
162 public void recordSnapshotForSourceGlobalID(NSArray gids, EOGlobalID gid, String name) {
163 NSMutableDictionary d = (NSMutableDictionary)_snapshots.objectForKey(gid);
164 if (d == null) {
165 d = new NSMutableDictionary();
166 _snapshots.setObjectForKey(d, gid);
167 }
168 d.setObjectForKey(gids, name);
169 }
170
171 public void recordSnapshots(NSDictionary snaps) {
172 _snapshots.addEntriesFromDictionary(snaps);
173 }
174
175 public void recordToManySnapshots(NSDictionary snaps) {
176 Enumeration enumeration = snaps.keyEnumerator();
177 while (enumeration.hasMoreElements()) {
178 EOGlobalID gid = (EOGlobalID)enumeration.nextElement();
179 NSDictionary rels = (NSDictionary)snaps.objectForKey(gid);
180 Enumeration relEnum = rels.keyEnumerator();
181 while (relEnum.hasMoreElements()) {
182 String relName = (String)relEnum.nextElement();
183 NSArray gids = (NSArray)rels.objectForKey(relName);
184 recordSnapshotForSourceGlobalID(gids, gid, relName);
185 }
186 }
187 }
188
189 public void registerContext(EODatabaseContext context) {
190 if (!_contexts.contains(context)) {
191 if (context.database() != this)
192 throw new IllegalStateException("Cannot register context assigned to a different database.");
193 _contexts.addObject(context);
194 }
195 }
196
197 public void unregisterContext(EODatabaseContext context) {
198 _contexts.removeObject(context);
199 }
200
201 public NSArray registeredContexts() {
202 return new NSArray(_contexts);
203 }
204
205 public NSArray resultCacheForEntityNamed(String name) {
206 return (NSArray)_resultCache.objectForKey(name);
207 }
208
209 public void setResultCache(NSArray cache, String entityName) {
210 _resultCache.setObjectForKey(cache, entityName);
211 }
212
213 public void setTimestampToNow() {
214 _timestamp = new NSTimestamp();
215 }
216
217 public NSDictionary snapshotForGlobalID(EOGlobalID gid) {
218 return (NSDictionary)_snapshots.objectForKey(gid);
219 }
220
221 public NSDictionary snapshotForGlobalID(EOGlobalID gid, long l) {
222 return null;
223 }
224
225 public NSArray snapshotForSourceGlobalID(EOGlobalID gid, String name) {
226 NSDictionary d = (NSDictionary)_snapshots.objectForKey(gid);
227 if (d == null)
228 return null;
229 return (NSArray)d.objectForKey(name);
230 }
231
232 public NSDictionary snapshotForSourceGlobalID(EOGlobalID gid, String s, long l) {
233 return null;
234 }
235
236 public NSDictionary snapshots() {
237 return _snapshots;
238 }
239
240 public long timestampForGlobalID(EOGlobalID gid) {
241 return NSTimestamp.DistantPast.timeIntervalSinceReferenceDate();
242 }
243
244 public long timestampForSourceGlobalID(EOGlobalID gid, String s) {
245 return 0;
246 }
247
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267