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 {
215 // Is the parameter valid?
216 if (null == userId) {
218 throw new NullPointerException("userId is null"); //NOI18N
219 } else if (userId < 1) {
221 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
222 } else if (!this.ifUserIdExists(userId)) {
224 throw new UserNotFoundException(userId);
227 // Create query instance
228 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
231 query.setParameter("id", userId); //NOI18N
233 // Fetch the result, it should be there by now
234 User user = (User) query.getSingleResult();
237 assert(user instanceof User) : "user is null"; //NOI18N
244 @SuppressWarnings ("unchecked")
245 public List<String> getEmailAddressList () {
247 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
250 List<String> emailAddressList = query.getResultList();
253 return emailAddressList;
257 @SuppressWarnings ("unchecked")
258 public List<String> getUserNameList () {
260 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
263 List<String> userNameList = query.getResultList();
270 public boolean ifUserExists (final User user) {
272 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
274 // userId should not be null
277 throw new NullPointerException("user is null"); //NOI18N
278 } else if (user.getUserId() == null) {
280 throw new NullPointerException("user.userId is null"); //NOI18N
281 } else if (user.getUserId() < 1) {
283 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
287 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
290 query.setParameter("id", user.getUserId()); //NOI18N
294 User dummy = (User) query.getSingleResult();
297 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
298 } catch (final NoResultException ex) {
300 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
302 // User name does not exist
304 } catch (final PersistenceException ex) {
305 // Something bad happened
306 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
313 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
320 public boolean ifUserIdExists (final Long userId) {
322 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
324 // userId should not be null
325 if (null == userId) {
327 throw new NullPointerException("userId is null"); //NOI18N
328 } else if (userId < 1) {
330 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
334 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
337 query.setParameter("id", userId); //NOI18N
341 User dummy = (User) query.getSingleResult();
344 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
345 } catch (final NoResultException ex) {
347 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
349 // User name does not exist
351 } catch (final PersistenceException ex) {
352 // Something bad happened
353 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
360 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
367 public boolean ifUserNameExists (final String userName) {
369 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
371 // userId should not be null
372 if (null == userName) {
374 throw new NullPointerException("userName is null"); //NOI18N
375 } else if (userName.isEmpty()) {
377 throw new NullPointerException("userName is empty"); //NOI18N
381 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
384 query.setParameter("param", userName); //NOI18N
388 User dummy = (User) query.getSingleResult();
391 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
392 } catch (final NoResultException ex) {
394 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
396 // User name does not exist
401 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
408 public boolean isEmailAddressRegistered (final User user) {
410 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
412 // user should not be null
415 throw new NullPointerException("user is null"); //NOI18N
419 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N
422 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
426 User dummy = (User) query.getSingleResult();
429 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
430 } catch (final NoResultException ex) {
432 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
434 // Email address does not exist
436 } catch (final PersistenceException ex) {
437 // Something bad happened
438 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
449 public boolean isUserNameRegistered (final User user) {
451 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
453 // user should not be null
456 throw new NullPointerException("user is null"); //NOI18N
460 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
463 query.setParameter("param", user.getUserName()); //NOI18N
467 User dummy = (User) query.getSingleResult();
470 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
471 } catch (final NoResultException ex) {
473 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
475 // User name does not exist
477 } catch (final PersistenceException ex) {
478 // Something bad happened
479 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
490 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
492 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
494 // user should not be null
495 // @TODO Add check for email address
498 throw new NullPointerException("user is null"); //NOI18N
499 } else if (user.getUserId() instanceof Long) {
501 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
502 } else if (user.getUserContact() == null) {
504 throw new NullPointerException("user.userContact is null"); //NOI18N
505 } else if (user.getUserContact().getContactId() == null) {
507 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
508 } else if (user.getUserContact().getContactId() < 1) {
509 // Not valid id number
510 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
511 } else if (user.getUserAccountStatus() == null) {
513 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
514 } else if (this.ifUserNameExists(user.getUserName())) {
515 // Name already found
516 throw new UserNameAlreadyRegisteredException(user.getUserName());
517 } else if (this.ifUserExists(user)) {
518 // User does not exist
519 throw new IllegalStateException("User does already exist."); //NOI18N
522 // Check if user is registered
523 if (this.registerBean.isUserNameRegistered(user)) {
525 throw new UserNameAlreadyRegisteredException(user);
526 } else if (this.registerBean.isEmailAddressRegistered(user)) {
528 throw new EmailAddressAlreadyRegisteredException(user);
531 // Set created timestamp
532 user.setUserCreated(new GregorianCalendar());
534 // Try to find the contact instance
535 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
538 Contact detachedContact = this.getEntityManager().merge(foundContact);
541 user.setUserContact(detachedContact);
544 this.getEntityManager().persist(user);
546 // Flush it to get id
547 this.getEntityManager().flush();
550 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N
552 // Return updated instanc
557 public User updateUserData (final User user) {
559 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
561 // user should not be null
564 throw new NullPointerException("user is null"); //NOI18N
565 } else if (user.getUserId() == null) {
567 throw new NullPointerException("user.userId is null"); //NOI18N
568 } else if (user.getUserId() < 1) {
570 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
571 } else if (user.getUserAccountStatus() == null) {
573 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
574 } else if (!this.ifUserExists(user)) {
575 // User does not exist
576 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
579 // Remove contact instance as this is not updatedf
580 user.setUserContact(null);
583 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
586 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
589 User detachedUser = this.getEntityManager().merge(foundUser);
592 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
595 detachedUser.copyAll(user);
598 detachedUser.setUserUpdated(new GregorianCalendar());
600 // Return updated instance
605 public User updateUserPersonalData (final User user) {
607 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
609 // user should not be null
612 throw new NullPointerException("user is null"); //NOI18N
613 } else if (user.getUserId() == null) {
615 throw new NullPointerException("user.userId is null"); //NOI18N
616 } else if (user.getUserId() < 1) {
618 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
619 } else if (user.getUserAccountStatus() == null) {
621 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
622 } else if (!this.ifUserExists(user)) {
623 // User does not exist
624 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
628 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
631 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
634 User detachedUser = this.getEntityManager().merge(foundUser);
637 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
640 detachedUser.copyAll(user);
643 detachedUser.setUserUpdated(new GregorianCalendar());
644 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
646 // Get contact from it and find it
647 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
650 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
653 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
655 // Merge contact instance
656 Contact detachedContact = this.getEntityManager().merge(foundContact);
659 detachedContact.copyAll(user.getUserContact());
661 // Set it back in user
662 user.setUserContact(detachedContact);
665 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
667 // Get cellphone instance
668 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
670 // Is there a cellphone instance set?
671 if (cellphone instanceof DialableCellphoneNumber) {
673 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
676 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
679 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
681 // Then merge it, too
682 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
685 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
688 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
691 detachedContact.setContactCellphoneNumber(detachedCellphone);
694 // Get cellphone instance
695 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
697 // Is there a fax instance set?
698 if (fax instanceof DialableFaxNumber) {
700 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
703 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
706 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
708 // Then merge it, too
709 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
712 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
715 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
718 detachedContact.setContactFaxNumber(detachedFax);
721 // Get cellphone instance
722 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
724 // Is there a fax instance set?
725 if (landLine instanceof DialableLandLineNumber) {
727 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
730 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
733 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
735 // Then merge it, too
736 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
739 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
742 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
745 detachedContact.setContactLandLineNumber(detachedLandLine);
748 // Return updated user instance