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 {
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.setUserUpdated(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) {
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 EJBException(MessageFormat.format("User with id {0} does not exist.", detachedUser.getUserId())); //NOI18N
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 final User managedUser = this.getEntityManager().merge(foundUser);
297 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", managedUser.getUserId()); //NOI18N
300 Users.copyAll(detachedUser, managedUser);
303 managedUser.setUserUpdated(new Date());
306 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
308 // Return updated instance
313 public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
315 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
317 // user should not be null
320 throw new NullPointerException("user is null"); //NOI18N
321 } else if (user.getUserId() == null) {
323 throw new NullPointerException("user.userId is null"); //NOI18N
324 } else if (user.getUserId() < 1) {
326 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
327 } else if (user.getUserName() == null) {
329 throw new NullPointerException("user.userName is null"); //NOI18N
330 } else if (user.getUserName().isEmpty()) {
332 throw new IllegalArgumentException("user.userName is empty"); //NOI18N
333 } else if (user.getUserAccountStatus() == null) {
335 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
336 } else if (user.getUserContact() == null) {
338 throw new NullPointerException("user.userContact is null"); //NOI18N
339 } else if (user.getUserContact().getContactId() == null) {
341 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
342 } else if (user.getUserContact().getContactId() < 1) {
344 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
345 } else if (user.getUserContact().getContactPersonalTitle() == null) {
347 throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
348 } else if (!this.ifUserExists(user)) {
349 // User does not exist
350 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
351 } else if (null == baseUrl) {
353 throw new NullPointerException("baseUrl is null"); //NOI18N
354 } else if (baseUrl.isEmpty()) {
356 throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
360 final User managedUser = this.updateUserData(user);
362 // Update user account
363 managedUser.setUserUpdated(new Date());
365 // Create history entry
366 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
368 // Set created timestamp
369 entry.setUserPasswordHistoryCreated(new Date());
371 // Merge user to make sure it is not re-persisted
372 final User mergedUser = this.getEntityManager().merge(managedUser);
373 entry.setUserPasswordHistoryUser(mergedUser);
376 this.getEntityManager().persist(entry);
378 // Send email to user
379 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
382 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
389 public User updateUserPersonalData (final User user) {
391 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
393 // user should not be null
396 throw new NullPointerException("user is null"); //NOI18N
397 } else if (user.getUserId() == null) {
399 throw new NullPointerException("user.userId is null"); //NOI18N
400 } else if (user.getUserId() < 1) {
402 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
403 } else if (user.getUserAccountStatus() == null) {
405 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
406 } else if (!this.ifUserExists(user)) {
407 // User does not exist
408 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
412 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
415 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
418 Users.copyAll(user, managedUser);
421 managedUser.setUserUpdated(new Date());
422 managedUser.getUserContact().setContactUpdated(new Date());
424 // Get contact from it and find it
425 final Contact foundContact = this.getEntityManager().find(managedUser.getUserContact().getClass(), managedUser.getUserContact().getContactId());
428 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
431 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
433 // Merge contact instance
434 final Contact managedContact = this.getEntityManager().merge(foundContact);
437 Contacts.copyAll(managedUser.getUserContact(), managedContact);
439 // Set it back in user
440 user.setUserContact(managedContact);
443 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
445 // Get mobile instance
446 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
448 // Is there a mobile instance set?
449 if (mobileNumber instanceof DialableMobileNumber) {
451 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getMobileId())); //NOI18N
454 final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getMobileId());
457 assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getMobileId()); //NOI18N
460 MobileNumbers.copyMobileNumber(managedUser.getUserContact().getContactMobileNumber(), foundMobile);
462 // Then merge it, too
463 final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
466 assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getMobileId()); //NOI18N
469 MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
472 managedContact.setContactMobileNumber(this.createManaged(mobileNumber, mobileNumber));
475 // Get mobile instance
476 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
478 // Is there a fax instance set?
479 if (faxNumber instanceof DialableFaxNumber) {
481 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
484 final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
487 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
490 FaxNumbers.copyFaxNumber(managedUser.getUserContact().getContactFaxNumber(), foundFax);
492 // Then merge it, too
493 final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
496 assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
499 FaxNumbers.copyAll(managedUser.getUserContact().getContactFaxNumber(), managedFax);
502 managedContact.setContactFaxNumber(managedFax);
505 // Get mobile instance
506 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
508 // Is there a fax instance set?
509 if (landLineNumber instanceof DialableLandLineNumber) {
511 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
514 final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
517 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
520 LandLineNumbers.copyLandLineNumber(managedUser.getUserContact().getContactLandLineNumber(), foundLandLine);
522 // Then merge it, too
523 final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
526 assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
529 LandLineNumbers.copyAll(managedUser.getUserContact().getContactLandLineNumber(), managedLandLine);
532 managedContact.setContactLandLineNumber(managedLandLine);
536 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
538 // Return updated user instance