package cz.hsrs.db.model;

import java.sql.ResultSet;
import java.sql.SQLException;
// default package
// Generated 3.6.2008 8:21:13 by Hibernate Tools 3.2.2.GA

import cz.hsrs.db.DBObject;
import cz.hsrs.db.util.SensorUtil;

/**
 * Phenomenon generated by hbm2java
 */
public class Phenomenon implements DBObject{

    @Override
    public DBObject getDBObject(ResultSet set) throws SQLException {
        return new Phenomenon(set);
    }

    private String phenomenonId;
    private String phenomenonName;
    private String unit;

    public Phenomenon() {
    }
    
    /**
     * Constructor for creating object from ResultSet
     * @param set
     * @throws SQLException
     */
    public Phenomenon(ResultSet set) throws SQLException{
        this.phenomenonId = set.getString("phenomenon_id");
        this.phenomenonName = set.getString("phenomenon_name");
        this.unit = set.getString("unit");
    }

    /**
     * Constructor creates object from ID and UoM
     * @param phenomenonId
     * @param unit
     * @param valuetype
     */
    public Phenomenon(String phenomenonId, String unit, String valuetype) {
        this.phenomenonId = phenomenonId;
        this.unit = unit;
    }

    /**
     * Constructor creates object with all attributes
     * @param phenomenonId
     * @param phenomenonName
     * @param unit
     * @param valuetype
     */
    public Phenomenon(String phenomenonId, String phenomenonName, String unit, String valuetype) {
        this.phenomenonId = phenomenonId;
        this.phenomenonName = phenomenonName;
        this.unit = unit;
    }
    
    /**
     * Constructor creates object without ID 
     * @param phenomenonName
     * @param unit
     */
    public Phenomenon(String phenomenonName, String unit) {
        this.phenomenonName = phenomenonName;
        this.unit = unit;
    }
    
    /**
     * Constructor for creating Phenomenon object that is already in DB
     * @param phenomenonId
     */
    public Phenomenon(String phenomenonId) {
        this.phenomenonId = phenomenonId;
    }
    
    public String internalGetPhenomenonId() {
        return this.phenomenonId;
    }
    
    public String getPhenomenonId() {
        return this.phenomenonId;
    }
    
    public void internalSetPhenomenonId(String id){
        this.phenomenonId = id;
    }

    public String getPhenomenonName() {
        return this.phenomenonName;
    }

    public String getUnit() {
        return this.unit;
    }
    
    @Override
    public String toString() {
        return "[phenomenonId=" + phenomenonId + ", phenomenonName="
                + phenomenonName + ", unit=" + unit + "]";
    }

    /**
     * Method to insert new Phenomenon to DB
     * @return instance of new Phenomenon with generated ID or Phenomenon that is already in DB
     * @throws SQLException if an error occurs during inserting
     * @throws NoItemFoundException 
     */
    public Phenomenon insertToDb() throws SQLException, NoItemFoundException {
        SensorUtil sUtil = new SensorUtil();
        if(this.phenomenonId != null){
            Phenomenon phenDB = sUtil.getPhenomenonById(phenomenonId);
            if(this.phenomenonName == null && this.unit == null){
                if(phenDB == null){
                    throw new NoItemFoundException("Phenomenon with given ID="+this.phenomenonId+" was not found!");
                }
                else {
                    return phenDB;
                }
            } else if(this.phenomenonName != null && this.unit != null){
                if(phenDB == null){
                    // insert new
                    int i = sUtil.insertNewPhenomenon(this);
                    if (i == 1 || i == 0){
                        return this;
                    }
                    else{
                        throw new SQLException("Phenomenon was not inserted!");
                    }
                } else {
                    if(phenDB.equals(this)){
                        return this;
                    } 
                    else if(phenDB.getPhenomenonName().equalsIgnoreCase(phenomenonName)==true 
                            && phenDB.getUnit().equalsIgnoreCase(unit) == false){
                        // insert new
                        int i = sUtil.insertNewPhenomenon(this);
                        if (i == 1 || i == 0){
                            return this;
                        }
                        else{
                            throw new SQLException("Phenomenon was not inserted!");
                        }
                    }
                    else{
                        throw new SQLException("It is not possible to insert Phenomenon with given attributes!");
                    }
                }
            }
            else{
                throw new SQLException("It is not possible to insert Phenomenon with given attributes!");
            }
        }
        else{
            if(this.phenomenonName != null && this.unit != null){
                Phenomenon phenDB = sUtil.getPhenomenonByName(phenomenonName);
                if(phenDB == null){
                    // get new ID + insert new
                    this.phenomenonId = sUtil.getNextPhenomenonId();
                    int i = sUtil.insertNewPhenomenon(this);
                    if (i == 1 || i == 0){
                        return this;
                    }
                    else{
                        throw new SQLException("Phenomenon was not inserted!");
                    }
                }
                else{
                    if(phenDB.getUnit().equalsIgnoreCase(unit)){
                        return phenDB;
                    }
                    else{
                        // get new ID + insert new
                        this.phenomenonId = sUtil.getNextPhenomenonId();
                        int i = sUtil.insertNewPhenomenon(this);
                        if (i == 1 || i == 0){
                            return this;
                        }
                        else{
                            throw new SQLException("Phenomenon was not inserted!");
                        }
                    }
                }
            }
            else{
                throw new SQLException("It is not possible to insert Phenomenon with given attributes!");
            }
        }
    }
}