/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.core.data.reference;

import fr.cenra.rhomeo.api.data.Reference;
import java.util.Objects;

/**
 * Odonata presence by department.
 * 
 *  Colonne |          Type          | Modificateurs 
 * ---------+------------------------+---------------
 *  espece  | character varying(255) | 
 *  dept    | character varying(255) | 
 *  valeur  | double precision       | 
 *  id_dept | character varying(2)   | 
 *  cd_nom  | integer                | 
 *  cd_ref  | integer                | 
 * 
 * (Source : base issue du script RhoMéO_Geomatys.backup)
 * 
 * Note : There is no explicit primary key, but it seems logic for the key to be 
 * (cd_nom, id_dept).
 * 
 * Remarques :
 * 
 * L'absence de contrainte d'intégrité rend l'analyse sémantique de la table non
 * triviale.
 * 
 * Sémantique
 * ==========
 * 
 * Même si logiquement la clef devrait être (cd_nom, id_dept), il
 * semble que les combinaisons (cd_ref, id_dept) soient également "sémantiquement" uniques
 * car en nombre identique.
 * 
 * select count(*) from (select distinct cd_ref, id_dept from odo_especes_par_dept) as tab;
 * count 
 *-------
 * 2436
 *
 * select count(*) from (select distinct cd_nom, id_dept from odo_especes_par_dept) as tab;
 * count 
 *-------
 * 2436
 * 
 * Toutefois, on ne peut pas en déduire l'identité entre cd_nom et cd_ref dans la
 * table (ce qui est logique). Nous avons d'ailleurs :
 * 
 * select distinct cd_nom, cd_ref from odo_especes_par_dept where cd_nom<>cd_ref;
 * cd_nom | cd_ref 
 *--------+--------
 * 820003 | 645873
 *
 *
 * Creation of the reference list from the database
 * ================================================
 * 
 * Export csv
 * ----------
 * copy (                                
 * select * from odo_especes_par_dept
 * ) 
 * to '/…/odo_especes_par_dept.csv' delimiter ';' csv header;
 * 
 * Dumps
 * -----
 * pg_dump -t referentiels_non_geo.odo_especes_par_dept rhomeo > /…/odo_especes_par_dept.copy.sql
 * pg_dump --inserts -t referentiels_non_geo.odo_especes_par_dept rhomeo > /…/odo_especes_par_dept.inserts.sql
 *
 * 
 *
 * @author Samuel Andrés (Geomatys) <samuel.andres at geomatys.com>
 */
public class OdonataDepartmentReference implements Reference {
    
    // META-INFORMATION : ATTRIBUTE NAMES
    // MUST BE CONSISTENT WITH CLASS ATTRIBUTES
    public static final String ATT_CD_NOM = "cd_nom";
    public static final String ATT_CD_REF = "cd_ref";
    public static final String ATT_ESPECE = "espece";
    public static final String ATT_ID_DEPT = "id_dept";
    public static final String ATT_DEPT = "dept";
    public static final String ATT_VALEUR = "valeur";
    
    // Species attributes
    private int cd_nom;
    private int cd_ref;
    private String espece;
    
    // Department attributes
    private String id_dept;
    private String dept;
    
    private double valeur;

    public int getCd_nom() {
        return cd_nom;
    }

    public void setCd_nom(int cd_nom) {
        this.cd_nom = cd_nom;
    }

    public int getCd_ref() {
        return cd_ref;
    }

    public void setCd_ref(int cd_ref) {
        this.cd_ref = cd_ref;
    }

    public String getEspece() {
        return espece;
    }

    public void setEspece(String espece) {
        this.espece = espece;
    }

    public String getId_dept() {
        return id_dept;
    }

    public void setId_dept(String id_dept) {
        this.id_dept = id_dept;
    }

    public String getDept() {
        return dept;
    }

    public void setDept(String dept) {
        this.dept = dept;
    }

    public double getValeur() {
        return valeur;
    }

    public void setValeur(double valeur) {
        this.valeur = valeur;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + this.cd_nom; // same cd_nom => same cd_ref and same espece (interpretation)
        hash = 89 * hash + Objects.hashCode(this.id_dept); // same id_dept => same dept
        hash = 89 * hash + (int) (Double.doubleToLongBits(this.valeur) ^ (Double.doubleToLongBits(this.valeur) >>> 32));
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final OdonataDepartmentReference other = (OdonataDepartmentReference) obj;
        if (this.cd_nom != other.cd_nom) {
            return false;
        }  // same cd_nom => same cd_ref and same espece (interpretation)
        if (Double.doubleToLongBits(this.valeur) != Double.doubleToLongBits(other.valeur)) {
            return false;
        }
        if (!Objects.equals(this.id_dept, other.id_dept)) {
            return false;
        } // same id_dept => same dept
        return true;
    }

    @Override
    public String toString() {
        return new StringBuilder(Integer.toString(cd_nom)).append('|').append(espece).append('|').append(dept).append('|').append(valeur).toString();
    }
}
