2 * Copyright (C) 2016 Roland Haeder
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.GregorianCalendar;
21 import java.util.List;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.PersistenceException;
27 import javax.persistence.Query;
28 import org.mxchange.jcontacts.contact.Contact;
29 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
30 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
31 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
32 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
33 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
34 import org.mxchange.jusercore.exceptions.UserNotFoundException;
35 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
37 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
38 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
43 * @author Roland Haeder<roland@mxchange.org>
45 @Stateless (name = "user", description = "A bean handling the user data")
46 public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserSessionBeanRemote {
51 private static final long serialVersionUID = 542_145_347_916L;
57 private UserRegistrationSessionBeanRemote registerBean;
62 public PizzaUserSessionBean () {
66 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
68 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
70 // userId should not be null
73 throw new NullPointerException("user is null"); //NOI18N
74 } else if (user.getUserId() == null) {
76 throw new NullPointerException("user.userId is null"); //NOI18N
77 } else if (user.getUserId() < 1) {
79 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
82 // Check if user is registered
83 if (this.registerBean.isUserNameRegistered(user)) {
85 throw new UserNameAlreadyRegisteredException(user);
86 } else if (this.registerBean.isEmailAddressRegistered(user)) {
88 throw new EmailAddressAlreadyRegisteredException(user);
91 // Set created timestamp
92 user.setUserCreated(new GregorianCalendar());
93 user.getUserContact().setContactCreated(new GregorianCalendar());
95 // Update cellphone, land-line and fax instance
96 this.setAllContactPhoneEntriesCreated(user.getUserContact());
99 this.getEntityManager().persist(user);
101 // Flush to get id back
102 this.getEntityManager().flush();
105 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
112 @SuppressWarnings ("unchecked")
113 public List<User> allMemberPublicVisibleUsers () {
115 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
118 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
121 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
122 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
123 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
126 List<User> users = query.getResultList();
129 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
136 @SuppressWarnings ("unchecked")
137 public List<User> allPublicUsers () {
139 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
142 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
145 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
146 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
149 List<User> users = query.getResultList();
152 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
159 @SuppressWarnings ("unchecked")
160 public List<User> allUsers () {
162 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
165 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
168 List<User> users = query.getResultList();
171 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
178 public User fillUserData (final User user) {
180 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
182 // user should not be null
185 throw new NullPointerException("user is null"); //NOI18N
189 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
192 query.setParameter("param", user.getUserName()); //NOI18N
194 // Initialize variable
195 User foundUser = null;
199 // Try to get single result
200 foundUser = (User) query.getSingleResult();
201 } catch (final NoResultException ex) {
203 this.getLoggerBeanLocal().logException(ex);
207 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
209 // Return prepared instance
214 public User findUserById (final Long userId) throws UserNotFoundException {
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: userId={0} - CALLED!", userId)); //NOI18N
218 // Is the parameter valid?
219 if (null == userId) {
221 throw new NullPointerException("userId is null"); //NOI18N
222 } else if (userId < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
225 } else if (!this.ifUserIdExists(userId)) {
227 throw new UserNotFoundException(userId);
230 // Create query instance
231 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
234 query.setParameter("id", userId); //NOI18N
236 // Fetch the result, it should be there by now
237 User user = (User) query.getSingleResult();
240 assert (user instanceof User) : "user is null"; //NOI18N
243 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: user={0} - EXIT!!", user)); //NOI18N
250 public String generateRandomUserName () {
252 this.getLoggerBeanLocal().logTrace("generateRandomUserName - CALLED!"); //NOI18N
255 List<String> userList = this.getUserNameList();
258 String userName = null;
260 // Loop until a user name is found
261 while ((userName == null) || (userList.contains(userName))) {
262 // Generate random name
263 userName = UserUtils.generateRandomUserName();
267 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateRandomUserName: userName={0} - EXIT!", userName)); //NOI18N
269 // Found one, so return it
274 @SuppressWarnings ("unchecked")
275 public List<String> getEmailAddressList () {
277 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
280 List<String> emailAddressList = query.getResultList();
283 return emailAddressList;
287 @SuppressWarnings ("unchecked")
288 public List<String> getUserNameList () {
290 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
293 List<String> userNameList = query.getResultList();
300 public boolean ifUserExists (final User user) {
302 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
304 // userId should not be null
307 throw new NullPointerException("user is null"); //NOI18N
308 } else if (user.getUserId() == null) {
310 throw new NullPointerException("user.userId is null"); //NOI18N
311 } else if (user.getUserId() < 1) {
313 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
317 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
320 query.setParameter("id", user.getUserId()); //NOI18N
324 User dummy = (User) query.getSingleResult();
327 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
328 } catch (final NoResultException ex) {
330 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
332 // User name does not exist
334 } catch (final PersistenceException ex) {
335 // Something bad happened
336 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
343 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
350 public boolean ifUserIdExists (final Long userId) {
352 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
354 // userId should not be null
355 if (null == userId) {
357 throw new NullPointerException("userId is null"); //NOI18N
358 } else if (userId < 1) {
360 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
364 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
367 query.setParameter("id", userId); //NOI18N
371 User dummy = (User) query.getSingleResult();
374 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
375 } catch (final NoResultException ex) {
377 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
379 // User name does not exist
381 } catch (final PersistenceException ex) {
382 // Something bad happened
383 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
390 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
397 public boolean ifUserNameExists (final String userName) {
399 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
401 // userId should not be null
402 if (null == userName) {
404 throw new NullPointerException("userName is null"); //NOI18N
405 } else if (userName.isEmpty()) {
407 throw new NullPointerException("userName is empty"); //NOI18N
411 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
414 query.setParameter("param", userName); //NOI18N
418 User dummy = (User) query.getSingleResult();
421 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
422 } catch (final NoResultException ex) {
424 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
426 // User name does not exist
431 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
438 public boolean isEmailAddressRegistered (final User user) {
440 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
442 // user should not be null
445 throw new NullPointerException("user is null"); //NOI18N
449 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N
452 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
456 User dummy = (User) query.getSingleResult();
459 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
460 } catch (final NoResultException ex) {
462 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
464 // Email address does not exist
466 } catch (final PersistenceException ex) {
467 // Something bad happened
468 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
479 public boolean isUserNameRegistered (final User user) {
481 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
483 // user should not be null
486 throw new NullPointerException("user is null"); //NOI18N
490 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
493 query.setParameter("param", user.getUserName()); //NOI18N
497 User dummy = (User) query.getSingleResult();
500 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
501 } catch (final NoResultException ex) {
503 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
505 // User name does not exist
507 } catch (final PersistenceException ex) {
508 // Something bad happened
509 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
520 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
522 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
524 // user should not be null
525 // @TODO Add check for email address
528 throw new NullPointerException("user is null"); //NOI18N
529 } else if (user.getUserId() instanceof Long) {
531 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
532 } else if (user.getUserContact() == null) {
534 throw new NullPointerException("user.userContact is null"); //NOI18N
535 } else if (user.getUserContact().getContactId() == null) {
537 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
538 } else if (user.getUserContact().getContactId() < 1) {
539 // Not valid id number
540 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
541 } else if (user.getUserAccountStatus() == null) {
543 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
544 } else if (this.ifUserNameExists(user.getUserName())) {
545 // Name already found
546 throw new UserNameAlreadyRegisteredException(user.getUserName());
547 } else if (this.ifUserExists(user)) {
548 // User does not exist
549 throw new IllegalStateException("User does already exist."); //NOI18N
552 // Try to find the contact
553 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
555 // Merge the contact to get a detached object back
556 Contact detachedContact = this.getEntityManager().merge(foundContact);
558 // Set detached object in rexcruiter instance
559 user.setUserContact(detachedContact);
562 user.setUserCreated(new GregorianCalendar());
565 this.getEntityManager().persist(user);
567 // Flush it to get updated instance back
568 this.getEntityManager().flush();
571 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N
573 // Return updated instanc
578 public User updateUserData (final User user) {
580 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
582 // user should not be null
585 throw new NullPointerException("user is null"); //NOI18N
586 } else if (user.getUserId() == null) {
588 throw new NullPointerException("user.userId is null"); //NOI18N
589 } else if (user.getUserId() < 1) {
591 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
592 } else if (user.getUserAccountStatus() == null) {
594 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
595 } else if (!this.ifUserExists(user)) {
596 // User does not exist
597 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
600 // Remove contact instance as this is not updatedf
601 user.setUserContact(null);
604 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
607 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
610 User detachedUser = this.getEntityManager().merge(foundUser);
613 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
616 detachedUser.copyAll(user);
619 detachedUser.setUserUpdated(new GregorianCalendar());
621 // Return updated instance
626 public User updateUserPersonalData (final User user) {
628 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
630 // user should not be null
633 throw new NullPointerException("user is null"); //NOI18N
634 } else if (user.getUserId() == null) {
636 throw new NullPointerException("user.userId is null"); //NOI18N
637 } else if (user.getUserId() < 1) {
639 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
640 } else if (user.getUserAccountStatus() == null) {
642 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
643 } else if (!this.ifUserExists(user)) {
644 // User does not exist
645 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
649 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
652 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
655 User detachedUser = this.getEntityManager().merge(foundUser);
658 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
661 detachedUser.copyAll(user);
664 detachedUser.setUserUpdated(new GregorianCalendar());
665 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
667 // Get contact from it and find it
668 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
671 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
674 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
676 // Merge contact instance
677 Contact detachedContact = this.getEntityManager().merge(foundContact);
680 detachedContact.copyAll(user.getUserContact());
682 // Set it back in user
683 user.setUserContact(detachedContact);
686 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
688 // Get cellphone instance
689 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
691 // Is there a cellphone instance set?
692 if (cellphone instanceof DialableCellphoneNumber) {
694 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
697 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
700 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
702 // Then merge it, too
703 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
706 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
709 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
712 detachedContact.setContactCellphoneNumber(detachedCellphone);
715 // Get cellphone instance
716 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
718 // Is there a fax instance set?
719 if (fax instanceof DialableFaxNumber) {
721 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
724 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
727 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
729 // Then merge it, too
730 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
733 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
736 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
739 detachedContact.setContactFaxNumber(detachedFax);
742 // Get cellphone instance
743 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
745 // Is there a fax instance set?
746 if (landLine instanceof DialableLandLineNumber) {
748 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
751 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
754 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
756 // Then merge it, too
757 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
760 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
763 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
766 detachedContact.setContactLandLineNumber(detachedLandLine);
769 // Return updated user instance