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