View Javadoc

1   package net.ramapuram.thomas.model;
2   
3   import org.apache.commons.lang.builder.ToStringBuilder;
4   import org.apache.commons.lang.builder.ToStringStyle;
5   import org.springframework.security.core.GrantedAuthority;
6   
7   import javax.persistence.Column;
8   import javax.persistence.Entity;
9   import javax.persistence.GeneratedValue;
10  import javax.persistence.GenerationType;
11  import javax.persistence.Id;
12  import javax.persistence.NamedQueries;
13  import javax.persistence.NamedQuery;
14  import javax.persistence.Table;
15  import javax.persistence.Transient;
16  import java.io.Serializable;
17  
18  /**
19   * This class is used to represent available roles in the database.
20   *
21   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
22   *         Version by Dan Kibler dan@getrolling.com
23   *         Extended to implement Acegi GrantedAuthority interface
24   *         by David Carter david@carter.net
25   */
26  @Entity
27  @Table(name = "role")
28  @NamedQueries({
29          @NamedQuery(
30                  name = "findRoleByName",
31                  query = "select r from Role r where r.name = :name "
32          )
33  })
34  public class Role extends BaseObject implements Serializable, GrantedAuthority {
35      private static final long serialVersionUID = 3690197650654049848L;
36      private Long id;
37      private String name;
38      private String description;
39  
40      /**
41       * Default constructor - creates a new instance with no values set.
42       */
43      public Role() {
44      }
45  
46      /**
47       * Create a new instance and set the name.
48       *
49       * @param name name of the role.
50       */
51      public Role(final String name) {
52          this.name = name;
53      }
54  
55      @Id
56      @GeneratedValue(strategy = GenerationType.AUTO)
57      public Long getId() {
58          return id;
59      }
60  
61      /**
62       * @return the name property (getAuthority required by Acegi's GrantedAuthority interface)
63       * @see org.springframework.security.core.GrantedAuthority#getAuthority()
64       */
65      @Transient
66      public String getAuthority() {
67          return getName();
68      }
69  
70      @Column(length = 20)
71      public String getName() {
72          return this.name;
73      }
74  
75      @Column(length = 64)
76      public String getDescription() {
77          return this.description;
78      }
79  
80      public void setId(Long id) {
81          this.id = id;
82      }
83  
84      public void setName(String name) {
85          this.name = name;
86      }
87  
88      public void setDescription(String description) {
89          this.description = description;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      public boolean equals(Object o) {
96          if (this == o) {
97              return true;
98          }
99          if (!(o instanceof Role)) {
100             return false;
101         }
102 
103         final Role role = (Role) o;
104 
105         return !(name != null ? !name.equals(role.name) : role.name != null);
106 
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     public int hashCode() {
113         return (name != null ? name.hashCode() : 0);
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     public String toString() {
120         return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
121                 .append(this.name)
122                 .toString();
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public int compareTo(Object o) {
129         return (equals(o) ? 0 : -1);
130     }
131 }