Re: Implementing an ordered tree
Re: Implementing an ordered tree
- Subject: Re: Implementing an ordered tree
- From: Lachlan Deck <email@hidden>
- Date: Sat, 28 Jan 2006 11:01:42 +1100
Hi there,
(removed cross-post)
On 23/01/2006, at 10:32 PM, Karoly Szantai wrote:
I have a Category entity. Each Category has zero or more
subcategories, which are ordered. There is a root Category (called
main category) which has no parent.
I have created this EO model:
oid
oidParentCategory
name
ordinalNumber
parentCategory (oidParentCategory --> oid)
subcategories (oid -->> oidParentCategory)
So, oid and oidParentCategory are primary and foreign keys and "name"
and "ordinalNumber" are class attributes. A category's ordinalNumber
is only relevant when it has a parentCategory.
An alternative model (which may help with below scenario(s)) would be
a many-to-many from Category to Category without flattened
relationships (so as to put the ordinalNumber in the join table.
---Category---
oid
name
parentCategory
subcategories
---Subcategories---
oid
pid
ordinalNumber
parentCategory
I need some hints on how to implement the following operations
(methods) with "ordered way":
-add a category
String name; // assume exists
Category aCategory;
aCategory = ( Category )EOUtilities.createAndInsertInstance( ec,
"Category" );
aCategory.takeValueForKey( name, "name" );
aCategory.addObjectToBothSidesOfRelationshipWithKey( parentCategory,
"parentCategory" );
-delete a category
Firsly, Make sure to set the "owns destination" on subcategories
relationship so that subcategories are automatically dropped when
their parent is deleted. The reverse direction should be "nullify"
Then to remove a subcat:
aSubcategory.removeObjectFromBothSidesOfRelationshipWithKey
( parentCategory, "parentCategory" );
ec.deleteObject( aSubcategory );
To delete a root parent:
ec.deleteObject( aParent );
-cut & paste a category (and its subcategories) from one level to
another level
aCategory.removeObjectFromBothSidesOfRelationshipWithKey
( oldParentCategory, "parentCategory" );
aCategory.addObjectToBothSidesOfRelationshipWithKey
( newParentCategory, "parentCategory" );
-reorder categories (move a category in its level)
EOSortOrdering sortOrder;
NSArray subcategories; // assume exists
sortOrder = EOSortOrdering.sortOrderingWithKey( "name",
EOSortOrder.CompareAscending );
subcategories = EOSortOrdering.sortedArrayUsingKeyOrderArray
( subcategories, new NSArray( sortOrder ) );
for ( int i = 0, count = subcategories.count(); i < count; i++ ) {
Category aCategory;
Number ordinalNumber;
aCategory = ( Category )subcategories.objectAtIndex( i );
ordinalNumber = aCategory.ordinalNumber();
if ( ordinalNumber == null || ordinalNumber.intValue() != i ) {
aCategory.takeValueForKey( new Integer( i ), "ordinalNumber" );
}
}
My big headache is manipulating the ordinalNumber attribute in "EO
way".
with regards,
--
Lachlan Deck
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