use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class NavajoExceptionEvent method getEventNavajo.
/**
* Return the event parameters as a Navajo object with a message __event__.
*/
@Override
public Navajo getEventNavajo() {
Navajo input = NavajoFactory.getInstance().createNavajo();
Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
try {
input.addMessage(event);
Property webserviceProperty = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
Property exception = NavajoFactory.getInstance().createProperty(input, "Exception", Property.STRING_PROPERTY, getException().getMessage(), 0, "", Property.DIR_OUT);
Property accessIdProperty = NavajoFactory.getInstance().createProperty(input, "AccessId", Property.STRING_PROPERTY, getAccessId(), 0, "", Property.DIR_OUT);
Property userProperty = NavajoFactory.getInstance().createProperty(input, "User", Property.STRING_PROPERTY, getUser(), 0, "", Property.DIR_OUT);
event.addProperty(webserviceProperty);
event.addProperty(exception);
event.addProperty(accessIdProperty);
event.addProperty(userProperty);
} catch (NavajoException e) {
logger.error("Error: ", e);
}
return input;
}
use of com.dexels.navajo.document.NavajoException in project navajo by Dexels.
the class CacheExpiryEvent method getEventNavajo.
@Override
public Navajo getEventNavajo() {
Navajo input = NavajoFactory.getInstance().createNavajo();
Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
try {
input.addMessage(event);
Property eventWebService = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
event.addProperty(eventWebService);
Property eventKey = NavajoFactory.getInstance().createProperty(input, "Key", Property.STRING_PROPERTY, getKey(), 0, "", Property.DIR_OUT);
event.addProperty(eventKey);
} catch (NavajoException e) {
logger.error("Error: ", e);
}
return input;
}
use of com.dexels.navajo.document.NavajoException 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.NavajoException 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.NavajoException in project navajo by Dexels.
the class NavajoContext method dumpTopElement.
public void dumpTopElement() {
Object o = myElementStack.peek();
if (o instanceof Navajo) {
Navajo n = (Navajo) o;
StringWriter sw = new StringWriter();
try {
n.write(sw);
logger.info("Navajo on top: {}", sw);
} catch (NavajoException e) {
logger.error("Error: ", e);
}
} else if (o instanceof Message) {
logger.info("Message on top: {}", ((Message) o).getFullMessageName());
} else if (o instanceof Property) {
try {
logger.info("Property on top: {}", ((Property) o).getFullPropertyName());
} catch (NavajoException e) {
logger.error("Error: ", e);
}
} else {
if (o != null) {
logger.info("Other object: {}", o.getClass());
} else {
logger.info("Null object on stack!");
}
}
}
Aggregations