package com.demo.demo.model.obj.demo;
import com.demo.demo.model.obj.demo.Line;
import com.demo.demo.model.obj.demo.iface.IStock;
import com.demo.demo.services.data.DataLayerDemoImpl;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.proxy.HibernateProxy;


/** 
 * Object mapping for hibernate-handled table: stock.
 * @author autogenerated
 */

@Entity
@Table(name = "stock", catalog = "demo")
public  class Stock implements Cloneable, Serializable, IStock {

    /** Serial Version UID. */
    private static final long serialVersionUID = -559029077L;


    /** Field mapping. */
    private Set<Line> lines = new HashSet<Line>();

    /** Field mapping. */
    private String description;
    /** Field mapping. */
    private Long id = 0L; // init for hibernate bug workaround
    /**
     * Default constructor, mainly for hibernate use.
     */
    public Stock() {
        // Default constructor
    } 

    /** Constructor taking a given ID.
     * @param id to set
     */
    public Stock(Long id) {
        this.id = id;
    }
    
    /** Constructor taking a given ID.
     * @param description String object;
     * @param id Long object;
     */
    public Stock(String description, Long id) {

        this.description = description;
        this.id = id;
    }
    
 


    /**
     * Return the value associated with the column: line.
     * @return A Set&lt;Line&gt; object (this.line)
     */
    @Column( name = "stock_id", nullable = false  )
    @OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "stock"  )
    public Set<Line> getLines() {
        return this.lines;
    }
    
    /**
     * Adds a bi-directional link of type Line to the lines set.
     * @param line item to add
     */
    public void addLine(Line line) {
        this.lines.add(line);
        line.setStock(this);
    }

  
    /**  
     * Set the value related to the column: line.
     * @param line the line value you wish to set
     */
    public void setLines(final Set<Line> line) {
        this.lines = line;
    }


    /**
     * Return the value associated with the column: description.
     * @return A String object (this.description)
     */
    @Column( nullable = false, length = 45  )
    public String getDescription() {
        return this.description;
    }
    

  
    /**  
     * Set the value related to the column: description.
     * @param description the description value you wish to set
     */
    public void setDescription(final String description) {
        this.description = description;
    }


    /**
     * Return the value associated with the column: id.
     * @return A Long object (this.id)
     */
    @Id 
    @Column( name = "stock_id", nullable = false  )
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {
        return this.id;
    }
    

  
    /**  
     * Set the value related to the column: id.
     * @param id the id value you wish to set
     */
    public void setId(final Long id) {
        this.id = id;
    }


   /**
    * Deep copy.
    * @return cloned object
    * @throws CloneNotSupportedException on error
    */
    @Override
    public Stock clone() throws CloneNotSupportedException {
        super.clone();  // keep hierarchy
        final Stock copy = new Stock();

        copy.getLines().addAll(this.getLines());
        copy.setDescription(this.getDescription());
        copy.setId(this.getId());
        return copy;
    }


    /** Provides toString implementation.
     * @see java.lang.Object#toString()
     * @return String representation of this class.
     */
    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        
        sb.append("description: " + this.getDescription() + ", ");
        sb.append("id: " + this.getId());
        return sb.toString();       
    }

    
    /** Equals implementation. 
     * @see java.lang.Object#equals(java.lang.Object)
     * @param aThat Object to compare with
     * @return true/false
     */
    @Override
    public boolean equals(final Object aThat) {
        Object unproxyThat = aThat;
        
        if ( this == aThat ) {
             return true;
        }
        
        if (aThat instanceof HibernateProxy) {
            // narrow down the proxy to the class we are dealing with.
            try {
                unproxyThat = DataLayerDemoImpl.getInstance().loadStock(((Stock)aThat).getId());
            } catch (ClassCastException e) {
                return false;
            }
        }
        if ((aThat == null) || ( !(unproxyThat.getClass().equals(this.getClass())))) {
             return false;
        }
        
        final Stock that = (Stock) unproxyThat;
        boolean result = true;
        result = result & (((this.getDescription() == null) && (that.getDescription() == null)) || (this.getDescription() != null  && this.getDescription().equals(that.getDescription())));

        return result;
    }


    
    /** Calculate the hashcode.
     * @see java.lang.Object#hashCode()
     * @return a calculated number
     */
    @Override
    public int hashCode() {
        int result = 0;
        result = 26 * result + (this.getDescription() == null ? 0 : this.getDescription().hashCode());

        return result;  
    }
}