/*
 * Copyright 2009, 2010 Lari Natri <lari.natri@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


package compid.data.tuples;

import java.util.HashMap;

/**
 *
 * @author Lari Natri
 */
public final class ProtDbInfo extends Tuple {
    public static final String TABLELABEL = "PROTDBINFO";

    private static final HashMap<String,ProtDbInfo> protDbs = new HashMap<String,ProtDbInfo>();

    static {
        protDbs.put("gi", new ProtDbInfo("gi","NCBInr",""));
        protDbs.put("gb", new ProtDbInfo("gb","GenBank",""));
        protDbs.put("emb", new ProtDbInfo("emb","EMBL Data Library",""));
        protDbs.put("dbj", new ProtDbInfo("dbj","DNA Database of Japan",""));
        protDbs.put("pir", new ProtDbInfo("pir","NBNF PIR",""));
        protDbs.put("prf", new ProtDbInfo("prf","Protein Research Foundation",""));
        protDbs.put("sp", new ProtDbInfo("sp","SWISS-PROT",""));
        protDbs.put("pdb", new ProtDbInfo("pdb","Brookhaven Protein Data Bank",""));
        protDbs.put("pat", new ProtDbInfo("pat","Patents",""));
        protDbs.put("bbs", new ProtDbInfo("bbs","GenInfo Backbone Id",""));
        protDbs.put("gnl", new ProtDbInfo("gnl","General database identifier",""));
        protDbs.put("ref", new ProtDbInfo("ref","NCBI Reference Sequence",""));
        protDbs.put("lcl", new ProtDbInfo("lcl","Local Sequence identifier",""));
        protDbs.put("tpg", new ProtDbInfo("tpg","TPG",""));
        protDbs.put("tpd", new ProtDbInfo("tpd","TPD",""));
        protDbs.put("tpe", new ProtDbInfo("tpe","TPE",""));
    }
 
    private String accPrefix = null;
    private String dbName = null;
    private String dbDescr = null;
   
  
    public ProtDbInfo(String accPrefix, String dbName, String dbDescr) {
        this.accPrefix = accPrefix;
        this.dbName = dbName;
        this.dbDescr = dbDescr;
        if (this.dbName==null)
            throw new NullPointerException("Database name not set.");
        if (this.dbName.length()<1)
            throw new IllegalArgumentException("Database name not set.");
        if (this.dbDescr==null) this.dbDescr="";
        if (this.accPrefix==null) this.accPrefix="";

        this.setTupleHasNoForeignKeys();
    }

    
    public String getAccPrefix() { return this.accPrefix; }
    
    public String getDbName() { return this.dbName; }
    
    public String getDbDescr() { return this.dbDescr; }    
    
   
    @Override
    public String getTableLabel() {
        return TABLELABEL;
    }

    @Override
    public HashMap<String,Object> getColsAndValues() {
        HashMap<String,Object> map = new HashMap<String,Object>(5,1);
        map.put("ACCPREFIX", this.accPrefix);
        map.put("DBNAME", this.dbName);
        map.put("DBDESCR", this.dbDescr);
        return map;
    }

    @Override
    public HashMap<String,Object> getKeysAndValuesForExistenceCheck() {
        HashMap<String,Object> map = new HashMap<String,Object>(1);
        map.put("ACCPREFIX", this.accPrefix);
        return map;
    }     
    
    @Override
    public boolean equals(Object obj)
    {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ProtDbInfo other = (ProtDbInfo) obj;
        if (this.accPrefix != other.accPrefix && (this.accPrefix == null || !this.accPrefix.equals(other.accPrefix))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 47 * hash + (this.accPrefix != null ? this.accPrefix.hashCode() : 0);
        return hash;
    }

    public static ProtDbInfo getProtDbByAccession(String accPrefix) {
        String pre;
        if (accPrefix==null) {
            pre = "";
        } else {
            pre = accPrefix.split("[|]")[0];
        }
        ProtDbInfo pdbi = protDbs.get(pre);
        if (pdbi==null) {
            pdbi = new ProtDbInfo("","UNKNOWN","Protein entries without known " +
                    "accession prefix are linked here"); //NOI18N
            pdbi.setPrimaryKey(1);
        }
        return pdbi;
    }

}
