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