(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         primaryKey = aGroup.primaryKey;
81         version = aGroup.version;
82         name = aGroup.name;
83     }
84
85     /**
86      * Creates a new Group object.
87      */
88     protected Group() {
89         super();
90         name = null;
91     }
92
93     /**
94      * Gets the name of the group.
95      * 
96      * @return Group name.
97      */
98     public String getName() {
99         return name;
100     }
101
102     /**
103      * Sets the group name.
104      * 
105      * @param aName
106      *            Group name.
107      */
108     void setName(String aName) {
109         name = aName;
110     }
111
112     /*
113      * (non-Javadoc)
114      * 
115      * @see java.lang.Object#equals(java.lang.Object)
116      */
117     @Override
118     public boolean equals(Object aGroup) {
119         if (!(aGroup instanceof Group)) {
120             return false;
121         }
122
123         return name.equals(((Group) aGroup).name);
124     }
125
126     /*
127      * (non-Javadoc)
128      * 
129      * @see java.lang.Object#hashCode()
130      */
131     @Override
132     public int hashCode() {
133         return name.hashCode();
134     }
135
136     /*
137      * (non-Javadoc)
138      * 
139      * @see java.lang.Comparable#compareTo(T)
140      */
141     public int compareTo(Object aGroup) {
142         return name.compareTo(((Group) aGroup).name);
143     }
144     
145     public Long getPrimaryKey() {
146         return primaryKey;
147     }
148
149     /*
150      * (non-Javadoc)
151      * 
152      * @see java.lang.Object#toString()
153      */
154     @Override
155     public String toString() {
156         return "Group(pk = " + getPrimaryKey() + ", name=" + name + ")";
157     }
158 }