All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.anteros.bean.validation.constraints.validators.ELAssertContext Maven / Gradle / Ivy

There is a newer version: 1.0.18
Show newest version
package br.com.anteros.bean.validation.constraints.validators;

import java.util.HashMap;
import java.util.Map;

import br.com.anteros.el.api.ELContext;
import br.com.anteros.el.api.ExpressionFactory;
import br.com.anteros.el.api.ValueExpression;
import br.com.anteros.el.util.SimpleContext;
import br.com.anteros.validation.api.ConstraintDeclarationException;

/**
 * Context used by validator implementations dealing with script expressions.
 * Instances are thread-safe and can be re-used several times to evaluate
 * different bindings against one given given script expression.
 */
class ELAssertContext {

	private final String expression;
	private ExpressionFactory expressionFactory;
	private ELContext context;

	public ELAssertContext(String expression) {
		this.expression = expression;
		this.expressionFactory = ExpressionFactory.newInstance();
		this.context = new SimpleContext();
	}

	public boolean evaluateAssertExpression(Object object, String alias) {
		Map bindings = new HashMap();
		bindings.put(alias, object);

		return evaluateAssertExpression(bindings);
	}

	public boolean evaluateAssertExpression(Map bindings) {
		Object result;

		ValueExpression valueExpression = expressionFactory.createValueExpression(context, this.expression,
				Object.class);
		for (String alias : bindings.keySet()){
			context.getELResolver().setValue(context, null, alias, bindings.get(alias));
		}
		
		result = valueExpression.getValue(context);

		return handleResult(result);
	}

	private boolean handleResult(Object evaluationResult) {
		if (evaluationResult == null) {
			throw new ConstraintDeclarationException("Expression \"" + expression
					+ "\" returned null, but must return either true or false.");
		}

		if (!(evaluationResult instanceof Boolean)) {
			throw new ConstraintDeclarationException("Script \"" + expression + "\" returned " + expression
					+ " (of type " + evaluationResult.getClass().getCanonicalName()
					+ "), but must return either true or false.");
		}

		return Boolean.TRUE.equals(evaluationResult);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy