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.jcontacts.model.contact.Contacts;
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;
43 import org.mxchange.pizzaaplication.enterprise.BasePizzaEnterpriseBean;
48 * @author Roland Häder<roland@mxchange.org>
50 @Stateless (name = "user", description = "A bean handling the user data")
51 public class PizzaUserSessionBean extends BasePizzaEnterpriseBean implements UserSessionBeanRemote {
56 private static final long serialVersionUID = 542_145_347_916L;
61 @EJB (lookup = "java:global/pizzaservice-ejb/userRegistration!org.mxchange.juserlogincore.model.user.register.UserRegistrationSessionBeanRemote")
62 private UserRegistrationSessionBeanRemote registerBean;
67 public PizzaUserSessionBean () {
68 // Call super constructor
69 super("jms/pizzaservice-queue-factory", "jms/pizzaservice-email-queue"); //NOI18N
73 public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException, UserNotFoundException {
75 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
77 // Parameter must be valid
80 throw new NullPointerException("user is null"); //NOI18N
81 } else if (user.getUserId() == null) {
83 throw new NullPointerException("user.userId is null"); //NOI18N
84 } else if (user.getUserId() < 1) {
86 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
87 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
88 // Account is already confirmed
89 throw new UserStatusConfirmedException(user);
90 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
91 // Account is already confirmed
92 throw new UserStatusLockedException(user);
93 } else if (user.getUserConfirmKey() == null) {
95 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
96 } else if (null == baseUrl) {
98 throw new NullPointerException("baseUrl is null"); //NOI18N
99 } else if (baseUrl.isEmpty()) {
101 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
104 // Update user account
105 final User managedUser = this.updateUserData(user);
107 // Update user status and remove confirmation key
108 managedUser.setUserAccountStatus(UserAccountStatus.CONFIRMED);
109 managedUser.setUserConfirmKey(null);
110 managedUser.setUserEntryUpdated(new Date());
113 this.sendEmail("User account confirmed", "user_account_confirmed", managedUser, baseUrl, null); //NOI18N
116 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
118 // Return updated instance
123 @SuppressWarnings ("unchecked")
124 public List<User> fetchAllUsers () {
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
129 final Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
132 final List<User> users = query.getResultList();
135 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
142 public boolean ifUserExists (final User user) {
144 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
146 // userId should not be null
149 throw new NullPointerException("user is null"); //NOI18N
150 } else if (user.getUserId() == null) {
152 throw new NullPointerException("user.userId is null"); //NOI18N
153 } else if (user.getUserId() < 1) {
155 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
158 // Default is not found
159 boolean isFound = false;
162 for (final User currentUser : this.fetchAllUsers()) {
164 if (Objects.equals(user, currentUser)) {
165 // Yes, then set flag and exit iteration
172 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
179 public boolean ifUserNameExists (final String userName) {
181 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
183 // userId should not be null
184 if (null == userName) {
186 throw new NullPointerException("userName is null"); //NOI18N
187 } else if (userName.isEmpty()) {
189 throw new NullPointerException("userName is empty"); //NOI18N
192 // Default is not registered
193 boolean isRegistered = false;
195 // Iterate over all records
196 for (final User currentUser : this.fetchAllUsers()) {
197 // Does the username match?
198 if (userName.equals(currentUser.getUserName())) {
199 // Yes, then set flag and exit iteration
206 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
213 public boolean isEmailAddressRegistered (final User user) {
215 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
217 // user should not be null
220 throw new NullPointerException("user is null"); //NOI18N
223 // Default is not registered
224 boolean isRegistered = false;
226 // Iterate over all records
227 for (final User currentUser : this.fetchAllUsers()) {
228 // Does the email address match?
229 if (user.getUserContact().getContactEmailAddress().equals(currentUser.getUserContact().getContactEmailAddress())) {
230 // Yes, then set flag and exit iteration
237 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
244 public boolean isUserNameRegistered (final User user) {
246 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
248 // user should not be null
251 throw new NullPointerException("user is null"); //NOI18N
255 final boolean isRegistered = this.ifUserNameExists(user.getUserName());
258 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: isRegistered={1} - EXIT!", this.getClass().getSimpleName(), isRegistered)); //NOI18N
265 public User updateUserData (final User detachedUser) throws UserNotFoundException {
267 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
269 // user should not be null
270 if (null == detachedUser) {
272 throw new NullPointerException("detachedUser is null"); //NOI18N
273 } else if (detachedUser.getUserId() == null) {
275 throw new NullPointerException("detachedUser.userId is null"); //NOI18N
276 } else if (detachedUser.getUserId() < 1) {
278 throw new IllegalArgumentException(MessageFormat.format("detachedUser.userId={0} is not valid", detachedUser.getUserId())); //NOI18N
279 } else if (detachedUser.getUserAccountStatus() == null) {
281 throw new NullPointerException("detachedUser.userAccountStatus is null"); //NOI18N
282 } else if (!this.ifUserExists(detachedUser)) {
283 // User does not exist
284 throw new UserNotFoundException(detachedUser.getUserId());
288 final User foundUser = this.getEntityManager().find(detachedUser.getClass(), detachedUser.getUserId());
291 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", detachedUser.getUserId()); //NOI18N
294 Users.copyUserData(detachedUser, foundUser);
297 final User managedUser = this.getEntityManager().merge(foundUser);
300 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", managedUser.getUserId()); //NOI18N
303 Users.copyAll(detachedUser, managedUser);
306 managedUser.setUserEntryUpdated(new Date());
309 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
311 // Return updated instance
316 public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
318 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
320 // user should not be null
323 throw new NullPointerException("user is null"); //NOI18N
324 } else if (user.getUserId() == null) {
326 throw new NullPointerException("user.userId is null"); //NOI18N
327 } else if (user.getUserId() < 1) {
329 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
330 } else if (user.getUserName() == null) {
332 throw new NullPointerException("user.userName is null"); //NOI18N
333 } else if (user.getUserName().isEmpty()) {
335 throw new IllegalArgumentException("user.userName is empty"); //NOI18N
336 } else if (user.getUserAccountStatus() == null) {
338 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
339 } else if (user.getUserContact() == null) {
341 throw new NullPointerException("user.userContact is null"); //NOI18N
342 } else if (user.getUserContact().getContactId() == null) {
344 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
345 } else if (user.getUserContact().getContactId() < 1) {
347 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
348 } else if (user.getUserContact().getContactPersonalTitle() == null) {
350 throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
351 } else if (!this.ifUserExists(user)) {
352 // User does not exist
353 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
354 } else if (null == baseUrl) {
356 throw new NullPointerException("baseUrl is null"); //NOI18N
357 } else if (baseUrl.isEmpty()) {
359 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
363 final User managedUser = this.updateUserData(user);
365 // Update user account
366 managedUser.setUserEntryUpdated(new Date());
368 // Create history entry
369 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
371 // Set created timestamp
372 entry.setUserPasswordHistoryCreated(new Date());
374 // Merge user to make sure it is not re-persisted
375 final User mergedUser = this.getEntityManager().merge(managedUser);
376 entry.setUserPasswordHistoryUser(mergedUser);
379 this.getEntityManager().persist(entry);
381 // Send email to user
382 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
385 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
392 public User updateUserPersonalData (final User user) {
394 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
396 // user should not be null
399 throw new NullPointerException("user is null"); //NOI18N
400 } else if (user.getUserId() == null) {
402 throw new NullPointerException("user.userId is null"); //NOI18N
403 } else if (user.getUserId() < 1) {
405 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
406 } else if (user.getUserAccountStatus() == null) {
408 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
409 } else if (!this.ifUserExists(user)) {
410 // User does not exist
411 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
415 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
418 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
421 Users.copyUserData(user, managedUser);
424 managedUser.setUserEntryUpdated(new Date());
426 // Get contact from it and find it
427 final Contact foundContact = this.getEntityManager().find(managedUser.getUserContact().getClass(), managedUser.getUserContact().getContactId());
430 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
433 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
435 // Merge contact instance
436 final Contact managedContact = this.getEntityManager().merge(foundContact);
439 Contacts.copyAll(managedUser.getUserContact(), managedContact);
441 // Set it back in user
442 user.setUserContact(managedContact);
445 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
447 // Get mobile instance
448 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
450 // Is there a mobile instance set?
451 if (mobileNumber instanceof DialableMobileNumber) {
453 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
456 final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
459 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
462 MobileNumbers.copyMobileNumberData(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
464 // Then merge it, too
465 final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
468 assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
471 MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
474 managedContact.setContactMobileNumber(this.createManaged(mobileNumber, mobileNumber));
477 // Get mobile instance
478 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
480 // Is there a fax instance set?
481 if (faxNumber instanceof DialableFaxNumber) {
483 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
486 final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
489 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
492 FaxNumbers.copyFaxNumberData(managedUser.getUserContact().getContactFaxNumber(), foundFax);
494 // Then merge it, too
495 final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
498 assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
501 FaxNumbers.copyAll(managedUser.getUserContact().getContactFaxNumber(), managedFax);
504 managedContact.setContactFaxNumber(managedFax);
507 // Get mobile instance
508 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
510 // Is there a fax instance set?
511 if (landLineNumber instanceof DialableLandLineNumber) {
513 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
516 final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
519 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
522 LandLineNumbers.copyLandLineNumberData(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
524 // Then merge it, too
525 final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
528 assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
531 LandLineNumbers.copyAll(managedUser.getUserContact().getContactLandLineNumber(), managedLandLine);
534 managedContact.setContactLandLineNumber(managedLandLine);
538 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
540 // Return updated user instance