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  
19  package net.wotonomy.control;
20  
21  import java.util.Collection;
22  import java.util.Map;
23  
24  import net.wotonomy.foundation.NSArray;
25  import net.wotonomy.foundation.NSDictionary;
26  import net.wotonomy.foundation.NSMutableArray;
27  import net.wotonomy.foundation.NSSet;
28  import net.wotonomy.foundation.internal.WotonomyException;
29  
30  /***
31  * EODatabaseSource is a general-purpose implementation
32  * of EODataSource that is EOClassDescription-aware and
33  * that can vend appropriate EODetailDataSources.
34  *
35  * @author michael@mpowers.net
36  * @author $Author: cgruber $
37  * @version $Revision: 894 $
38  */
39  public abstract class EODatabaseDataSource
40  {
41      EOQualifier auxiliaryQualifier;
42      EOEditingContext editingContext;
43      String entityName;
44      String fetchSpecificationName;
45      EOFetchSpecification fetchSpecification;
46      NSDictionary qualifierBindings;
47      EOClassDescription classDescription;
48      boolean fetchEnabled;
49  
50      /***
51      * Constructs a data source that fetches all objects of 
52      * the specified entity type.
53      */
54      public EODatabaseDataSource( 
55          EOEditingContext aContext, String anEntityName)
56      {
57          this( aContext, anEntityName, null );
58      }
59      
60      /***
61      * Constructs a data source that fetches objects of the
62      * specified entity type according to the fetch specification
63      * with the specified name.
64      */ 
65      public EODatabaseDataSource( 
66          EOEditingContext aContext, String anEntityName, String aFetchSpecName)
67      {
68          fetchEnabled = true;
69          editingContext = aContext;
70          entityName = anEntityName;
71          setFetchSpecificationByName( fetchSpecificationName );
72      }
73  
74      /***
75      * Returns the qualifier that is applied to the results fetched by the fetch
76      * specification before objects are returned by fetch objects, or null if no 
77      * such qualifier has been specified.
78      */
79      public EOQualifier auxiliaryQualifier()
80      {
81          return auxiliaryQualifier;
82      }
83  
84      /***
85      * Returns the description of the class of the
86      * objects that is vended by this data source,
87      * or null if no entity name is specified.
88      */
89      public EOClassDescription classDescriptionForObjects ()
90      {
91          if ( entityName == null ) return null;
92          return EOClassDescription.classDescriptionForEntityName( entityName );
93      }
94  
95      /***
96      * Returns the object store at the root of the 
97      * editing context's editing hierarchy.
98      */
99      public EOObjectStore databaseContext()
100     {
101         EOObjectStore store = editingContext();
102         while ( store instanceof EOEditingContext )
103         {
104             store = ((EOEditingContext)store).parentObjectStore();
105         }
106         return store;
107     }
108 
109     /***
110     * Returns a detail data source that is capable of
111     * manipulating objects of the type returned by 
112     * applying the specified key to objects 
113     * vended by this data source.
114     * @see #qualifyWithRelationshipKey
115     */
116     public EODataSource dataSourceQualifiedByKey ( String aKey )
117     {
118         throw new WotonomyException( "Not implemented yet." );
119     }
120 
121     /***
122     * Deletes the specified object from this data source.
123     * This implementation deletes the specified object from
124     * the editing context.
125     */
126     public void deleteObject ( Object anObject )
127     {
128         editingContext.deleteObject( anObject );
129     }
130 
131     /***
132     * Returns the editing context for this data source,
133     * or null if no editing context was specified.
134     */
135     public EOEditingContext editingContext ()
136     {
137         return editingContext;
138     }
139 
140 /*
141     public EOEntity entity() {}
142 */
143 
144     /***
145     * Returns a List containing the objects of the current
146     * entity type that conform to the specified fetch specification.
147     * If an auxiliary qualifier has been specified, that qualifier
148     * is applied to the objects before returning the result.  
149     * If fetch is not enabled, this method returns null.
150     */
151     public NSArray fetchObjects ()
152     {
153         if ( ! isFetchEnabled() ) return null;
154         NSArray result = 
155             editingContext.objectsWithFetchSpecification( fetchSpecification() );
156         if ( auxiliaryQualifier() != null )
157         {
158             result = EOQualifier.filteredArrayWithQualifier( result, auxiliaryQualifier() );
159         }
160         return result;
161     }
162     
163     /***
164     * Returns the fetch specification currently used by this data
165     * source to fetch objects, or null if none is specified.
166     * If null, this fetchObjects() will return all objects of the
167     * specified entity type.
168     */
169     public EOFetchSpecification fetchSpecification()
170     {
171         return fetchSpecification;
172     }
173 
174     /***
175     * Returns a copy of the fetch specification that will be used to 
176     * determine fetch for this data source.  If this data source has
177     * an auxiliary qualifier, that qualifier will be inserted into
178     * the returned fetch specification's qualifier.
179     */
180     public EOFetchSpecification fetchSpecificationForFetch()
181     {
182         EOFetchSpecification result = (EOFetchSpecification) fetchSpecification.clone();
183         if ( auxiliaryQualifier() != null )
184         {
185             NSMutableArray join = new NSMutableArray();
186             join.addObject( fetchSpecification.qualifier() );
187             join.addObject( auxiliaryQualifier() );
188             result.setQualifier( new EOAndQualifier( join ) );
189         }
190         return result;
191     }
192 
193     /***
194     * Returns the name of the current fetch specification, or null
195     * if no name has been specified.
196     */
197     public String fetchSpecificationName()
198     {
199         return fetchSpecificationName;
200     }
201 
202     /***
203     * Inserts the specified object into this data source.
204     * This implementation registers the object as an inserted
205     * object with the editing context.
206     */
207     public void insertObject ( Object anObject )
208     {
209         editingContext.insertObject( anObject );
210     }
211 
212     /***
213     * Returns whether fetching is currently allowed.
214     * If false, fetchObjects() will return null.
215     * Default is true.
216     */
217     public boolean isFetchEnabled()
218     {
219         return fetchEnabled;
220     }
221 
222     /***
223     * Returns a List of the union of the binding keys for the fetch spec's
224     * qualifier and the auxiliary qualifier.
225     */
226     public NSArray qualifierBindingKeys()
227     {
228         NSSet union = new NSSet();
229         if ( ( fetchSpecification != null ) 
230         && ( fetchSpecification.qualifier() != null ) )
231         {
232             union.addAll( fetchSpecification.qualifier().bindingKeys() );
233         }
234         if ( auxiliaryQualifier() != null )
235         {
236             union.addAll( auxiliaryQualifier().bindingKeys() );
237         }
238         return new NSArray( (Collection) union );
239     }
240 
241     /***
242     * Returns a Map of the bindings that will be applied against
243     * the fetch spec's qualifier and the auxiliary qualifier, 
244     * or null if no bindings exist.
245     */
246     public NSDictionary qualifierBindings()
247     {
248         if ( qualifierBindings == null ) return null;
249         return new NSDictionary( (Map) qualifierBindings );
250     }
251 
252     /***
253     * Restricts this data source to vend those 
254     * objects that are associated with the specified 
255     * key on the specified object.
256     */
257     public void qualifyWithRelationshipKey ( 
258         String aKey, Object anObject )
259     {
260         throw new WotonomyException( "Not implemented yet" );
261     }
262 
263     /***
264     * Sets the auxiliary qualifier that will be applied to 
265     * objects returned from the fetch described by the fetch specification.
266     */
267     public void setAuxiliaryQualifier(EOQualifier aQualifier)
268     {
269         auxiliaryQualifier = aQualifier;
270     }
271 
272     /***
273     * Sets whether fetches are currently allowed.  
274     * If false, fetchObjects() will return null.
275     */
276     public void setFetchEnabled(boolean isFetchEnabled)
277     {
278         fetchEnabled = isFetchEnabled;
279     }
280 
281     /***
282     * Sets the fetch specification used by this data source.
283     * If null, all objects of the specified entity type will 
284     * be returned by fetchObjects().
285     */
286     public void setFetchSpecification( EOFetchSpecification aFetchSpec)
287     {
288         fetchSpecificationName = null;
289         fetchSpecification = aFetchSpec;
290     }
291 
292     /***
293     * Sets the fetch specification used by this data source,
294     * requesting it from the class description for this data source's
295     * entity class description, if any.  If the name cannot be resolved,
296     * the fetch specification will be set to null.
297     */
298     public void setFetchSpecificationByName(String aName)
299     {
300         fetchSpecificationName = aName;
301         fetchSpecification = EOFetchSpecification.fetchSpecificationNamed( aName, entityName );
302     }
303 
304     /*
305     public void setParentDataSourceRelationshipKey( EODataSource aDataSource, String aKey)
306     */
307 
308     /***
309     * Sets the bindings to be applied to the fetch specification and the auxiliary qualifier.
310     */
311     public void setQualifierBindings(Map aBindingMap)
312     {
313         if ( aBindingMap == null )
314         {
315             qualifierBindings = null;
316         }
317         else
318         {
319             qualifierBindings = new NSDictionary( (Map) aBindingMap );
320         }
321     }
322 }
323 
324 /*
325  * $Log$
326  * Revision 1.2  2006/02/16 16:47:14  cgruber
327  * Move some classes in to "internal" packages and re-work imports, etc.
328  *
329  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
330  *
331  * Revision 1.1  2006/02/16 13:19:57  cgruber
332  * Check in all sources in eclipse-friendly maven-enabled packages.
333  *
334  * Revision 1.1  2001/11/24 17:38:00  mpowers
335  * Contributing EODatabaseDataSource.
336  *
337  *
338  */
339