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

com.bizmda.bizsip.integrator.checkrule.FieldCheckRuleThread Maven / Gradle / Ivy

There is a newer version: 1.0.0.Beta9
Show newest version
package com.bizmda.bizsip.integrator.checkrule;

import cn.hutool.core.text.StrFormatter;
import cn.hutool.json.JSONObject;
import com.bizmda.bizsip.common.BizException;
import com.bizmda.bizsip.common.BizResultEnum;

import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * @author 史正烨
 */
public class FieldCheckRuleThread implements Callable {
    private FieldCheckRule fieldCheckRule;
    private JSONObject jsonObject;

    public FieldCheckRuleThread(JSONObject jsonObject, FieldCheckRule fieldCheckRule) {
        this.fieldCheckRule = fieldCheckRule;
        this.jsonObject = jsonObject;
    }

    @Override
    public FieldChcekRuleResult call() throws Exception {
        Method method;
        try {
            method = FieldCheckRuleFunction.class.getMethod(fieldCheckRule.getRule(), Object.class, List.class);
        } catch (NoSuchMethodException e) {
            throw new BizException(BizResultEnum.CHECKRULE_FIELD_CHECK_FUNCTION_NOTFOUND);
        }

        Object value = this.jsonObject.getByPath(this.fieldCheckRule.getField());
        boolean result = (boolean)method.invoke(null,value,this.fieldCheckRule.getArgs());
        if (result) {
            return null;
        }
        return FieldChcekRuleResult.builder()
                .field(this.fieldCheckRule.getField())
                .value(value)
                .rule(this.fieldCheckRule.getRule())
                .args(this.fieldCheckRule.getArgs())
                .message(StrFormatter.format(this.fieldCheckRule.getMessage(),value))
                .build();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy