]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jusercore / model / user / JobsUserSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.persistence.Query;
26 import org.mxchange.jcontacts.model.contact.Contact;
27 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
28 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
29 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumbers;
30 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
31 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumbers;
32 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
33 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumbers;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
36 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
37 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
38 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
39 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
40 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
41
42 /**
43  * A user EJB
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Stateless (name = "user", description = "A bean handling the user data")
48 public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserSessionBeanRemote {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 542_145_347_916L;
54
55         /**
56          * Default constructor
57          */
58         public JobsUserSessionBean () {
59                 // Call super constructor
60                 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
61         }
62
63         @Override
64         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
65                 // Trace message
66                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
67
68                 // Parameter must be valid
69                 if (null == user) {
70                         // Abort here
71                         throw new NullPointerException("user is null"); //NOI18N
72                 } else if (user.getUserId() == null) {
73                         // Abort here
74                         throw new NullPointerException("user.userId is null"); //NOI18N
75                 } else if (user.getUserId() < 1) {
76                         // Invalid number
77                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
78                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
79                         // Account is already confirmed
80                         throw new UserStatusConfirmedException(user);
81                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
82                         // Account is already confirmed
83                         throw new UserStatusLockedException(user);
84                 } else if (user.getUserConfirmKey() == null) {
85                         // Throw NPE
86                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
87                 } else if (null == baseUrl) {
88                         // Throw it again
89                         throw new NullPointerException("baseUrl is null"); //NOI18N
90                 } else if (baseUrl.isEmpty()) {
91                         // Throw IAE
92                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
93                 }
94
95                 // Update user account
96                 final User managedUser = this.updateUserData(user);
97
98                 // Update user status and remove confirmation key
99                 managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
100                 managedUser.setUserConfirmKey(null);
101                 managedUser.setUserEntryUpdated(new Date());
102
103                 // Send out email
104                 this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
105
106                 // Trace message
107                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
108
109                 // Return updated instance
110                 return managedUser;
111         }
112
113         @Override
114         @SuppressWarnings ("unchecked")
115         public List<User> fetchAllUsers () {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
118
119                 // Get named query
120                 final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
121
122                 // Get result
123                 final List<User> users = query.getResultList();
124
125                 // Trace message
126                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
127
128                 // Return full list
129                 return users;
130         }
131
132         @Override
133         public boolean ifUserExists (final User user) {
134                 // Trace message
135                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
136
137                 // userId should not be null
138                 if (null == user) {
139                         // Abort here
140                         throw new NullPointerException("user is null"); //NOI18N
141                 } else if (user.getUserId() == null) {
142                         // Abort here
143                         throw new NullPointerException("user.userId is null"); //NOI18N
144                 } else if (user.getUserId() < 1) {
145                         // Invalid number
146                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
147                 }
148
149                 // Default is not found
150                 boolean isFound = false;
151
152                 // Fetch whole list
153                 for (final User currentUser : this.fetchAllUsers()) {
154                         // Is found?
155                         if (Objects.equals(user, currentUser)) {
156                                 // Yes, then set flag and exit iteration
157                                 isFound = true;
158                                 break;
159                         }
160                 }
161
162                 // Trace message
163                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
164
165                 // Return flag
166                 return isFound;
167         }
168
169         @Override
170         public boolean ifUserNameExists (final String userName) {
171                 // Trace message
172                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
173
174                 // userId should not be null
175                 if (null == userName) {
176                         // Abort here
177                         throw new NullPointerException("userName is null"); //NOI18N
178                 } else if (userName.isEmpty()) {
179                         // Abort here
180                         throw new NullPointerException("userName is empty"); //NOI18N
181                 }
182
183                 // Default is not registered
184                 boolean isRegistered = false;
185
186                 // Iterate over all records
187                 for (final User currentUser : this.fetchAllUsers()) {
188                         // Does the username match?
189                         if (userName.equals(currentUser.getUserName())) {
190                                 // Yes, then set flag and exit iteration
191                                 isRegistered = true;
192                                 break;
193                         }
194                 }
195
196                 // Trace message
197                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
198
199                 // Found it
200                 return true;
201         }
202
203         @Override
204         public boolean isEmailAddressRegistered (final User user) {
205                 // Trace message
206                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
207
208                 // user should not be null
209                 if (null == user) {
210                         // Abort here
211                         throw new NullPointerException("user is null"); //NOI18N
212                 }
213
214                 // Default is not registered
215                 boolean isRegistered = false;
216
217                 // Iterate over all records
218                 for (final User currentUser : this.fetchAllUsers()) {
219                         // Does the email address match?
220                         if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
221                                 // Yes, then set flag and exit iteration
222                                 isRegistered = true;
223                                 break;
224                         }
225                 }
226
227                 // Trace message
228                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
229
230                 // Found it
231                 return isRegistered;
232         }
233
234         @Override
235         public boolean isUserNameRegistered (final User user) {
236                 // Trace message
237                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
238
239                 // user should not be null
240                 if (null == user) {
241                         // Abort here
242                         throw new NullPointerException("user is null"); //NOI18N
243                 }
244
245                 // Ask other method
246                 final boolean isRegistered = this.ifUserNameExists(user.getUserName());
247
248                 // Trace message
249                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
250
251                 // Return flag
252                 return isRegistered;
253         }
254
255         @Override
256         public User updateUserData (final User user) throws UserNotFoundException {
257                 // Trace message
258                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
259
260                 // user should not be null
261                 if (null == user) {
262                         // Abort here
263                         throw new NullPointerException("user is null"); //NOI18N
264                 } else if (user.getUserId() == null) {
265                         // Throw NPE again
266                         throw new NullPointerException("user.userId is null"); //NOI18N
267                 } else if (user.getUserId() < 1) {
268                         // Not valid
269                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
270                 } else if (user.getUserAccountStatus() == null) {
271                         // Throw NPE again
272                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
273                 } else if (!this.ifUserExists(user)) {
274                         // User does not exist
275                         throw new UserNotFoundException(user.getUserId());
276                 }
277
278                 // Find the instance
279                 final User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
280
281                 // Should be found!
282                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
283
284                 // Copy all data
285                 Users.copyUserData(user, foundUser);
286
287                 // Merge user
288                 final User managedUser = this.getEntityManager().merge(foundUser);
289
290                 // Should be found!
291                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
292
293                 // Set as updated
294                 managedUser.setUserEntryUpdated(new Date());
295
296                 // Trace message
297                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
298
299                 // Return updated instance
300                 return managedUser;
301         }
302
303         @Override
304         public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
305                 // Trace message
306                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
307
308                 // user should not be null
309                 if (null == user) {
310                         // Throw NPE
311                         throw new NullPointerException("user is null"); //NOI18N
312                 } else if (user.getUserId() == null) {
313                         // Throw NPE again
314                         throw new NullPointerException("user.userId is null"); //NOI18N
315                 } else if (user.getUserId() < 1) {
316                         // Not valid number
317                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
318                 } else if (user.getUserName() == null) {
319                         // Throw NPE again
320                         throw new NullPointerException("user.userName is null"); //NOI18N
321                 } else if (user.getUserName().isEmpty()) {
322                         // Empty string
323                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
324                 } else if (user.getUserAccountStatus() == null) {
325                         // Throw NPE
326                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
327                 } else if (user.getUserContact() == null) {
328                         // Throw it again
329                         throw new NullPointerException("user.userContact is null"); //NOI18N
330                 } else if (user.getUserContact().getContactId() == null) {
331                         // .. and again
332                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
333                 } else if (user.getUserContact().getContactId() < 1) {
334                         // Invalid id
335                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
336                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
337                         // Throw NPE again
338                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
339                 } else if (!this.ifUserExists(user)) {
340                         // User does not exist
341                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
342                 } else if (null == baseUrl) {
343                         // Throw it again
344                         throw new NullPointerException("baseUrl is null"); //NOI18N
345                 } else if (baseUrl.isEmpty()) {
346                         // Invalid parameter
347                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
348                 }
349
350                 // Call other method
351                 final User managedUser = this.updateUserData(user);
352
353                 // Update user account
354                 managedUser.setUserEntryUpdated(new Date());
355
356                 // Create history entry
357                 final PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
358
359                 // Set created timestamp
360                 entry.setUserPasswordHistoryCreated(new Date());
361
362                 // Merge user to make sure it is not re-persisted
363                 final User mergedUser = this.getEntityManager().merge(managedUser);
364                 entry.setUserPasswordHistoryUser(mergedUser);
365
366                 // Persist it
367                 this.getEntityManager().persist(entry);
368
369                 // Send email to user
370                 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
371
372                 // Trace message
373                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
374
375                 // Return it
376                 return entry;
377         }
378
379         @Override
380         public User updateUserPersonalData (final User user) {
381                 // Trace message
382                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
383
384                 // user should not be null
385                 if (null == user) {
386                         // Abort here
387                         throw new NullPointerException("user is null"); //NOI18N
388                 } else if (user.getUserId() == null) {
389                         // Throw NPE again
390                         throw new NullPointerException("user.userId is null"); //NOI18N
391                 } else if (user.getUserId() < 1) {
392                         // Not valid
393                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
394                 } else if (user.getUserAccountStatus() == null) {
395                         // Throw NPE again
396                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
397                 } else if (!this.ifUserExists(user)) {
398                         // User does not exist
399                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
400                 }
401
402                 // Find the instance
403                 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
404
405                 // Should be found!
406                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
407
408                 // Copy all data
409                 Users.copyUserData(user, managedUser);
410
411                 // Set as updated
412                 managedUser.setUserEntryUpdated(new Date());
413
414                 // Update user data
415                 final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
416
417                 // Set it back in user
418                 managedUser.setUserContact(managedContact);
419
420                 // Should be found!
421                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", managedUser.getUserContact().getContactId()); //NOI18N
422
423                 // Get mobile instance
424                 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
425
426                 // Is there a  mobile instance set?
427                 if (mobileNumber instanceof DialableMobileNumber) {
428                         // Debug message
429                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
430
431                         // Then find it, too
432                         final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
433
434                         // Should be there
435                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
436
437                         // Copy all
438                         MobileNumbers.copyMobileNumberData(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
439
440                         // Then merge it, too
441                         final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
442
443                         // Should be there
444                         assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
445
446                         // Set it back
447                         managedContact.setContactMobileNumber(managedMobile);
448                 }
449
450                 // Get mobile instance
451                 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
452
453                 // Is there a  fax instance set?
454                 if (faxNumber instanceof DialableFaxNumber) {
455                         // Debug message
456                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
457
458                         // Then find it, too
459                         final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
460
461                         // Should be there
462                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
463
464                         // Copy all
465                         FaxNumbers.copyFaxNumberData(managedUser.getUserContact().getContactFaxNumber(), foundFax);
466
467                         // Then merge it, too
468                         final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
469
470                         // Should be there
471                         assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
472
473                         // Set it back
474                         managedContact.setContactFaxNumber(managedFax);
475                 }
476
477                 // Get mobile instance
478                 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
479
480                 // Is there a  fax instance set?
481                 if (landLineNumber instanceof DialableLandLineNumber) {
482                         // Debug message
483                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
484
485                         // Then find it, too
486                         final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
487
488                         // Should be there
489                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
490
491                         // Copy all
492                         LandLineNumbers.copyLandLineNumberData(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
493
494                         // Then merge it, too
495                         final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
496
497                         // Should be there
498                         assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
499
500                         // Set it back
501                         managedContact.setContactLandLineNumber(managedLandLine);
502                 }
503
504                 // Trace message
505                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
506
507                 // Return updated user instance
508                 return managedUser;
509         }
510
511 }