View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.model;
24  
25  import java.io.Serializable;
26  
27  import javax.persistence.Column;
28  import javax.persistence.Entity;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.GenerationType;
31  import javax.persistence.Id;
32  import javax.persistence.Table;
33  import javax.persistence.Transient;
34  
35  import org.springframework.security.GrantedAuthority;
36  import org.apache.commons.lang.builder.ToStringBuilder;
37  import org.apache.commons.lang.builder.ToStringStyle;
38  
39  /**
40   * This class is used to represent available roles in the database.
41   * 
42   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> Version by
43   *         Dan Kibler dan@getrolling.com Extended to implement Acegi
44   *         GrantedAuthority interface by David Carter david@carter.net
45   */
46  @Entity
47  @Table(name = "role")
48  public class Role extends BaseObject implements Serializable, GrantedAuthority {
49      private static final long serialVersionUID = 3690197650654049848L;
50  
51      private Long id;
52  
53      private String name;
54  
55      private String description;
56  
57      /**
58       * Default constructor - creates a new instance with no values set.
59       */
60      public Role() {
61      }
62  
63      /**
64       * Create a new instance and set the name.
65       * 
66       * @param name
67       *                name of the role.
68       */
69      public Role(final String name) {
70  	this.name = name;
71      }
72  
73      @Id
74      @GeneratedValue(strategy = GenerationType.AUTO)
75      public Long getId() {
76  	return id;
77      }
78  
79      /**
80       * @see org.acegisecurity.GrantedAuthority#getAuthority()
81       * @return the name property (getAuthority required by Acegi's
82       *         GrantedAuthority interface)
83       */
84      @Transient
85      public String getAuthority() {
86  	return getName();
87      }
88  
89      @Column(length = 20)
90      public String getName() {
91  	return this.name;
92      }
93  
94      @Column(length = 64)
95      public String getDescription() {
96  	return this.description;
97      }
98  
99      public void setId(Long id) {
100 	this.id = id;
101     }
102 
103     public void setName(String name) {
104 	this.name = name;
105     }
106 
107     public void setDescription(String description) {
108 	this.description = description;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public boolean equals(Object o) {
116 	if (this == o) {
117 	    return true;
118 	}
119 	if (!(o instanceof Role)) {
120 	    return false;
121 	}
122 
123 	final Role role = (Role) o;
124 
125 	return !(name != null ? !name.equals(role.name) : role.name != null);
126 
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     @Override
133     public int hashCode() {
134 	return (name != null ? name.hashCode() : 0);
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public String toString() {
142 	return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE).append(
143 		this.name).toString();
144     }
145 
146 	public int compareTo(Object o) {
147 		if (this == o) {
148 		    return 0;
149 		}
150 		if (!(o instanceof Role)) {
151 		    return -1;
152 		}
153 
154 		final Role role = (Role) o;
155 
156 		if (name != null && role.name!=null && name.equals(role.name)){
157 			return 0;
158 		} else {
159 			return -1;
160 		}
161 	}
162 
163 }