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    import java.util.ArrayList;
019    import java.util.List;
020    
021    import javax.persistence.Column;
022    import javax.persistence.Entity;
023    import javax.persistence.GeneratedValue;
024    import javax.persistence.Id;
025    import javax.persistence.JoinColumn;
026    import javax.persistence.JoinTable;
027    import javax.persistence.ManyToMany;
028    import javax.persistence.OrderBy;
029    import javax.persistence.Table;
030    
031    import org.hibernate.validator.Length;
032    import org.hibernate.validator.NotNull;
033    import org.hibernate.validator.Size;
034    
035    /**
036     * Class that represents a group of permissions.
037     * 
038     * @author Thiago H. de Paula Figueiredo
039     */
040    @Entity
041    @Table(name = "permissiongroup")
042    final public class PermissionGroup implements Comparable<Permission>, Serializable {
043    
044            private static final long serialVersionUID = 1L;
045    
046            /**
047             * Minimum name length.
048             */
049            public static final int MINIMUM_NAME_LENGTH = 2;
050    
051            /**
052             * Maximum name length.
053             */
054            public static final int MAXIMUM_NAME_LENGTH = 50;
055    
056            /**
057             * Name of the permission group that contains all users.
058             */
059            public static final String ALL_USERS_PERMISSION_GROUP_NAME = "All users";
060    
061            private Integer id;
062    
063            private String name;
064    
065            private List<Permission> permissions = new ArrayList<Permission>();
066    
067            /**
068             * No-arg constructor.
069             */
070            public PermissionGroup() {
071            }
072    
073            /**
074             * Constructor that receives a name.
075             * 
076             * @param name a {@link String}. It cannot be null.
077             * @throws IllegalArgumentException if <code>name</code> is null.
078             */
079            public PermissionGroup(String name) {
080    
081                    if (name == null) {
082                            throw new IllegalArgumentException("Parameter name cannot be null");
083                    }
084    
085                    this.name = name;
086    
087            }
088    
089            /**
090             * @see java.lang.Object#hashCode()
091             */
092            @Override
093            public int hashCode() {
094                    return name != null ? name.hashCode() : super.hashCode();
095            }
096    
097            /**
098             * @see java.lang.Object#equals(java.lang.Object)
099             */
100            @Override
101            public boolean equals(Object obj) {
102                    if (this == obj) {
103                            return true;
104                    }
105                    if (obj == null) {
106                            return false;
107                    }
108                    if (getClass() != obj.getClass()) {
109                            return false;
110                    }
111                    final PermissionGroup other = (PermissionGroup) obj;
112                    if (name == null) {
113                            if (other.name != null) {
114                                    return false;
115                            }
116                    }
117                    else if (!name.equals(other.name)) {
118                            return false;
119                    }
120                    return true;
121    
122            }
123    
124            /**
125             * @see java.lang.Comparable#compareTo(java.lang.Object)
126             */
127            public int compareTo(Permission o) {
128                    return getName().compareToIgnoreCase(o.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            /**
173             * Returns the value of the <code>permissions</code> property.
174             * 
175             * @return a {@link List<Permission>}.
176             */
177            @ManyToMany
178            @JoinTable(name = "permissiongroup_permission", joinColumns = @JoinColumn(name = "permissiongroup_id", nullable = false), inverseJoinColumns = @JoinColumn(name = "permission_id", nullable = false))
179            @OrderBy("name asc")
180            @Size(min = 1, max = 100)
181            public List<Permission> getPermissions() {
182                    return permissions;
183            }
184    
185            /**
186             * Adds a permission to this group.
187             * 
188             * @param permission a {@link Permission}.
189             */
190            public void add(Permission permission) {
191    
192                    if (permissions.contains(permission) == false) {
193                            permissions.add(permission);
194                    }
195    
196            }
197    
198            /**
199             * Removes a permission from this group.
200             * 
201             * @param permission a {@link Permission}.
202             */
203            public void remove(Permission permission) {
204                    permissions.remove(permission);
205            }
206    
207            /**
208             * Changes the value of the <code>permissions</code> property.
209             * 
210             * @param permissions a {@link List<Permission>}.
211             * @deprecated Use {@link #add(Permission)} and {@link #remove(Permission)} instead.
212             */
213            public void setPermissions(List<Permission> permissions) {
214                    this.permissions = permissions;
215            }
216    
217            /**
218             * Returns the <code>name</code> property.
219             * 
220             * @return a {@link String}.
221             */
222            @Override
223            public String toString() {
224                    return name;
225            }
226    
227    }