2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user;
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;
45 * @author Roland Häder<roland@mxchange.org>
47 @Stateless (name = "user", description = "A bean handling the user data")
48 public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserSessionBeanRemote {
53 private static final long serialVersionUID = 542_145_347_916L;
58 public JobsUserSessionBean () {
59 // Call super constructor
60 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
64 public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
66 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
68 // Parameter must be valid
71 throw new NullPointerException("user is null"); //NOI18N
72 } else if (user.getUserId() == null) {
74 throw new NullPointerException("user.userId is null"); //NOI18N
75 } else if (user.getUserId() < 1) {
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) {
86 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
87 } else if (null == baseUrl) {
89 throw new NullPointerException("baseUrl is null"); //NOI18N
90 } else if (baseUrl.isEmpty()) {
92 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
95 // Update user account
96 final User managedUser = this.updateUserData(user);
98 // Update user status and remove confirmation key
99 managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
100 managedUser.setUserConfirmKey(null);
101 managedUser.setUserEntryUpdated(new Date());
104 this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
107 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
109 // Return updated instance
114 @SuppressWarnings ("unchecked")
115 public List<User> fetchAllUsers () {
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
120 final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
123 final List<User> users = query.getResultList();
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
133 public boolean ifUserExists (final User user) {
135 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
137 // userId should not be null
140 throw new NullPointerException("user is null"); //NOI18N
141 } else if (user.getUserId() == null) {
143 throw new NullPointerException("user.userId is null"); //NOI18N
144 } else if (user.getUserId() < 1) {
146 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
149 // Default is not found
150 boolean isFound = false;
153 for (final User currentUser : this.fetchAllUsers()) {
155 * E.g. userAccountStatus can be fifferent because lockUserAccount
156 * is setting it BEFORE this method is invoked. So this is enough
157 * for us in this case. But otherwise e.g. when a new user is
158 * created with same data, then this method must return FALSE even
159 * when userId is currently NULL.
161 //* NOISY-DEBUG: */ this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserExists: currentUser={1},currentUser.userId={2}", this.getClass(), currentUser, currentUser.getUserId())); //NOI18N
162 if ((Objects.equals(user.getUserId(), currentUser.getUserId()) && user.getUserId() != null) || Objects.equals(user, currentUser)) {
163 // Yes, then set flag and exit iteration
170 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
177 public boolean ifUserNameExists (final String userName) {
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
181 // userId should not be null
182 if (null == userName) {
184 throw new NullPointerException("userName is null"); //NOI18N
185 } else if (userName.isEmpty()) {
187 throw new NullPointerException("userName is empty"); //NOI18N
190 // Default is not registered
191 boolean isRegistered = false;
193 // Iterate over all records
194 for (final User currentUser : this.fetchAllUsers()) {
195 // Does the username match?
196 if (userName.equals(currentUser.getUserName())) {
197 // Yes, then set flag and exit iteration
204 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
211 public boolean isEmailAddressRegistered (final User user) {
213 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
215 // user should not be null
218 throw new NullPointerException("user is null"); //NOI18N
221 // Default is not registered
222 boolean isRegistered = false;
224 // Iterate over all records
225 for (final User currentUser : this.fetchAllUsers()) {
226 // Does the email address match?
227 if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
228 // Yes, then set flag and exit iteration
235 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
242 public boolean isUserNameRegistered (final User user) {
244 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
246 // user should not be null
249 throw new NullPointerException("user is null"); //NOI18N
253 final boolean isRegistered = this.ifUserNameExists(user.getUserName());
256 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
263 public User updateUserData (final User user) throws UserNotFoundException {
265 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
267 // user should not be null
270 throw new NullPointerException("user is null"); //NOI18N
271 } else if (user.getUserId() == null) {
273 throw new NullPointerException("user.userId is null"); //NOI18N
274 } else if (user.getUserId() < 1) {
276 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
277 } else if (user.getUserAccountStatus() == null) {
279 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
280 } else if (!this.ifUserExists(user)) {
281 // User does not exist
282 throw new UserNotFoundException(user.getUserId());
286 final User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
289 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
292 Users.copyUserData(user, foundUser);
295 final User managedUser = this.getEntityManager().merge(foundUser);
298 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
301 managedUser.setUserEntryUpdated(new Date());
304 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
306 // Return updated instance
311 public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
313 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
315 // user should not be null
318 throw new NullPointerException("user is null"); //NOI18N
319 } else if (user.getUserId() == null) {
321 throw new NullPointerException("user.userId is null"); //NOI18N
322 } else if (user.getUserId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
325 } else if (user.getUserName() == null) {
327 throw new NullPointerException("user.userName is null"); //NOI18N
328 } else if (user.getUserName().isEmpty()) {
330 throw new IllegalArgumentException("user.userName is empty"); //NOI18N
331 } else if (user.getUserAccountStatus() == null) {
333 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
334 } else if (user.getUserContact() == null) {
336 throw new NullPointerException("user.userContact is null"); //NOI18N
337 } else if (user.getUserContact().getContactId() == null) {
339 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
340 } else if (user.getUserContact().getContactId() < 1) {
342 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
343 } else if (user.getUserContact().getContactPersonalTitle() == null) {
345 throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
346 } else if (!this.ifUserExists(user)) {
347 // User does not exist
348 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
349 } else if (null == baseUrl) {
351 throw new NullPointerException("baseUrl is null"); //NOI18N
352 } else if (baseUrl.isEmpty()) {
354 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
358 final User managedUser = this.updateUserData(user);
360 // Update user account
361 managedUser.setUserEntryUpdated(new Date());
363 // Create history entry
364 final PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
366 // Set created timestamp
367 entry.setUserPasswordHistoryCreated(new Date());
369 // Merge user to make sure it is not re-persisted
370 final User mergedUser = this.getEntityManager().merge(managedUser);
371 entry.setUserPasswordHistoryUser(mergedUser);
374 this.getEntityManager().persist(entry);
376 // Send email to user
377 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
380 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
387 public User updateUserPersonalData (final User user) {
389 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
391 // user should not be null
394 throw new NullPointerException("user is null"); //NOI18N
395 } else if (user.getUserId() == null) {
397 throw new NullPointerException("user.userId is null"); //NOI18N
398 } else if (user.getUserId() < 1) {
400 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
401 } else if (user.getUserAccountStatus() == null) {
403 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
404 } else if (!this.ifUserExists(user)) {
405 // User does not exist
406 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
410 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
413 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
416 Users.copyUserData(user, managedUser);
419 managedUser.setUserEntryUpdated(new Date());
422 final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
424 // Set it back in user
425 managedUser.setUserContact(managedContact);
428 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", managedUser.getUserContact().getContactId()); //NOI18N
430 // Get mobile instance
431 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
433 // Is there a mobile instance set?
434 if (mobileNumber instanceof DialableMobileNumber) {
436 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
439 final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
442 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
445 MobileNumbers.copyMobileNumberData(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
447 // Then merge it, too
448 final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
451 assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
454 managedContact.setContactMobileNumber(managedMobile);
457 // Get mobile instance
458 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
460 // Is there a fax instance set?
461 if (faxNumber instanceof DialableFaxNumber) {
463 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
466 final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
469 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
472 FaxNumbers.copyFaxNumberData(managedUser.getUserContact().getContactFaxNumber(), foundFax);
474 // Then merge it, too
475 final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
478 assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
481 managedContact.setContactFaxNumber(managedFax);
484 // Get mobile instance
485 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
487 // Is there a fax instance set?
488 if (landLineNumber instanceof DialableLandLineNumber) {
490 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
493 final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
496 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
499 LandLineNumbers.copyLandLineNumberData(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
501 // Then merge it, too
502 final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
505 assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
508 managedContact.setContactLandLineNumber(managedLandLine);
512 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
514 // Return updated user instance