Example usage for org.springframework.expression.spel SpelEvaluationException getMessageCode

List of usage examples for org.springframework.expression.spel SpelEvaluationException getMessageCode

Introduction

In this page you can find the example usage for org.springframework.expression.spel SpelEvaluationException getMessageCode.

Prototype

public SpelMessage getMessageCode() 

Source Link

Document

Return the message code.

Usage

From source file:com.qcadoo.model.internal.ExpressionServiceImpl.java

@Override
public String getValue(final Entity entity, final String expression, final Locale locale) {
    if (StringUtils.isEmpty(expression) || "null".equals(expression)) {
        LOG.warn("Calculating empty expressions");
        return null;
    }/*www  .  j  a va 2  s .c om*/

    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression(expression);
    EvaluationContext context = new StandardEvaluationContext();

    if (entity != null) {
        Map<String, Object> values = getValuesForEntity(entity, locale, 2);

        for (Map.Entry<String, Object> entry : values.entrySet()) {
            context.setVariable(entry.getKey(), entry.getValue());
        }
    }

    String value = null;

    try {
        value = String.valueOf(exp.getValue(context));

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calculating value of expression \"" + expression + "\" for " + entity + " : " + value);
        }
    } catch (SpelEvaluationException e) {
        if (SpelMessage.CANNOT_INDEX_INTO_NULL_VALUE.equals(e.getMessageCode())) {
            return "";
        } else {
            LOG.error("Error while calculating value of expression \"" + expression + "\" for " + entity, e);
            value = "!!!";
        }
    }

    if (StringUtils.isEmpty(value) || "null".equals(value)) {
        return null;
    } else {
        return translate(value, locale);
    }
}