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.addressbook.database.BaseAddressbookDatabaseBean;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
31 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
32 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
33 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
34 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
37 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
43 * @author Roland Haeder<roland@mxchange.org>
45 @Stateless (name = "user", description = "A bean handling the user data")
46 public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean implements UserSessionBeanRemote {
51 private static final long serialVersionUID = 542_145_347_916L;
57 private UserRegistrationSessionBeanRemote registerBean;
62 public AddressbookUserSessionBean () {
66 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
68 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
70 // user should not be null
73 throw new NullPointerException("user is null"); //NOI18N
74 } else if (user.getUserId() != null) {
76 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
79 // Check if user is registered
80 if (this.registerBean.isUserNameRegistered(user)) {
82 throw new UserNameAlreadyRegisteredException(user);
83 } else if (this.registerBean.isEmailAddressRegistered(user)) {
85 throw new EmailAddressAlreadyRegisteredException(user);
88 // Set created timestamp
89 user.setUserCreated(new GregorianCalendar());
90 user.getUserContact().setContactCreated(new GregorianCalendar());
92 // Update cellphone, land-line and fax instance
93 this.setAllContactPhoneEntriesCreated(user.getUserContact());
96 this.getEntityManager().persist(user);
98 // Flush to get id back
99 this.getEntityManager().flush();
102 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
109 @SuppressWarnings ("unchecked")
110 public List<User> allMemberPublicVisibleUsers () {
112 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
115 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
118 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
119 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
120 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
123 List<User> users = query.getResultList();
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
133 @SuppressWarnings ("unchecked")
134 public List<User> allPublicUsers () {
136 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
139 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
142 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
143 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
146 List<User> users = query.getResultList();
149 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
156 @SuppressWarnings ("unchecked")
157 public List<User> allUsers () {
159 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
162 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
165 List<User> users = query.getResultList();
168 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
175 public User fillUserData (final User user) {
177 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
179 // user should not be null
182 throw new NullPointerException("user is null"); //NOI18N
186 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
189 query.setParameter("param", user.getUserName()); //NOI18N
191 // Initialize variable
192 User foundUser = null;
196 // Try to get single result
197 foundUser = (User) query.getSingleResult();
198 } catch (final NoResultException ex) {
200 this.getLoggerBeanLocal().logException(ex);
204 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
206 // Return prepared instance
211 public User findUserById (final Long userId) throws UserNotFoundException {
212 // Is the parameter valid?
213 if (null == userId) {
215 throw new NullPointerException("userId is null"); //NOI18N
216 } else if (userId < 1) {
218 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
219 } else if (!this.ifUserIdExists(userId)) {
221 throw new UserNotFoundException(userId);
224 // Create query instance
225 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
228 query.setParameter("id", userId); //NOI18N
230 // Fetch the result, it should be there by now
231 User user = (User) query.getSingleResult();
234 assert(user instanceof User) : "user is null"; //NOI18N
241 @SuppressWarnings ("unchecked")
242 public List<String> getEmailAddressList () {
244 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
247 List<String> emailAddressList = query.getResultList();
250 return emailAddressList;
254 @SuppressWarnings ("unchecked")
255 public List<String> getUserNameList () {
257 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
260 List<String> userNameList = query.getResultList();
267 public boolean ifUserExists (final User user) {
269 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
271 // userId should not be null
274 throw new NullPointerException("user is null"); //NOI18N
275 } else if (user.getUserId() == null) {
277 throw new NullPointerException("user.userId is null"); //NOI18N
278 } else if (user.getUserId() < 1) {
280 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
284 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
287 query.setParameter("id", user.getUserId()); //NOI18N
291 User dummy = (User) query.getSingleResult();
294 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
295 } catch (final NoResultException ex) {
297 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
299 // User name does not exist
301 } catch (final PersistenceException ex) {
302 // Something bad happened
303 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
310 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
317 public boolean ifUserIdExists (final Long userId) {
319 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
321 // userId should not be null
322 if (null == userId) {
324 throw new NullPointerException("userId is null"); //NOI18N
325 } else if (userId < 1) {
327 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
331 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
334 query.setParameter("id", userId); //NOI18N
338 User dummy = (User) query.getSingleResult();
341 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
342 } catch (final NoResultException ex) {
344 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
346 // User name does not exist
348 } catch (final PersistenceException ex) {
349 // Something bad happened
350 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
357 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
364 public boolean ifUserNameExists (final String userName) {
366 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
368 // userId should not be null
369 if (null == userName) {
371 throw new NullPointerException("userName is null"); //NOI18N
372 } else if (userName.isEmpty()) {
374 throw new NullPointerException("userName is empty"); //NOI18N
378 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
381 query.setParameter("param", userName); //NOI18N
385 User dummy = (User) query.getSingleResult();
388 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
389 } catch (final NoResultException ex) {
391 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
393 // User name does not exist
398 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
405 public boolean isEmailAddressRegistered (final User user) {
407 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
409 // user should not be null
412 throw new NullPointerException("user is null"); //NOI18N
416 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N
419 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
423 User dummy = (User) query.getSingleResult();
426 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
427 } catch (final NoResultException ex) {
429 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
431 // Email address does not exist
433 } catch (final PersistenceException ex) {
434 // Something bad happened
435 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
446 public boolean isUserNameRegistered (final User user) {
448 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
450 // user should not be null
453 throw new NullPointerException("user is null"); //NOI18N
457 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
460 query.setParameter("param", user.getUserName()); //NOI18N
464 User dummy = (User) query.getSingleResult();
467 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
468 } catch (final NoResultException ex) {
470 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
472 // User name does not exist
474 } catch (final PersistenceException ex) {
475 // Something bad happened
476 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
487 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
489 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
491 // user should not be null
492 // @TODO Add check for email address
495 throw new NullPointerException("user is null"); //NOI18N
496 } else if (user.getUserId() instanceof Long) {
498 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
499 } else if (user.getUserContact() == null) {
501 throw new NullPointerException("user.userContact is null"); //NOI18N
502 } else if (user.getUserContact().getContactId() == null) {
504 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
505 } else if (user.getUserContact().getContactId() < 1) {
506 // Not valid id number
507 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
508 } else if (user.getUserAccountStatus() == null) {
510 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
511 } else if (this.ifUserNameExists(user.getUserName())) {
512 // Name already found
513 throw new UserNameAlreadyRegisteredException(user.getUserName());
514 } else if (this.ifUserExists(user)) {
515 // User does not exist
516 throw new IllegalStateException("User does already exist."); //NOI18N
519 // Try to find the contact
520 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
522 // Merge the contact to get a detached object back
523 Contact detachedContact = this.getEntityManager().merge(foundContact);
525 // Set detached object in rexcruiter instance
526 user.setUserContact(detachedContact);
529 user.setUserCreated(new GregorianCalendar());
532 this.getEntityManager().persist(user);
534 // Flush it to get updated instance back
535 this.getEntityManager().flush();
538 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N
540 // Return updated instanc
545 public User updateUserData (final User user) {
547 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
549 // user should not be null
552 throw new NullPointerException("user is null"); //NOI18N
553 } else if (user.getUserId() == null) {
555 throw new NullPointerException("user.userId is null"); //NOI18N
556 } else if (user.getUserId() < 1) {
558 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
559 } else if (user.getUserAccountStatus() == null) {
561 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
562 } else if (!this.ifUserExists(user)) {
563 // User does not exist
564 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
567 // Remove contact instance as this is not updatedf
568 user.setUserContact(null);
571 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
574 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
577 User detachedUser = this.getEntityManager().merge(foundUser);
580 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
583 detachedUser.copyAll(user);
586 detachedUser.setUserUpdated(new GregorianCalendar());
588 // Return updated instance
593 public User updateUserPersonalData (final User user) {
595 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
597 // user should not be null
600 throw new NullPointerException("user is null"); //NOI18N
601 } else if (user.getUserId() == null) {
603 throw new NullPointerException("user.userId is null"); //NOI18N
604 } else if (user.getUserId() < 1) {
606 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
607 } else if (user.getUserAccountStatus() == null) {
609 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
610 } else if (!this.ifUserExists(user)) {
611 // User does not exist
612 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
616 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
619 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
622 User detachedUser = this.getEntityManager().merge(foundUser);
625 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
628 detachedUser.copyAll(user);
631 detachedUser.setUserUpdated(new GregorianCalendar());
632 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
634 // Get contact from it and find it
635 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
638 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
641 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
643 // Merge contact instance
644 Contact detachedContact = this.getEntityManager().merge(foundContact);
647 detachedContact.copyAll(user.getUserContact());
649 // Set it back in user
650 user.setUserContact(detachedContact);
653 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
655 // Get cellphone instance
656 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
658 // Is there a cellphone instance set?
659 if (cellphone instanceof DialableCellphoneNumber) {
661 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
664 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
667 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
669 // Then merge it, too
670 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
673 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
676 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
679 detachedContact.setContactCellphoneNumber(detachedCellphone);
682 // Get cellphone instance
683 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
685 // Is there a fax instance set?
686 if (fax instanceof DialableFaxNumber) {
688 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
691 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
694 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
696 // Then merge it, too
697 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
700 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
703 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
706 detachedContact.setContactFaxNumber(detachedFax);
709 // Get cellphone instance
710 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
712 // Is there a fax instance set?
713 if (landLine instanceof DialableLandLineNumber) {
715 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
718 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
721 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
723 // Then merge it, too
724 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
727 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
730 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
733 detachedContact.setContactLandLineNumber(detachedLandLine);
736 // Return updated user instance