Example usage for org.springframework.expression.spel SpelMessage CANNOT_INDEX_INTO_NULL_VALUE

List of usage examples for org.springframework.expression.spel SpelMessage CANNOT_INDEX_INTO_NULL_VALUE

Introduction

In this page you can find the example usage for org.springframework.expression.spel SpelMessage CANNOT_INDEX_INTO_NULL_VALUE.

Prototype

SpelMessage CANNOT_INDEX_INTO_NULL_VALUE

To view the source code for org.springframework.expression.spel SpelMessage CANNOT_INDEX_INTO_NULL_VALUE.

Click Source Link

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;
    }//from  w  ww .ja  va2 s  .com

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