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

import com.google.common.base.Preconditions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

/**
 *
 * Holds the entries from 'DB_XREFS_ENTITIES.dat.gz'
 * Ask this class if the id is a valid gene product id.
 * Can either use defaults or supply the identifying database and typeName (protein or gene).
 *
 *
 * @author Tony Wardell
 *         Date: 18/04/2016
 *         Time: 13:36
 *         Created with IntelliJ IDEA.
 */
public class GeneProductDbXRefIDFormats {

    private final Map<GeneProductDbXRefIDFormats.Key, GeneProductDbXRefIDFormat> geneProductXrefEntities;
    static final Key[] targetDBs = new Key[]{ new Key("UniProtKB","PR:000000001"), new Key("IntAct","GO:0043234"),
            new Key("RNAcentral","CHEBI:33697")};

    private GeneProductDbXRefIDFormats(Map<Key, GeneProductDbXRefIDFormat> geneProductXrefEntities) {
        checkArgument(geneProductXrefEntities != null, "Gene product xref entities map cannot be null");

        this.geneProductXrefEntities = geneProductXrefEntities;
    }

    /**
     * Test if a gene product id is valid.
     * @param id The gene product ID passed in from the client
     * @return true if the id is valid, false otherwise
     */
    public boolean isValidId(String id) {

        //If we haven't managed to load the validation regular expressions then pass everything
        if (geneProductXrefEntities.size()==0) {
            return true;
        }
        for(Key dbKey : targetDBs){
            if(this.geneProductXrefEntities.get(dbKey).matches(id)) return true;
        }

        //no matches
        return false;
    }

    /**
     * Create an instance of this class
     * @param entities A list of regex expressions specified by database and usage.
     * @return
     */
    public static GeneProductDbXRefIDFormats createWithData(List<GeneProductDbXRefIDFormat> entities) {
        Preconditions.checkArgument(entities != null, "The list of GeneProductDbXRefIDFormat entities is null, which " +
                "is illegal");

        Map<Key, GeneProductDbXRefIDFormat> mappedEntities = new HashMap<>();

        for(GeneProductDbXRefIDFormat entity : entities) {
            GeneProductDbXRefIDFormats.Key key = new GeneProductDbXRefIDFormats.Key(entity.getDatabase(), entity
                    .getEntityType());
            mappedEntities.put(key, entity);
        }

        return new GeneProductDbXRefIDFormats(mappedEntities);
    }

    /**
     * Key class holds the variables we need to lookup up the validating regex
     */

    public static class Key {
        String database;
        String entityType;

        public Key(String database, String entityType) {
            this.database = database;
            this.entityType = entityType;
        }

        @Override public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }

            Key key = (Key) o;

            if (!database.equals(key.database)) {
                return false;
            }
            return entityType.equals(key.entityType);

        }

        @Override public int hashCode() {
            int result = database.hashCode();
            result = 31 * result + entityType.hashCode();
            return result;
        }

        @Override public String toString() {
            return "Key{" +
                    "database='" + database + '\'' +
                    ", entityType='" + entityType + '\'' +
                    '}';
        }
    }
}

@@ -775,16 +775,24 @@
 es;%0A    
+private 
 static f
@@ -802,25 +802,26 @@
 l Key%5B%5D 
-targetDBs
+TARGET_DBS
  = new K
@@ -825,17 +825,16 @@
 w Key%5B%5D%7B
- 
 new Key(
@@ -845,16 +845,17 @@
 ProtKB%22,
+ 
 %22PR:0000
@@ -862,16 +862,28 @@
 00001%22),
+%0A           
  new Key
@@ -892,16 +892,17 @@
 IntAct%22,
+ 
 %22GO:0043
@@ -941,16 +941,17 @@
 entral%22,
+ 
 %22CHEBI:3
@@ -1598,18 +1598,20 @@
 s.size()
+ 
 ==
+ 
 0) %7B%0A   
@@ -1653,16 +1653,17 @@
      for
+ 
 (Key dbK
@@ -1671,18 +1671,20 @@
 y : 
-targetDBs)
+TARGET_DBS) 
 %7B%0A  
@@ -1695,16 +1695,17 @@
       if
+ 
 (this.ge
@@ -1750,28 +1750,60 @@
 es(id)) 
-return true;
+%7B%0A                return true;%0A            %7D
 %0A       
@@ -2358,16 +2358,17 @@
      for
+ 
 (GenePro

Fix potential security issue with array that was accessible outside of the class.