summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/EODatabaseDataSource.java
blob: 489d779b1829262c928f230e6962c34a4dfe8fe0 (plain)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
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.control;

import java.util.Collection;
import java.util.Map;

import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableArray;
import net.wotonomy.foundation.NSSet;
import net.wotonomy.foundation.internal.WotonomyException;

/**
 * EODatabaseSource is a general-purpose implementation of EODataSource that is
 * EOClassDescription-aware and that can vend appropriate EODetailDataSources.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 894 $
 */
public abstract class EODatabaseDataSource {
	EOQualifier auxiliaryQualifier;
	EOEditingContext editingContext;
	String entityName;
	String fetchSpecificationName;
	EOFetchSpecification fetchSpecification;
	NSDictionary qualifierBindings;
	EOClassDescription classDescription;
	boolean fetchEnabled;

	/**
	 * Constructs a data source that fetches all objects of the specified entity
	 * type.
	 */
	public EODatabaseDataSource(EOEditingContext aContext, String anEntityName) {
		this(aContext, anEntityName, null);
	}

	/**
	 * Constructs a data source that fetches objects of the specified entity type
	 * according to the fetch specification with the specified name.
	 */
	public EODatabaseDataSource(EOEditingContext aContext, String anEntityName, String aFetchSpecName) {
		fetchEnabled = true;
		editingContext = aContext;
		entityName = anEntityName;
		setFetchSpecificationByName(fetchSpecificationName);
	}

	/**
	 * Returns the qualifier that is applied to the results fetched by the fetch
	 * specification before objects are returned by fetch objects, or null if no
	 * such qualifier has been specified.
	 */
	public EOQualifier auxiliaryQualifier() {
		return auxiliaryQualifier;
	}

	/**
	 * Returns the description of the class of the objects that is vended by this
	 * data source, or null if no entity name is specified.
	 */
	public EOClassDescription classDescriptionForObjects() {
		if (entityName == null)
			return null;
		return EOClassDescription.classDescriptionForEntityName(entityName);
	}

	/**
	 * Returns the object store at the root of the editing context's editing
	 * hierarchy.
	 */
	public EOObjectStore databaseContext() {
		EOObjectStore store = editingContext();
		while (store instanceof EOEditingContext) {
			store = ((EOEditingContext) store).parentObjectStore();
		}
		return store;
	}

	/**
	 * Returns a detail data source that is capable of manipulating objects of the
	 * type returned by applying the specified key to objects vended by this data
	 * source.
	 * 
	 * @see #qualifyWithRelationshipKey
	 */
	public EODataSource dataSourceQualifiedByKey(String aKey) {
		throw new WotonomyException("Not implemented yet.");
	}

	/**
	 * Deletes the specified object from this data source. This implementation
	 * deletes the specified object from the editing context.
	 */
	public void deleteObject(Object anObject) {
		editingContext.deleteObject(anObject);
	}

	/**
	 * Returns the editing context for this data source, or null if no editing
	 * context was specified.
	 */
	public EOEditingContext editingContext() {
		return editingContext;
	}

	/*
	 * public EOEntity entity() {}
	 */

	/**
	 * Returns a List containing the objects of the current entity type that conform
	 * to the specified fetch specification. If an auxiliary qualifier has been
	 * specified, that qualifier is applied to the objects before returning the
	 * result. If fetch is not enabled, this method returns null.
	 */
	public NSArray fetchObjects() {
		if (!isFetchEnabled())
			return null;
		NSArray result = editingContext.objectsWithFetchSpecification(fetchSpecification());
		if (auxiliaryQualifier() != null) {
			result = EOQualifier.filteredArrayWithQualifier(result, auxiliaryQualifier());
		}
		return result;
	}

	/**
	 * Returns the fetch specification currently used by this data source to fetch
	 * objects, or null if none is specified. If null, this fetchObjects() will
	 * return all objects of the specified entity type.
	 */
	public EOFetchSpecification fetchSpecification() {
		return fetchSpecification;
	}

	/**
	 * Returns a copy of the fetch specification that will be used to determine
	 * fetch for this data source. If this data source has an auxiliary qualifier,
	 * that qualifier will be inserted into the returned fetch specification's
	 * qualifier.
	 */
	public EOFetchSpecification fetchSpecificationForFetch() {
		EOFetchSpecification result = (EOFetchSpecification) fetchSpecification.clone();
		if (auxiliaryQualifier() != null) {
			NSMutableArray join = new NSMutableArray();
			join.addObject(fetchSpecification.qualifier());
			join.addObject(auxiliaryQualifier());
			result.setQualifier(new EOAndQualifier(join));
		}
		return result;
	}

	/**
	 * Returns the name of the current fetch specification, or null if no name has
	 * been specified.
	 */
	public String fetchSpecificationName() {
		return fetchSpecificationName;
	}

	/**
	 * Inserts the specified object into this data source. This implementation
	 * registers the object as an inserted object with the editing context.
	 */
	public void insertObject(Object anObject) {
		editingContext.insertObject(anObject);
	}

	/**
	 * Returns whether fetching is currently allowed. If false, fetchObjects() will
	 * return null. Default is true.
	 */
	public boolean isFetchEnabled() {
		return fetchEnabled;
	}

	/**
	 * Returns a List of the union of the binding keys for the fetch spec's
	 * qualifier and the auxiliary qualifier.
	 */
	public NSArray qualifierBindingKeys() {
		NSSet union = new NSSet();
		if ((fetchSpecification != null) && (fetchSpecification.qualifier() != null)) {
			union.addAll(fetchSpecification.qualifier().bindingKeys());
		}
		if (auxiliaryQualifier() != null) {
			union.addAll(auxiliaryQualifier().bindingKeys());
		}
		return new NSArray((Collection) union);
	}

	/**
	 * Returns a Map of the bindings that will be applied against the fetch spec's
	 * qualifier and the auxiliary qualifier, or null if no bindings exist.
	 */
	public NSDictionary qualifierBindings() {
		if (qualifierBindings == null)
			return null;
		return new NSDictionary((Map) qualifierBindings);
	}

	/**
	 * Restricts this data source to vend those objects that are associated with the
	 * specified key on the specified object.
	 */
	public void qualifyWithRelationshipKey(String aKey, Object anObject) {
		throw new WotonomyException("Not implemented yet");
	}

	/**
	 * Sets the auxiliary qualifier that will be applied to objects returned from
	 * the fetch described by the fetch specification.
	 */
	public void setAuxiliaryQualifier(EOQualifier aQualifier) {
		auxiliaryQualifier = aQualifier;
	}

	/**
	 * Sets whether fetches are currently allowed. If false, fetchObjects() will
	 * return null.
	 */
	public void setFetchEnabled(boolean isFetchEnabled) {
		fetchEnabled = isFetchEnabled;
	}

	/**
	 * Sets the fetch specification used by this data source. If null, all objects
	 * of the specified entity type will be returned by fetchObjects().
	 */
	public void setFetchSpecification(EOFetchSpecification aFetchSpec) {
		fetchSpecificationName = null;
		fetchSpecification = aFetchSpec;
	}

	/**
	 * Sets the fetch specification used by this data source, requesting it from the
	 * class description for this data source's entity class description, if any. If
	 * the name cannot be resolved, the fetch specification will be set to null.
	 */
	public void setFetchSpecificationByName(String aName) {
		fetchSpecificationName = aName;
		fetchSpecification = EOFetchSpecification.fetchSpecificationNamed(aName, entityName);
	}

	/*
	 * public void setParentDataSourceRelationshipKey( EODataSource aDataSource,
	 * String aKey)
	 */

	/**
	 * Sets the bindings to be applied to the fetch specification and the auxiliary
	 * qualifier.
	 */
	public void setQualifierBindings(Map aBindingMap) {
		if (aBindingMap == null) {
			qualifierBindings = null;
		} else {
			qualifierBindings = new NSDictionary((Map) aBindingMap);
		}
	}
}

/*
 * $Log$ Revision 1.2 2006/02/16 16:47:14 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.1 2001/11/24 17:38:00 mpowers Contributing EODatabaseDataSource.
 *
 *
 */