package info.bukova.isspst.data;

import info.bukova.isspst.Constants;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.springframework.security.core.GrantedAuthority;

@Entity
@Table(name="ROLE")
public class Role extends BaseSimpleData implements GrantedAuthority, DataModel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5677876140880991163L;
	
	@Column(name="AUTHORITY", unique=true)
	private String authority;
	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;
	@ManyToMany
	@LazyCollection(LazyCollectionOption.FALSE)
	@JoinTable(name="ROLE_PERMISSION", joinColumns={@JoinColumn(name="ROLE_ID")}, inverseJoinColumns={@JoinColumn(name="PERMISSION_ID")})
	private List<Permission> permissions;
	@Column(name = "WORKGROUP")
	private boolean workgroup;
	@Column(name = "CENTRE")
	private boolean centre;
		
	public Role(String authority, String description) {
		this();
		this.authority = authority;
		this.description = description; 
	}
	
	public Role() {
		this.permissions = new ArrayList<Permission>();
		centre = false;
		workgroup = false;
	}

	@Override
	public String getAuthority() {
		return authority;
	}

	public String getDescription() {
		return description;
	}

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

	public void setAuthority(String authority) {
		this.authority = authority;
	}

	public List<Permission> getPermissions() {
		return permissions;
	}

	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}
	
	public void addPermission(Permission permission) {
		this.permissions.add(permission);
	}

	@Override
	public boolean equals(Object o) {
		if ((o instanceof Role) && ((Role)o).getId() == this.getId()) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isWorkgroup() {
		return workgroup;
	}

	public void setWorkgroup(boolean workgroup) {
		this.workgroup = workgroup;
	}

	public boolean isCentre() {
		return centre;
	}

	public void setCentre(boolean centre) {
		this.centre = centre;
	}

}
