Converter question

I would like to store phone numbers as text in a database without any formatting. That is, if a user enters 111-222-3333 or (111)222-3333 or even 111 222 3333, the number would be stored as 1112223333. The idea is to allow the user enter phone numbers any way he wants, as long as it contains 10 numbers. (I defined a validator to make sure of that.)

When a phone number is retrieved from the database, I want to format it before displaying it. So 1112223333 would be displayed as 111-222-3333.

I thought a converter would do exactly that, but that doesn't appear to be the case.

Here's my understanding of what a converter does:

1. getAsObject takes the contents of, say a text field or static text, and returns an object that represents the internal data representation.

In my case, the input is a phone number entered by a user and the output is the phone number with all formatting removed.

2. getAsString takes the internal data representation and returns a String that represents the value displayed in say a text field or static text.

In my case, the input is a phone number without formatting and the output is a phone number with formatting.

I created a converter whose getAsObject returns a String without formatting and whose getAsString returns a String with formatting. That didn't work.

So I created another converter whose getAsObject returns a StringValue object. StirngValue is just a wrapper for a String.

publicclass StringValue{

public StringValue(){

}

private String value;

public String getValue(){

return this.value;

}

publicvoid setValue(String value){

this.value = value;

}

}

This doesn't work either.

Can anyone suggest how I can make this work?

Or is my understanding of converters off base?

Should I just use a fixed format for phone numbers and force the user to enter them in that format?

I appreciate your assistance.

[2487 byte] By [Futeleufu_Johna] at [2007-11-27 11:50:59]
# 1

Hi

I would suggest you to use the SQL feature of substr(string, m, n) while you are getting the results from database

something like ,,,SELECT SUBSTR(PHONENUM,1,3) || '-' || SUBSTR(PHONENUM,4,3) || '-' || SUBSTR(PHONENUM,7,4).... blah blah blah ...

Hope this helps ...

Thanks

Srinivas Guda

118975a at 2007-7-29 18:35:27 > top of Java-index,Development Tools,Java Tools...
# 2

I tested your SQL with MySQL and, with some syntax adjustments, it does what you advertise. I appreciate that.

But this approach causes problems with data binding and for automatically updating a phone number.

I can solve the data binding problem by defining an alias. For exampleSELECT concat(substr(phone_number,1,3),'-',substr(phone_number,4,3),'-',substr(phone_number,7,4)) as number

I should then be able to bind this formatted phone number to a text field, etc.

What I can't do, and what I want to do, is update a phone number using the data provider's commitChanges method. For example, a user changes a phone number and clicks Update.

This is where I thought a converter would help. I got the converter to work with phone numbers that weren't bound to a data provider, but it didn't work as I expected when the phone number was bound to a data provider.

So while this solves part of the problem, it doesn't solve all of it.

Futeleufu_Johna at 2007-7-29 18:35:27 > top of Java-index,Development Tools,Java Tools...
# 3

The following two links might help:

http://developers.sun.com/jscreator/learning/tutorials/2/converters.html#03

http://developers.sun.com/docs/jscreator/help/2update1/connect_data/jdbc_type_c onversions.html

Rradhikaa at 2007-7-29 18:35:27 > top of Java-index,Development Tools,Java Tools...
# 4

I think the converter you have should work; I wrote a little test program:

using NB55, with its Sun Appserver 9, and using derby database.

However, my test program used Java Persistence in the Java EE 5 Platform to take care the data source. I can view the table from derby and they are all digits.

One thing to note is that you need to Use the converter tag in your JSPs . That was the problem I had at the beginning.

There is a phone number converter example, see

http://www.ibm.com/developerworks/java/library/j-jsf3/

Hope this helps

Kenneth_Ha at 2007-7-29 18:35:27 > top of Java-index,Development Tools,Java Tools...