View Javadoc

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  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  * $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  */