Role.java

package com.tradecloud.authentication;

import com.tradecloud.domain.RoleGroup;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.springframework.security.core.GrantedAuthority;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;

@Entity
@Table(name = "role")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@NamedQueries({@NamedQuery(name = "findAllByTypeExcludingGroups",
        query = "from Role where authority is not null and roleGroup not in (:roleGroup)"),
        @NamedQuery(name = "findAllKPI",
                query = "from Role where lower(authority) like lower('%KPI_%')")})
@XmlRootElement
public class Role implements GrantedAuthority, Serializable, Comparable<Role> {

    private static final long serialVersionUID = 1L;

    @XmlAttribute
    @Id
    @NotNull(message = "Authority is required")
    private String authority;

    @XmlAttribute
    @NotNull(message = "Description is required")
    private String description;

    @Enumerated(EnumType.STRING)
    private RoleGroup roleGroup;

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

    @Deprecated
    protected Role() {
    }

    /**
     * The description is now a required field so should be passed into the
     * constructor.
     *
     * @see public Role(String authority, String description)
     */
    @Deprecated
    public Role(String authority) {
        this.authority = authority;
    }

    public Role(String authority, String description) {
        this.authority = authority;
        this.description = description;
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof Role))
            return false;
        Role castOther = (Role) other;
        return new EqualsBuilder().append(authority, castOther.authority).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(authority).toHashCode();
    }

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

    public String getDescription() {
        return description;
    }

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

    @Override
    public int compareTo(Role o) {
        //return getAuthority().compareToIgnoreCase(o.getAuthority());
        return getDescription().compareTo(o.getDescription());
    }

    public RoleGroup getRoleGroup() {
        return roleGroup;
    }

    public void setRoleGroup(RoleGroup roleGroup) {
        this.roleGroup = roleGroup;
    }
}