package uk.ac.ebi.quickgo.annotation.model;

import uk.ac.ebi.quickgo.annotation.service.search.SearchConfig;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Modify filter values for references.
 *
 *
 * @author Tony Wardell
 * Date: 24/05/2016
 * Time: 17:25
 * Created with IntelliJ IDEA.
 */
public class ReferenceModifier implements FilterModifier{

    private static final Pattern ALL_NUMERIC = Pattern.compile("\\d*");
    private final SearchConfig searchConfig;

    @Autowired
    public ReferenceModifier(SearchConfig searchConfig) {
        this.searchConfig = searchConfig;
    }

    public String[] modify(String[] original){
        Set<String> modifiedArgs = new LinkedHashSet<>();
        for (int i = 0; i < original.length; i++) {
            String s = original[i];

            //If given just a
            if( s.equals("DOI") || s.equals("GO_REF") || s.equals("PMID") || s.equals("Reactome")  ){
                modifiedArgs.add(original[i]+":*");
            }

            //If we have just an id, create a request for all known DBs
            Matcher m = ALL_NUMERIC.matcher(s);
            if(m.matches()){
                modifiedArgs.add("DOI:" + original[i]);
                modifiedArgs.add("GO_REF:" + original[i]);
                modifiedArgs.add("PMID:" + original[i]);
                modifiedArgs.add("Reactome:" + original[i]);
            }
        }
        return original;
    }
}

@@ -856,173 +856,149 @@
 ) %7B%0A
-            String s = original%5Bi%5D;%0A%0A            //If given just a%0A            if( s.equals(%22DOI%22) %7C%7C s.equals(%22GO_REF%22) %7C%7C s.equals(%22PMID%22) %7C%7C s.equals(%22Reactome%22) 
+%0A            //If given just a Database identifier, make it a wildcard search%0A            if( searchConfig.referenceDbs.contains(original%5Bi%5D)
  )%7B%0A
@@ -1058,26 +1058,37 @@
            %7D
-%0A%0A
+ else %7B%0A%0A    
             
@@ -1159,16 +1159,20 @@
         
+    
 Matcher 
@@ -1199,12 +1199,26 @@
 her(
-s);%0A
+original%5Bi%5D);%0A    
     
@@ -1227,16 +1227,17 @@
       if
+ 
 (m.match
@@ -1241,16 +1241,17 @@
 tches())
+ 
 %7B%0A      
@@ -1264,48 +1264,64 @@
     
+  
-modifiedArgs.add(%22DOI:%22 + original%5Bi%5D);%0A
+  for (String db : searchConfig.referenceDbs)%0A        
     
@@ -1353,15 +1353,14 @@
 add(
-%22GO_REF
+db + %22
 :%22 +
@@ -1394,49 +1394,21 @@
     
-modifiedArgs.add(%22PMID:%22 + original%5Bi%5D);%0A
+%7D else %7B%0A    
     
@@ -1440,22 +1440,8 @@
 add(
-%22Reactome:%22 + 
 orig
@@ -1454,34 +1454,42 @@
 %5D);%0A            
+   
+ 
 %7D%0A
+    
         %7D%0A      
@@ -1494,23 +1494,78 @@
     
-return original
+%7D%0A        return modifiedArgs.toArray(new String%5BmodifiedArgs.size()%5D)
 ;%0A  

Use preloaded list to test reference filter values.