/**
 * 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;

/**
 *
 * Affinity of Odonata species with different kinds of odonatologic habitat.
 *
 * Value range is from 1 to 4.
 *
 * Colonne |          Type          | Modificateurs
 *---------+------------------------+---------------
 * espece  | character varying(255) |
 * biogeo  | character varying(255) |
 * habitat | character varying(255) |
 * valeur  | character varying(255) |
 * zbio    | character varying(255) |
 * 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, zbio, habitat).
 *
 * Creation of the reference list from the database
 * ================================================
 *
 * Export csv
 * ----------
 * copy (
 * select * from odo_dependance_habitat
 * )
 * to '/…/odo_dependance_habitat.csv' delimiter ';' csv header;
 *
 * Dumps
 * -----
 * pg_dump -t referentiels_non_geo.odo_dependance_habitat rhomeo > /…/odo_dependance_habitat.copy.sql
 * pg_dump --inserts -t referentiels_non_geo.odo_dependance_habitat rhomeo > /…/odo_dependance_habitat.inserts.sql
 *
 * @author Samuel Andrés (Geomatys) <samuel.andres at geomatys.com>
 */
public class OdonataHabitatReference 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_ZBIO = "zbio";
    public static final String ATT_BIOGEO = "biogeo";
    public static final String ATT_HABITAT = "habitat";
    public static final String ATT_VALEUR = "valeur";

    // Species attributes
    private int cd_nom;
    private int cd_ref;
    private String espece;

    // biozone attributes
    private String zbio;
    private String biogeo;

    // Habitat
    private String habitat;

    // Value for a association between a species, a biozone and an habitat.
    private int 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 getZbio() {
        return zbio;
    }

    public void setZbio(String zbio) {
        this.zbio = zbio;
    }

    public String getBiogeo() {
        return biogeo;
    }

    public void setBiogeo(String biogeo) {
        this.biogeo = biogeo;
    }

    public String getHabitat() {
        return habitat;
    }

    public void setHabitat(String habitat) {
        if (habitat != null && habitat.startsWith("0"))
            habitat = habitat.substring(1);
        this.habitat = habitat;
    }

    public int getValeur() {
        return valeur;
    }

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

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + this.cd_nom; // same cd_nom => same cd_ref and same espece (interpretation)
        hash = 59 * hash + Objects.hashCode(this.zbio); // same zbio => same biogeo (interpretation)
        hash = 59 * hash + Objects.hashCode(this.habitat);
        hash = 61 * hash + this.valeur;
        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 OdonataHabitatReference other = (OdonataHabitatReference) obj;
        if (this.cd_nom != other.cd_nom) {
            return false;
        } // same cd_nom => same cd_ref and same espece (interpretation)
        if (!Objects.equals(this.zbio, other.zbio)) {
            return false;
        } // same zbio => same biogeo (interpretation)
        if (!Objects.equals(this.habitat, other.habitat)) {
            return false;
        }
        if (this.valeur != other.valeur) {
            return false;
        }
        return true;
    }

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