Search in sources :

Example 31 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class BaseActionForm method validateInterest.

protected DoubleConversionResult validateInterest(String interestString, String fieldPropertyKey, ActionErrors errors) {
    String fieldName = getLocalizedMessage(fieldPropertyKey);
    DoubleConversionResult conversionResult = parseDoubleForInterest(interestString);
    for (ConversionError error : conversionResult.getErrors()) {
        addError(errors, fieldPropertyKey, "errors.generic", fieldName, getConversionErrorText(error));
    }
    return conversionResult;
}
Also used : ConversionError(org.mifos.framework.util.helpers.ConversionError) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult)

Example 32 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class LocalizationConverter method parseDoubleDecimalForMoney.

public DoubleConversionResult parseDoubleDecimalForMoney(String doubleStr) {
    DoubleConversionResult result = new DoubleConversionResult();
    if (doubleStr == null) {
        List<ConversionError> errors = new ArrayList<ConversionError>();
        errors.add(ConversionError.CONVERSION_ERROR);
        result.setErrors(errors);
        return result;
    }
    List<ConversionError> errors = checkDigits(digitsBeforeDecimalForMoney, multipleDigitsAfterDecimalForMoney, ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_MONEY, ConversionError.EXCEEDING_NUMBER_OF_DIGITS_AFTER_DECIMAL_SEPARATOR_FOR_MONEY, doubleStr, false);
    result.setErrors(errors);
    if (errors.size() > 0) {
        return result;
    }
    try {
        result.setDoubleValue(getDoubleValueForCurrentLocale(doubleStr));
    } catch (Exception ex) {
        // after all the checkings this is not likely to happen, but just in
        // case
        result.getErrors().add(ConversionError.CONVERSION_ERROR);
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ConversionError(org.mifos.framework.util.helpers.ConversionError) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult)

Example 33 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class BaseActionFormTest method xtestParseDoubleForMoney_is_IS.

/**
     * Currently broken -- incomplete support for multiple locales for numeric
     * input.
     */
@Ignore
@Test
public void xtestParseDoubleForMoney_is_IS() {
    LocalizationConverter converter = new LocalizationConverter();
    String doubleStr = "222.12345";
    Double value = 222.12345;
    DoubleConversionResult result = baseActionForm.parseDoubleForInterest("222,12345");
    result = baseActionForm.parseDoubleForMoney("222,4");
    Assert.assertEquals(value, result.getDoubleValue());
    doubleStr = "222.4";
    result = baseActionForm.parseDoubleForMoney(doubleStr);
    Assert.assertEquals(ConversionError.NOT_ALL_NUMBER, result.getErrors().get(0));
    doubleStr = "222a4";
    result = baseActionForm.parseDoubleForMoney(doubleStr);
    Assert.assertEquals(ConversionError.NOT_ALL_NUMBER, result.getErrors().get(0));
    doubleStr = "222,40000";
    result = baseActionForm.parseDoubleForMoney(doubleStr);
    Assert.assertEquals(ConversionError.EXCEEDING_NUMBER_OF_DIGITS_AFTER_DECIMAL_SEPARATOR_FOR_MONEY, result.getErrors().get(0));
    doubleStr = "222222222222,4";
    result = baseActionForm.parseDoubleForMoney(doubleStr);
    Assert.assertEquals(ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_MONEY, result.getErrors().get(0));
// converter.setCurrentLocale(locale);
}
Also used : LocalizationConverter(org.mifos.framework.util.LocalizationConverter) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class LocalizationConverterTest method testParseDoubleForInterest.

@Test
public void testParseDoubleForInterest() {
    String doubleValueString = "222.59562";
    Double dValue = 222.59562;
    Short digitsAfterForInterestSaved = AccountingRules.getDigitsAfterDecimalForInterest();
    Short digitsAfterForInterest = 5;
    AccountingRules.setDigitsAfterDecimalForInterest(digitsAfterForInterest);
    Locale locale = Localization.getInstance().getConfiguredLocale();
    short digitsAfterDecimalForMoney = Short.valueOf("1");
    short digitsBeforeDecimalForMoney = Short.valueOf("1");
    short digitsAfterDecimalForInterest = Short.valueOf("5");
    short digitsBeforeDecimalForInterest = Short.valueOf("6");
    short digitsBeforeDecimalForCashFlowValidations = Short.valueOf("1");
    short digitsAfterDecimalForCashFlowValidations = Short.valueOf("5");
    converter = new LocalizationConverter(digitsAfterDecimalForMoney, digitsBeforeDecimalForMoney, digitsAfterDecimalForInterest, digitsBeforeDecimalForInterest, digitsBeforeDecimalForCashFlowValidations, digitsAfterDecimalForCashFlowValidations);
    DoubleConversionResult result = converter.parseDoubleForInterest(doubleValueString);
    if (locale.getCountry().equalsIgnoreCase("GB") && locale.getLanguage().equalsIgnoreCase("EN")) {
        Assert.assertEquals(result.getDoubleValue(), dValue);
        // if the wrong decimal separator is entered, error will be returned
        doubleValueString = "222,59562";
        result = converter.parseDoubleForInterest(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.NOT_ALL_NUMBER);
        doubleValueString = "2a5922";
        result = converter.parseDoubleForInterest(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.NOT_ALL_NUMBER);
        doubleValueString = "222.595690";
        result = converter.parseDoubleForInterest(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.EXCEEDING_NUMBER_OF_DIGITS_AFTER_DECIMAL_SEPARATOR_FOR_INTEREST);
        doubleValueString = "22222222222.5";
        result = converter.parseDoubleForInterest(doubleValueString);
        Assert.assertEquals(result.getErrors().get(0), ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_INTEREST);
    }
    AccountingRules.setDigitsAfterDecimalForInterest(digitsAfterForInterestSaved);
    converter.setCurrentLocale(locale);
}
Also used : Locale(java.util.Locale) DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) Test(org.junit.Test)

Example 35 with DoubleConversionResult

use of org.mifos.framework.util.helpers.DoubleConversionResult in project head by mifos.

the class LocalizationConverterTest method testParseDoubleForCashFlowValidations.

@Test
public void testParseDoubleForCashFlowValidations() {
    MifosCurrency mifosCurrency = new MifosCurrency(Short.valueOf("1"), "Rupee", BigDecimal.valueOf(1), "INR");
    LocalizationConverter localizationConverter = new LocalizationConverter(mifosCurrency);
    DoubleConversionResult result = parseForCashFlow(localizationConverter, "22.59");
    assertThat(result.getDoubleValue(), is(22.59));
    result = parseForCashFlow(localizationConverter, "222,59562");
    assertThat(result.getErrors().get(0), is(ConversionError.NOT_ALL_NUMBER));
    result = parseForCashFlow(localizationConverter, "2a5922");
    assertThat(result.getErrors().get(0), is(ConversionError.NOT_ALL_NUMBER));
    result = parseForCashFlow(localizationConverter, "22222222222.5");
    assertThat(result.getErrors().get(0), is(ConversionError.EXCEEDING_NUMBER_OF_DIGITS_BEFORE_DECIMAL_SEPARATOR_FOR_CASHFLOW_VALIDATION));
    result = parseForCashFlow(localizationConverter, "222.595690");
    assertThat(result.getErrors().get(0), is(ConversionError.EXCEEDING_NUMBER_OF_DIGITS_AFTER_DECIMAL_SEPARATOR_FOR_CASHFLOW_VALIDATION));
    result = parseForCashFlow(localizationConverter, "222.59");
    assertThat(result.getErrors().get(0), is(ConversionError.CASH_FLOW_THRESHOLD_OUT_OF_RANGE));
}
Also used : DoubleConversionResult(org.mifos.framework.util.helpers.DoubleConversionResult) MifosCurrency(org.mifos.application.master.business.MifosCurrency) Test(org.junit.Test)

Aggregations

DoubleConversionResult (org.mifos.framework.util.helpers.DoubleConversionResult)43 ConversionError (org.mifos.framework.util.helpers.ConversionError)20 Locale (java.util.Locale)9 ArrayList (java.util.ArrayList)7 ActionErrors (org.apache.struts.action.ActionErrors)7 ActionMessage (org.apache.struts.action.ActionMessage)7 Test (org.junit.Test)7 ResourceBundle (java.util.ResourceBundle)6 InvalidDateException (org.mifos.application.admin.servicefacade.InvalidDateException)5 MifosCurrency (org.mifos.application.master.business.MifosCurrency)4 LocalizationConverter (org.mifos.framework.util.LocalizationConverter)4 HashMap (java.util.HashMap)2 Ignore (org.junit.Ignore)2 List (java.util.List)1 Map (java.util.Map)1 FeeDto (org.mifos.accounts.fees.business.FeeDto)1 LoanAccountDetailsDto (org.mifos.dto.domain.LoanAccountDetailsDto)1 Money (org.mifos.framework.util.helpers.Money)1