package com.cf.imes.module.report.validation.template;

import com.bstek.ureport.export.ProducerEnum;
import org.apache.commons.lang3.StringUtils;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.Objects;

/**
 * 自定义报表模板生成文件类型入参校验器
 *
 * @author Gqr
 * @since 2024/7/17 9:33
 */
public class ReportTemplateProducerTypeInEnumValidator implements ConstraintValidator<ReportTemplateProducerTypeInEnum, String> {

    @Override
    public void initialize(ReportTemplateProducerTypeInEnum constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }
        for (ProducerEnum producerEnum : ProducerEnum.values()) {
            if (Objects.equals(producerEnum.name(), value.toUpperCase())) {
                return true;
            }
        }
        return false;
    }

}

