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);
}
}
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);
}
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);
}
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;
}
Aggregations