Search in sources :

Example 11 with Money

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

the class UtilsMoneyTest method testSubtractMoneyAndLong.

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

Example 12 with Money

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

the class GetCents method main.

public static void main(String[] args) throws Exception {
    java.util.Locale.setDefault(new java.util.Locale("nl", "NL"));
    // Tests.
    GetCents tm = new GetCents();
    tm.reset();
    tm.insertMoneyOperand(new Money(10.55));
    System.out.println("result = " + tm.evaluate());
}
Also used : Money(com.dexels.navajo.document.types.Money)

Example 13 with Money

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

the class SumExpressions method evaluate.

/* (non-Javadoc)
	 * @see com.dexels.navajo.parser.FunctionInterface#evaluate()
	 */
@Override
public Object evaluate() throws TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            System.err.println("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String expression = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                Operand o = Expression.evaluate(expression, m.getRootDoc(), null, m);
                if (o.value == null) {
                    throw new TMLExpressionException(this, "Null value encountered");
                }
                if (o.value instanceof Integer) {
                    sum += ((Integer) o.value).doubleValue();
                } else if (o.value instanceof Double) {
                    sum += ((Double) o.value).doubleValue();
                } else {
                    throw new TMLExpressionException(this, "Incompatible type while summing: " + o.value.getClass().getName());
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException)

Example 14 with Money

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

the class SumProperties method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.info("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String propertyName = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = p.getTypedValue();
                    if (o == null) {
                        continue;
                    }
                    if (!(o instanceof Integer || o instanceof Double || o instanceof Float || o instanceof Money || o instanceof Percentage || o instanceof Boolean || o instanceof String)) {
                        throw new TMLExpressionException(this, "Only numbers are supported a sum. Not: " + (o.getClass().toString()) + " value: " + o);
                    }
                    if (o instanceof String) {
                        if ("".equals(o)) {
                        // ignore
                        } else {
                            logger.error("Only numbers are supported a sum. Not strings. Value:  " + o);
                            throw new TMLExpressionException(this, "Only numbers are supported a sum. Not strings. Value:  " + o + (o.getClass().toString()));
                        }
                    }
                    if (o instanceof Integer) {
                        sumType = "int";
                        sum += ((Integer) o).doubleValue();
                    } else if (o instanceof Double) {
                        // if (!((Double)o).equals(Double.valueOf(Double.NaN))) {
                        sumType = "float";
                        sum += ((Double) o).doubleValue();
                    // }
                    } else if (o instanceof Float) {
                        // if (!((Float)o).equals(new Float(Float.NaN))) {
                        sumType = "float";
                        sum += ((Float) o).doubleValue();
                    // }
                    } else if (o instanceof Money) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "money";
                        sum += ((Money) o).doubleValue();
                    // }
                    } else if (o instanceof Percentage) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "percentage";
                        sum += ((Percentage) o).doubleValue();
                    // }
                    } else if (o instanceof Boolean) {
                        sumType = "int";
                        sum += ((Boolean) o).booleanValue() ? 1 : 0;
                    }
                } else {
                    throw new TMLExpressionException(this, "Property does not exist: " + propertyName);
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property)

Example 15 with Money

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

the class ToString method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object s = this.getOperands().get(0);
    if (s == null) {
        return null;
    }
    if (s instanceof com.dexels.navajo.document.types.Binary) {
        Binary b = (Binary) s;
        byte[] data = b.getData();
        StringWriter w = new StringWriter();
        for (int i = 0; i < data.length; i++) {
            w.write(data[i]);
        }
        return w.toString();
    }
    if (s instanceof Money) {
        Money m = (Money) s;
        return m.formattedString();
    }
    return s.toString();
}
Also used : Money(com.dexels.navajo.document.types.Money) StringWriter(java.io.StringWriter) Binary(com.dexels.navajo.document.types.Binary)

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