Re: Mapping Integer attribute to WOPopupButton list.
Re: Mapping Integer attribute to WOPopupButton list.
- Subject: Re: Mapping Integer attribute to WOPopupButton list.
- From: "Jerry W. Walker" <email@hidden>
- Date: Sun, 6 Aug 2006 12:47:59 -0400
Hi, Quinton,
I think this problem legitimately comes under the heading, "Model-
View-Controller" (MVC) which the makers of WebObjects (and its
predecessor, NeXT) took quite seriously.
Let's presume that an integer representation, as exists in the
database, is the legitimate form for these fields (given that even if
it is not, you probably have little control over the form and content
of a legacy database). That would be the "Model's" representation of
those fields. This makes sense, since it's much easier to do
arithmetic and numeric comparisons with integers than with strings.
Using strings to view that data is a concern of the "View" and should
be a concern separate from the "Model". Under the MVC pattern, code
applying to the model and code applying to the view should not be mixed.
There is some contention in WO community whether a WOComponent is a
"View" component or a "Controller" component, but I know of no one
(who takes MVC seriously) who would suggest that it is a component of
the "Model".
The standard WO approach to this problem is to use subclasses from
the Java Formatter class. The appropriate one to use here would be
the com.webobjects.foundation.NSNumberFormatter. There is a similar
com.webobjects.foundation.NSDateFormatter class for a similar problem
with date formats.
These classes take an object of the type defined for the database
attribute and convert it to a String object, or take a String object
and parse it to create an object of the type in the database. WO has
made it trivial to use these classes by including numberformat,
dateformat and formatter bindings for the WOString and WOTextField
WODynamicElements.
In your case, select a WOTextField in WebObjects Builder and display
the Info panel to set its bindings. Select the numberformat binding
and click the reveal triangles on the far right side of the line for
that binding. A popup list appears with several formats for numbers.
Choose the one that applies in your case.
When you do this, WO binds an NSNumberFormatter to your WOTextField
and provides the format you selected for its initialization. Now,
when you enter strings into the text field, it will automatically
parse them to convert them to integers to be persisted. When you
display a record from the database in the form containing this
WOTextField, the integer value is obtained from the database and
automatically converted to a String object, according to the format
you specified in the binding, to be displayed.
That's all you need to do for a wide variety of conversion formats
between Strings and either Number objects or NSTimestamp (date/time)
objects. If you find that the format you desire is close, but not
quite any of the supplied formats, note what WO puts in the binding
field for a couple of formats that contain the pieces you're looking
for. Then fashion a format from those pieces to create the format you
need. Alternatively, go to the WO JavaDocs for NSNumberFormatter (or
NSDateFormatter) and review the entire set of formatting codes to
custom build your own.
Also note that this approach will work for more complex formatting
requirements than those for just numbers and dates. You can create
your own Formatter subclass for such things as postal codes,
telephone numbers and, frankly, anything for which a database object
of any kind can be represented in your view as a String. This can be
a good deal more complex than just choosing a format from a pop-up
list of formats, but still represents an elegant solution to the
formatting problem.
Though your code solution will work as you've described, overall, I
think you'll find WO's approach superior in a number of ways.
Regards,
Jerry
On Aug 6, 2006, at 11:52 AM, Q wrote:
I am currently reimplementing parts of an existing legacy web
application using WO as a learning exercise (I'm a WO newbie), and
have been trying both D2W and regular components. One issue I have
encountered with the database I am using is that it uses quite a
number of integer fields to represent properties that map to
strings when presented to the user. However this mapping is not
present in the database, only the legacy application's interface
code. (I am not able to modify the database schema)
After playing with D2W for a bit and creating a couple of custom
components that were virtually identical it occurred to me that
this mapping data doesn't really belong in a custom component,
instead it should actually be in the eomodel. So I wrote a custom
D2W component to map the integer value to the appropriate display
string using some custom keys in the attribute's userinfo
dictionary from the eomodel. This appears to be an elegant
solution, works nicely and seems to fit well with the WO way of
doing things, something I am still becoming familiar with.
Anyway, now that I have something working, I was curious how this
actually "should" be done, but a search didn't reveal anything "out
there" to actually do this using the eomodel to store the
relational mapping.
Have I not looked in the right places, or is it just that nobody
does it this way?
How does everyone else do this?
--
Seeya...Q
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
_____ / Quinton Dolan - email@hidden
__ __/ / / __/ / /
/ __ / _/ / / Gold Coast, QLD, Australia
__/ __/ __/ ____/ / - / Ph: +61 419 729 806
_______ /
_\
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
--
__ Jerry W. Walker,
WebObjects Developer/Instructor for High Performance Industrial
Strength Internet Enabled Systems
email@hidden
203 278-4085 office
_______________________________________________
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