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.jcoreee.database.BaseDatabaseBean;
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", mappedName = "ejb/stateless-jjobs-user", description = "A bean handling the user data")
46 public class JobsUserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
51 private static final long serialVersionUID = 542_145_347_916L;
57 private UserRegistrationSessionBeanRemote registerBean;
62 public JobsUserSessionBean () {
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 // Get all phone instances
93 DialableLandLineNumber landLineNumber = user.getUserContact().getContactLandLineNumber();
94 DialableFaxNumber faxNumber = user.getUserContact().getContactFaxNumber();
95 DialableCellphoneNumber cellphoneNumber = user.getUserContact().getContactCellphoneNumber();
97 // Is a phone number instance set?
98 if (landLineNumber instanceof DialableLandLineNumber) {
99 // Set created timestamp
100 landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
103 // Is a fax number instance set?
104 if (faxNumber instanceof DialableFaxNumber) {
105 // Set created timestamp
106 faxNumber.setPhoneEntryCreated(new GregorianCalendar());
109 // Is a mobile number instance set?
110 if (cellphoneNumber instanceof DialableCellphoneNumber) {
111 // Set created timestamp
112 cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar());
116 this.getEntityManager().persist(user);
118 // Flush to get id back
119 this.getEntityManager().flush();
122 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
129 @SuppressWarnings ("unchecked")
130 public List<User> allMemberPublicVisibleUsers () {
132 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
135 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
138 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
139 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
140 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
143 List<User> users = query.getResultList();
146 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
153 @SuppressWarnings ("unchecked")
154 public List<User> allPublicUsers () {
156 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
159 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
162 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
163 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
166 List<User> users = query.getResultList();
169 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
176 @SuppressWarnings ("unchecked")
177 public List<User> allUsers () {
179 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
182 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
185 List<User> users = query.getResultList();
188 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
195 public User fillUserData (final User user) {
197 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
199 // user should not be null
202 throw new NullPointerException("user is null"); //NOI18N
206 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
209 query.setParameter("param", user.getUserName()); //NOI18N
211 // Initialize variable
212 User foundUser = null;
216 // Try to get single result
217 foundUser = (User) query.getSingleResult();
218 } catch (final NoResultException ex) {
220 this.getLoggerBeanLocal().logException(ex);
224 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
226 // Return prepared instance
231 public User findUserById (final Long userId) throws UserNotFoundException {
232 // Is the parameter valid?
233 if (null == userId) {
235 throw new NullPointerException("userId is null"); //NOI18N
236 } else if (userId < 1) {
238 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
239 } else if (!this.ifUserIdExists(userId)) {
241 throw new UserNotFoundException(userId);
244 // Create query instance
245 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
248 query.setParameter("id", userId); //NOI18N
250 // Fetch the result, it should be there by now
251 User user = (User) query.getSingleResult();
254 assert(user instanceof User) : "user is null"; //NOI18N
261 @SuppressWarnings ("unchecked")
262 public List<String> getEmailAddressList () {
264 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
267 List<String> emailAddressList = query.getResultList();
270 return emailAddressList;
274 @SuppressWarnings ("unchecked")
275 public List<String> getUserNameList () {
277 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
280 List<String> userNameList = query.getResultList();
287 public boolean ifUserExists (final User user) {
289 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
291 // userId should not be null
294 throw new NullPointerException("user is null"); //NOI18N
295 } else if (user.getUserId() == null) {
297 throw new NullPointerException("user.userId is null"); //NOI18N
298 } else if (user.getUserId() < 1) {
300 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
304 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
307 query.setParameter("id", user.getUserId()); //NOI18N
311 User dummy = (User) query.getSingleResult();
314 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
315 } catch (final NoResultException ex) {
317 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
319 // User name does not exist
321 } catch (final PersistenceException ex) {
322 // Something bad happened
323 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
330 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
337 public boolean ifUserIdExists (final Long userId) {
339 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
341 // userId should not be null
342 if (null == userId) {
344 throw new NullPointerException("userId is null"); //NOI18N
345 } else if (userId < 1) {
347 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
351 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
354 query.setParameter("id", userId); //NOI18N
358 User dummy = (User) query.getSingleResult();
361 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
362 } catch (final NoResultException ex) {
364 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
366 // User name does not exist
368 } catch (final PersistenceException ex) {
369 // Something bad happened
370 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
377 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
384 public boolean ifUserNameExists (final String userName) {
386 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
388 // userId should not be null
389 if (null == userName) {
391 throw new NullPointerException("userName is null"); //NOI18N
392 } else if (userName.isEmpty()) {
394 throw new NullPointerException("userName is empty"); //NOI18N
398 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
401 query.setParameter("param", userName); //NOI18N
405 User dummy = (User) query.getSingleResult();
408 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
409 } catch (final NoResultException ex) {
411 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
413 // User name does not exist
418 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
425 public boolean isEmailAddressReqistered (final User user) {
427 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
429 // user should not be null
432 throw new NullPointerException("user is null"); //NOI18N
436 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
439 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
443 User dummy = (User) query.getSingleResult();
446 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
447 } catch (final NoResultException ex) {
449 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
451 // Email address does not exist
453 } catch (final PersistenceException ex) {
454 // Something bad happened
455 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
466 public boolean isUserNameReqistered (final User user) {
468 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
470 // user should not be null
473 throw new NullPointerException("user is null"); //NOI18N
477 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
480 query.setParameter("param", user.getUserName()); //NOI18N
484 User dummy = (User) query.getSingleResult();
487 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
488 } catch (final NoResultException ex) {
490 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
492 // User name does not exist
494 } catch (final PersistenceException ex) {
495 // Something bad happened
496 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
507 public User updateUserData (final User user) {
509 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
511 // user should not be null
514 throw new NullPointerException("user is null"); //NOI18N
515 } else if (user.getUserId() == null) {
517 throw new NullPointerException("user.userId is null"); //NOI18N
518 } else if (user.getUserId() < 1) {
520 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
521 } else if (user.getUserAccountStatus() == null) {
523 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
524 } else if (!this.ifUserExists(user)) {
525 // User does not exist
526 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
529 // Remove contact instance as this is not updatedf
530 user.setUserContact(null);
533 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
536 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
539 User detachedUser = this.getEntityManager().merge(foundUser);
542 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
545 detachedUser.copyAll(user);
548 detachedUser.setUserUpdated(new GregorianCalendar());
550 // Return updated instance
555 public User updateUserPersonalData (final User user) {
557 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
559 // user should not be null
562 throw new NullPointerException("user is null"); //NOI18N
563 } else if (user.getUserId() == null) {
565 throw new NullPointerException("user.userId is null"); //NOI18N
566 } else if (user.getUserId() < 1) {
568 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
569 } else if (user.getUserAccountStatus() == null) {
571 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
572 } else if (!this.ifUserExists(user)) {
573 // User does not exist
574 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
578 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
581 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
584 User detachedUser = this.getEntityManager().merge(foundUser);
587 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
590 detachedUser.copyAll(user);
593 detachedUser.setUserUpdated(new GregorianCalendar());
594 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
596 // Get contact from it and find it
597 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
600 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
603 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
605 // Merge contact instance
606 Contact detachedContact = this.getEntityManager().merge(foundContact);
609 detachedContact.copyAll(user.getUserContact());
611 // Set it back in user
612 user.setUserContact(detachedContact);
615 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
617 // Get cellphone instance
618 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
620 // Is there a cellphone instance set?
621 if (cellphone instanceof DialableCellphoneNumber) {
623 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
626 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
629 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
631 // Then merge it, too
632 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
635 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
638 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
641 detachedContact.setContactCellphoneNumber(detachedCellphone);
644 // Get cellphone instance
645 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
647 // Is there a fax instance set?
648 if (fax instanceof DialableFaxNumber) {
650 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
653 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
656 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
658 // Then merge it, too
659 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
662 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
665 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
668 detachedContact.setContactFaxNumber(detachedFax);
671 // Get cellphone instance
672 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
674 // Is there a fax instance set?
675 if (landLine instanceof DialableLandLineNumber) {
677 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
680 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
683 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
685 // Then merge it, too
686 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
689 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
692 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
695 detachedContact.setContactLandLineNumber(detachedLandLine);
698 // Return updated user instance