package uk.ac.ebi.quickgo.common.validator;

import uk.ac.ebi.quickgo.common.loader.DbXRefLoader;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
//import org.springframework.beans.factory.annotation.Value;

/**
 * Gene product IDs that don't match the regular expressions for recognised/supported gene product types should be
 * rejected with an error; those that do match should be treated as valid (plausible), even if they don't actually
 * identify a gene product in any of the supported databases.
 * The supported databases should be: UniProtKB, RNAcentral, IntAct, and any of the databases that are available in
 * the ID mapping function.
 *
 * @author Tony Wardell
 * Date: 14/06/2016
 * Time: 13:32
 * Created with IntelliJ IDEA.
 */
public class GeneProductIDValidator implements ConstraintValidator<GeneProductIDList,String>{

//    @Value("${geneproduct.db.xref.valid.regexes}") //todo define in common
//    private String xrefValidationRegexFile;
    private String xrefValidationRegexFile="src/test/resources/DB_XREFS_ENTITIES.dat.gz"; //todo remove
    Predicate<String> idValidator;

    @Override public void initialize(GeneProductIDList constraintAnnotation) {
        GeneProductDbXRefIDFormats
                dbXrefEntities = GeneProductDbXRefIDFormats.createWithData(geneProductLoader().load());
        idValidator = dbXrefEntities::isValidId;
    }

    @Override public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        List invalidGeneProdIDs = Arrays.stream(s.split(",")).filter(idValidator.negate()).collect(Collectors.toList());
        return invalidGeneProdIDs.size() == 0;
        //return "P99999".equalsIgnoreCase(s);
    }

    private DbXRefLoader geneProductLoader() {
        return new DbXRefLoader(this.xrefValidationRegexFile);
    }
}

@@ -42,62 +42,8 @@
 r;%0A%0A
-import uk.ac.ebi.quickgo.common.loader.DbXRefLoader;%0A%0A
 impo
@@ -256,18 +256,16 @@
 ontext;%0A
-//
 import o
@@ -312,14 +312,19 @@
 ion.
-Value;
+Autowired;%0A
 %0A%0A/*
@@ -957,525 +957,233 @@
 %3E%7B%0A%0A
-//
     @
-Value(%22$%7Bgeneproduct.db.xref.valid.regexes%7D%22) //todo define in common%0A//    private String xrefValidationRegexFile;%0A    private String xrefValidationRegexFile=%22src/test/resources/DB_XREFS_ENTITIES.dat.gz%22; //todo remove%0A    Predicate%3CString%3E idValidator;%0A%0A    @Override public void initialize(GeneProductIDList constraintAnnotation) %7B%0A        GeneProductDbXRefIDFormats%0A                dbXrefEntities = GeneProductDbXRefIDFormats.createWithData(geneProductLoader().load());%0A        idValidator = dbXrefEntities
+Autowired%0A    GeneProductDbXRefIDFormats geneProductValidator;%0A    Predicate%3CString%3E idValidator;%0A%0A    @Override public void initialize(GeneProductIDList constraintAnnotation) %7B%0A        idValidator = geneProductValidator
 ::is
@@ -1400,16 +1400,33 @@
 .collect
+%0A                
 (Collect
@@ -1491,172 +1491,8 @@
  0;%0A
-        //return %22P99999%22.equalsIgnoreCase(s);%0A    %7D%0A%0A    private DbXRefLoader geneProductLoader() %7B%0A        return new DbXRefLoader(this.xrefValidationRegexFile);%0A
     

Move the creation of the GeneProductDbXRefIDFormats to the calling module.