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

de.thksystems.validation.beanvalidation.OneNotEmptyValidator Maven / Gradle / Ivy

Go to download

Commons for persistence, hibernate, xstreams, enterprise, spring, servlets, ...

There is a newer version: 3.9.0
Show newest version
package de.thksystems.validation.beanvalidation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import de.thksystems.exception.ServiceRuntimeException;
import de.thksystems.util.reflection.ReflectionUtils;

public class OneNotEmptyValidator extends AbstractNotEmptyValidator implements ConstraintValidator {

	private String[] fieldnames;

	@Override
	public void initialize(OneNotEmpty constraintAnnotation) {
		fieldnames = constraintAnnotation.fieldnames();
	}

	@Override
	public boolean isValid(Object container, ConstraintValidatorContext context) {
		try {
			for (String fieldname : fieldnames) {
				Object fieldValue = ReflectionUtils.getFieldValue(container, container.getClass().getDeclaredField(fieldname));
				if (isNotEmpty(fieldValue)) {
					return true;
				} else {
					continue;
				}
			}
			return false;
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) {
			throw new ServiceRuntimeException(e.getMessage(), e);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy