Re: Filtered to many relationship
Re: Filtered to many relationship
- Subject: Re: Filtered to many relationship
- From: Pierre Bernard <email@hidden>
- Date: Fri, 2 May 2008 09:22:33 +0200
The brave may experiment with this:
public class GenericRecord extends EOGenericRecord
{
// Relationship filtering
public void clearFilteredRelationships()
{
Enumeration filteredRelationshipKeys =
_filteredRelationshipKeys().objectEnumerator();
willRead();
while (filteredRelationshipKeys.hasMoreElements())
{
String key = (String) filteredRelationshipKeys.nextElement();
_clearFilteredRelationship(key);
}
}
/** Returns the list of keys to potentially filtered toMany
relationships
*
* @return NSArray of String relationship keys
*/
protected NSArray _filteredRelationshipKeys()
{
return NSArray.EmptyArray;
}
protected void _clearFilteredRelationship(String key)
{
FilteredMutableArray filteredArray = _filteredArrayForKey(key);
filteredArray
.turnIntoFault
(FilteringArrayFaultHandler.faultHandler(editingContext()));
}
/** Filtering
* @return _filteredArrayForKey
*/
protected FilteredMutableArray _filteredArrayForKey(String key)
{
FilteredMutableArray array =
(FilteredMutableArray) _filteredArrayRepository().valueForKey(key);
if (array == null)
{
synchronized (this)
{
if (array == null)
{
array = new FilteredMutableArray(this, key);
_filteredArrayRepository().setObjectForKey(array, key);
}
}
}
return array;
}
/** Filtering
* @return _filteredArrayRepository
*/
private NSMutableDictionary _filteredArrayRepository()
{
if (_filteredArrayRepository == null)
{
_filteredArrayRepository = new NSMutableDictionary();
}
return _filteredArrayRepository;
}
}
public class SomeEO extends GenericRecord
{
public NSArray someToManyKey()
{
EOEditingContext editingContext = editingContext();
if ((editingContext instanceof FilteringEditingContext)
&& (! ((FilteringEditingContext) editingContext).saveInProgress()))
{
_clearFilteredRelationship(SOME_TO_MANY_KEY);
return _filteredArrayForKey(SOME_TO_MANY_KEY);
}
else
{
return (NSArray) storedValueForKey(SOME_TO_MANY_KEY);
}
}
}
/** Mutable array used by the Map objects for representing filtered
toMany relationships
*
* @author bernard
*/
public class FilteredMutableArray extends _EOCheapCopyMutableArray
{
// Priovate instance variables
private String key;
private EOEnterpriseObject owner;
// Constructor
public FilteredMutableArray(EOEnterpriseObject owner, String key)
{
this.owner = owner;
this.key = key;
}
// Public instance methods
public void addObject(Object object)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).addObject(object);
}
super.addObject(object);
}
public void addObjects(Object[] objects)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).addObjects(objects);
}
super.addObjects(objects);
}
public void removeAllObjects()
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
NSMutableArray mutableArray = (NSMutableArray) storedArray;
Enumeration objects = objectEnumerator();
while (objects.hasMoreElements())
{
mutableArray.removeIdenticalObject(objects.nextElement());
}
}
super.removeAllObjects();
}
public Object removeObjectAtIndex(int index)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
Object object = objectAtIndex(index);
((NSMutableArray) storedArray).removeIdenticalObject(object);
}
return super.removeObjectAtIndex(index);
}
public Object replaceObjectAtIndex(Object object, int index)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
NSMutableArray mutableArray = (NSMutableArray) storedArray;
Object oldObject = objectAtIndex(index);
mutableArray.removeIdenticalObject(oldObject);
mutableArray.addObject(object);
}
return super.replaceObjectAtIndex(object, index);
}
/** Caveat: may remove an object from the master array despite it not
being in the filtered one.
*/
public void setArray(NSArray array)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
/*
NSMutableArray mutableArray = (NSMutableArray) storedArray;
Enumeration objects = objectEnumerator();
while (objects.hasMoreElements())
{
mutableArray.removeIdenticalObject(objects.nextElement());
}
mutableArray.addObjectsFromArray(array);
*/
NSMutableArray mutableArray = (NSMutableArray) storedArray;
mutableArray.setArray(array);
}
super.setArray(array);
}
public void addObjectsFromArray(NSArray array)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).addObjectsFromArray(array);
}
super.addObjectsFromArray(array);
}
public boolean removeIdenticalObject(Object object)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).removeIdenticalObject(object);
}
return super.removeIdenticalObject(object);
}
public Object removeLastObject()
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
Object oldObject = lastObject();
((NSMutableArray) storedArray).removeIdenticalObject(oldObject);
}
return super.removeLastObject();
}
/** Caveat: may remove an object from the master array despite it not
being in the filtered one.
*/
public boolean removeObject(Object object, NSRange range)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).removeIdenticalObject(object);
}
return super.removeObject(object, range);
}
/** Caveat: may remove an object from the master array despite it not
being in the filtered one.
*/
public boolean removeObject(Object object)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).removeObject(object);
}
return super.removeObject(object);
}
/** Caveat: may remove an object from the master array despite it not
being in the filtered one.
*/
public void removeObjects(Object[] objects)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).removeObjects(objects);
}
super.removeObjects(objects);
}
/** Caveat: may remove an object from the master array despite it not
being in the filtered one.
*/
public void removeObjectsInArray(NSArray array)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
((NSMutableArray) storedArray).removeObjectsInArray(array);
}
super.removeObjectsInArray(array);
}
public void removeObjectsInRange(NSRange range)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
Object[] objects = objects(range);
((NSMutableArray) storedArray).removeObjects(objects);
}
super.removeObjectsInRange(range);
}
public void replaceObjectsInRange(NSRange range, NSArray otherArray,
NSRange otherRange)
{
NSArray storedArray = storedArray();
if (storedArray instanceof NSMutableArray)
{
NSMutableArray mutableArray = (NSMutableArray) storedArray;
Object[] objects = objects(range);
mutableArray.removeObjects(objects);
mutableArray.addObjects(otherArray.objects(otherRange));
}
super.replaceObjectsInRange(range, otherArray, otherRange);
}
// Protected instance methods
protected String key()
{
return this.key;
}
protected EOEnterpriseObject owner()
{
return this.owner;
}
protected NSArray storedArray()
{
NSArray storedArray;
owner().willRead();
storedArray = (NSArray) owner().storedValueForKey(key());
if (storedArray == null)
{
storedArray = new _EOCheapCopyMutableArray();;
owner().takeStoredValueForKey(storedArray, key);
}
return storedArray;
}
protected void refreshWithFilter(EOQualifier qualifier)
{
NSArray array = storedArray();
if (qualifier != null)
{
if (qualifier instanceof BestMatchQualifier)
((BestMatchQualifier) qualifier).prepareWithArray(array);
array = EOQualifier.filteredArrayWithQualifier(array, qualifier);
}
super.setArray(array);
}
}
/** Allows the filtering of toMany relationships.
*
* @author bernard, trotobas
*/
public class FilteringArrayFaultHandler extends EOFaultHandler
{
// Private class variables
private static WeakHashMap handlers = new WeakHashMap();
// Private instance variables
private EOEditingContext editingContext;
// Private constructor
/** Use factory method
*
* @param editingContext
*/
private FilteringArrayFaultHandler(
EOEditingContext editingContext)
{
this.editingContext = editingContext;
}
// Public instance methods
/**
* @see
com
.webobjects
.eocontrol
.EOFaultHandler#completeInitializationOfObject(java.lang.Object)
*/
public void completeInitializationOfObject(Object toMany)
{
FilteredMutableArray _filteredArray = (FilteredMutableArray) toMany;
EOQualifier qualifier = null;
((EOFaulting) _filteredArray).clearFault();
if (this.editingContext instanceof FilteringEditingContext)
{
qualifier =
((FilteringEditingContext)
this.editingContext).periodQualifier(); // Well, whatever you see fit
}
_filteredArray.refreshWithFilter(qualifier);
}
/**@see
com.webobjects.eocontrol.EOFaultHandler#faultWillFire(java.lang.Object)
*/
public void faultWillFire(Object arg0)
{
}
// Public class methods
public static FilteringArrayFaultHandler
faultHandler(EOEditingContext editingContext)
{
FilteringArrayFaultHandler faultHandler =
(FilteringArrayFaultHandler) handlers.get(editingContext);
if (faultHandler == null)
{
synchronized (handlers)
{
faultHandler = (FilteringArrayFaultHandler)
handlers.get(editingContext);
if (faultHandler == null)
{
faultHandler = new FilteringArrayFaultHandler(editingContext);
handlers.put(editingContext, faultHandler);
}
}
}
return faultHandler;
}
}
Best,
Pierre Bernard
Houdah Software s.à r.l.
On 2 May 2008, at 06:10, Chuck Hill wrote:
On May 1, 2008, at 7:31 PM, Steve Peery wrote:
I am trying to set up a to many relationship in Entity Modeler that
filters
the destination objects based on a variable in the destination
object.
For example, in my School object I have an array of Student objects
some
have a variable that defines them as fulltime. Now in my School
object I
want to create a to many relationship that just contains the
Student objects
that are fulltime. How is this done?
It is not. At least not without some rather clever work in your EO
class. The "normal" way to do this is to add a method to your EO
class, something like this:
public NSArray fulltimeStudents() {
return EOQualifier. filteredArrayWithQualifier(students(), <your
full-time student qualifier here>);
}
Chuck
--
Practical WebObjects - for developers who want to increase their
overall knowledge of WebObjects or who are trying to solve specific
problems.
http://www.global-village.net/products/practical_webobjects
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
---
Pierre Bernard
http://www.bernard-web.com/pierre
http://www.houdah.com
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden