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