Hi Steve,
I had much the same problem recently. Here's my way of solving it on a
per table basis.
1. create a new class TrimCriteria that extends Criteria and has a
single method
public Criteria add (String column, String value)
{
if (value == null)
{
super.add(column,"");
}
else
{
add(column, (Object)value.trim(), EQUAL);
}
return this;
}
2. Create a new method in the <table>Peer class called
buildTrimCriteria(). Just copy
the existing method from the <table>BasePeer and instantiate the class
from 1. e.g.
TrimCriteria criteria = new TrimCriteria();
which is one line change.
3. Create a new method doTrimInsert(<tableobj>,Connection) which is a
copy of
the existing BasePeer method but using the method of 2.
4. call doTrimInsert() to insert rows.
It would be a lot neater to just edit the Criteria class directly and
repackage the Torque jar,
but it may have broken something else (?) experts may like to comment.
Regards,
Malcolm Kendall
Steve Vanspall wrote:
Thanks, yes I am new to torque.
Basically, I was looking for an easy way to make sure the empty
strings aren't inserted into the database, particularly for required
fields.
If the user has not entered anything, or jsut whitespace, than I want
it to be set to null. Then in validation I only need to check for a
null. Or if required Torque can throw an exception when inserting.
I see what you mean about the applications of it.
In the end what I have done is given a struts form the same getter and
setter methods and have had them do the check before passing it onto
the bean.
clunky but it does the trick
Thanks again
Steve
Greg Monroe wrote:
I think the only way to currently do this "correctly" would be to
modify the ind
ividual set methods in the TableBeans (the ones that are a subclass
of the BaseTableBean).
You could modify the templates to automatically generate this
code, but I would be very careful here. You'd be changing some of
the "fundamentals" and it might cause grief when you
try to upgrade Torque a year from now. If you do, look at
the code that generates the non-base class.
As to should this be a Torque feature... IMHO, this is a highly
application specific area and not a general DB thing. Issues that
come to mind are:
What if the DB field does not accept nulls?
What if your application needs/wants the missing, no answer, answer
status maintained (Talk to a Statistition about the
value of missing vs no answer for an interesting conversation.)
What is a "normalized" string? In the case of HTML forms, different
browsers will return different values. I've seen
versions of IE that return a "special" character (xFF?) instead
of a space " ". I think there is one variation out there that
helpfully translates spaces into characters. So normalizing
HTML forms strings is not just trimming blanks, etc. And then what
happens if Torque is doing this extra normalization but the
application is a Web-based?
In writing all this, it did strike me that there might be an
relatively simple, application safe way for Torque to be modified
that would make it easier for application specific normalization to
be added on top of a Base Torque generation.
Here's the idea:
1) Add two "do-nothing" methods to the BaseObject class like:
Object normalizeSetValue( String fieldName, Object value)
Object normailzeGetValue( String fieldName, Object value)
The default methods in BaseObject would just return the value
object unchanged.
2) Modify the templates to add calls to these methods in all the
generated get and set methods.
To do application specific normalization, there would only need
to one method overridden in the Table class. For generic
normalization (like all Strings), no extra coding would be needed
as fields were added or deleted to a table.
I suppose an enhancement on this would be to have a way that
application specific BaseObject and BasePeer subclass objects could
easily be used in the Templates. Then generic normalization could
be done once for all tables, etc.
-----Original Message-----
From: Steve Vanspall [mailto:[EMAIL PROTECTED] Sent:
Wednesday, March 08, 2006 7:45 AM
To: Apache Torque Users List
Subject: trim whitespace
Hi there,
Wondering if there is a way to configure Torque to genreate code to
trim the whitespace of Strings and change empty strings to null.
This would make it simpler to populate a bean directly from a form
(using struts) rather than having to populate afterwards.
Thanks in advance
Steve
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
Duke CE Privacy Statement
Please be advised that this e-mail and any files transmitted with it
are confidential communication or may otherwise be privileged or
confidential and are intended solely for the individual or entity to
whom they are addressed. If you are not the intended recipient you
may not rely on the contents of this email or any attachments, and we
ask that you please not read, copy or retransmit this communication,
but reply to the sender and destroy the email, its contents, and all
copies thereof immediately. Any unauthorized dissemination,
distribution or copying of this communication is strictly prohibited.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]