2 * Copyright (C) 2016 - 2020 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;
24 import javax.ejb.EJBException;
25 import javax.ejb.Stateless;
26 import javax.persistence.Query;
27 import org.mxchange.jcontacts.model.contact.Contact;
28 import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
29 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
30 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumbers;
31 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
32 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumbers;
33 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
34 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumbers;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
39 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
40 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
41 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 import org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote;
47 * @author Roland Häder<roland@mxchange.org>
49 @Stateless (name = "user", description = "A bean handling the user data")
50 public class JobsUserSessionBean extends BaseJobsEnterpriseBean implements UserSessionBeanRemote {
55 private static final long serialVersionUID = 542_145_347_916L;
60 @EJB (lookup = "java:global/jjobs-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
61 private UserRegistrationSessionBeanRemote registerBean;
66 public JobsUserSessionBean () {
67 // Call super constructor
68 super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
72 public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
74 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
76 // Parameter must be valid
79 throw new NullPointerException("user is null"); //NOI18N
80 } else if (user.getUserId() == null) {
82 throw new NullPointerException("user.userId is null"); //NOI18N
83 } else if (user.getUserId() < 1) {
85 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
86 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
87 // Account is already confirmed
88 throw new UserStatusConfirmedException(user);
89 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
90 // Account is already confirmed
91 throw new UserStatusLockedException(user);
92 } else if (user.getUserConfirmKey() == null) {
94 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
95 } else if (null == baseUrl) {
97 throw new NullPointerException("baseUrl is null"); //NOI18N
98 } else if (baseUrl.isEmpty()) {
100 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
103 // Update user account
104 final User managedUser = this.updateUserData(user);
106 // Update user status and remove confirmation key
107 managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
108 managedUser.setUserConfirmKey(null);
109 managedUser.setUserUpdated(new Date());
112 this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
115 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
117 // Return updated instance
122 @SuppressWarnings ("unchecked")
123 public List<User> fetchAllUsers () {
125 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
128 final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
131 final List<User> users = query.getResultList();
134 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
141 public boolean ifUserExists (final User user) {
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
145 // userId should not be null
148 throw new NullPointerException("user is null"); //NOI18N
149 } else if (user.getUserId() == null) {
151 throw new NullPointerException("user.userId is null"); //NOI18N
152 } else if (user.getUserId() < 1) {
154 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
157 // Default is not found
158 boolean isFound = false;
161 for (final User currentUser : this.fetchAllUsers()) {
163 if (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) {
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 EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
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.", detachedUser.getUserId()); //NOI18N
293 Users.copyUserData(detachedUser, 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.setUserUpdated(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.setUserUpdated(new Date());
364 // Create history entry
365 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 Users.copyUserData(user, managedUser);
420 managedUser.setUserUpdated(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 MobileNumbers.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 FaxNumbers.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 LandLineNumbers.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