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.landline.DialableLandLineNumber;
30 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
31 import org.mxchange.jphone.model.utils.FaxNumberUtils;
32 import org.mxchange.jphone.model.utils.LandLineNumberUtils;
33 import org.mxchange.jphone.model.utils.MobileNumberUtils;
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 import org.mxchange.jusercore.model.utils.UserUtils;
46 * @author Roland Häder<roland@mxchange.org>
48 @Stateless (name = "user", description = "A bean handling the user data")
49 public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserSessionBeanRemote {
54 private static final long serialVersionUID = 542_145_350_001L;
59 public JobsUserSessionBean () {
60 // Call super constructor
61 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
65 public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
67 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
69 // Parameter must be valid
72 throw new NullPointerException("user is null"); //NOI18N
73 } else if (user.getUserId() == null) {
75 throw new NullPointerException("user.userId is null"); //NOI18N
76 } else if (user.getUserId() < 1) {
78 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
79 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
80 // Account is already confirmed
81 throw new UserStatusConfirmedException(user);
82 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
83 // Account is already confirmed
84 throw new UserStatusLockedException(user);
85 } else if (user.getUserConfirmKey() == null) {
87 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
88 } else if (null == baseUrl) {
90 throw new NullPointerException("baseUrl is null"); //NOI18N
91 } else if (baseUrl.isEmpty()) {
93 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
96 // Update user account
97 final User managedUser = this.updateUserData(user);
99 // Update user status and remove confirmation key
100 managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
101 managedUser.setUserConfirmKey(null);
102 managedUser.setUserEntryUpdated(new Date());
105 this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
110 // Return updated instance
115 @SuppressWarnings ("unchecked")
116 public List<User> fetchAllUsers () {
118 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
121 final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
124 final List<User> users = query.getResultList();
127 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
134 public boolean ifUserExists (final User user) {
136 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
138 // userId should not be null
141 throw new NullPointerException("user is null"); //NOI18N
142 } else if (user.getUserId() == null) {
144 throw new NullPointerException("user.userId is null"); //NOI18N
145 } else if (user.getUserId() < 1) {
147 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
150 // Default is not found
151 boolean isFound = false;
154 for (final User currentUser : this.fetchAllUsers()) {
156 * E.g. userAccountStatus can be fifferent because lockUserAccount
157 * is setting it BEFORE this method is invoked. So this is enough
158 * for us in this case. But otherwise e.g. when a new user is
159 * created with same data, then this method must return FALSE even
160 * when userId is currently NULL.
162 //* NOISY-DEBUG: */ this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserExists: currentUser={1},currentUser.userId={2}", this.getClass(), currentUser, currentUser.getUserId())); //NOI18N
163 if ((Objects.equals(user.getUserId(), currentUser.getUserId()) && user.getUserId() != null) || Objects.equals(user, currentUser)) {
164 // Yes, then set flag and exit iteration
171 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
178 public boolean ifUserNameExists (final String userName) {
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
182 // userId should not be null
183 if (null == userName) {
185 throw new NullPointerException("userName is null"); //NOI18N
186 } else if (userName.isEmpty()) {
188 throw new NullPointerException("userName is empty"); //NOI18N
191 // Default is not registered
192 boolean isRegistered = false;
194 // Iterate over all records
195 for (final User currentUser : this.fetchAllUsers()) {
196 // Does the username match?
197 if (userName.equals(currentUser.getUserName())) {
198 // Yes, then set flag and exit iteration
205 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
212 public boolean isEmailAddressRegistered (final User user) {
214 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
216 // user should not be null
219 throw new NullPointerException("user is null"); //NOI18N
222 // Default is not registered
223 boolean isRegistered = false;
225 // Iterate over all records
226 for (final User currentUser : this.fetchAllUsers()) {
227 // Does the email address match?
228 if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
229 // Yes, then set flag and exit iteration
236 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
243 public boolean isUserNameRegistered (final User user) {
245 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
247 // user should not be null
250 throw new NullPointerException("user is null"); //NOI18N
254 final boolean isRegistered = this.ifUserNameExists(user.getUserName());
257 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
264 public User updateUserData (final User user) throws UserNotFoundException {
266 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
268 // user should not be null
271 throw new NullPointerException("user is null"); //NOI18N
272 } else if (user.getUserId() == null) {
274 throw new NullPointerException("user.userId is null"); //NOI18N
275 } else if (user.getUserId() < 1) {
277 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
278 } else if (user.getUserAccountStatus() == null) {
280 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
281 } else if (!this.ifUserExists(user)) {
282 // User does not exist
283 throw new UserNotFoundException(user.getUserId());
287 final User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
290 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
293 UserUtils.copyUserData(user, foundUser);
296 final User managedUser = this.getEntityManager().merge(foundUser);
299 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
302 managedUser.setUserEntryUpdated(new Date());
305 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
307 // Return updated instance
312 public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
314 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
316 // user should not be null
319 throw new NullPointerException("user is null"); //NOI18N
320 } else if (user.getUserId() == null) {
322 throw new NullPointerException("user.userId is null"); //NOI18N
323 } else if (user.getUserId() < 1) {
325 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
326 } else if (user.getUserName() == null) {
328 throw new NullPointerException("user.userName is null"); //NOI18N
329 } else if (user.getUserName().isEmpty()) {
331 throw new IllegalArgumentException("user.userName is empty"); //NOI18N
332 } else if (user.getUserAccountStatus() == null) {
334 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
335 } else if (user.getUserContact() == null) {
337 throw new NullPointerException("user.userContact is null"); //NOI18N
338 } else if (user.getUserContact().getContactId() == null) {
340 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
341 } else if (user.getUserContact().getContactId() < 1) {
343 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
344 } else if (user.getUserContact().getContactPersonalTitle() == null) {
346 throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
347 } else if (!this.ifUserExists(user)) {
348 // User does not exist
349 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
350 } else if (null == baseUrl) {
352 throw new NullPointerException("baseUrl is null"); //NOI18N
353 } else if (baseUrl.isEmpty()) {
355 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
359 final User managedUser = this.updateUserData(user);
361 // Update user account
362 managedUser.setUserEntryUpdated(new Date());
364 // Create history entry
365 final PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
367 // Set created timestamp
368 entry.setUserPasswordHistoryCreated(new Date());
370 // Merge user to make sure it is not re-persisted
371 final User mergedUser = this.getEntityManager().merge(managedUser);
372 entry.setUserPasswordHistoryUser(mergedUser);
375 this.getEntityManager().persist(entry);
377 // Send email to user
378 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
381 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
388 public User updateUserPersonalData (final User user) {
390 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
392 // user should not be null
395 throw new NullPointerException("user is null"); //NOI18N
396 } else if (user.getUserId() == null) {
398 throw new NullPointerException("user.userId is null"); //NOI18N
399 } else if (user.getUserId() < 1) {
401 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
402 } else if (user.getUserAccountStatus() == null) {
404 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
405 } else if (!this.ifUserExists(user)) {
406 // User does not exist
407 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
411 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
414 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
417 UserUtils.copyUserData(user, managedUser);
420 managedUser.setUserEntryUpdated(new Date());
423 final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
425 // Set it back in user
426 managedUser.setUserContact(managedContact);
429 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", managedUser.getUserContact().getContactId()); //NOI18N
431 // Get mobile instance
432 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
434 // Is there a mobile instance set?
435 if (mobileNumber instanceof DialableMobileNumber) {
437 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
440 final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
443 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
446 MobileNumberUtils.copyMobileNumberData(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
448 // Then merge it, too
449 final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
452 assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
455 managedContact.setContactMobileNumber(managedMobile);
458 // Get mobile instance
459 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
461 // Is there a fax instance set?
462 if (faxNumber instanceof DialableFaxNumber) {
464 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
467 final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
470 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
473 FaxNumberUtils.copyFaxNumberData(managedUser.getUserContact().getContactFaxNumber(), foundFax);
475 // Then merge it, too
476 final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
479 assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
482 managedContact.setContactFaxNumber(managedFax);
485 // Get mobile instance
486 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
488 // Is there a fax instance set?
489 if (landLineNumber instanceof DialableLandLineNumber) {
491 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
494 final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
497 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
500 LandLineNumberUtils.copyLandLineNumberData(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
502 // Then merge it, too
503 final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
506 assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
509 managedContact.setContactLandLineNumber(managedLandLine);
513 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
515 // Return updated user instance