Re: EOF Design Questions
Francis Labrie <[email protected]> Thu, 8 Apr 2004 10:38:33 -0400
| Newsgroups | gmane.comp.web.webobjects.eof |
|---|---|
| Message-ID | <[email protected]> |
Hi Ashley,
Ashley Aitken wrote:
> The standard use of EOF is something like this:
>
> Fetch a number of EOs into an EC (as needed).
> Make changes to some or all of the EOs, i.e. EOi(StateAi) --->
> EOi(StateBi)
> Add some new EOs or delete some of the fetched EOs
>
> I am wondering how to the following efficiently, after the above:
>
> Clone all the EOs in the EC that have been change, i.e. EOi(StateBi)
> ---> EOj(StateBi)
> Revert only the original EOs to their fetched state, i.e. EOi(StateBi)
> ---> EOi(StateAi)
> Note: Any deleted EOs would not be cloned but still reverted, i.e.
> no-longer deleted
> Make some more changes to the original fetched EOs, i.e. EOi(StateAi)
> ---> EOi(StateCi)
> Save all the originally fetched EOs and new EOs i.e. ec.saveChanges()
> Note: The new EOs include (really) new EOs and cloned EOs.
>
> I've looked at the APIs for EC and EOGenericRecord and nothing pops
> out, specifically with respect to:
>
> 1. How best to clone an EO in an EC?
> Create and insert a new EO, transfer state, ...
You can define an abstract superclass (i.e. ClonableGenericRecord) for
all your cloneable entities extending EOGenericRecord Cloneable with a
method that does something like this:
public EOEnterpriseObject clone() {
Enumeration keys;
EOEnterpriseObject object;
Object value;
String key;
object =
EOClassDescription.classDescriptionForEntityName(entityName()).
createInstanceWithEditingContext(editingContext(), null);
keys = allPropertyKeys().objectEnumerator();
while(keys.hasMoreElements()) {
key = (String)keys.nextElement();
object.takeValueForKey(valueForKey(key));
} // while
return(object);
} // clone
But this clone the targeted entity only, i.e. all relationships will be
same for both the original and the cloned entity:
EOA.toEOB -> clone -> EOA'.toEOB
If you want to clone entities in relationships as well like this:
EOA.toEOB -> clone -> EOA'.toEOB'
Then you'll have to modify the clone() loop to build one for each
attributeKeys(), toOneRelationshipKeys() and toManyRelationshipKeys()
instead of using allPropertyKeys().
> 2. How best to revert some EOs (but not all) in the EC?
> Using the original snapshot ... but could that change (is that a
> problem)?
You can use the editing context revert() method. Setting up the
original snapshot shouldn't be a problem though.
--
Francis Labrie
Saint-Bruno-de-Montarville, Quebec, Canada