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