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


import com.acme.structure.BaseEntity;
import com.acme.structure.BaseEntityListener;

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

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.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity(name = "RoleGroup")
@Table(name = "MAINROLEGROUPS")
//this is for oracle db @SequenceGenerator(name = "ROLEGROUP_SEQ", sequenceName = "ROLEGROUP_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class RoleGroup extends BaseEntity
{

    private String name;
    private String description;
    private Collection<RoleGroupDetail> roleGroupDetails;

    public RoleGroup()
    {
        entityName = "RoleGroup";
        roleGroupDetails = new ArrayList<RoleGroupDetail>();
    }

    public RoleGroup(int id, String name)
    {
        entityName = "RoleGroup";
        roleGroupDetails = new ArrayList<RoleGroupDetail>();

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

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

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

    @Column(name = "DESCRIPTION")
    public String getDescription()
    {
        return description;
    }

    public void setDescription(String description)
    {
        this.description = description;
    }

    @Column(name = "NAME")
    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @OneToMany(targetEntity = RoleGroupDetail.class, fetch = (FetchType.EAGER), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "ROLEGROUPID")
    public Collection<RoleGroupDetail> getRoleGroupDetails()
    {
        return roleGroupDetails;
    }

    public void setRoleGroupDetails(Collection<RoleGroupDetail> roleGroupDetail)
    {
        this.roleGroupDetails = roleGroupDetail;
    }

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

        RoleGroupDetail roleGroupDetail = null;
        Iterator itr = this.roleGroupDetails.iterator();

        while (itr.hasNext())
        {
            roleGroupDetail = (RoleGroupDetail)itr.next();
            roleGroupDetail.clearIdFields();
        }
    }

    @Override
    public String toString()
    {
        return getName();
    }

}
