UserRole.java 4.1 KB
package org.legrog.entities;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

/**
 * The database representation of a user role or group.
 * A given {@link User} may be part of one or more {@link UserRole}.
 * <br/>
 * Warning: due to laziness of mapped objects, private attributes of all DB entities shall never be used directly.
 * You shall always use the getter/setter methods.
 * alias UserRole
 *
 */
/*
    Importé depuis la v2.
 */
@Entity
// TODO évaluer extend v2
public class UserRole /* extends org.roliste.data.DbEntity */
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name="USER_ROLE_ID")
    private int userRoleId;

    /**
     * The role identifier.
     */
    private String rolename;

    /**
     * The {@link User}s for this user role.
     */
    @ManyToMany(mappedBy = "roles")
    @Column(name = "USER_ID")
    private Set<User> users;

    /**
     * Builds a new and empty user role definition.
     * All attributes are set to their default value.
     * <br/>
     * Needed by Hibernate for Java reflection.
     */
    public UserRole() {
        super();
        rolename = null;
        visible = true;
        // no need to synchronize this
        users = new HashSet<User>();
    }

    public int getUserRoleId() {
        return userRoleId;
    }

    public void setUserRoleId(int userRoleId) {
        this.userRoleId = userRoleId;
    }

    /**
     * Returns the role identifier.
     * @return the {@link String} identifier.
     * @see #setRolename(String)
     * hibernate.property
     *  	column="NOM_ROLE"
     *  	not-null="true"
     *  	unique="true"
     *  	access="property"
     *  	length="50"
     */
    public String getRolename() {
        return rolename;
    }

    /**
     * Initializes the role identifier.
     * @param name the new {@link String} identifier.
     * @see #getRolename()
     */
    public void setRolename(String name) {
        rolename = name;
    }

    /**
     * The role validation status.
     * A role may be temporarily deactivated.
     */
    private boolean visible;

    /**
     * Indicates if the role is visible.
     * If not, users should not be able to access the privileges
     * they inherit from being part of this role.
     * @return the visible flag.
     * @see #setVisible(boolean)
     * hibernate.property
     *  	column="IND_VISIBLE"
     *  	access="property"
     */
    public boolean isVisible() {
        return visible;
    }

    /**
     * Initializes the user visible flag.
     * @param visible the new flag value.
     * @see #isVisible
     */
    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    /**
     * Retrieves the list of {@link User}s for this user role.
     * SHALL be used as a read-only attribute. In particular, avoid
     * using {@link Set#add(Object)} or {@link Set#remove(Object)} on
     * the returned value without caution.
     * @return a {@link Set} of {@link User}. May be <code>null</code>.
     * @see #setUsers(Set)
     * hibernate.many-to-many
     *  	column="UTILISATEUR_FK"
     *  	class="org.roliste.data.db.User"
     *      foreign-key="FK_UTILISATEURROLE_UTILISATEUR"
     * hibernate.key
     * 		column="ROLE_FK"
     * 		not-null="true"
     *      foreign-key="FK_UTILISATEURROLE_ROLEUTILISATEUR"
     * hibernate.set
     * 		access="property"
     * 		table="role_utilisateur"
     *		lazy="true"
     *      inverse="true"
     */
    public Set<User> getUsers() {
        return users;
    }

    /**
     * Sets the list of {@link User}s for this user role.
     * @param users the new {@link Set} of {@link User}s. May be
     * <code>null</code> (we don't handle the relation from this side).
     * @see #getUsers()
     */
    protected void setUsers(Set<User> users) {
        this.users = users;
    }

    /**
     * Returns a string representation of this user role definition.
     * @return a string representing this user role definition.
     * hidden
     */
    @Override
    public String toString() {
        return "ID_ROLE=" + getUserRoleId() + " NOM_ROLE=" + rolename + " IND_VISIBLE=" + visible;
    }
}