(no commit message)
[utils] / security / impl / src / main / java / org / wamblee / usermgt / Group.java
1 /*
2  * Copyright 2005-2010 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.usermgt;
17
18 import java.io.Serializable;
19
20 import javax.persistence.Column;
21 import javax.persistence.Entity;
22 import javax.persistence.GeneratedValue;
23 import javax.persistence.GenerationType;
24 import javax.persistence.Id;
25 import javax.persistence.NamedQueries;
26 import javax.persistence.NamedQuery;
27 import javax.persistence.Table;
28 import javax.persistence.UniqueConstraint;
29 import javax.persistence.Version;
30
31 import org.wamblee.security.AbstractPersistent;
32
33 /**
34  * Represents a group.
35  * 
36  * @author Erik Brakkee
37  */
38 @Entity
39 @Table(name = "SEC_GROUP")
40 @NamedQueries( {
41     @NamedQuery(name = Group.QUERY_FIND_BY_NAME, query = "select g from Group g where g.name = :" +
42         Group.NAME_PARAM),
43     @NamedQuery(name = Group.QUERY_COUNT_GROUPS, query = "select count(g) from Group g"),
44     @NamedQuery(name = Group.QUERY_ALL_GROUPS, query = "select g from Group g") })
45 public class Group implements Serializable, Comparable {
46
47     public static final String QUERY_FIND_BY_NAME = "Group.findByName";
48     public static final String QUERY_COUNT_GROUPS = "Group.count";
49     public static final String QUERY_ALL_GROUPS = "Group.all";
50     public static final String NAME_PARAM = "name";
51
52     @Id
53     @GeneratedValue(strategy = GenerationType.AUTO)
54     private Long primaryKey;
55
56     @Version
57     private int version;
58
59     /**
60      * Group name.
61      */
62     @Column(nullable = false, unique = true)
63     private String name;
64
65     /**
66      * Constructs the group.
67      * 
68      * @param aName
69      */
70     Group(String aName) {
71         super();
72         name = aName;
73     }
74
75     /**
76      * Creates a new Group object.
77      * 
78      */
79     public Group(Group aGroup) {
80         name = aGroup.name;
81     }
82
83     /**
84      * Creates a new Group object.
85      */
86     protected Group() {
87         super();
88         name = null;
89     }
90
91     /**
92      * Gets the name of the group.
93      * 
94      * @return Group name.
95      */
96     public String getName() {
97         return name;
98     }
99
100     /**
101      * Sets the group name.
102      * 
103      * @param aName
104      *            Group name.
105      */
106     void setName(String aName) {
107         name = aName;
108     }
109
110     /*
111      * (non-Javadoc)
112      * 
113      * @see java.lang.Object#equals(java.lang.Object)
114      */
115     @Override
116     public boolean equals(Object aGroup) {
117         if (!(aGroup instanceof Group)) {
118             return false;
119         }
120
121         return name.equals(((Group) aGroup).name);
122     }
123
124     /*
125      * (non-Javadoc)
126      * 
127      * @see java.lang.Object#hashCode()
128      */
129     @Override
130     public int hashCode() {
131         return name.hashCode();
132     }
133
134     /*
135      * (non-Javadoc)
136      * 
137      * @see java.lang.Comparable#compareTo(T)
138      */
139     public int compareTo(Object aGroup) {
140         return name.compareTo(((Group) aGroup).name);
141     }
142     
143     public Long getPrimaryKey() {
144         return primaryKey;
145     }
146
147     /*
148      * (non-Javadoc)
149      * 
150      * @see java.lang.Object#toString()
151      */
152     @Override
153     public String toString() {
154         return "Group(pk = " + getPrimaryKey() + ", name=" + name + ")";
155     }
156 }