org.opentestsystem.delivery.testreg.domain.constraintvalidators.JexlAssertValidator.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testreg.domain.constraintvalidators.JexlAssertValidator.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/

package org.opentestsystem.delivery.testreg.domain.constraintvalidators;

import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.validation.ConstraintDeclarationException;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.apache.commons.lang.StringUtils;
import org.opentestsystem.delivery.testreg.domain.ImplicitEligibilityRule;
import org.opentestsystem.delivery.testreg.domain.constraints.JexlAssert;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This is an JSR-223 Jexl script validator for the {@link JexlAssert} constraint annotation.
 */

public class JexlAssertValidator implements ConstraintValidator<JexlAssert, Object> {

    private String script;

    private String alias;

    @Autowired
    private JexlScriptEngineWrapper jexlScriptEngineWrapper;

    @Override
    public void initialize(final JexlAssert constraintAnnotation) {
        this.script = constraintAnnotation.expression();
        this.alias = constraintAnnotation.alias();
    }

    @Override
    public boolean isValid(final Object value, final ConstraintValidatorContext context) {
        Object evaluationResult;
        final ScriptEngine jexlScriptEngine = getJexlScriptEngine();
        boolean hasValueInImplicitEligibilityRule = false;
        final boolean isImplicitEligibilityRule = value instanceof ImplicitEligibilityRule;
        if (isImplicitEligibilityRule) {
            final ImplicitEligibilityRule ierValue = (ImplicitEligibilityRule) value;
            hasValueInImplicitEligibilityRule = StringUtils.isNotBlank(ierValue.getValue());
        }

        if (!isImplicitEligibilityRule || hasValueInImplicitEligibilityRule) {
            jexlScriptEngine.put(this.alias, value);

            try {
                evaluationResult = jexlScriptEngine.eval(this.script);
            } catch (final ScriptException e) {
                throw new ConstraintDeclarationException(e);
            }

            if (!(evaluationResult instanceof Boolean)) {
                throw new ConstraintDeclarationException("Evaluation Result should be a boolean type");
            }

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

    private ScriptEngine getJexlScriptEngine() {
        return this.jexlScriptEngineWrapper.getJEXLScriptEngine();
    }

}