]> git.mxchange.org Git - juser-login-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
If the administrator updates only user data, contact is null
[juser-login-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.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.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 org.mxchange.jcontacts.contact.Contact;
40 import org.mxchange.jcontacts.contact.UserContact;
41 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
42 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
43
44 /**
45  * A generic user entity class
46  * <p>
47  * @author Roland Haeder<roland@mxchange.org>
48  */
49 @Entity (name = "users")
50 @Table (
51                 name = "users",
52                 indexes = {
53                         @Index (
54                                         name = "confirmation_key",
55                                         unique = true,
56                                         columnList = "user_confirm_key"
57                         ),
58                         @Index (
59                                         name = "user_name",
60                                         unique = true,
61                                         columnList = "user_name"
62                         )
63                 }
64 )
65 @NamedQueries (
66                 {
67                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
68                         @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"),
69                         @NamedQuery (name = "SearchUserName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:param)"),
70                         @NamedQuery (name = "SearchUserId", query = "SELECT u FROM users AS u WHERE u.userId = :id"),
71                         @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)"),
72                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
73                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),
74                         @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode = :mode ORDER BY u.userId ASC"),
75                         @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")
76                 }
77 )
78 public class LoginUser implements User {
79
80         /**
81          * Serial number
82          */
83         private static final long serialVersionUID = 4_328_454_581_751L;
84
85         /**
86          * Last locked reason
87          */
88         @Lob
89         @Column (name = "user_last_locked_reason")
90         private String lastLockedReason;
91
92         /**
93          * Account status
94          */
95         @Basic (optional = false)
96         @Column (name = "user_account_status", nullable = false)
97         @Enumerated (value = EnumType.STRING)
98         private UserAccountStatus userAccountStatus;
99
100         /**
101          * Confirmation key
102          */
103         @Column (name = "user_confirm_key", length = 50)
104         private String userConfirmKey;
105
106         /**
107          * Id number from "contacts" table
108          */
109         @JoinColumn (name = "user_contact_id", referencedColumnName = "contact_id", nullable = false, updatable = false, unique = true)
110         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
111         private Contact userContact;
112
113         /**
114          * "created" timestamp
115          */
116         @Basic (optional = false)
117         @Temporal (TemporalType.TIMESTAMP)
118         @Column (name = "user_created", nullable = false)
119         private Calendar userCreated;
120
121         /**
122          * Encrypted password
123          */
124         @Basic (optional = false)
125         @Column (name = "user_encrypted_password", nullable = false)
126         private String userEncryptedPassword;
127
128         /**
129          * User id
130          */
131         @Id
132         @Column (name = "user_id", nullable = false, length = 20, updatable = false)
133         @GeneratedValue (strategy = GenerationType.IDENTITY)
134         private Long userId;
135
136         /**
137          * Last "locked" timestamp
138          */
139         @Temporal (TemporalType.TIMESTAMP)
140         @Column (name = "user_last_locked_timestamp")
141         private Calendar userLastLocked;
142
143         /**
144          * User name
145          */
146         @Basic (optional = false)
147         @Column (name = "user_name", nullable = false, length = 20)
148         private String userName;
149
150         /**
151          * Profile mode of this user
152          */
153         @Basic (optional = false)
154         @Enumerated (EnumType.STRING)
155         @Column (name = "user_profile_mode", nullable = false)
156         private ProfileMode userProfileMode;
157
158         /**
159          * When this user has been updated
160          */
161         @Temporal (TemporalType.TIMESTAMP)
162         @Column (name = "user_updated")
163         private Calendar userUpdated;
164
165         /**
166          * Default constructor
167          */
168         public LoginUser () {
169                 // Default is invisible
170                 this.userProfileMode = ProfileMode.INVISIBLE;
171         }
172
173         @Override
174         public void copyAll (final User user) {
175                 // Is contact set?
176                 if (user.getUserContact() instanceof Contact) {
177                         // Copy also contact data
178                         this.getUserContact().copyAll(user.getUserContact());
179                 }
180
181                 // Copy other data
182                 this.setUserConfirmKey(user.getUserConfirmKey());
183                 this.setUserName(user.getUserName());
184                 this.setUserEncryptedPassword(user.getUserEncryptedPassword());
185                 this.setUserAccountStatus(user.getUserAccountStatus());
186                 this.setUserCreated(user.getUserCreated());
187                 this.setUserLastLocked(user.getUserLastLocked());
188         }
189
190         @Override
191         public boolean equals (final Object object) {
192                 if (null == object) {
193                         return false;
194                 }
195                 if (this.getClass() != object.getClass()) {
196                         return false;
197                 }
198
199                 final User other = (User) object;
200
201                 return ((Objects.equals(this.getUserName(), other.getUserName())) &&
202                                 (Objects.equals(this.getUserId(), other.getUserId())));
203         }
204
205         @Override
206         public int hashCode () {
207                 int hash = 5;
208                 hash = 83 * hash + Objects.hashCode(this.getUserName());
209                 hash = 83 * hash + Objects.hashCode(this.getUserId());
210                 return hash;
211         }
212
213         @Override
214         public String getLastLockedReason () {
215                 return this.lastLockedReason;
216         }
217
218         @Override
219         public void setLastLockedReason (final String lastLockedReason) {
220                 this.lastLockedReason = lastLockedReason;
221         }
222
223         @Override
224         public UserAccountStatus getUserAccountStatus () {
225                 return this.userAccountStatus;
226         }
227
228         @Override
229         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
230                 this.userAccountStatus = userAccountStatus;
231         }
232
233         @Override
234         public String getUserConfirmKey () {
235                 return this.userConfirmKey;
236         }
237
238         @Override
239         public void setUserConfirmKey (final String userConfirmKey) {
240                 this.userConfirmKey = userConfirmKey;
241         }
242
243         @Override
244         public Contact getUserContact () {
245                 return this.userContact;
246         }
247
248         @Override
249         public void setUserContact (final Contact userContact) {
250                 this.userContact = userContact;
251         }
252
253         @Override
254         public Calendar getUserCreated () {
255                 return this.userCreated;
256         }
257
258         @Override
259         public void setUserCreated (final Calendar userCreated) {
260                 this.userCreated = userCreated;
261         }
262
263         @Override
264         public String getUserEncryptedPassword () {
265                 return this.userEncryptedPassword;
266         }
267
268         @Override
269         public void setUserEncryptedPassword (final String userEncryptedPassword) {
270                 this.userEncryptedPassword = userEncryptedPassword;
271         }
272
273         @Override
274         public Long getUserId () {
275                 return this.userId;
276         }
277
278         @Override
279         public void setUserId (final Long userId) {
280                 this.userId = userId;
281         }
282
283         @Override
284         public Calendar getUserLastLocked () {
285                 return this.userLastLocked;
286         }
287
288         @Override
289         public void setUserLastLocked (final Calendar userLastLocked) {
290                 this.userLastLocked = userLastLocked;
291         }
292
293         @Override
294         public String getUserName () {
295                 return this.userName;
296         }
297
298         @Override
299         public void setUserName (final String userName) {
300                 this.userName = userName;
301         }
302
303         @Override
304         public ProfileMode getUserProfileMode () {
305                 return this.userProfileMode;
306         }
307
308         @Override
309         public void setUserProfileMode (final ProfileMode userProfileMode) {
310                 this.userProfileMode = userProfileMode;
311         }
312
313         @Override
314         public Calendar getUserUpdated () {
315                 return this.userUpdated;
316         }
317
318         @Override
319         public void setUserUpdated (final Calendar userUpdated) {
320                 this.userUpdated = userUpdated;
321         }
322
323 }