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