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 if (Objects.equals(user, currentUser)) {
156 // Yes, then set flag and exit iteration
163 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
170 public boolean ifUserNameExists (final String userName) {
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
174 // userId should not be null
175 if (null == userName) {
177 throw new NullPointerException("userName is null"); //NOI18N
178 } else if (userName.isEmpty()) {
180 throw new NullPointerException("userName is empty"); //NOI18N
183 // Default is not registered
184 boolean isRegistered = false;
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
197 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
204 public boolean isEmailAddressRegistered (final User user) {
206 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
208 // user should not be null
211 throw new NullPointerException("user is null"); //NOI18N
214 // Default is not registered
215 boolean isRegistered = false;
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
228 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
235 public boolean isUserNameRegistered (final User user) {
237 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
239 // user should not be null
242 throw new NullPointerException("user is null"); //NOI18N
246 final boolean isRegistered = this.ifUserNameExists(user.getUserName());
249 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
256 public User updateUserData (final User user) throws UserNotFoundException {
258 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
260 // user should not be null
263 throw new NullPointerException("user is null"); //NOI18N
264 } else if (user.getUserId() == null) {
266 throw new NullPointerException("user.userId is null"); //NOI18N
267 } else if (user.getUserId() < 1) {
269 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
270 } else if (user.getUserAccountStatus() == null) {
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());
279 final User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
282 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
285 Users.copyUserData(user, foundUser);
288 final User managedUser = this.getEntityManager().merge(foundUser);
291 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
294 managedUser.setUserEntryUpdated(new Date());
297 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
299 // Return updated instance
304 public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
306 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
308 // user should not be null
311 throw new NullPointerException("user is null"); //NOI18N
312 } else if (user.getUserId() == null) {
314 throw new NullPointerException("user.userId is null"); //NOI18N
315 } else if (user.getUserId() < 1) {
317 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
318 } else if (user.getUserName() == null) {
320 throw new NullPointerException("user.userName is null"); //NOI18N
321 } else if (user.getUserName().isEmpty()) {
323 throw new IllegalArgumentException("user.userName is empty"); //NOI18N
324 } else if (user.getUserAccountStatus() == null) {
326 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
327 } else if (user.getUserContact() == null) {
329 throw new NullPointerException("user.userContact is null"); //NOI18N
330 } else if (user.getUserContact().getContactId() == null) {
332 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
333 } else if (user.getUserContact().getContactId() < 1) {
335 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
336 } else if (user.getUserContact().getContactPersonalTitle() == null) {
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) {
344 throw new NullPointerException("baseUrl is null"); //NOI18N
345 } else if (baseUrl.isEmpty()) {
347 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
351 final User managedUser = this.updateUserData(user);
353 // Update user account
354 managedUser.setUserEntryUpdated(new Date());
356 // Create history entry
357 final PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
359 // Set created timestamp
360 entry.setUserPasswordHistoryCreated(new Date());
362 // Merge user to make sure it is not re-persisted
363 final User mergedUser = this.getEntityManager().merge(managedUser);
364 entry.setUserPasswordHistoryUser(mergedUser);
367 this.getEntityManager().persist(entry);
369 // Send email to user
370 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
373 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
380 public User updateUserPersonalData (final User user) {
382 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
384 // user should not be null
387 throw new NullPointerException("user is null"); //NOI18N
388 } else if (user.getUserId() == null) {
390 throw new NullPointerException("user.userId is null"); //NOI18N
391 } else if (user.getUserId() < 1) {
393 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
394 } else if (user.getUserAccountStatus() == null) {
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
403 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
406 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
409 Users.copyUserData(user, managedUser);
412 managedUser.setUserEntryUpdated(new Date());
415 final Contact managedContact = this.mergeContactData(managedUser.getUserContact());
417 // Set it back in user
418 managedUser.setUserContact(managedContact);
421 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", managedUser.getUserContact().getContactId()); //NOI18N
423 // Get mobile instance
424 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
426 // Is there a mobile instance set?
427 if (mobileNumber instanceof DialableMobileNumber) {
429 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
432 final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
435 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
438 MobileNumbers.copyMobileNumberData(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
440 // Then merge it, too
441 final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
444 assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
447 managedContact.setContactMobileNumber(managedMobile);
450 // Get mobile instance
451 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
453 // Is there a fax instance set?
454 if (faxNumber instanceof DialableFaxNumber) {
456 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
459 final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
462 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
465 FaxNumbers.copyFaxNumberData(managedUser.getUserContact().getContactFaxNumber(), foundFax);
467 // Then merge it, too
468 final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
471 assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
474 managedContact.setContactFaxNumber(managedFax);
477 // Get mobile instance
478 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
480 // Is there a fax instance set?
481 if (landLineNumber instanceof DialableLandLineNumber) {
483 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
486 final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
489 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
492 LandLineNumbers.copyLandLineNumberData(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
494 // Then merge it, too
495 final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
498 assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
501 managedContact.setContactLandLineNumber(managedLandLine);
505 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
507 // Return updated user instance