Search in sources :

Example 26 with Money

use of com.dexels.navajo.document.types.Money in project navajo by Dexels.

the class UtilsMoneyTest method testAddLongAndMoney.

@Test
public void testAddLongAndMoney() {
    Long a = 15L;
    Money b = new Money(10);
    Money result = (Money) Utils.add(a, b, "ToLong(15) + ToMoney(10)");
    assertEquals(new Money(25), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Example 27 with Money

use of com.dexels.navajo.document.types.Money in project navajo by Dexels.

the class UtilsMoneyTest method testSubtractTwoMoneys.

// SUBTRACT
@Test
public void testSubtractTwoMoneys() {
    Money a = new Money(15);
    Money b = new Money(10);
    Money result = (Money) Utils.subtract(a, b, "ToMoney(15) - ToMoney(10)");
    assertEquals(new Money(5), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Example 28 with Money

use of com.dexels.navajo.document.types.Money in project navajo by Dexels.

the class UtilsMoneyTest method testSubtractMoneyAndInteger.

@Test
public void testSubtractMoneyAndInteger() {
    Money a = new Money(15);
    Integer b = 10;
    Money result = (Money) Utils.subtract(a, b, "ToMoney(15) - 10");
    assertEquals(new Money(5), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Example 29 with Money

use of com.dexels.navajo.document.types.Money in project navajo by Dexels.

the class UtilsMoneyTest method testSubtractMoneyAndDouble.

@Test
public void testSubtractMoneyAndDouble() {
    Money a = new Money(15);
    Double b = 10.0;
    Money result = (Money) Utils.subtract(a, b, "ToMoney(15) - 10.0");
    assertEquals(new Money(5), result);
}
Also used : Money(com.dexels.navajo.document.types.Money) Test(org.junit.Test)

Example 30 with Money

use of com.dexels.navajo.document.types.Money in project navajo by Dexels.

the class BasePropertyImpl method getTypedValue.

/**
 * Get the value of a property as a Java object.
 *
 * @return
 */
@Override
public final Object getTypedValue() {
    if (getType().equals(Property.STRING_PROPERTY)) {
        return getValue();
    }
    if (getType().equals(Property.BOOLEAN_PROPERTY)) {
        if (getValue() != null && !getValue().equals("")) {
            return Boolean.valueOf(getValue().equalsIgnoreCase("true"));
        } else {
            return null;
        }
    }
    if (getType().equals(EXPRESSION_LITERAL_PROPERTY)) {
        if (getValue() != null && !getValue().equals("")) {
            return new NavajoExpression(getValue());
        } else {
            return null;
        }
    }
    if (getType().equals(EXPRESSION_PROPERTY)) {
        if (evaluatedValue == null) {
            evaluatedValue = getEvaluatedValue();
            return evaluatedValue;
        } else {
            return evaluatedValue;
        }
    }
    if (getType().equals(Property.PERCENTAGE_PROPERTY)) {
        if (getValue() != null) {
            return new Percentage(getValue());
        } else {
            return null;
        }
    }
    if (getType().equals(Property.MONEY_PROPERTY)) {
        if (getValue() == null || "".equals(getValue())) {
            return new Money((Double) null, getSubType());
        }
        String val = getValue();
        NumberFormat fn = NumberFormat.getNumberInstance(Locale.US);
        Number parse;
        try {
            parse = fn.parse(val);
        } catch (ParseException e) {
            return null;
        }
        return new Money(parse.doubleValue(), getSubType());
    } else if (getType().equals(Property.CLOCKTIME_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        try {
            return new ClockTime(getValue(), getSubType());
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.STOPWATCHTIME_PROPERTY)) {
        try {
            return new StopwatchTime(getValue(), getSubType());
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.DATE_PROPERTY) || getType().equals(Property.TIMESTAMP_PROPERTY)) {
        if (getValue() == null || getValue().equals("") || getValue().equals("null")) {
            return null;
        }
        if (myDate != null) {
            return myDate;
        }
        // Try in order from most specific to least specific
        try {
            return timestampFormat.get().parse(getValue());
        } catch (Exception ex) {
            try {
                return dateFormat4.get().parse(getValue());
            } catch (Exception ex2) {
                try {
                    return dateFormat1.get().parse(getValue());
                } catch (Exception ex3) {
                    try {
                        return dateFormat2.get().parse(getValue());
                    } catch (Exception ex4) {
                        try {
                            Long l = Long.parseLong(getValue());
                            Date d = new java.util.Date();
                            d.setTime(l);
                            return d;
                        } catch (Exception e5) {
                            logger.info("Sorry I really can't parse that date: {}", getValue());
                            return null;
                        }
                    }
                }
            }
        }
    } else if (getType().equals(Property.INTEGER_PROPERTY)) {
        if (getValue() == null || getValue().equals("") || getValue().trim().equals("null")) {
            return null;
        }
        try {
            return Integer.valueOf(Integer.parseInt(getValue().trim()));
        } catch (NumberFormatException ex3) {
            logger.info("Numberformat exception...: {}", getValue().trim());
            return null;
        }
    } else if (getType().equals(Property.LONG_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        try {
            // Added a trim. Frank.
            return Long.valueOf(Long.parseLong(getValue().trim()));
        } catch (NumberFormatException ex3) {
            logger.info("Numberformat exception...");
            return null;
        }
    } else if (getType().equals(Property.FLOAT_PROPERTY)) {
        if (getValue() == null || getValue().equals("")) {
            return null;
        }
        String v = getValue();
        String w = v;
        // Sometimes the number formatting creates
        if (v.indexOf(',') != -1) {
            w = v.replaceAll(",", "");
        }
        Double d;
        try {
            d = Double.valueOf(Double.parseDouble(w));
        } catch (NumberFormatException ex) {
            logger.info("Can not format double with: {}", w);
            return null;
        }
        return d;
    } else if (getType().equals(Property.BINARY_PROPERTY)) {
        try {
            return myBinary;
        } catch (Exception e) {
            logger.error("Error: ", e);
        }
    } else if (getType().equals(Property.SELECTION_PROPERTY)) {
        return getAllSelectedSelections();
    } else if (getType().equals(Property.TIPI_PROPERTY)) {
        return tipiProperty;
    } else if (getType().equals(Property.LIST_PROPERTY)) {
        if (tipiProperty != null || myValue == null) {
            return tipiProperty;
        }
        try {
            if (myValue.indexOf('[') == 0) {
                // Parse back into a list
                String stripped = myValue.substring(1, myValue.length() - 1);
                tipiProperty = Arrays.asList(stripped.split(", "));
                return tipiProperty;
            } else if (myValue.length() > 0) {
                logger.info("Failed to parse {} as a list!", myValue);
            }
        } catch (Exception e) {
            logger.warn("Exception on parsing {} as a list!", myValue, e);
        }
        return null;
    } else if (getType().equals(Property.BINARY_DIGEST_PROPERTY)) {
        return new BinaryDigest(getValue());
    } else if (getType().equals(Property.COORDINATE_PROPERTY)) {
        try {
            return new Coordinate(myValue);
        } catch (Exception e) {
            logger.error("Cannot create Coordinate Property: ", e);
        }
    }
    return getValue();
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Percentage(com.dexels.navajo.document.types.Percentage) Date(java.util.Date) ClockTime(com.dexels.navajo.document.types.ClockTime) PropertyTypeException(com.dexels.navajo.document.PropertyTypeException) NavajoException(com.dexels.navajo.document.NavajoException) ParseException(java.text.ParseException) ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) IOException(java.io.IOException) Date(java.util.Date) StopwatchTime(com.dexels.navajo.document.types.StopwatchTime) Money(com.dexels.navajo.document.types.Money) BinaryDigest(com.dexels.navajo.document.types.BinaryDigest) Coordinate(com.dexels.navajo.document.types.Coordinate) ParseException(java.text.ParseException) NumberFormat(java.text.NumberFormat)

Aggregations

Money (com.dexels.navajo.document.types.Money)39 Test (org.junit.Test)25 Percentage (com.dexels.navajo.document.types.Percentage)10 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)8 ClockTime (com.dexels.navajo.document.types.ClockTime)6 Navajo (com.dexels.navajo.document.Navajo)5 Property (com.dexels.navajo.document.Property)5 Date (java.util.Date)5 Operand (com.dexels.navajo.document.Operand)4 Message (com.dexels.navajo.document.Message)3 NavajoException (com.dexels.navajo.document.NavajoException)3 Binary (com.dexels.navajo.document.types.Binary)3 StopwatchTime (com.dexels.navajo.document.types.StopwatchTime)3 StringWriter (java.io.StringWriter)3 DatePattern (com.dexels.navajo.document.types.DatePattern)2 SystemException (com.dexels.navajo.script.api.SystemException)2 StringReader (java.io.StringReader)2 ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 PropertyTypeException (com.dexels.navajo.document.PropertyTypeException)1 BaseMessageImpl (com.dexels.navajo.document.base.BaseMessageImpl)1