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


import com.acme.model.domain.ejf.entity.User;
import com.acme.structure.BaseEntity;
import com.acme.structure.BaseEntityListener;

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

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;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;


@Entity(name = "Employee")
@Table(name = "EMPLOYEES")
//this is for oracle db @SequenceGenerator(name = "EMPLOYEE_SEQ", sequenceName = "EMPLOYEE_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class Employee extends BaseEntity
{
    private String firstName;
    private String lastName;
    private Long identityId;
    private Date dateOfBirth;
    private boolean passive;
    private Branch branch;
    private Department department;
    private Date dateOfHire;
    private User user;
    private Address address;
    private Collection<EmployeeContact> contacts;

    private String title;

    public Employee()
    {
        entityName = "Employee";
        
        this.contacts=new ArrayList<EmployeeContact>();
    }

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

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

    @Column(name = "PASSIVE")
    public boolean isPassive()
    {
        return passive;
    }

    public void setPassive(boolean inUse)
    {
        this.passive = inUse;
    }

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

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

    @OneToOne(targetEntity = User.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @JoinColumn(name = "USERID")
    public User getUser()
    {
        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    public void setDateOfHire(Date dateOfHire)
    {
        this.dateOfHire = dateOfHire;
    }

    @Column(name = "DATEOFHIRE")
    @Temporal(TemporalType.DATE)
    public Date getDateOfHire()
    {
        return dateOfHire;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    @Column(name = "FIRSTNAME")
    public String getFirstName()
    {
        return firstName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    @Column(name = "LASTNAME")
    public String getLastName()
    {
        return lastName;
    }

    public void setIdentityId(Long identityId)
    {
        this.identityId = identityId;
    }

    @Column(name = "IDENTITYID")
    public Long getIdentityId()
    {
        return identityId;
    }

    public void setDepartment(Department department)
    {
        this.department = department;
    }

    @ManyToOne(targetEntity = Department.class, cascade = { })
    @JoinColumn(name = "DEPARTMENTID")
    public Department getDepartment()
    {
        return department;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    @Column(name = "TITLE")
    public String getTitle()
    {
        return title;
    }

    public void setDateOfBirth(Date dateOfBirth)
    {
        this.dateOfBirth = dateOfBirth;
    }

    @Column(name = "DATEOFBIRTH")
    @Temporal(TemporalType.DATE)
    public Date getDateOfBirth()
    {
        return dateOfBirth;
    }

    public void setContacts(Collection<EmployeeContact> contacts)
    {
        this.contacts = contacts;
    }

    @OneToMany(targetEntity = EmployeeContact.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "EMPLOYEEID")
    public Collection<EmployeeContact> getContacts()
    {
        return contacts;
    }

    @OneToOne(targetEntity = Address.class, cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "ADDRESSID")
    public Address getAddress()
    {
        return address;
    }

    public void setAddress(Address address)
    {
        this.address = address;
    }

    @Override
    @Transient
    public void clearIdFields()
    {
        super.clearIdFields();

        if (user != null)
            user.clearIdFields();
    }

    public String toString()
    {
        return getFirstName() + " " + getLastName();
    }
}
