AccountRole.java 4.12 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 Account} may be part of one or more {@link AccountRole}.
 * <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 AccountRole
 *
 */
/*
    Importé depuis la v2.
 */
@Entity
// TODO évaluer extend v2
public class AccountRole /* extends org.roliste.data.DbEntity */
{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userRoleId;

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

    /**
     * The {@link Account}s for this user role.
     */
    @ManyToMany(mappedBy = "roles")
    private Set<Account> accounts;

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

    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, accounts 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 Account}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 Account}. May be <code>null</code>.
     * @see #setAccounts(Set)
     * hibernate.many-to-many
     *  	column="UTILISATEUR_FK"
     *  	class="org.roliste.data.db.Person"
     *      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<Account> getAccounts() {
        return accounts;
    }

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

    /**
     * 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;
    }
}