/*
* Copyright (c) JForum Team. All rights reserved.
*
* The software in this package is published under the terms of the LGPL
* license a copy of which has been included with this distribution in the
* license.txt file.
*
* The JForum Project
* http://www.jforum.net
*/
package net.jforum.entities;
import java.util.ArrayList;
import java.util.List;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.CollectionOfElements;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.PrototypeScoped;
/**
* @author Rafael Steil
*/
@Entity
@Table(name = "jforum_roles")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Component
@PrototypeScoped
public class Role {
@Id
@SequenceGenerator(name = "sequence", sequenceName = "jforum_roles_seq")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence")
@Column(name = "role_id")
private int id;
@Column(name = "name")
private String name;
@ManyToOne
@JoinColumn(name = "group_id")
private Group group;
@CollectionOfElements(fetch = FetchType.EAGER)
@JoinTable(name = "jforum_role_values", joinColumns = @JoinColumn(name = "role_id"))
@Column(name = "role_value")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Fetch(FetchMode.SUBSELECT)
private List<Integer> roleValues = new ArrayList<Integer>();
public Role() { }
public Role(Role role) {
this.id = role.id;
this.name = role.name;
this.group = role.group;
this.roleValues = new ArrayList<Integer>(role.roleValues);
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the group
*/
public Group getGroup() {
return group;
}
/**
* @param group the group to set
*/
public void setGroup(Group group) {
this.group = group;
}
/**
* @return the values
*/
public List<Integer> getRoleValues() {
return roleValues;
}
/**
* Add a new role value to this role
* @param value
*/
public void addRoleValue(int value) {
this.roleValues.add(value);
}
/**
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof Role)) {
return false;
}
return ((Role)o).getId() == this.getId();
}
/**
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return this.getId();
}
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("%d => %s (%s)", this.getId(), this.getName(), this.roleValues);
}
}