]> git.mxchange.org Git - juser-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
Continued:
[juser-core.git] / src / org / mxchange / jusercore / model / user / LoginUser.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.Date;
20 import java.util.Locale;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.mxchange.jcontacts.model.contact.Contact;
41 import org.mxchange.jcontacts.model.contact.UserContact;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44
45 /**
46  * A generic user entity class
47  * <p>
48  * @author Roland Häder<roland@mxchange.org>
49  */
50 @Entity (name = "users")
51 @Table (
52                 name = "users"
53 )
54 @NamedQueries (
55                 {
56                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
57                         @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"),
58                         @NamedQuery (name = "SearchUserByName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:userName)"),
59                         @NamedQuery (name = "SearchUserByEmailAddress", query = "SELECT u FROM users AS u INNER JOIN contacts AS c ON u.userContact = c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:emailAddress)"),
60                         @NamedQuery (name = "SearchUserByConfirmKey", query = "SELECT u FROM users AS u WHERE u.userConfirmKey = :confirmKey"),
61                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
62                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),
63                         @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode = :mode ORDER BY u.userId ASC"),
64                         @NamedQuery (name = "AllMemberPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode IN (:public, :members) ORDER BY u.userId ASC")
65                 }
66 )
67 @SuppressWarnings ("PersistenceUnitPresent")
68 public class LoginUser implements User {
69
70         /**
71          * Serial number
72          */
73         @Transient
74         private static final long serialVersionUID = 4_328_454_581_751L;
75
76         /**
77          * Account status
78          */
79         @Basic (optional = false)
80         @Column (name = "user_account_status", nullable = false)
81         @Enumerated (value = EnumType.STRING)
82         private UserAccountStatus userAccountStatus;
83
84         /**
85          * Confirmation key
86          */
87         @Column (name = "user_confirm_key", unique = true)
88         private String userConfirmKey;
89
90         /**
91          * Id number from "contacts" table
92          */
93         @JoinColumn (name = "user_contact_id", referencedColumnName = "contact_id", nullable = false, updatable = false, unique = true)
94         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
95         private Contact userContact;
96
97         /**
98          * "created" timestamp
99          */
100         @Basic (optional = false)
101         @Temporal (TemporalType.TIMESTAMP)
102         @Column (name = "user_created", nullable = false, updatable = false)
103         private Date userCreated;
104
105         /**
106          * Encrypted password
107          */
108         @Basic (optional = false)
109         @Column (name = "user_encrypted_password", nullable = false)
110         private String userEncryptedPassword;
111
112         /**
113          * User id
114          */
115         @Id
116         @Column (name = "user_id", nullable = false, updatable = false)
117         @GeneratedValue (strategy = GenerationType.IDENTITY)
118         private Long userId;
119
120         /**
121          * Last "locked" timestamp
122          */
123         @Temporal (TemporalType.TIMESTAMP)
124         @Column (name = "user_last_locked_timestamp")
125         private Date userLastLocked;
126
127         /**
128          * Last locked reason
129          */
130         @Lob
131         @Column (name = "user_last_locked_reason")
132         private String userLastLockedReason;
133
134         /**
135          * User locale
136          */
137         @Column (name = "user_locale")
138         private Locale userLocale;
139
140         /**
141          * Whether the user must change password after login
142          */
143         @Basic (optional = false)
144         @Column (name = "user_must_change_password", nullable = false)
145         private Boolean userMustChangePassword;
146
147         /**
148          * User name
149          */
150         @Basic (optional = false)
151         @Column (name = "user_name", nullable = false, length = 30, unique = true)
152         private String userName;
153
154         /**
155          * Profile mode of this user
156          */
157         @Basic (optional = false)
158         @Enumerated (EnumType.STRING)
159         @Column (name = "user_profile_mode", nullable = false)
160         private ProfileMode userProfileMode;
161
162         /**
163          * When this user has been updated
164          */
165         @Temporal (TemporalType.TIMESTAMP)
166         @Column (name = "user_updated", insertable = false)
167         private Date userUpdated;
168
169         /**
170          * Default constructor
171          */
172         public LoginUser () {
173                 // Default is invisible
174                 this.userProfileMode = ProfileMode.INVISIBLE;
175         }
176
177         /**
178          * Constructor with all required fields
179          * <p>
180          * @param userName               Username
181          * @param userProfileMode        Profile mode
182          * @param userMustChangePassword Whether user must change password
183          * @param userEncryptedPassword  Encrypted password
184          * @param userAccountStatus      Account status
185          * @param userContact User's contact data
186          */
187         public LoginUser (final String userName, final ProfileMode userProfileMode, final Boolean userMustChangePassword, final String userEncryptedPassword, final UserAccountStatus userAccountStatus, final Contact userContact) {
188                 // Call other constructor first
189                 this();
190
191                 // Validate all parameter
192                 if (null == userName) {
193                         // Throw NPE
194                         throw new NullPointerException("userName is null"); //NOI18N
195                 } else if (userName.isEmpty()) {
196                         // Throw IAE
197                         throw new IllegalArgumentException("userName is empty"); //NOI18N
198                 } else if (null == userProfileMode) {
199                         // Throw NPE
200                         throw new NullPointerException("userProfileMode is null"); //NOI18N
201                 } else if (null == userMustChangePassword) {
202                         // Throw it again
203                         throw new NullPointerException("userMustChangePassword is null"); //NOI18N
204                 } else if (null == userEncryptedPassword) {
205                         // Throw it again
206                         throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
207                 } else if (userEncryptedPassword.isEmpty()) {
208                         // Throw IAE
209                         throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
210                 } else if (null == userAccountStatus) {
211                         // Throw NPE
212                         throw new NullPointerException("userAccountStatus is null"); //NOI18N
213                 } else if (null == userContact) {
214                         // Throw it again
215                         throw new NullPointerException("userContact is null"); //NOI18N
216                 }
217
218                 // Set all fields
219                 this.userAccountStatus = userAccountStatus;
220                 this.userEncryptedPassword = userEncryptedPassword;
221                 this.userMustChangePassword = userMustChangePassword;
222                 this.userName = userName;
223                 this.userProfileMode = userProfileMode;
224                 this.userContact = userContact;
225         }
226
227         @Override
228         public boolean equals (final Object object) {
229                 if (null == object) {
230                         return false;
231                 } else if (this.getClass() != object.getClass()) {
232                         return false;
233                 }
234
235                 final User other = (User) object;
236
237                 if (!Objects.equals(this.getUserName(), other.getUserName())) {
238                         return false;
239                 } else if (!Objects.equals(this.getUserId(), other.getUserId())) {
240                         return false;
241                 }
242
243                 return true;
244         }
245
246         @Override
247         public UserAccountStatus getUserAccountStatus () {
248                 return this.userAccountStatus;
249         }
250
251         @Override
252         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
253                 this.userAccountStatus = userAccountStatus;
254         }
255
256         @Override
257         public String getUserConfirmKey () {
258                 return this.userConfirmKey;
259         }
260
261         @Override
262         public void setUserConfirmKey (final String userConfirmKey) {
263                 this.userConfirmKey = userConfirmKey;
264         }
265
266         @Override
267         public Contact getUserContact () {
268                 return this.userContact;
269         }
270
271         @Override
272         public void setUserContact (final Contact userContact) {
273                 this.userContact = userContact;
274         }
275
276         @Override
277         @SuppressWarnings ("ReturnOfDateField")
278         public Date getUserCreated () {
279                 return this.userCreated;
280         }
281
282         @Override
283         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
284         public void setUserCreated (final Date userCreated) {
285                 this.userCreated = userCreated;
286         }
287
288         @Override
289         public String getUserEncryptedPassword () {
290                 return this.userEncryptedPassword;
291         }
292
293         @Override
294         public void setUserEncryptedPassword (final String userEncryptedPassword) {
295                 this.userEncryptedPassword = userEncryptedPassword;
296         }
297
298         @Override
299         public Long getUserId () {
300                 return this.userId;
301         }
302
303         @Override
304         public void setUserId (final Long userId) {
305                 this.userId = userId;
306         }
307
308         @Override
309         @SuppressWarnings ("ReturnOfDateField")
310         public Date getUserLastLocked () {
311                 return this.userLastLocked;
312         }
313
314         @Override
315         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
316         public void setUserLastLocked (final Date userLastLocked) {
317                 this.userLastLocked = userLastLocked;
318         }
319
320         @Override
321         public String getUserLastLockedReason () {
322                 return this.userLastLockedReason;
323         }
324
325         @Override
326         public void setUserLastLockedReason (final String userLastLockedReason) {
327                 this.userLastLockedReason = userLastLockedReason;
328         }
329
330         @Override
331         public Locale getUserLocale () {
332                 return this.userLocale;
333         }
334
335         @Override
336         public void setUserLocale (final Locale userLocale) {
337                 this.userLocale = userLocale;
338         }
339
340         @Override
341         public Boolean getUserMustChangePassword () {
342                 return this.userMustChangePassword;
343         }
344
345         @Override
346         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
347                 this.userMustChangePassword = userMustChangePassword;
348         }
349
350         @Override
351         public String getUserName () {
352                 return this.userName;
353         }
354
355         @Override
356         public void setUserName (final String userName) {
357                 this.userName = userName;
358         }
359
360         @Override
361         public ProfileMode getUserProfileMode () {
362                 return this.userProfileMode;
363         }
364
365         @Override
366         public void setUserProfileMode (final ProfileMode userProfileMode) {
367                 this.userProfileMode = userProfileMode;
368         }
369
370         @Override
371         @SuppressWarnings ("ReturnOfDateField")
372         public Date getUserUpdated () {
373                 return this.userUpdated;
374         }
375
376         @Override
377         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
378         public void setUserUpdated (final Date userUpdated) {
379                 this.userUpdated = userUpdated;
380         }
381
382         @Override
383         public int hashCode () {
384                 int hash = 5;
385                 hash = 83 * hash + Objects.hashCode(this.getUserName());
386                 hash = 83 * hash + Objects.hashCode(this.getUserId());
387                 return hash;
388         }
389
390 }