package com.acme.model.domain.sc.entity;


import com.acme.model.domain.ad.entity.Branch;
import com.acme.structure.BaseEntity;
import com.acme.structure.BaseEntityListener;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;


@Entity(name = "Section")
@javax.persistence.Table(name = "SECTIONS")
//this is for oracle db @SequenceGenerator(name = "SECTION_SEQ", sequenceName = "SECTION_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class Section extends BaseEntity
{
    private String name;
    private Branch branch;
    private Collection<Table> tables;

    public Section()
    {
        this.entityName = "Section";
        this.tables = new ArrayList<Table>();
    }

    public Section(int id, String name)
    {
        this.entityName = "Section";
        this.tables = new ArrayList<Table>();

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

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) //, this is for oracle db, generator = "SECTION_SEQ")
    @Column(name = "ID")
    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public void setName(String name)
    {
        this.name = name;
    }
    
    @Column(name = "NAME")
    public String getName()
    {
        return name;
    }

    public void setTables(Collection<Table> tables)
    {
        this.tables = tables;
    }


    @OneToMany(targetEntity = Table.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "SECTIONID")
    public Collection<Table> getTables()
    {
        return tables;
    }

    public void setBranch(Branch branch)
    {
        this.branch = branch;
    }

    @ManyToOne(targetEntity = Branch.class, cascade = { })
    @JoinColumn(name = "BRANCHID")
    public Branch getBranch()
    {
        return branch;
    }
    
    @Override
    public String toString()
    {
        return getName();
    }
}
