Re: Custom type EOModeler issue
Re: Custom type EOModeler issue
- Subject: Re: Custom type EOModeler issue
- From: Chuck Hill <email@hidden>
- Date: Wed, 28 Dec 2005 10:23:05 -0800
Hi Wolfram,
On Dec 27, 2005, at 1:27 PM, Wolfram Stebel wrote:
Nice christmas? Hopefully.
Hope you had a nice one as well.
As a training session i created a custom data type " MyType":
I created my own class MyType with the recommended methods (see
below).
In EOModeler i map MyType to:
External type: varchar
Type: Custom
Width: 3
class: MyType
factory method: valueOf: (see below!)
conversion method: toString ( see below!)
Init Arguments: NSString
Getting values from the DB works like a charme.
Save.Changes leads to:
I think you are slightly mistaken. The stack trace indicates that
the problem happens during the takeValues phase, which occurs before
invokeAction and the code tied to the Submit button.
java.lang.IllegalArgumentException: While trying to invoke the set
method
"public void CSampleTable.setAvalue(MyType)" on an object of type
CSampleTable we received an argument of type java.lang.String. This
often
happens if you forget to use a formatter.
In effect your code tried to call setAValue(MyType) with a parameter
like setAvalue("ABC"). As Java / KVC can't convert between String
and MyType you get this error. The custom type in the EOModel is
only for conversion to / from the database. It is not used for the
KVC or the UI.
<snip>
com.webobjects.appserver.WOComponent.validateTakeValueForKeyPath
(WOComponent.java:1273)
<snip>
com.webobjects.appserver._private.WOTextField.takeValuesFromRequest
(WOTextField.java:81)
<snip>
com.webobjects.appserver.WOComponent.takeValuesFromRequest
(WOComponent.java:914)
com.webobjects.appserver.WOSession.takeValuesFromRequest
(WOSession.java:1139)
com.webobjects.appserver.WOApplication.takeValuesFromRequest
(WOApplication.java:1350)
Those are the lines from the trace that indicate what is really
happening.
----------
I tried to follow "Practical WebObjects" but without success!
What am i missing?
There are a few things you can do. One is to follow EOF's suggestion
and add a formatter that can format (convert between) MyType and
String. Given a String s, it will return new MyType(s). Given a
MyType t, it will return t.getValue().
Another option is to write a special WOTextField UI that can do this
converstion, or to bind your WOTextField to a String in the page and
then add code to the page to do this conversion.
Finally, you could add a method like this to CSampleTable:
public void CSampleTable.setAvalue(String s) {
setAvalue(new MyType(s));
}
I'd prefer a formatter: it keeps the code all in one place and is
easy to reuse. I'd avoid adding code to CSampleTable as you would
have to do this in every object that used MyType.
It seems to me, that one or more additional methods are required,
to convert
objects correctly for this purpose.
In a way, yes. You are confusing EO <--> DB conversion with UI <-->
EO conversion. They are different animals.
In comparison: i have the sample Boolean class of Practical WO, it
works!
That is because KVC knows about java.lang.String and
java.lang.Boolean and can convert as needed. I can't recall a way to
"teach" KVC about your new class.
public class MyType {
private String value;
public MyType ( String v )
{
this.setValue ( v );
Change to
value = v;
see next:
}
public void setValue ( String v )
{
value = v;
}
OUCH! See the bottom half of page 43 in Practical WebObjects: Thou
shalt not use mutable attributes. Take this method out.
public String getValue ()
{
return value;
}
public static MyType valueOf ( String v )
{
return new MyType ( v );
}
public String toString ()
{
return value;
}
public Object clone () throws CloneNotSupportedException
{
return ( ( MyType ) super.clone () );
}
As this object should be immutable, just implement this as
return this;
public boolean equals ( Object obj )
{
return ( ( ( MyType ) obj ).getValue ().equals
( this.getValue() )
);
}
HTH
Chuck
--
Coming in 2006 - an introduction to web applications using WebObjects
and Xcode http://www.global-village.net/wointro
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