]> git.mxchange.org Git - juser-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
Also copy this flag from other to this user
[juser-core.git] / src / org / mxchange / jusercore / model / user / LoginUser.java
1 /*
2  * Copyright (C) 2016 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.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.contact.Contact;
41 import org.mxchange.jcontacts.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 Haeder<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 = "SearchUserById", query = "SELECT u FROM users AS u WHERE u.userId = :id"),
60                         @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)"),
61                         @NamedQuery (name = "SearchUserByConfirmKey", query = "SELECT u FROM users AS u WHERE u.userConfirmKey = :confirmKey"),
62                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
63                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),
64                         @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode = :mode ORDER BY u.userId ASC"),
65                         @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")
66                 }
67 )
68 @SuppressWarnings ("PersistenceUnitPresent")
69 public class LoginUser implements User {
70
71         /**
72          * Serial number
73          */
74         @Transient
75         private static final long serialVersionUID = 4_328_454_581_751L;
76
77         /**
78          * Account status
79          */
80         @Basic (optional = false)
81         @Column (name = "user_account_status", nullable = false)
82         @Enumerated (value = EnumType.STRING)
83         private UserAccountStatus userAccountStatus;
84
85         /**
86          * Confirmation key
87          */
88         @Column (name = "user_confirm_key", unique = true)
89         private String userConfirmKey;
90
91         /**
92          * Id number from "contacts" table
93          */
94         @JoinColumn (name = "user_contact_id", referencedColumnName = "contact_id", nullable = false, updatable = false, unique = true)
95         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
96         private Contact userContact;
97
98         /**
99          * "created" timestamp
100          */
101         @Basic (optional = false)
102         @Temporal (TemporalType.TIMESTAMP)
103         @Column (name = "user_created", nullable = false)
104         private Calendar userCreated;
105
106         /**
107          * Encrypted password
108          */
109         @Basic (optional = false)
110         @Column (name = "user_encrypted_password", nullable = false)
111         private String userEncryptedPassword;
112
113         /**
114          * User id
115          */
116         @Id
117         @Column (name = "user_id", nullable = false, length = 20, updatable = false)
118         @GeneratedValue (strategy = GenerationType.IDENTITY)
119         private Long userId;
120
121         /**
122          * Last "locked" timestamp
123          */
124         @Temporal (TemporalType.TIMESTAMP)
125         @Column (name = "user_last_locked_timestamp")
126         private Calendar userLastLocked;
127
128         /**
129          * Last locked reason
130          */
131         @Lob
132         @Column (name = "user_last_locked_reason")
133         private String userLastLockedReason;
134
135         /**
136          * User locale
137          */
138         @Column (name = "user_locale")
139         private Locale userLocale;
140
141         /**
142          * Whether the user must change password after login
143          */
144         @Column (name = "user_must_change_password")
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")
167         private Calendar userUpdated;
168
169         /**
170          * Default constructor
171          */
172         public LoginUser () {
173                 // Default is invisible
174                 this.userProfileMode = ProfileMode.INVISIBLE;
175         }
176
177         @Override
178         public void copyAll (final User user) {
179                 // Is contact set?
180                 if (user.getUserContact() instanceof Contact) {
181                         // Copy also contact data
182                         this.getUserContact().copyAll(user.getUserContact());
183                 }
184
185                 // Copy other data
186                 this.setUserConfirmKey(user.getUserConfirmKey());
187                 this.setUserName(user.getUserName());
188                 this.setUserEncryptedPassword(user.getUserEncryptedPassword());
189                 this.setUserAccountStatus(user.getUserAccountStatus());
190                 this.setUserCreated(user.getUserCreated());
191                 this.setUserLastLocked(user.getUserLastLocked());
192                 this.setUserLastLockedReason(user.getUserLastLockedReason());
193                 this.setUserUpdated(user.getUserUpdated());
194                 this.setUserProfileMode(user.getUserProfileMode());
195                 this.setUserLocale(user.getUserLocale());
196                 this.setUserMustChangePassword(user.getUserMustChangePassword());
197         }
198
199         @Override
200         public boolean equals (final Object object) {
201                 if (null == object) {
202                         return false;
203                 } else if (this.getClass() != object.getClass()) {
204                         return false;
205                 }
206
207                 final User other = (User) object;
208
209                 return ((Objects.equals(this.getUserName(), other.getUserName())) &&
210                                 (Objects.equals(this.getUserId(), other.getUserId())));
211         }
212
213         @Override
214         public UserAccountStatus getUserAccountStatus () {
215                 return this.userAccountStatus;
216         }
217
218         @Override
219         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
220                 this.userAccountStatus = userAccountStatus;
221         }
222
223         @Override
224         public String getUserConfirmKey () {
225                 return this.userConfirmKey;
226         }
227
228         @Override
229         public void setUserConfirmKey (final String userConfirmKey) {
230                 this.userConfirmKey = userConfirmKey;
231         }
232
233         @Override
234         public Contact getUserContact () {
235                 return this.userContact;
236         }
237
238         @Override
239         public void setUserContact (final Contact userContact) {
240                 this.userContact = userContact;
241         }
242
243         @Override
244         @SuppressWarnings ("ReturnOfDateField")
245         public Calendar getUserCreated () {
246                 return this.userCreated;
247         }
248
249         @Override
250         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
251         public void setUserCreated (final Calendar userCreated) {
252                 this.userCreated = userCreated;
253         }
254
255         @Override
256         public String getUserEncryptedPassword () {
257                 return this.userEncryptedPassword;
258         }
259
260         @Override
261         public void setUserEncryptedPassword (final String userEncryptedPassword) {
262                 this.userEncryptedPassword = userEncryptedPassword;
263         }
264
265         @Override
266         public Long getUserId () {
267                 return this.userId;
268         }
269
270         @Override
271         public void setUserId (final Long userId) {
272                 this.userId = userId;
273         }
274
275         @Override
276         @SuppressWarnings ("ReturnOfDateField")
277         public Calendar getUserLastLocked () {
278                 return this.userLastLocked;
279         }
280
281         @Override
282         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
283         public void setUserLastLocked (final Calendar userLastLocked) {
284                 this.userLastLocked = userLastLocked;
285         }
286
287         @Override
288         public String getUserLastLockedReason () {
289                 return this.userLastLockedReason;
290         }
291
292         @Override
293         public void setUserLastLockedReason (final String userLastLockedReason) {
294                 this.userLastLockedReason = userLastLockedReason;
295         }
296
297         @Override
298         public Locale getUserLocale () {
299                 return this.userLocale;
300         }
301
302         @Override
303         public void setUserLocale (final Locale userLocale) {
304                 this.userLocale = userLocale;
305         }
306
307         @Override
308         public Boolean getUserMustChangePassword () {
309                 return this.userMustChangePassword;
310         }
311
312         @Override
313         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
314                 this.userMustChangePassword = userMustChangePassword;
315         }
316
317         @Override
318         public String getUserName () {
319                 return this.userName;
320         }
321
322         @Override
323         public void setUserName (final String userName) {
324                 this.userName = userName;
325         }
326
327         @Override
328         public ProfileMode getUserProfileMode () {
329                 return this.userProfileMode;
330         }
331
332         @Override
333         public void setUserProfileMode (final ProfileMode userProfileMode) {
334                 this.userProfileMode = userProfileMode;
335         }
336
337         @Override
338         @SuppressWarnings ("ReturnOfDateField")
339         public Calendar getUserUpdated () {
340                 return this.userUpdated;
341         }
342
343         @Override
344         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
345         public void setUserUpdated (final Calendar userUpdated) {
346                 this.userUpdated = userUpdated;
347         }
348
349         @Override
350         public int hashCode () {
351                 int hash = 5;
352                 hash = 83 * hash + Objects.hashCode(this.getUserName());
353                 hash = 83 * hash + Objects.hashCode(this.getUserId());
354                 return hash;
355         }
356
357 }