]> git.mxchange.org Git - juser-login-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
added named query for user lookup by its id number
[juser-login-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.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import javax.persistence.Temporal;
37 import javax.persistence.TemporalType;
38 import org.mxchange.jcontacts.contact.Contact;
39 import org.mxchange.jcontacts.contact.UserContact;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41
42 /**
43  * A shop customer class.
44  * <p>
45  * @author Roland Haeder<roland@mxchange.org>
46  */
47 @Entity (name = "users")
48 @Table (
49                 name = "users",
50                 indexes = {
51                         @Index (
52                                         name = "confirmation_key",
53                                         unique = true,
54                                         columnList = "user_confirm_key"
55                         ),
56                         @Index (
57                                         name = "user_name",
58                                         unique = true,
59                                         columnList = "user_name"
60                         )
61                 }
62 )
63 @NamedQueries (
64                 {
65                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
66                         @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"),
67                         @NamedQuery (name = "SearchUserName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:param)"),
68                         @NamedQuery (name = "SearchUserId", query = "SELECT u FROM users AS u WHERE u.userId = :id"),
69                         @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)"),
70                         @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :param AND u.userPublicProfile = TRUE")
71                 }
72 )
73 public class LoginUser implements User {
74
75         /**
76          * Serial number
77          */
78         private static final long serialVersionUID = 4_328_454_581_751L;
79
80         /**
81          * Account status
82          */
83         @Basic (optional = false)
84         @Column (name = "user_account_status", nullable = false)
85         @Enumerated (value = EnumType.STRING)
86         private UserAccountStatus userAccountStatus;
87
88         /**
89          * Confirmation key
90          */
91         @Column (name = "user_confirm_key", length = 50)
92         private String userConfirmKey;
93
94         /**
95          * Id number from "contacts" table
96          */
97         @JoinColumn (name = "user_contact_id", nullable = false, updatable = false)
98         @OneToOne (targetEntity = UserContact.class, optional = false, cascade = CascadeType.ALL)
99         private Contact userContact;
100
101         /**
102          * "created" timestamp
103          */
104         @Basic (optional = false)
105         @Temporal (TemporalType.TIMESTAMP)
106         @Column (name = "user_created", nullable = false)
107         private Calendar userCreated;
108
109         /**
110          * Encrypted password
111          */
112         @Column (name = "user_encrypted_password", nullable = false)
113         private String userEncryptedPassword;
114
115         /**
116          * User id
117          */
118         @Id
119         @Column (name = "user_id", nullable = false, length = 20, updatable = false)
120         @GeneratedValue (strategy = GenerationType.IDENTITY)
121         private Long userId;
122
123         /**
124          * "locked" timestamp
125          */
126         @Temporal (TemporalType.TIMESTAMP)
127         @Column (name = "user_locked")
128         private Calendar userLocked;
129
130         /**
131          * User name
132          */
133         @Column (name = "user_name", nullable = false, length = 20)
134         private String userName;
135
136         /**
137          * Whether the user wants a public profile. This means that it can be viewed
138          * from the Internet by everyone (only profile, no personal data, this needs
139          * explicit agreement) and it can be found for sharing addresses with.
140          */
141         @Basic (optional = false)
142         @Column (name = "user_public_profile_flag", nullable = false)
143         private Boolean userPublicProfile;
144
145         /**
146          * Default constructor
147          */
148         public LoginUser () {
149                 // Default is not public
150                 this.userPublicProfile = Boolean.FALSE;
151         }
152
153         @Override
154         public void copyAll (final User user) {
155                 // Copy also contact data
156                 this.getUserContact().copyAll(user.getUserContact());
157
158                 // Copy other data
159                 this.setUserConfirmKey(user.getUserConfirmKey());
160                 this.setUserName(user.getUserName());
161                 this.setUserEncryptedPassword(user.getUserEncryptedPassword());
162                 this.setUserAccountStatus(user.getUserAccountStatus());
163                 this.setUserCreated(user.getUserCreated());
164                 this.setUserLocked(user.getUserLocked());
165         }
166
167         @Override
168         public boolean equals (final Object object) {
169                 if (object == null) {
170                         return false;
171                 }
172                 if (getClass() != object.getClass()) {
173                         return false;
174                 }
175
176                 final User other = (User) object;
177
178                 return ((Objects.equals(this.getUserName(), other.getUserName()))
179                                 && (Objects.equals(this.getUserId(), other.getUserId())));
180         }
181
182         @Override
183         public UserAccountStatus getUserAccountStatus () {
184                 return this.userAccountStatus;
185         }
186
187         @Override
188         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
189                 this.userAccountStatus = userAccountStatus;
190         }
191
192         @Override
193         public String getUserConfirmKey () {
194                 return this.userConfirmKey;
195         }
196
197         @Override
198         public void setUserConfirmKey (final String customerConfirmKey) {
199                 this.userConfirmKey = customerConfirmKey;
200         }
201
202         @Override
203         public Contact getUserContact () {
204                 return this.userContact;
205         }
206
207         @Override
208         public void setUserContact (final Contact userContact) {
209                 this.userContact = userContact;
210         }
211
212         @Override
213         public Calendar getUserCreated () {
214                 return this.userCreated;
215         }
216
217         @Override
218         public void setUserCreated (final Calendar userCreated) {
219                 this.userCreated = userCreated;
220         }
221
222         @Override
223         public String getUserEncryptedPassword () {
224                 return this.userEncryptedPassword;
225         }
226
227         @Override
228         public void setUserEncryptedPassword (final String userEncryptedPassword) {
229                 this.userEncryptedPassword = userEncryptedPassword;
230         }
231
232         @Override
233         public Long getUserId () {
234                 return this.userId;
235         }
236
237         @Override
238         public void setUserId (final Long userId) {
239                 this.userId = userId;
240         }
241
242         @Override
243         public Calendar getUserLocked () {
244                 return this.userLocked;
245         }
246
247         @Override
248         public void setUserLocked (final Calendar userLocked) {
249                 this.userLocked = userLocked;
250         }
251
252         @Override
253         public String getUserName () {
254                 return this.userName;
255         }
256
257         @Override
258         public void setUserName (final String userName) {
259                 this.userName = userName;
260         }
261
262         @Override
263         public Boolean getUserPublicProfile () {
264                 return this.userPublicProfile;
265         }
266
267         @Override
268         public void setUserPublicProfile (final Boolean userPublicProfile) {
269                 this.userPublicProfile = userPublicProfile;
270         }
271
272         @Override
273         public int hashCode () {
274                 int hash = 5;
275                 hash = 83 * hash + Objects.hashCode(this.getUserName());
276                 hash = 83 * hash + Objects.hashCode(this.getUserId());
277                 return hash;
278         }
279 }