package com.palmnest.model;

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

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.security.core.GrantedAuthority;

/**
 * This class is used to represent available roles in the database.
 *
 * @author <a href="mailto:chasel.java@gmail.com">Chasel Li</a>
 */
@Entity
@Table(name = "tb_app_role")
public class Role extends BaseObject implements Serializable, GrantedAuthority {

	private static final long serialVersionUID = 3690197650654049848L;

	private Long    id;
	private String  name;
	private String  description;
	private Date    lastUpdatedTime;
	private String  lastUpdatedBy;
	private Integer statusFlag;
	private Integer roleType;
	private String  isenalbed;  //1-有效 0-无效
	private String  SYSCODE;
	private Integer ISINITIALIZATION;
	private Integer FLOOR_REPAIR;
	private Integer orgId;
	private String sucName;

	private Set<RoleFunction> roleFunctions = new HashSet<RoleFunction>();

	/**
	 * Default constructor - creates a new instance with no values set.
	 */
	public Role() {
	}

	/**
	 * Create a new instance and set the name.
	 *
	 * @param name name of the role.
	 */
	public Role(final String sucName) {
		this.sucName = sucName;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "ID", unique = true, nullable = false)
	public Long getId() {
		return id;
	}

	/**
	 * @return the name property (getAuthority required by Acegi's
	 * GrantedAuthority interface)
	 * @see org.springframework.security.core.GrantedAuthority#getAuthority()
	 */
	@Transient
	public String getAuthority() {
		return getName();
	}

	@Column(name = "SCU_NAME", length = 20)
	public String getName() {
		return this.sucName;
	}

	@Column(length = 64)
	public String getDescription() {
		return this.description;
	}

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

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

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

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "LAST_UPDATED_TIME", length = 19)
	public Date getLastUpdatedTime() {
		return this.lastUpdatedTime;
	}

	public void setLastUpdatedTime(Date lastUpdatedTime) {
		this.lastUpdatedTime = lastUpdatedTime;
	}

	@Column(name = "LAST_UPDATED_BY", length = 20)
	public String getLastUpdatedBy() {
		return this.lastUpdatedBy;
	}

	public void setLastUpdatedBy(String lastUpdatedBy) {
		this.lastUpdatedBy = lastUpdatedBy;
	}

	@Column(name = "STATUS_FLAG")
	public Integer getStatusFlag() {
		return this.statusFlag;
	}

	public void setStatusFlag(Integer statusFlag) {
		this.statusFlag = statusFlag;
	}

	@Column(name = "ROLE_TYPE")
	public Integer getRoleType() {
		return this.roleType;
	}

	public void setRoleType(Integer roleType) {
		this.roleType = roleType;
	}

	@Column(name = "ISENABLED")
	public String getIsenalbed() {
		return isenalbed;
	}

	public void setIsenalbed(String isenalbed) {
		this.isenalbed = isenalbed;
	}

	/*@ManyToMany(fetch = FetchType.EAGER)
	@JoinTable(name = "tr_app_role_function", joinColumns = { @JoinColumn(name = "ROLEID") }, inverseJoinColumns = @JoinColumn(name = "FUNCTION_ID"))
	public Set<AppFunction> getAppFunctions() {
		return this.appFunctions;
	}

	public void setAppFunctions(Set<AppFunction> appFunctions) {
		this.appFunctions = appFunctions;
	}*/
	@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.REMOVE}, fetch = FetchType.LAZY, mappedBy = "role")
	@JsonIgnore
	public Set<RoleFunction> getRoleFunctions() {
		return roleFunctions;
	}

	public void setRoleFunctions(Set<RoleFunction> roleFunctions) {
		this.roleFunctions = roleFunctions;
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Role)) {
			return false;
		}

		final Role role = (Role) o;

		return !(name != null ? !name.equals(role.name) : role.name != null);

	}

	/**
	 * {@inheritDoc}
	 */
	public int hashCode() {
		return (name != null ? name.hashCode() : 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
				this.name).toString();
	}


	/**
	 * longmeizhen
	 *
	 * @return
	 */
	public String getSYSCODE() {
		return SYSCODE;
	}

	public void setSYSCODE(String sYSCODE) {
		SYSCODE = sYSCODE;
	}

	public Integer getISINITIALIZATION() {
		return ISINITIALIZATION;
	}

	public void setISINITIALIZATION(Integer iSINITIALIZATION) {
		ISINITIALIZATION = iSINITIALIZATION;
	}

	public Integer getFLOOR_REPAIR() {
		return FLOOR_REPAIR;
	}

	public void setFLOOR_REPAIR(Integer fLOOR_REPAIR) {
		FLOOR_REPAIR = fLOOR_REPAIR;
	}

	@Column(name = "ORG_ID")
	public Integer getOrgId() {
		return orgId;
	}

	public void setOrgId(Integer orgId) {
		this.orgId = orgId;
	}

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

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