Coverage Report - net.wotonomy.access.EODatabase
 
Classes in this File Line Coverage Branch Coverage Complexity
EODatabase
0% 
0% 
1.829
 
 1  
 /*
 2  
  Wotonomy: OpenStep design patterns for pure Java applications.
 3  
  Copyright (C) 2001 Michael Powers
 4  
 
 5  
  This library is free software; you can redistribute it and/or
 6  
  modify it under the terms of the GNU Lesser General Public
 7  
  License as published by the Free Software Foundation; either
 8  
  version 2.1 of the License, or (at your option) any later version.
 9  
 
 10  
  This library is distributed in the hope that it will be useful,
 11  
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  Lesser General Public License for more details.
 14  
 
 15  
  You should have received a copy of the GNU Lesser General Public
 16  
  License along with this library; if not, see http://www.gnu.org
 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  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  
  * $Log$
 251  
  * Revision 1.2  2006/02/16 16:47:13  cgruber
 252  
  * Move some classes in to "internal" packages and re-work imports, etc.
 253  
  *
 254  
  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
 255  
  *
 256  
  * Revision 1.1  2006/02/16 13:19:57  cgruber
 257  
  * Check in all sources in eclipse-friendly maven-enabled packages.
 258  
  *
 259  
  * Revision 1.2  2005/05/11 15:21:53  cgruber
 260  
  * Change enum to enumeration, since enum is now a keyword as of Java 5.0
 261  
  *
 262  
  * A few other comments in the code.
 263  
  *
 264  
  * Revision 1.1  2003/08/19 01:54:43  chochos
 265  
  * The EODatabase layer still needs a lot of work, but it's on its way...
 266  
  *
 267  
  */