/*
 * 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.sql.Date;
import java.util.HashMap;


/**
 *
 * @author Lari Natri
 */
public final class DatasetInfo extends Tuple {
    
    public enum AlgType {
        UNKNOWN {
            @Override public String toShortString() { return "[unknown]"; }
            @Override String toDbString() { return "Unknown"; } }, //NOI18N
        MASCOT {
            @Override public String toShortString() { return "[ms]"; }
            @Override String toDbString() { return "Mascot"; } }, //NOI18N
        PARAGON {
            @Override public String toShortString() { return "[pg]"; }
            @Override String toDbString() { return "Paragon"; } }; //NOI18N
            
        public static AlgType getAlgTypeFromString(String algStr) {
            if (algStr==null) return AlgType.UNKNOWN;
            algStr = algStr.toUpperCase();
            if (algStr.equals(MASCOT.toDbString().toUpperCase())) return AlgType.MASCOT;
            if (algStr.equals(PARAGON.toDbString().toUpperCase())) return AlgType.PARAGON;
            return AlgType.UNKNOWN;
        }
        public abstract String toShortString();
        @Override public String toString() {
            return this.toDbString();
        }
        abstract String toDbString();
    }
    
    
    public static final String TABLELABEL = "DATASETINFO"; //NOI18N
    
    private final String name;
    private final String description;
    private final String protDataBase;
    private final String source;
    private final AlgType algorithm;
    private String params;
    private String dataCounts;
    private final java.sql.Date additionMoment;
    
    public DatasetInfo(String name, String descr, String db, String source, 
                       String params, AlgType algorithm)
    {
        this(name,descr,db,source,algorithm,params,null);
    }
   
    public DatasetInfo(String name, String descr, String db, String source, 
                       AlgType algorithm, String params, java.util.Date date)
    {
        if (name==null) throw new NullPointerException("Dataset name not set");
        this.name = name;
        this.description = descr==null ? "" : descr;
        this.protDataBase = db==null ? "" : db;
        this.source = source==null ? "" : source;
        this.algorithm = algorithm==null ? AlgType.UNKNOWN : algorithm;
        this.params = params==null ? "[unknown]" : params;
        this.additionMoment = date==null ?
            new java.sql.Date(new java.util.Date().getTime())
            : new java.sql.Date(date.getTime());
        this.dataCounts = "";
        this.setTupleHasNoForeignKeys();
    }

    
    public final String getName() { return this.name; }
    
    public final String getDescription() { return this.description; }
    
    public final String getUsedProtDataBase() { return this.protDataBase; }

    public final String getDataSource() { return this.source; }
    
    public final AlgType getAlgorithm() { return this.algorithm; }

    public final String getParameters() { return this.params; }
    
    public final Date getDate() { return this.additionMoment; }

    public final String getDataCountsStr() { return this.dataCounts; }

    public final void setParams(String paramStr) {
        this.params = paramStr==null ? "[unknown]" : paramStr;
    }

    public final void setDataCountsStr(String dataCounts) {
        this.dataCounts = dataCounts==null ? "" : dataCounts;
    }
    
    @Override
    public final String getTableLabel() {
        return TABLELABEL;
    }

    @Override
    public final HashMap<String,Object> getColsAndValues() {
        HashMap<String,Object> map = new HashMap<String,Object>(8,1);
        map.put("DSNAME", this.name);
        map.put("DATACOUNTS", this.dataCounts);
        map.put("DSDESCR", this.description);
        map.put("PEPDB", this.protDataBase);
        map.put("SOURCESTR", this.source);
        map.put("ALGORITHM", this.algorithm.toDbString());
        map.put("PARAMS", this.params);
        map.put("ADDTIME", this.additionMoment);
        return map;
    }

    @Override
    public final HashMap<String,Object> getKeysAndValuesForExistenceCheck() {
        HashMap<String,Object> map = new HashMap<String,Object>(1,1);
        map.put("DSNAME", this.name);
        return map;
    }

    @Override
    public final boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final DatasetInfo other = (DatasetInfo) obj;
        if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
            return false;
        }
        if (this.algorithm != other.algorithm && (this.algorithm == null || !this.algorithm.equals(other.algorithm))) {
            return false;
        }
        return true;
    }

    @Override
    public final int hashCode() {
        int hash = 7;
        hash = 83 * hash + (this.name != null ? this.name.hashCode() : 0);
        hash = 83 * hash + (this.algorithm != null ? this.algorithm.hashCode() : 0);
        return hash;
    }


    @Override
    public final String toString() {
        int pk = this.getPrimaryKey();
        if (pk<1) {
            return this.name;
        } else {
            return "["+this.getPrimaryKey()+"] " + this.name;
        }
    }

}
