• Post Reply Bookmark Topic Watch Topic
  • New Topic

using Strings for representation of missing or unknown data

 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a jsp ((using jstl) I want to display String representations ("Unknown", "Missing", etc.) for numerical and date field values that are unknown in our database.

Much of our data is incomplete. So, for example, in our database, I might insert clearly understandable dummy numbers, say, -3000, -3001, and -3002, in a data field called "length", to represent various kinds of unknown or missing data.

When I display the data in the jsp, I want to show various string fields in place of the dummy number values: "Cataloguer unsure", "Missing", "Not yet supplied", and so on.

I have a databean that provides the data to the jsp. The problem is that the bean fields are typed according to the database fields--as Strings, or ints, or dates, and so on. This is perfect for display of complete data, but not for missing data: "Unknown" or "Cataloguer unsure" will of course not be accommodated by a field expecting a type of int, double, or Date. Simply leaving the database field null is not an option, as there are the several statuses of unknown data.


I have a LOT of fields to display (92) so even simple conversion logic in a jsp for each field would take a lot of time.

What's the best way to do a String representation of such data? My thought was to allocate the string representation logic in my underlying databean, but I was not sure this would be best. My thought was to duplicate all my databean fields with names like "dataBeanFieldString" and of course supply the appropriate getters and setters.

Any ideas jump out at you for realizing such string representation in jsp of missing or irregular data?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instinct of keeping the goo off the JSP is one that I laud.

Personally, I'd handle this by creating a facade wrapper around the bean that returns the appopriate "translations" from the getters. The servlet controller would be responsible for wrapping the bean just before sending it off to the page. Alternatively, I would consider, rather than sending the bean to the page, copying its data into a Map to send to the page. The bean->Map conversion would handle the translation from "unknown value" to "corresponding" string.

In fact, upon a moment of reflection, I think I'd opt for the latter approach as a wrapper would not need to be defined for each different type of bean.

I'd not push this back down too far -- and certainly not into the database -- if it's a display-only consideration.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution depends on the complexity of your unknown values. If the data representation should be "Unknown" for -3000 and "Cannot load" for -3001 etc etc you might be better off creating a lookup table for such statuses. Your controller can delegate work of creating a lookup map for these values and then attaching them in a scope for the JSP to retrieve or manipulate the values based on the lookups and either put them in a bean or a seperate map.

Alternatively you could also write that wrapper like Bear suggested. Say a range of -3000 to -3010 should have Unknown, or something like that.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Meyers:
you might be better off creating a lookup table for such statuses


Otherwise known as a Map .
[ April 27, 2007: Message edited by: Bear Bibeault ]
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
a facade wrapper around the bean that returns the appopriate "translations" from the getters
This looks rather like the Null Object design pattern to me.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed... now it has a name!
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


Otherwise known as a Map .

[ April 27, 2007: Message edited by: Bear Bibeault ]


Yes Bear that is what I was suggesting as well.

This looks rather like the Null Object design pattern to me.


Cool. More food for the brain. I didnt know it had a name. Thanks
 
Benjamin Weaver
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Bear, Paul and John for these very clever solutions. I used them all. I ended up writing a wrapper that loaded several maps from a properties file: a displayValuesMap, a captionValueMap, and an UnknownDataDisplayMap. I included a single pair of methods for each of these maps, e.g.
[CODE}
setDisplayValue(String beanValueToString) {
this.displayValue = (String)displayProperties.getProperty(displayValue);
}

String getDisplayValue() {
return this.displayValue;
}
[/CODE]

Which I could call for any property in the bean. This did the trick for what I needed.

Thanks again.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!