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

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

/**
 * 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[]> {
    private static final String MESSAGE = "At least one 'Gene Product ID' value is invalid: %s";

    @Autowired
    DbXRefEntityValidation xRefFormats;
    private Predicate<String> idValidator;

    @Override public void initialize(GeneProductIDList constraintAnnotation) {
        idValidator = xRefFormats::test;
    }

    @Override public boolean isValid(String[] geneProducts, ConstraintValidatorContext context) {
        if (geneProducts == null) {
            return true;
        }
        String invalid = Arrays.stream(geneProducts).filter(idValidator.negate()).collect
                (Collectors.joining(", "));

        if(invalid != null && invalid.isEmpty()){
            return true;
        }
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(String.format(MESSAGE, invalid))
                .addConstraintViolation();

        return false;
    }
}

@@ -937,22 +937,21 @@
  %7B%0A    p
-rivate
+ublic
  static 
@@ -967,30 +967,35 @@
 ing 
-MESSAGE = %22At least on
+DEFAULT_ERROR_MESSAGE = %22Th
 e 'G
@@ -1014,15 +1014,25 @@
 ID' 
-value i
+parameter contain
 s in
@@ -1036,16 +1036,23 @@
  invalid
+ values
 : %25s%22;%0A%0A
@@ -1070,16 +1070,24 @@
 red%0A    
+private 
 DbXRefEn
@@ -1274,14 +1274,8 @@
 mats
-::test
 ;%0A  
@@ -1373,24 +1373,57 @@
  context) %7B%0A
+        boolean isValid = true;%0A%0A
         if (
@@ -1435,17 +1435,17 @@
 roducts 
-=
+!
 = null) 
@@ -1454,39 +1454,8 @@
     
-        return true;%0A        %7D%0A
     
@@ -1472,16 +1472,21 @@
  invalid
+GpIds
  = Array
@@ -1507,16 +1507,37 @@
 roducts)
+%0A                    
 .filter(
@@ -1561,16 +1561,8 @@
 e())
-.collect
 %0A   
@@ -1574,16 +1574,28 @@
         
+    .collect
 (Collect
@@ -1627,37 +1627,29 @@
     
-if(invalid != null && invalid
+    if (!invalidGpIds
 .isE
@@ -1655,16 +1655,17 @@
 Empty())
+ 
 %7B%0A      
@@ -1674,29 +1674,38 @@
     
+ 
-return tru
+   isValid = fals
 e;%0A
+%0A
         
 %7D%0A  
@@ -1700,18 +1700,16 @@
         
-%7D%0A
         
@@ -1753,16 +1753,24 @@
 tion();%0A
+        
         
@@ -1828,16 +1828,30 @@
 .format(
+DEFAULT_ERROR_
 MESSAGE,
@@ -1858,19 +1858,32 @@
  invalid
-))%0A
+GpIds))%0A        
         
@@ -1916,16 +1916,40 @@
 ation();
+%0A            %7D%0A        %7D
 %0A%0A      
@@ -1957,21 +1957,23 @@
  return 
-false
+isValid
 ;%0A    %7D%0A
@@ -1973,9 +1973,8 @@
 %0A    %7D%0A%7D
-%0A

Change error message printed by GeneProductValidator to be more similar to the one output by the ArrayPattern