package com.demo.demo.model.obj.demo;
import com.demo.demo.model.obj.demo.Order;
import com.demo.demo.model.obj.demo.iface.ICustomer;
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: customer.
 * @author autogenerated
 */

@Entity
@Table(name = "customer", catalog = "demo")
public  class Customer implements Cloneable, Serializable, ICustomer {

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


    /** Field mapping. */
    private Set<Order> orders = new HashSet<Order>();

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

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

        this.id = id;
        this.name = name;
        this.surname = surname;
    }
    
 


    /**
     * Return the value associated with the column: order.
     * @return A Set&lt;Order&gt; object (this.order)
     */
    @Column( name = "customer_id", nullable = false  )
    @OneToMany( fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "customer"  )
    public Set<Order> getOrders() {
        return this.orders;
    }
    
    /**
     * Adds a bi-directional link of type Order to the orders set.
     * @param order item to add
     */
    public void addOrder(Order order) {
        this.orders.add(order);
        order.setCustomer(this);
    }

  
    /**  
     * Set the value related to the column: order.
     * @param order the order value you wish to set
     */
    public void setOrders(final Set<Order> order) {
        this.orders = order;
    }


    /**
     * Return the value associated with the column: id.
     * @return A Long object (this.id)
     */
    @Id 
    @Column( name = "customer_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;
    }


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

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


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

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


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

        copy.getOrders().addAll(this.getOrders());
        copy.setId(this.getId());
        copy.setName(this.getName());
        copy.setSurname(this.getSurname());
        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("id: " + this.getId() + ", ");
        sb.append("name: " + this.getName() + ", ");
        sb.append("surname: " + this.getSurname());
        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().loadCustomer(((Customer)aThat).getId());
            } catch (ClassCastException e) {
                return false;
            }
        }
        if ((aThat == null) || ( !(unproxyThat.getClass().equals(this.getClass())))) {
             return false;
        }
        
        final Customer that = (Customer) unproxyThat;
        boolean result = true;
        result = result & (((this.getName() == null) && (that.getName() == null)) || (this.getName() != null  && this.getName().equals(that.getName())));
        result = result & (((this.getSurname() == null) && (that.getSurname() == null)) || (this.getSurname() != null  && this.getSurname().equals(that.getSurname())));

        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.getName() == null ? 0 : this.getName().hashCode());
        result = 26 * result + (this.getSurname() == null ? 0 : this.getSurname().hashCode());

        return result;  
    }
}