Hello,
Here is the problem I am trying to solve:
1. I have an 'Invoice' entity with a 'number' attribute. The attribute's type is VARCHAR. This is completely intentional, as the client may want the "number" to contain letters at some point. (Pretend the attribute name is "identifier" if it hurts your brain. :-)
2. I want to display the Invoice objects in a WODisplayGroup in a custom order. At the moment, that order is simple: all values for Invoice.number are currently actual numbers. So, for now, I want to cast them to integers, and sort them numerically. (This may change later, as letters may be appended.)
3. Using a standard (array of) EOSortOrdering (Invoice.NUMBER.ascs()), the "numbers" are obviously sorted in ASCII order. {1, 10, 11, 2...} For now, what I want is {1, 2, 3, ... 10, 11, ...}.
This was my first attempt (in Invoice.java):
private static NSSelector<Integer> numberAsInteger = new NSSelector<Integer>("compareNumberAsInteger", new Class[] { String.class, String.class });
public static NSArray<EOSortOrdering> NUMBER_SORT_ORD = new NSArray<EOSortOrdering>(new EOSortOrdering(NUMBER_KEY, numberAsInteger));
public static int compareNumberAsInteger(String s1, String s2) { Integer i1 = new Integer(s1); Integer i2 = new Integer(s2); int compare = i1.compareTo(i2); if (compare == 0) { return NSComparator.OrderedSame; } else if (compare < 0) { return NSComparator.OrderedAscending; } else { return NSComparator.OrderedDescending; } }
But I am obviously misunderstanding the semantics of EOSortOrdering's constructor, because it appears that the NSSelector is being called on String, rather than on Invoice using the String keys:
May 05 11:05:34 PBF[56565] WARN NSLog - <com.webobjects.appserver._private.WOComponentRequestHandler>: Exception occurred while handling request: com.webobjects.foundation.NSForwardException [java.lang.NoSuchMethodException] Class java.lang.String does not implement method compareNumberAsInteger:java.lang.NoSuchMethodException: Class java.lang.String does not implement method compareNumberAsInteger [2010-5-5 11:5:34 CST] <WorkerThread10> com.webobjects.foundation.NSForwardException [java.lang.NoSuchMethodException] Class java.lang.String does not implement method compareNumberAsInteger:java.lang.NoSuchMethodException: Class java.lang.String does not implement method compareNumberAsInteger at com.webobjects.foundation.NSForwardException._runtimeExceptionForThrowable(NSForwardException.java:41) at com.webobjects.eocontrol.EOSortOrdering$ComparisonSupport._compareWithArbitrarySelector(EOSortOrdering.java:553) at com.webobjects.eocontrol.EOSortOrdering$ComparisonSupport.compareValues(EOSortOrdering.java:545) at com.webobjects.eocontrol.EOSortOrdering$_SingleValueComparator.compare(EOSortOrdering.java:373)
What am I missing here? I can write an NSComparator for these number-as-string keys, if that's what's required, but then what do I do with that? What I want is the EOSortOrdering for the WODisplayGroup (don't I?), I don't see where the comparator would fit in.
As a workaround, I can add this to Invoice.java:
public Integer numberAsInteger() { return new Integer(number()); }
and then pass:
new EOSortOrdering("numberAsInteger", EOSortOrdering.CompareAscending)
to the WODisplayGroup. But that's only going to work while I know the attribute values are integers-as-strings. What I need is to be able to customise the comparison myself to guard against the format of the values changing in the future.
(There's an old thread here:
which poses a very similar problem. What I need is for Chuck to expand on his response: "3. Create a new comparator that does what you want." :-) Where do I put that comparator, Chuck?)
|