1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Michael Powers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.access;
import java.util.Enumeration;
import net.wotonomy.control.EOEnterpriseObject;
import net.wotonomy.control.EOGlobalID;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSMutableDictionary;
import net.wotonomy.foundation.NSTimestamp;
/**
*
* @author ezamudio@nasoft.com
* @author $Author: cgruber $
* @version $Revision: 894 $
*/
public class EODatabase {
protected EOAdaptor _adaptor;
protected NSMutableArray _models = new NSMutableArray();
protected NSMutableArray _contexts = new NSMutableArray();
protected NSMutableDictionary _resultCache = new NSMutableDictionary();
protected NSMutableDictionary _snapshots = new NSMutableDictionary();
protected NSTimestamp _timestamp;
protected static boolean _releaseUnrefSnapshots = true;
public EODatabase(EOAdaptor adaptor) {
super();
if (adaptor == null)
throw new IllegalArgumentException("Adaptor cannot be null.");
_adaptor = adaptor;
}
public EODatabase(EOModel model) {
super();
_adaptor = EOAdaptor.adaptorWithModel(model);
addModel(model);
}
public EOAdaptor adaptor() {
return _adaptor;
}
public void addModel(EOModel model) {
if (!addModelIfCompatible(model))
throw new IllegalArgumentException("Model is not compatible with this database.");
}
public void removeMode(EOModel model) {
_models.removeObject(model);
}
public boolean addModelIfCompatible(EOModel model) {
if (_models.containsObject(model))
return false;
if (model.adaptorName().equals(adaptor().name())) {
if (adaptor().canServiceModel(model)) {
_models.addObject(model);
return true;
}
}
return false;
}
public void decrementSnapshotCountForGlobalID(EOGlobalID gid) {
if (_releaseUnrefSnapshots) {
}
}
public void incrementSnapshotCountForGlobalID(EOGlobalID gid) {
if (_releaseUnrefSnapshots) {
}
}
public static void disableSnapshotRefCounting() {
_releaseUnrefSnapshots = false;
}
public EOEntity entityForObject(EOEnterpriseObject eo) {
String cname = eo.getClass().getName();
for (int i = 0; i < _models.count(); i++) {
EOModel m = (EOModel)_models.objectAtIndex(i);
NSArray ents = m.entities();
for (int j = 0; j < ents.count(); j++) {
EOEntity e = (EOEntity)ents.objectAtIndex(i);
if (e.className().equals(cname))
return e;
}
}
return null;
}
public EOEntity entityNamed(String name) {
for (int i = 0; i < _models.count(); i++) {
EOModel m = (EOModel)_models.objectAtIndex(i);
NSArray ents = m.entities();
for (int j = 0; j < ents.count(); j++) {
EOEntity e = (EOEntity)ents.objectAtIndex(i);
if (e.name().equals(name))
return e;
}
}
return null;
}
public void forgetAllSnapshots() {
_snapshots.removeAllObjects();
}
public void forgetSnapshotForGlobalID(EOGlobalID gid) {
_snapshots.removeObjectForKey(gid);
}
public void forgetSnapshotsForGlobalIDs(NSArray gids) {
for (int i = 0; i < gids.count(); i++)
forgetSnapshotForGlobalID((EOGlobalID)gids.objectAtIndex(i));
}
public void handleDroppedConnection() {
adaptor().handleDroppedConnection();
for (int i = 0; i < _contexts.count(); i++) {
EODatabaseContext c = (EODatabaseContext)_contexts.objectAtIndex(i);
c.handleDroppedConnection();
}
}
public void invalidateResultCache() {
_resultCache.removeAllObjects();
}
public void invalidateResultCacheForEntityNamed(String name) {
_resultCache.removeObjectForKey(name);
}
public NSArray models() {
return new NSArray(_models);
}
public void recordSnapshotForGlobalID(NSDictionary snap, EOGlobalID gid) {
_snapshots.setObjectForKey(snap, gid);
}
public void recordSnapshotForSourceGlobalID(NSArray gids, EOGlobalID gid, String name) {
NSMutableDictionary d = (NSMutableDictionary)_snapshots.objectForKey(gid);
if (d == null) {
d = new NSMutableDictionary();
_snapshots.setObjectForKey(d, gid);
}
d.setObjectForKey(gids, name);
}
public void recordSnapshots(NSDictionary snaps) {
_snapshots.addEntriesFromDictionary(snaps);
}
public void recordToManySnapshots(NSDictionary snaps) {
Enumeration enumeration = snaps.keyEnumerator();
while (enumeration.hasMoreElements()) {
EOGlobalID gid = (EOGlobalID)enumeration.nextElement();
NSDictionary rels = (NSDictionary)snaps.objectForKey(gid);
Enumeration relEnum = rels.keyEnumerator();
while (relEnum.hasMoreElements()) {
String relName = (String)relEnum.nextElement();
NSArray gids = (NSArray)rels.objectForKey(relName);
recordSnapshotForSourceGlobalID(gids, gid, relName);
}
}
}
public void registerContext(EODatabaseContext context) {
if (!_contexts.contains(context)) {
if (context.database() != this)
throw new IllegalStateException("Cannot register context assigned to a different database.");
_contexts.addObject(context);
}
}
public void unregisterContext(EODatabaseContext context) {
_contexts.removeObject(context);
}
public NSArray registeredContexts() {
return new NSArray(_contexts);
}
public NSArray resultCacheForEntityNamed(String name) {
return (NSArray)_resultCache.objectForKey(name);
}
public void setResultCache(NSArray cache, String entityName) {
_resultCache.setObjectForKey(cache, entityName);
}
public void setTimestampToNow() {
_timestamp = new NSTimestamp();
}
public NSDictionary snapshotForGlobalID(EOGlobalID gid) {
return (NSDictionary)_snapshots.objectForKey(gid);
}
public NSDictionary snapshotForGlobalID(EOGlobalID gid, long l) {
return null;
}
public NSArray snapshotForSourceGlobalID(EOGlobalID gid, String name) {
NSDictionary d = (NSDictionary)_snapshots.objectForKey(gid);
if (d == null)
return null;
return (NSArray)d.objectForKey(name);
}
public NSDictionary snapshotForSourceGlobalID(EOGlobalID gid, String s, long l) {
return null;
}
public NSDictionary snapshots() {
return _snapshots;
}
public long timestampForGlobalID(EOGlobalID gid) {
return NSTimestamp.DistantPast.timeIntervalSinceReferenceDate();
}
public long timestampForSourceGlobalID(EOGlobalID gid, String s) {
return 0;
}
}
/*
* $Log$
* Revision 1.2 2006/02/16 16:47:13 cgruber
* Move some classes in to "internal" packages and re-work imports, etc.
*
* Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
*
* Revision 1.1 2006/02/16 13:19:57 cgruber
* Check in all sources in eclipse-friendly maven-enabled packages.
*
* Revision 1.2 2005/05/11 15:21:53 cgruber
* Change enum to enumeration, since enum is now a keyword as of Java 5.0
*
* A few other comments in the code.
*
* Revision 1.1 2003/08/19 01:54:43 chochos
* The EODatabase layer still needs a lot of work, but it's on its way...
*
*/
|