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


@Entity(name = "User")
@Table(name = "MAINUSERS")
//this is for oracle db @SequenceGenerator(name = "USER_SEQ", sequenceName = "USER_SEQ", allocationSize = 1)
@EntityListeners( { BaseEntityListener.class })
public class User extends BaseEntity
{
    private String userName;
    private String password;
    private boolean passive;
    private Date lastLogin;
    private String email;
    private String userType;

    private Collection<UserRoleGroup> userRoleGroups;

    public User()
    {
        entityName = "User";
        userRoleGroups = new ArrayList<UserRoleGroup>();
        setUserType(this.getEntityName());
    }

    public User(int id, String userName, String password)
    {
        this.id = id;
        this.userName = userName;
        this.password = password;
    }

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

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

    @Column(name = "USERNAME", unique = true)
    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String username)
    {
        this.userName = username;
    }

    @Column(name = "PASSWORD")
    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    @OneToMany(targetEntity = UserRoleGroup.class, fetch = (FetchType.LAZY), cascade = { CascadeType.ALL })
    @org.hibernate.annotations.Cascade( { org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
    @JoinColumn(name = "USERID")
    public Collection<UserRoleGroup> getUserRoleGroups()
    {
        return userRoleGroups;
    }

    public void setUserRoleGroups(Collection<UserRoleGroup> userRoleGroups)
    {
        this.userRoleGroups = userRoleGroups;
    }

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

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

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LASTLOGIN")
    public Date getLastLogin()
    {
        return lastLogin;
    }

    public void setLastLogin(Date lastLogin)
    {
        this.lastLogin = lastLogin;
    }

    public void setUserType(String userType)
    {
        this.userType = userType;
    }

    @Column(name = "USERTYPE")
    public String getUserType()
    {
        return userType;
    }

    @Column(name = "EMAIL")
    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getEmail()
    {
        return email;
    }

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

        UserRoleGroup userRoleGroup = null;
        Iterator itr = userRoleGroups.iterator();

        while (itr.hasNext())
        {
            userRoleGroup = (UserRoleGroup)itr.next();
            userRoleGroup.clearIdFields();
        }
    }
    
    @Override
    public String toString()
    {
        return getUserName();
    }
}

