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", LoginUser.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", LoginUser.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", LoginUser.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", LoginUser.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", LoginUser.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 isEmailAddressReqistered (final User user) {
407 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: 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", LoginUser.class); //NOI18N
419 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
423 User dummy = (User) query.getSingleResult();
426 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
427 } catch (final NoResultException ex) {
429 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: 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 isUserNameReqistered (final User user) {
448 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: 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", LoginUser.class); //NOI18N
460 query.setParameter("param", user.getUserName()); //NOI18N
464 User dummy = (User) query.getSingleResult();
467 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
468 } catch (final NoResultException ex) {
470 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: 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 (User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
488 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
492 public User updateUserData (final User user) {
494 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
496 // user should not be null
499 throw new NullPointerException("user is null"); //NOI18N
500 } else if (user.getUserId() == null) {
502 throw new NullPointerException("user.userId is null"); //NOI18N
503 } else if (user.getUserId() < 1) {
505 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
506 } else if (user.getUserAccountStatus() == null) {
508 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
509 } else if (!this.ifUserExists(user)) {
510 // User does not exist
511 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
514 // Remove contact instance as this is not updatedf
515 user.setUserContact(null);
518 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
521 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
524 User detachedUser = this.getEntityManager().merge(foundUser);
527 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
530 detachedUser.copyAll(user);
533 detachedUser.setUserUpdated(new GregorianCalendar());
535 // Return updated instance
540 public User updateUserPersonalData (final User user) {
542 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
544 // user should not be null
547 throw new NullPointerException("user is null"); //NOI18N
548 } else if (user.getUserId() == null) {
550 throw new NullPointerException("user.userId is null"); //NOI18N
551 } else if (user.getUserId() < 1) {
553 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
554 } else if (user.getUserAccountStatus() == null) {
556 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
557 } else if (!this.ifUserExists(user)) {
558 // User does not exist
559 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
563 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
566 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
569 User detachedUser = this.getEntityManager().merge(foundUser);
572 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
575 detachedUser.copyAll(user);
578 detachedUser.setUserUpdated(new GregorianCalendar());
579 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
581 // Get contact from it and find it
582 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
585 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
588 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
590 // Merge contact instance
591 Contact detachedContact = this.getEntityManager().merge(foundContact);
594 detachedContact.copyAll(user.getUserContact());
596 // Set it back in user
597 user.setUserContact(detachedContact);
600 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
602 // Get cellphone instance
603 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
605 // Is there a cellphone instance set?
606 if (cellphone instanceof DialableCellphoneNumber) {
608 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
611 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
614 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
616 // Then merge it, too
617 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
620 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
623 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
626 detachedContact.setContactCellphoneNumber(detachedCellphone);
629 // Get cellphone instance
630 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
632 // Is there a fax instance set?
633 if (fax instanceof DialableFaxNumber) {
635 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
638 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
641 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
643 // Then merge it, too
644 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
647 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
650 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
653 detachedContact.setContactFaxNumber(detachedFax);
656 // Get cellphone instance
657 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
659 // Is there a fax instance set?
660 if (landLine instanceof DialableLandLineNumber) {
662 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
665 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
668 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
670 // Then merge it, too
671 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
674 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
677 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
680 detachedContact.setContactLandLineNumber(detachedLandLine);
683 // Return updated user instance