Search in sources :

Example 1 with Currency

use of org.jaffa.datatypes.Currency in project jaffa-framework by jaffa-projects.

the class DynamicRulesTest method testCurrencyFieldValidator.

/**
 * This tests the CurrencyFieldValidator.
 */
public void testCurrencyFieldValidator() {
    try {
        // The following update should flow through without any exceptions
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", "101", m_uow);
        } catch (ValidationException e) {
            e.printStackTrace();
            fail("Exception was raised while updating the field with a valid Currency value");
        }
        // The following update should flow through without any exceptions
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", new Currency(101), m_uow);
        } catch (ValidationException e) {
            e.printStackTrace();
            fail("Exception was raised while updating the field with a valid Currency value");
        }
        // The following invalid value should raise an exception
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", "zzzzz", m_uow);
            fail("No exception was raised while updating a field with an invalid Currency value");
        } catch (FormatCurrencyException e) {
        // the exception is expected
        }
        // The following invalid value should raise an exception
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", "99", m_uow);
            fail("No exception was raised while updating a field with an invalid Currency value");
        } catch (BelowMinimumException e) {
        // the exception is expected
        }
        // The following invalid value should raise an exception
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", "5001", m_uow);
            fail("No exception was raised while updating a field with an invalid Currency value");
        } catch (ExceedsMaximumException e) {
        // the exception is expected
        }
        // The following invalid value should raise an exception
        try {
            RulesEngine.doAllValidationsForDomainField("Dummy", "CurrencyField", "101.12345678", m_uow);
            fail("No exception was raised while updating a field with an invalid Currency value");
        } catch (TooMuchDataException e) {
        // the exception is expected
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : Currency(org.jaffa.datatypes.Currency)

Example 2 with Currency

use of org.jaffa.datatypes.Currency in project jaffa-framework by jaffa-projects.

the class CurrencyConverter method convertOutbound.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.directwebremoting.Converter#convertOutbound(java.lang.Object, 
	 * org.directwebremoting.OutboundContext)
	 */
public OutboundVariable convertOutbound(Object data, OutboundContext outctx) throws MarshallException {
    Double out = 0.0;
    // Error out if an unsupported class is passed
    if (!(data instanceof Currency)) {
        log.warn("Unsupported input. Class=" + data.getClass() + ", data=" + data);
        throw new MarshallException(data.getClass());
    }
    String currency = data.toString();
    if (currency != null && currency != "") {
        String layout = CurrencyFieldMetaData.getCurrencyFormat();
        NumberFormat fmt;
        if (LocaleContext.getLocale() != null) {
            fmt = NumberFormat.getCurrencyInstance(LocaleContext.getLocale());
        } else {
            fmt = NumberFormat.getCurrencyInstance();
        }
        fmt.setCurrency(java.util.Currency.getInstance(Currency.USD));
        if (layout != null && layout.length() > 0 && fmt instanceof DecimalFormat) {
            ((DecimalFormat) fmt).applyPattern(layout);
        }
        try {
            out = Double.valueOf((fmt.parse(currency)).doubleValue());
            currency = out.toString();
        } catch (ParseException e) {
        }
    }
    return new SimpleOutboundVariable(currency, outctx, true);
}
Also used : MarshallException(org.directwebremoting.extend.MarshallException) Currency(org.jaffa.datatypes.Currency) DecimalFormat(java.text.DecimalFormat) ParseException(java.text.ParseException) SimpleOutboundVariable(org.directwebremoting.dwrp.SimpleOutboundVariable) NumberFormat(java.text.NumberFormat)

Example 3 with Currency

use of org.jaffa.datatypes.Currency in project jaffa-framework by jaffa-projects.

the class CurrencyCriteriaField method parseAndAdd.

private static void parseAndAdd(String str, CurrencyFieldMetaData meta, List values) throws FormatCurrencyException {
    try {
        Currency parsedValue = null;
        if (str != null && str.length() > 0) {
            if (meta != null)
                parsedValue = Parser.parseCurrency(str, meta.getLayout());
            else
                parsedValue = Parser.parseCurrency(str);
        }
        values.add(parsedValue);
    } catch (FormatCurrencyException e) {
        e.setField(meta != null ? meta.getLabelToken() : "");
        throw e;
    }
}
Also used : FormatCurrencyException(org.jaffa.datatypes.exceptions.FormatCurrencyException) Currency(org.jaffa.datatypes.Currency)

Example 4 with Currency

use of org.jaffa.datatypes.Currency in project jaffa-framework by jaffa-projects.

the class CurrencyCriteriaField method getCurrencyCriteriaField.

/**
 * This will generate a CriteriaField object based on the input parameters.
 * @param operator The operator of the criteria.
 * @param value The value for the criteria. Multiple values should be separated by comma.
 * @param meta The FieldMetaData object to obtain the layout for parsing.
 * @return a CriteriaField object based on the input parameters.
 * @throws FormatCurrencyException if the value is incorrectly formatted.
 */
public static CurrencyCriteriaField getCurrencyCriteriaField(String operator, String value, CurrencyFieldMetaData meta) throws FormatCurrencyException {
    CurrencyCriteriaField criteriaField = null;
    Currency nullValue = null;
    if (value != null)
        value = value.trim();
    if (value != null && value.length() > 0) {
        List values = new ArrayList();
        if (RELATIONAL_BETWEEN.equals(operator) || RELATIONAL_IN.equals(operator)) {
            // replace ",," with ", ,"
            value = StringHelper.replace(value, CONSECUTIVE_SEPARATORS, CONSECUTIVE_SEPARATORS_WITH_SPACE);
            if (value.startsWith(SEPARATOR_FOR_IN_BETWEEN_OPERATORS))
                values.add(null);
            StringTokenizer tknzr = new StringTokenizer(value, SEPARATOR_FOR_IN_BETWEEN_OPERATORS);
            while (tknzr.hasMoreTokens()) parseAndAdd(tknzr.nextToken().trim(), meta, values);
            if (value.endsWith(SEPARATOR_FOR_IN_BETWEEN_OPERATORS))
                values.add(null);
        } else {
            parseAndAdd(value, meta, values);
        }
        if (values.size() > 0)
            criteriaField = new CurrencyCriteriaField(operator, (Currency[]) values.toArray(new Currency[0]));
        else
            criteriaField = new CurrencyCriteriaField(operator, nullValue);
    } else
        criteriaField = new CurrencyCriteriaField(operator, nullValue);
    return criteriaField;
}
Also used : Currency(org.jaffa.datatypes.Currency)

Example 5 with Currency

use of org.jaffa.datatypes.Currency in project jaffa-framework by jaffa-projects.

the class MaxLengthValidator method validateProperty.

/**
 * {@inheritDoc}
 */
@Override
protected void validateProperty(T targetObject, String targetPropertyName, Object targetPropertyValue, List<RuleMetaData> rules) throws ApplicationException, FrameworkException {
    if (targetPropertyValue != null) {
        for (RuleMetaData rule : rules) {
            if (log.isDebugEnabled()) {
                log.debug("Applying " + rule + " on " + targetPropertyValue);
            }
            if (rule.getName().equalsIgnoreCase(getName())) {
                // Determine the intSize and fracSize
                Integer intSize = null;
                Integer fracSize = null;
                try {
                    String[] sizes = rule.getParameter(RuleMetaData.PARAMETER_VALUE).split(",");
                    if (sizes.length >= 1 && sizes[0] != null && sizes[0].length() > 0) {
                        intSize = new Integer(sizes[0]);
                    }
                    if (sizes.length >= 2 && sizes[1] != null && sizes[1].length() > 0) {
                        fracSize = new Integer(sizes[1]);
                    }
                } catch (NumberFormatException e) {
                    throw new InvalidRuleParameterException(targetPropertyName, getName(), "value", rule.getParameter(RuleMetaData.PARAMETER_VALUE));
                }
                if (intSize != null || fracSize != null) {
                    if (targetPropertyValue instanceof String) {
                        if (intSize != null && ((String) targetPropertyValue).length() > intSize.intValue()) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else if (targetPropertyValue instanceof Number) {
                        if (!checkLength((Number) targetPropertyValue, intSize, fracSize)) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else if (targetPropertyValue instanceof Currency) {
                        if (!checkLength(((Currency) targetPropertyValue).getValue(), intSize, fracSize)) {
                            throw logErrorCreateException(targetObject, targetPropertyName, targetPropertyValue, rule);
                        }
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("This rule does not support instances of " + targetPropertyValue.getClass().getName());
                    }
                }
            }
        }
    }
}
Also used : InvalidRuleParameterException(org.jaffa.rules.rulemeta.InvalidRuleParameterException) Currency(org.jaffa.datatypes.Currency) RuleMetaData(org.jaffa.rules.meta.RuleMetaData)

Aggregations

Currency (org.jaffa.datatypes.Currency)5 DecimalFormat (java.text.DecimalFormat)1 NumberFormat (java.text.NumberFormat)1 ParseException (java.text.ParseException)1 SimpleOutboundVariable (org.directwebremoting.dwrp.SimpleOutboundVariable)1 MarshallException (org.directwebremoting.extend.MarshallException)1 FormatCurrencyException (org.jaffa.datatypes.exceptions.FormatCurrencyException)1 RuleMetaData (org.jaffa.rules.meta.RuleMetaData)1 InvalidRuleParameterException (org.jaffa.rules.rulemeta.InvalidRuleParameterException)1