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