package info.bukova.isspst.data;

import info.bukova.isspst.Constants;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;

@Entity
@Table(name="PERMISSION")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Permission extends BaseSimpleData implements GrantedAuthority {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Column(name="AUTHORITY")
	private String authority;
	@Column(name = "DESCRIPTION", length = Constants.LEN_DESCRIPTION)
	private String description;
	@Column(name="MODULE")
	private String module;
	@Column(name = "TYPE")
	@Enumerated(EnumType.ORDINAL)
	private PermissionType type;
	
	public Permission(String authority, String description) {
		this();
		this.authority = authority;
		this.description = description;
		this.module = "";
	}
	
	public Permission(String authority, String description, String module) {
		this(authority, description);
		this.module = module;
	}
	
	public Permission(String authority, String description, PermissionType type) {
		this(authority, description, "", type);
	}
	
	public Permission(String authority, String description, String module, PermissionType type) {
		this(authority, description, module);
		this.type = type;
	}
	
	public Permission() {
		type = PermissionType.GLOBAL;
	}

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

	public String getDescription() {
		return description;
	}

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

	public String getModule() {
		return module;
	}

	public void setModule(String module) {
		this.module = module;
	}

	public void setAuthority(String authority) {
		this.authority = authority;
	}
	
	@Override
	public boolean equals(Object o) {
		if ((o instanceof Permission) && ((Permission)o).getId() == this.getId()) {
			return true;
		} else {
			return false;
		}
	}

	public PermissionType getType() {
		return type;
	}

	public void setType(PermissionType type) {
		this.type = type;
	}

}
