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

io.afu.baseframework.annotations.impls.NotEmptyIfParamIsImpl Maven / Gradle / Ivy

package io.afu.baseframework.annotations.impls;

import io.afu.baseframework.annotations.NotEmptyIfParamIs;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class NotEmptyIfParamIsImpl implements ConstraintValidator {


    private String[] targetElement;

    private String moniteElement;

    private String elementValue;


    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        Set toValidateElements = new HashSet<>(Arrays.asList(targetElement));
        Class cls = o.getClass();
        boolean verify = true;
        Field[] fields = cls.getDeclaredFields();
        for (Field field:fields){
            try {
            field.setAccessible(true);
            String name = field.getName();
            Class eleType = field.getType();
            if (eleType.isInstance(String.class)){
                    String value = (String) field.get(o);
                    if (value!= null && !value.equals("")){
                        verify = true;
                    }
            }else {
                Object v = field.get(o);
                if (v != null){
                    verify = true;
                }
            }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return verify;
    }

    @Override
    public void initialize(NotEmptyIfParamIs constraintAnnotation) {
        this.targetElement = constraintAnnotation.targetElement();
        this.moniteElement = constraintAnnotation.moniteElement();
        this.elementValue = constraintAnnotation.elementValue();
    }






}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy