SupportedLanguageValidator.java

/*
 * Copyright 2024 Global Crop Diversity Trust
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.genesys.spring.validation.javax;

import org.apache.commons.lang3.StringUtils;
import org.genesys.blocks.util.CurrentApplicationContext;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Set;

public class SupportedLanguageValidator implements ConstraintValidator<SupportedLanguage, String> {

	private Set<String> supportedLocales;

	/**
	 * Initializes the validator in preparation for
	 * {@link #isValid(String, ConstraintValidatorContext)} (String, ConstraintValidatorContext)} calls.
	 * The constraint annotation for a given constraint declaration
	 * is passed.
	 * <p/>
	 * This method is guaranteed to be called before any use of this instance for
	 * validation.
	 *
	 * @param constraintAnnotation annotation instance for a given constraint declaration
	 */
	@Override
	public void initialize(SupportedLanguage constraintAnnotation) {
		if (CurrentApplicationContext.getContext() == null) {
			System.err.println("Cannot get supportedLocales! CurrentApplicationContext.getContext() == null");
			supportedLocales = Set.of("en", "fr", "es");
			return;
		}
		supportedLocales = CurrentApplicationContext.getContext().getBean("supportedLocales", Set.class);
	}

	/**
	 * Implements the validation logic.
	 * The state of {@code value} must not be altered.
	 * <p/>
	 * This method can be accessed concurrently, thread-safety must be ensured
	 * by the implementation.
	 *
	 * @param value   object to validate
	 * @param context context in which the constraint is evaluated
	 * @return {@code false} if {@code value} does not pass the constraint
	 */
	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (value == null) {
			return true; // nothing to validate here
		}
		
		if (supportedLocales.stream().noneMatch(e -> StringUtils.equals(e, value))) {
			context.disableDefaultConstraintViolation();
			String message = value + " is not one of " + StringUtils.join(supportedLocales, ", ");
			context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
			return false;
		}

		return true;
	}
}