]> git.mxchange.org Git - juser-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
Opps, forgot to update the named query, too. You need to update query.setParameter...
[juser-core.git] / src / org / mxchange / jusercore / model / user / LoginUser.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.util.Calendar;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.EnumType;
26 import javax.persistence.Enumerated;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.Index;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import javax.persistence.Temporal;
37 import javax.persistence.TemporalType;
38 import org.mxchange.jcontacts.contact.Contact;
39 import org.mxchange.jcontacts.contact.UserContact;
40 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42
43 /**
44  * A generic user entity class
45  * <p>
46  * @author Roland Haeder<roland@mxchange.org>
47  */
48 @Entity (name = "users")
49 @Table (
50                 name = "users",
51                 indexes = {
52                         @Index (
53                                         name = "confirmation_key",
54                                         unique = true,
55                                         columnList = "user_confirm_key"
56                         ),
57                         @Index (
58                                         name = "user_name",
59                                         unique = true,
60                                         columnList = "user_name"
61                         )
62                 }
63 )
64 @NamedQueries (
65                 {
66                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
67                         @NamedQuery (name = "AllEmailAddresses", query = "SELECT DISTINCT c.contactEmailAddress FROM contacts AS c INNER JOIN users AS u ON u.userContact = c ORDER BY c.contactId ASC"),
68                         @NamedQuery (name = "SearchUserName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:param)"),
69                         @NamedQuery (name = "SearchUserId", query = "SELECT u FROM users AS u WHERE u.userId = :id"),
70                         @NamedQuery (name = "SearchEmailAddress", query = "SELECT u FROM users AS u INNER JOIN contacts AS c ON u.userContact = c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:param)"),
71                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
72                         @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode = :mode")
73                 }
74 )
75 public class LoginUser implements User {
76
77         /**
78          * Serial number
79          */
80         private static final long serialVersionUID = 4_328_454_581_751L;
81
82         /**
83          * Account status
84          */
85         @Basic (optional = false)
86         @Column (name = "user_account_status", nullable = false)
87         @Enumerated (value = EnumType.STRING)
88         private UserAccountStatus userAccountStatus;
89
90         /**
91          * Confirmation key
92          */
93         @Column (name = "user_confirm_key", length = 50)
94         private String userConfirmKey;
95
96         /**
97          * Id number from "contacts" table
98          */
99         @JoinColumn (name = "user_contact_id", nullable = false, updatable = false)
100         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
101         private Contact userContact;
102
103         /**
104          * "created" timestamp
105          */
106         @Basic (optional = false)
107         @Temporal (TemporalType.TIMESTAMP)
108         @Column (name = "user_created", nullable = false)
109         private Calendar userCreated;
110
111         /**
112          * Encrypted password
113          */
114         @Column (name = "user_encrypted_password", nullable = false)
115         private String userEncryptedPassword;
116
117         /**
118          * User id
119          */
120         @Id
121         @Column (name = "user_id", nullable = false, length = 20, updatable = false)
122         @GeneratedValue (strategy = GenerationType.IDENTITY)
123         private Long userId;
124
125         /**
126          * "locked" timestamp
127          */
128         @Temporal (TemporalType.TIMESTAMP)
129         @Column (name = "user_locked")
130         private Calendar userLocked;
131
132         /**
133          * User name
134          */
135         @Column (name = "user_name", nullable = false, length = 20)
136         private String userName;
137
138         /**
139          * Profile mode of this user
140          */
141         @Basic (optional = false)
142         @Enumerated (EnumType.STRING)
143         @Column (name = "user_profile_mode", nullable = false)
144         private ProfileMode userProfileMode;
145
146         /**
147          * Default constructor
148          */
149         public LoginUser () {
150                 // Default is invisible
151                 this.userProfileMode = ProfileMode.INVISIBLE;
152         }
153
154         @Override
155         public void copyAll (final User user) {
156                 // Copy also contact data
157                 this.getUserContact().copyAll(user.getUserContact());
158
159                 // Copy other data
160                 this.setUserConfirmKey(user.getUserConfirmKey());
161                 this.setUserName(user.getUserName());
162                 this.setUserEncryptedPassword(user.getUserEncryptedPassword());
163                 this.setUserAccountStatus(user.getUserAccountStatus());
164                 this.setUserCreated(user.getUserCreated());
165                 this.setUserLocked(user.getUserLocked());
166         }
167
168         @Override
169         public boolean equals (final Object object) {
170                 if (object == null) {
171                         return false;
172                 }
173                 if (getClass() != object.getClass()) {
174                         return false;
175                 }
176
177                 final User other = (User) object;
178
179                 return ((Objects.equals(this.getUserName(), other.getUserName())) &&
180                                 (Objects.equals(this.getUserId(), other.getUserId())));
181         }
182
183         @Override
184         public UserAccountStatus getUserAccountStatus () {
185                 return this.userAccountStatus;
186         }
187
188         @Override
189         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
190                 this.userAccountStatus = userAccountStatus;
191         }
192
193         @Override
194         public String getUserConfirmKey () {
195                 return this.userConfirmKey;
196         }
197
198         @Override
199         public void setUserConfirmKey (final String userConfirmKey) {
200                 this.userConfirmKey = userConfirmKey;
201         }
202
203         @Override
204         public Contact getUserContact () {
205                 return this.userContact;
206         }
207
208         @Override
209         public void setUserContact (final Contact userContact) {
210                 this.userContact = userContact;
211         }
212
213         @Override
214         public Calendar getUserCreated () {
215                 return this.userCreated;
216         }
217
218         @Override
219         public void setUserCreated (final Calendar userCreated) {
220                 this.userCreated = userCreated;
221         }
222
223         @Override
224         public String getUserEncryptedPassword () {
225                 return this.userEncryptedPassword;
226         }
227
228         @Override
229         public void setUserEncryptedPassword (final String userEncryptedPassword) {
230                 this.userEncryptedPassword = userEncryptedPassword;
231         }
232
233         @Override
234         public Long getUserId () {
235                 return this.userId;
236         }
237
238         @Override
239         public void setUserId (final Long userId) {
240                 this.userId = userId;
241         }
242
243         @Override
244         public Calendar getUserLocked () {
245                 return this.userLocked;
246         }
247
248         @Override
249         public void setUserLocked (final Calendar userLocked) {
250                 this.userLocked = userLocked;
251         }
252
253         @Override
254         public String getUserName () {
255                 return this.userName;
256         }
257
258         @Override
259         public void setUserName (final String userName) {
260                 this.userName = userName;
261         }
262
263         @Override
264         public ProfileMode getUserProfileMode () {
265                 return this.userProfileMode;
266         }
267
268         @Override
269         public void setUserProfileMode (final ProfileMode userProfileMode) {
270                 this.userProfileMode = userProfileMode;
271         }
272
273         @Override
274         public int hashCode () {
275                 int hash = 5;
276                 hash = 83 * hash + Objects.hashCode(this.getUserName());
277                 hash = 83 * hash + Objects.hashCode(this.getUserId());
278                 return hash;
279         }
280 }