001 // Copyright 2008 Thiago H. de Paula Figueiredo
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package br.com.arsmachina.authentication.entity;
016
017 import java.io.Serializable;
018
019 import javax.persistence.Column;
020 import javax.persistence.Entity;
021 import javax.persistence.GeneratedValue;
022 import javax.persistence.Id;
023 import javax.persistence.Table;
024
025 import org.hibernate.validator.Length;
026 import org.hibernate.validator.NotNull;
027
028 /**
029 * Class that represents a single permission.
030 *
031 * @author Thiago H. de Paula Figueiredo
032 */
033 @Entity
034 @Table(name = "permission")
035 final public class Permission implements Comparable<Permission>, Serializable {
036
037 private static final long serialVersionUID = 1L;
038
039 /**
040 * Name of the role that all users have.
041 */
042 public static final String USER_ROLE_NAME = "ROLE_USER";
043
044 /**
045 * Minimum name length.
046 */
047 public static final int MINIMUM_NAME_LENGTH = 2;
048
049 /**
050 * Maximum name length.
051 */
052 public static final int MAXIMUM_NAME_LENGTH = 50;
053
054 private Integer id;
055
056 private String name;
057
058 /**
059 * No-arg constructor.
060 */
061 public Permission() {
062 }
063
064 /**
065 * Constructor that receives a name.
066 *
067 * @param name a {@link String}. It cannot be null.
068 * @throws IllegalArgumentException if <code>name</code> is null.
069 */
070 public Permission(String name) {
071
072 if (name == null) {
073 throw new IllegalArgumentException("Parameter name cannot be null");
074 }
075
076 this.name = name;
077
078 }
079
080 /**
081 * @see java.lang.Object#hashCode()
082 */
083 @Override
084 public int hashCode() {
085 return name != null ? name.hashCode() : super.hashCode();
086 }
087
088 /**
089 * @see java.lang.Object#equals(java.lang.Object)
090 */
091 @Override
092 public boolean equals(Object obj) {
093 if (this == obj) {
094 return true;
095 }
096 if (obj == null) {
097 return false;
098 }
099 if (getClass() != obj.getClass()) {
100 return false;
101 }
102 final Permission other = (Permission) obj;
103 if (name == null) {
104 if (other.name != null) {
105 return false;
106 }
107 }
108 else if (!name.equals(other.name)) {
109 return false;
110 }
111 return true;
112 }
113
114 /**
115 * @see java.lang.Comparable#compareTo(java.lang.Object)
116 */
117 public int compareTo(Permission o) {
118 return getName().compareToIgnoreCase(o.getName());
119 }
120
121 /**
122 * Returns the <code>name</code> property.
123 *
124 * @return a {@link String}.
125 */
126 @Override
127 public String toString() {
128 return getName();
129 }
130
131 /**
132 * Returns the value of the <code>id</code> property.
133 *
134 * @return a {@link Integer}.
135 */
136 @Id
137 @GeneratedValue
138 public Integer getId() {
139 return id;
140 }
141
142 /**
143 * Changes the value of the <code>id</code> property.
144 *
145 * @param id a {@link Integer}.
146 */
147 public void setId(Integer id) {
148 this.id = id;
149 }
150
151 /**
152 * Returns the value of the <code>name</code> property.
153 *
154 * @return a {@link String}.
155 */
156 @NotNull
157 @Length(min = MINIMUM_NAME_LENGTH, max = MAXIMUM_NAME_LENGTH)
158 @Column(nullable = false, unique = true, length = MAXIMUM_NAME_LENGTH)
159 public String getName() {
160 return name;
161 }
162
163 /**
164 * Changes the value of the <code>name</code> property.
165 *
166 * @param name a {@link String}.
167 */
168 public void setName(String name) {
169 this.name = name;
170 }
171
172 }