001package model.validation;
002
003import model.entity.User;
004import model.repository.GenericRepository;
005
006import javax.inject.Inject;
007import javax.validation.ConstraintValidator;
008import javax.validation.ConstraintValidatorContext;
009
010public class UniqueUsernameValidator implements ConstraintValidator<UniqueUsername, String> {
011
012    @Inject GenericRepository genericRepository;
013
014    @Override
015    public void initialize(UniqueUsername userExists) {
016
017    }
018
019    @Override
020    public boolean isValid(String username, ConstraintValidatorContext constraintValidatorContext) {
021        if(username == null) return true;
022        return genericRepository.findByNaturalId(User.class, username) == null;
023    }
024}