Translator for BigDecimal values
--------------------------------

         Key: TAPESTRY-817
         URL: http://issues.apache.org/jira/browse/TAPESTRY-817
     Project: Tapestry
        Type: New Feature
    Versions: 4.0    
    Reporter: Vjeran Marcinko


Tapestry currently lacks Translator for BigDecimal values. NumberTranslator is 
no good since it uses DecimalFormat underneath which converts String value into 
Double object, which then cannot accurately be converted to BigDecimal by it's 
very nature.

Solution is to introduce new BigDecimalTranslator, something like :

import org.apache.hivemind.ApplicationRuntimeException;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.translator.AbstractTranslator;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidationStrings;
import org.apache.tapestry.valid.ValidatorException;

import java.math.BigDecimal;
import java.util.Locale;


public class BigDecimalTranslator extends AbstractTranslator {

    public BigDecimalTranslator() {
    }

    // Needed until HIVEMIND-134 fix is available
    public BigDecimalTranslator(String initializer) {
        super(initializer);
    }

    protected String formatObject(IFormComponent field, Locale locale, Object 
object) {
        if (!(object instanceof BigDecimal)) {
            throw new ApplicationRuntimeException("BigDecimalTranslator 
translates values of type BigDecimal, not: " + object.getClass());
        }
        return object.toString();
    }

    protected Object parseText(IFormComponent field, ValidationMessages 
messages, String text) throws ValidatorException {
        try {
            return new BigDecimal(text);
        } catch (NumberFormatException e) {
            throw new ValidatorException(buildMessage(messages, field, 
ValidationStrings.INVALID_NUMBER), ValidationConstraint.NUMBER_FORMAT);
        }
    }
}


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to