package com.brilliance.isc.common.validator.es.rule.impl;

import com.brilliance.isc.common.validator.es.support.IRuleValidator;
import jdk.nashorn.api.scripting.ScriptObjectMirror;

import javax.script.ScriptEngine;
import java.util.Map;

/**
 * @author fukai
 * 规则校验实现抽象类
 */
public abstract class AbstractRuleValidator implements IRuleValidator {
    /**
     * 校验函数入口
     *
     * @param value        当前字段的值
     * @param source
     * @param type
     * @param rule         规则对象
     * @param scriptEngine 当前ScriptEngine
     * @return
     */
    @Override
    public boolean validate(Object value, Map<String,Object> source, String type, ScriptObjectMirror rule, ScriptEngine scriptEngine) {
        String function = getValidateFunction();
        ScriptObjectMirror functionMirror = (ScriptObjectMirror)scriptEngine.get(function);
        boolean isSuccess = (Boolean) functionMirror.call(null,rule,value,source,type);
        return isSuccess;
    }

    public String toUpperFirst(String str){
        return str.substring(0,1).toUpperCase()+str.substring(1);
    }
}
