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