Search in sources :

Example 36 with Money

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

the class SumMessage 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) == null || getOperand(1) == null) {
        throw new TMLExpressionException(this, "Null operands! arguments: " + getOperand(0) + " - " + getOperand(1));
    }
    if (!(getOperand(0) instanceof Message && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    Message message = (Message) getOperand(0);
    String expression = (String) getOperand(1);
    Message parent = message;
    List<Message> arrayMsg = parent.getAllMessages();
    if (arrayMsg == null) {
        throw new TMLExpressionException(this, "Empty or non existing array message: " + message);
    }
    String sumType = "int";
    double sum = 0;
    for (int i = 0; i < arrayMsg.size(); i++) {
        Message m = arrayMsg.get(i);
        // Operand o = Expression.evaluate(expression, m.getRootDoc(), null, m);
        Property ppp = m.getProperty(expression);
        Object o = ppp.getTypedValue();
        if (o == null) {
            continue;
        }
        if (o instanceof Integer) {
            sum += ((Integer) o).doubleValue();
            sumType = "int";
        } else if (o instanceof Double) {
            sum += ((Double) o).doubleValue();
            sumType = "double";
        } else if (o instanceof Money) {
            sum += ((Money) o).doubleValue();
            sumType = "money";
        } else if (o instanceof Percentage) {
            sum += ((Percentage) o).doubleValue();
            sumType = "percentage";
        } else {
            throw new TMLExpressionException(this, "Incompatible type while summing: " + o);
        }
    }
    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);
    }
}
Also used : Money(com.dexels.navajo.document.types.Money) Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 37 with Money

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

the class ToMoney method main.

public static void main(String[] args) throws Exception {
    java.util.Locale.setDefault(new java.util.Locale("nl", "NL"));
    // Tests.
    ToMoney tm = new ToMoney();
    tm.reset();
    tm.insertFloatOperand(Double.valueOf(1024.4990));
    System.out.println("result = " + ((Money) tm.evaluate()).formattedString());
    // Using expressions.
    String expression = "ToMoney(1024.50) + 500 - 5.7 + ToMoney(1)/2";
    Operand o = Expression.evaluate(expression, null);
    System.out.println("o = " + o.value);
    System.out.println("type = " + o.type);
    System.err.println("ToMoney('') = " + Expression.evaluate("ToMoney('')", null).value);
}
Also used : Money(com.dexels.navajo.document.types.Money) Operand(com.dexels.navajo.document.Operand)

Example 38 with Money

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

the class StandardFunctionsTest method testGetCents.

@Test
public void testGetCents() {
    FunctionInterface fi = fff.getInstance(cl, "GetCents");
    fi.reset();
    fi.setInMessage(createTestNavajo());
    fi.insertMoneyOperand(new Money(100.50));
    Object o = fi.evaluateWithTypeChecking();
    assertNotNull(o);
}
Also used : Money(com.dexels.navajo.document.types.Money) FunctionInterface(com.dexels.navajo.expression.api.FunctionInterface) Test(org.junit.Test)

Example 39 with Money

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

the class SQLMapHelper method setParameter.

/**
 * Set the parameters for the statement
 * @param statement
 * @param param
 * @param idx
 * @param binaryStreamList
 * @param dbIdentifier
 * @param isLegacyMode
 * @param debug
 * @param myAccess
 * @return PreparedStatement
 * @throws java.sql.SQLException
 */
public static PreparedStatement setParameter(PreparedStatement statement, final Object param, final int idx, StreamClosable callback, String dbIdentifier, boolean isLegacyMode, boolean debug, Access myAccess) throws java.sql.SQLException {
    Access access = myAccess;
    if (access == null) {
        access = new Access();
        if (debug) {
            Access.writeToConsole(access, "Created a new Access object to write to the console");
        }
    }
    if ((param == null) || (param instanceof NavajoType && !(param instanceof Binary) && ((NavajoType) param).isEmpty())) {
        if (SQLMapConstants.POSTGRESDB.equals(dbIdentifier) || SQLMapConstants.ENTERPRISEDB.equals(dbIdentifier) || SQLMapConstants.ORACLEDB.equals(dbIdentifier)) {
            if (debug) {
                Access.writeToConsole(access, "Had to do something in order to not get the cast error from a null value, because it concerns " + dbIdentifier + "\n");
            }
            if (param == null) {
                statement.setNull(idx + 1, Types.NULL);
            } else {
                statement.setNull(idx + 1, Types.VARCHAR);
            }
        } else {
            statement.setNull(idx + 1, Types.VARCHAR);
        }
    } else if (param instanceof String) {
        statement.setString(idx + 1, (String) param);
    } else if (param instanceof Integer) {
        statement.setInt(idx + 1, ((Integer) param).intValue());
    } else if (param instanceof Long) {
        statement.setLong(idx + 1, ((Long) param).longValue());
    } else if (param instanceof Double) {
        statement.setDouble(idx + 1, ((Double) param).doubleValue());
    } else if (param instanceof Percentage) {
        statement.setDouble(idx + 1, ((Percentage) param).doubleValue());
    } else if (param instanceof java.util.Date) {
        long time = ((java.util.Date) param).getTime();
        if (isLegacyMode) {
            java.sql.Date sqlDate = new java.sql.Date(time);
            statement.setDate(idx + 1, sqlDate);
        } else {
            Timestamp sqlDate = new java.sql.Timestamp(time);
            statement.setTimestamp(idx + 1, sqlDate);
        }
    } else if (param instanceof Boolean) {
        // So prevent the error by using setInt instead
        if (SQLMapConstants.POSTGRESDB.equals(dbIdentifier) || SQLMapConstants.ENTERPRISEDB.equals(dbIdentifier)) {
            if (debug) {
                Access.writeToConsole(access, "Used setInt instead of setBoolean, because it concerns " + dbIdentifier + "\n");
            }
            statement.setInt(idx + 1, ((Boolean) param).booleanValue() == Boolean.TRUE ? 1 : 0);
        } else {
            statement.setBoolean(idx + 1, ((Boolean) param).booleanValue());
        }
    } else if (param instanceof ClockTime) {
        java.sql.Timestamp sqlDate = new java.sql.Timestamp(((ClockTime) param).dateValue().getTime());
        statement.setTimestamp(idx + 1, sqlDate);
    } else if (param instanceof Money) {
        statement.setDouble(idx + 1, ((Money) param).doubleValue());
    } else if (param instanceof Memo) {
        String memoString = ((Memo) param).toString();
        statement.setCharacterStream(idx + 1, new StringReader(memoString), memoString.length());
    } else if (param instanceof Binary) {
        Binary b = (Binary) param;
        setBlob(statement, idx, b, callback);
        if (debug) {
            Access.writeToConsole(access, "ADDED BLOB\n");
        }
    } else {
        throw new SQLException("Unknown type encountered in SQLMap.setStatementParameters(): " + param);
    }
    return statement;
}
Also used : Percentage(com.dexels.navajo.document.types.Percentage) SQLException(java.sql.SQLException) NavajoType(com.dexels.navajo.document.types.NavajoType) Timestamp(java.sql.Timestamp) Access(com.dexels.navajo.script.api.Access) ClockTime(com.dexels.navajo.document.types.ClockTime) Timestamp(java.sql.Timestamp) Date(java.sql.Date) Date(java.sql.Date) Money(com.dexels.navajo.document.types.Money) StringReader(java.io.StringReader) Binary(com.dexels.navajo.document.types.Binary) Memo(com.dexels.navajo.document.types.Memo)

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