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.jjobs.database.BaseJobsDatabaseBean;
31 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
32 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
33 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
34 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
35 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
36 import org.mxchange.jusercore.exceptions.UserNotFoundException;
37 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
38 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
39 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44 * @author Roland Haeder<roland@mxchange.org>
46 @Stateless (name = "user", description = "A bean handling the user data")
47 public class JobsUserSessionBean extends BaseJobsDatabaseBean implements UserSessionBeanRemote {
52 private static final long serialVersionUID = 542_145_347_916L;
58 private UserRegistrationSessionBeanRemote registerBean;
63 public JobsUserSessionBean () {
67 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
69 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
71 // user should not be null
74 throw new NullPointerException("user is null"); //NOI18N
75 } else if (user.getUserId() != null) {
77 throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
80 // Check if user is registered
81 if (this.registerBean.isUserNameRegistered(user)) {
83 throw new UserNameAlreadyRegisteredException(user);
84 } else if (this.registerBean.isEmailAddressRegistered(user)) {
86 throw new EmailAddressAlreadyRegisteredException(user);
89 // Set created timestamp
90 user.setUserCreated(new GregorianCalendar());
91 user.getUserContact().setContactCreated(new GregorianCalendar());
93 // Update cellphone, land-line and fax instance
94 this.setAllContactPhoneEntriesCreated(user.getUserContact());
97 this.getEntityManager().persist(user);
99 // Flush to get id back
100 this.getEntityManager().flush();
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
110 @SuppressWarnings ("unchecked")
111 public List<User> allMemberPublicVisibleUsers () {
113 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
116 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
119 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
120 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
121 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
124 List<User> users = query.getResultList();
127 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
134 @SuppressWarnings ("unchecked")
135 public List<User> allPublicUsers () {
137 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
140 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
143 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
144 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
147 List<User> users = query.getResultList();
150 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
157 @SuppressWarnings ("unchecked")
158 public List<User> allUsers () {
160 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
163 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
166 List<User> users = query.getResultList();
169 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
176 public User fillUserData (final User user) {
178 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
180 // user should not be null
183 throw new NullPointerException("user is null"); //NOI18N
187 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
190 query.setParameter("param", user.getUserName()); //NOI18N
192 // Initialize variable
193 User foundUser = null;
197 // Try to get single result
198 foundUser = (User) query.getSingleResult();
199 } catch (final NoResultException ex) {
201 this.getLoggerBeanLocal().logException(ex);
205 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
207 // Return prepared instance
212 public User findUserById (final Long userId) throws UserNotFoundException {
213 // Is the parameter valid?
214 if (null == userId) {
216 throw new NullPointerException("userId is null"); //NOI18N
217 } else if (userId < 1) {
219 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
220 } else if (!this.ifUserIdExists(userId)) {
222 throw new UserNotFoundException(userId);
225 // Create query instance
226 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
229 query.setParameter("id", userId); //NOI18N
231 // Fetch the result, it should be there by now
232 User user = (User) query.getSingleResult();
235 assert(user instanceof User) : "user is null"; //NOI18N
242 @SuppressWarnings ("unchecked")
243 public List<String> getEmailAddressList () {
245 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
248 List<String> emailAddressList = query.getResultList();
251 return emailAddressList;
255 @SuppressWarnings ("unchecked")
256 public List<String> getUserNameList () {
258 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
261 List<String> userNameList = query.getResultList();
268 public boolean ifUserExists (final User user) {
270 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
272 // userId should not be null
275 throw new NullPointerException("user is null"); //NOI18N
276 } else if (user.getUserId() == null) {
278 throw new NullPointerException("user.userId is null"); //NOI18N
279 } else if (user.getUserId() < 1) {
281 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
285 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
288 query.setParameter("id", user.getUserId()); //NOI18N
292 User dummy = (User) query.getSingleResult();
295 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
296 } catch (final NoResultException ex) {
298 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
300 // User name does not exist
302 } catch (final PersistenceException ex) {
303 // Something bad happened
304 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
311 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
318 public boolean ifUserIdExists (final Long userId) {
320 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
322 // userId should not be null
323 if (null == userId) {
325 throw new NullPointerException("userId is null"); //NOI18N
326 } else if (userId < 1) {
328 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
332 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
335 query.setParameter("id", userId); //NOI18N
339 User dummy = (User) query.getSingleResult();
342 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
343 } catch (final NoResultException ex) {
345 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
347 // User name does not exist
349 } catch (final PersistenceException ex) {
350 // Something bad happened
351 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
358 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
365 public boolean ifUserNameExists (final String userName) {
367 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
369 // userId should not be null
370 if (null == userName) {
372 throw new NullPointerException("userName is null"); //NOI18N
373 } else if (userName.isEmpty()) {
375 throw new NullPointerException("userName is empty"); //NOI18N
379 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
382 query.setParameter("param", userName); //NOI18N
386 User dummy = (User) query.getSingleResult();
389 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
390 } catch (final NoResultException ex) {
392 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
394 // User name does not exist
399 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
406 public boolean isEmailAddressReqistered (final User user) {
408 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
410 // user should not be null
413 throw new NullPointerException("user is null"); //NOI18N
417 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
420 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
424 User dummy = (User) query.getSingleResult();
427 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
428 } catch (final NoResultException ex) {
430 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
432 // Email address does not exist
434 } catch (final PersistenceException ex) {
435 // Something bad happened
436 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
447 public boolean isUserNameReqistered (final User user) {
449 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
451 // user should not be null
454 throw new NullPointerException("user is null"); //NOI18N
458 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
461 query.setParameter("param", user.getUserName()); //NOI18N
465 User dummy = (User) query.getSingleResult();
468 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
469 } catch (final NoResultException ex) {
471 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
473 // User name does not exist
475 } catch (final PersistenceException ex) {
476 // Something bad happened
477 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
488 public User linkUser (User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
489 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
493 public User updateUserData (final User user) {
495 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
497 // user should not be null
500 throw new NullPointerException("user is null"); //NOI18N
501 } else if (user.getUserId() == null) {
503 throw new NullPointerException("user.userId is null"); //NOI18N
504 } else if (user.getUserId() < 1) {
506 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
507 } else if (user.getUserAccountStatus() == null) {
509 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
510 } else if (!this.ifUserExists(user)) {
511 // User does not exist
512 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
515 // Remove contact instance as this is not updatedf
516 user.setUserContact(null);
519 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
522 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
525 User detachedUser = this.getEntityManager().merge(foundUser);
528 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
531 detachedUser.copyAll(user);
534 detachedUser.setUserUpdated(new GregorianCalendar());
536 // Return updated instance
541 public User updateUserPersonalData (final User user) {
543 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
545 // user should not be null
548 throw new NullPointerException("user is null"); //NOI18N
549 } else if (user.getUserId() == null) {
551 throw new NullPointerException("user.userId is null"); //NOI18N
552 } else if (user.getUserId() < 1) {
554 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
555 } else if (user.getUserAccountStatus() == null) {
557 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
558 } else if (!this.ifUserExists(user)) {
559 // User does not exist
560 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
564 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
567 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
570 User detachedUser = this.getEntityManager().merge(foundUser);
573 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
576 detachedUser.copyAll(user);
579 detachedUser.setUserUpdated(new GregorianCalendar());
580 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
582 // Get contact from it and find it
583 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
586 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
589 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
591 // Merge contact instance
592 Contact detachedContact = this.getEntityManager().merge(foundContact);
595 detachedContact.copyAll(user.getUserContact());
597 // Set it back in user
598 user.setUserContact(detachedContact);
601 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
603 // Get cellphone instance
604 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
606 // Is there a cellphone instance set?
607 if (cellphone instanceof DialableCellphoneNumber) {
609 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
612 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
615 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
617 // Then merge it, too
618 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
621 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
624 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
627 detachedContact.setContactCellphoneNumber(detachedCellphone);
630 // Get cellphone instance
631 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
633 // Is there a fax instance set?
634 if (fax instanceof DialableFaxNumber) {
636 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
639 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
642 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
644 // Then merge it, too
645 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
648 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
651 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
654 detachedContact.setContactFaxNumber(detachedFax);
657 // Get cellphone instance
658 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
660 // Is there a fax instance set?
661 if (landLine instanceof DialableLandLineNumber) {
663 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
666 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
669 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
671 // Then merge it, too
672 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
675 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
678 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
681 detachedContact.setContactLandLineNumber(detachedLandLine);
684 // Return updated user instance