Re: EOGenerator templates
Re: EOGenerator templates
- Subject: Re: EOGenerator templates
- From: David Aspinall <email@hidden>
- Date: Thu, 21 Oct 2004 10:15:43 -0400
I use:
public <$Attribute.javaValueClassName$>
validate<$Attribute.name.initialCapitalString$>(<$Attribute.javaValueCla
ssName$> value)
{
<$if Attribute.allowsNull$><$else$>if ( value == null )
{
throw new NSValidation.ValidationException( "<$Attribute.name$>
cannot be empty." );
}<$endif$>
EOAttribute attr = entity().attributeNamed( "<$Attribute.name$>" );
if ((value != null) && (attr != null))
{
<$if Attribute.javaValueClassName=String$>
int width = attr.width();
if (width < ((String)value).length() )
{
/* I take the tuncated value, but return an exception so the user is
not surprised
that the string is trunc'd, but the value does not dissappear in the
UI. */
set<$Attribute.name.initialCapitalString$>(
((String)value).substring(0, width -1) );
throw new NSValidation.ValidationException(
"<$Attribute.name.initialCapitalString$> can only be up to " + width +
" characters in length." );
}
value = value.trim();
<$endif$>
<$if Attribute.javaValueClassName=BigDecimal$>
int scale = attr.scale();
if ((scale >= 0) && (scale != ((BigDecimal)value).scale() ))
{
try
{
value = ((BigDecimal)value).setScale( scale, Const.DEFAULT_ROUNDING
);
}
catch ( ArithmeticException e )
{
Log.Error( this.getClass().getName() + ".scaleForAttribute() " +
e.getClass().getName() + ": " + e.getMessage() );
}
catch( IllegalArgumentException iae )
{
Log.Error( this.getClass().getName() + ".scaleForAttribute() " +
iae.getClass().getName() + ": " + iae.getMessage() );
}
}
<$endif$>
}
return value;
}
On 20-Oct-04, at 6:46 PM, Greg Hulands wrote:
Hi,
I am trying to add automatic validating to any string attribute by
making sure that the length of the string won't excced the width of
the attribute.
The problem I am having is trying to get the comparison correct. I
have tried == with no luck. I also have tried NSString instead of
java.lang.String.
<$if Attribute.className.isEqualToString("java.lang.String")$>
public Object
validate<$Attribute.name.initialCapitalString$>(Object value) {
String str = (String)value;
EOEntity <$classNameWithoutPackage$> =
EOModelGroup.defaultGroup().entityNamed(entityName());
EOAttribute attr<$Attribute.name.initialCapitalString$> =
<$classNameWithoutPackage$>.attributeNamed("<$Attribute.name$>");
if (str.length() >
attr<$Attribute.name.initialCapitalString$>.width()) {
return str.substring(0,
attr<$Attribute.name.initialCapitalString$>.width() - 1);
}
return str;
}
<$endif$>
Does anyone know if this is possible?
Greg
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
This email sent to email@hidden
_______________________________________________
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