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
76 // Check if user is registered
77 if (this.registerBean.isUserNameRegistered(user)) {
79 throw new UserNameAlreadyRegisteredException(user);
80 } else if (this.registerBean.isEmailAddressRegistered(user)) {
82 throw new EmailAddressAlreadyRegisteredException(user);
85 // Set created timestamp
86 user.setUserCreated(new GregorianCalendar());
87 user.getUserContact().setContactCreated(new GregorianCalendar());
89 // Get all phone instances
90 DialableLandLineNumber landLineNumber = user.getUserContact().getContactLandLineNumber();
91 DialableFaxNumber faxNumber = user.getUserContact().getContactFaxNumber();
92 DialableCellphoneNumber cellphoneNumber = user.getUserContact().getContactCellphoneNumber();
94 // Is a phone number instance set?
95 if (landLineNumber instanceof DialableLandLineNumber) {
96 // Set created timestamp
97 landLineNumber.setPhoneEntryCreated(new GregorianCalendar());
100 // Is a fax number instance set?
101 if (faxNumber instanceof DialableFaxNumber) {
102 // Set created timestamp
103 faxNumber.setPhoneEntryCreated(new GregorianCalendar());
106 // Is a mobile number instance set?
107 if (cellphoneNumber instanceof DialableCellphoneNumber) {
108 // Set created timestamp
109 cellphoneNumber.setPhoneEntryCreated(new GregorianCalendar());
113 this.getEntityManager().persist(user);
115 // Flush to get id back
116 this.getEntityManager().flush();
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
126 @SuppressWarnings ("unchecked")
127 public List<User> allMemberPublicVisibleUsers () {
129 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
132 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
135 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
136 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
137 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
140 List<User> users = query.getResultList();
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
150 @SuppressWarnings ("unchecked")
151 public List<User> allPublicUsers () {
153 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
156 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
159 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
160 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
163 List<User> users = query.getResultList();
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
173 @SuppressWarnings ("unchecked")
174 public List<User> allUsers () {
176 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
179 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
182 List<User> users = query.getResultList();
185 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
192 public User fillUserData (final User user) {
194 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
196 // user should not be null
199 throw new NullPointerException("user is null"); //NOI18N
203 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
206 query.setParameter("param", user.getUserName()); //NOI18N
208 // Initialize variable
209 User foundUser = null;
213 // Try to get single result
214 foundUser = (User) query.getSingleResult();
215 } catch (final NoResultException ex) {
217 this.getLoggerBeanLocal().logException(ex);
221 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
223 // Return prepared instance
228 public User findUserById (final Long userId) throws UserNotFoundException {
229 // Is the parameter valid?
230 if (null == userId) {
232 throw new NullPointerException("userId is null"); //NOI18N
233 } else if (userId < 1) {
235 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
236 } else if (!this.ifUserIdExists(userId)) {
238 throw new UserNotFoundException(userId);
241 // Create query instance
242 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
245 query.setParameter("id", userId); //NOI18N
247 // Fetch the result, it should be there by now
248 User user = (User) query.getSingleResult();
251 assert(user instanceof User) : "user is null"; //NOI18N
258 @SuppressWarnings ("unchecked")
259 public List<String> getEmailAddressList () {
261 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
264 List<String> emailAddressList = query.getResultList();
267 return emailAddressList;
271 @SuppressWarnings ("unchecked")
272 public List<String> getUserNameList () {
274 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
277 List<String> userNameList = query.getResultList();
284 public boolean ifUserExists (final User user) {
286 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
288 // userId should not be null
291 throw new NullPointerException("user is null"); //NOI18N
292 } else if (user.getUserId() == null) {
294 throw new NullPointerException("user.userId is null"); //NOI18N
295 } else if (user.getUserId() < 1) {
297 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
301 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
304 query.setParameter("id", user.getUserId()); //NOI18N
308 User dummy = (User) query.getSingleResult();
311 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
312 } catch (final NoResultException ex) {
314 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
316 // User name does not exist
318 } catch (final PersistenceException ex) {
319 // Something bad happened
320 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
327 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
334 public boolean ifUserIdExists (final Long userId) {
336 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
338 // userId should not be null
339 if (null == userId) {
341 throw new NullPointerException("userId is null"); //NOI18N
342 } else if (userId < 1) {
344 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
348 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
351 query.setParameter("id", userId); //NOI18N
355 User dummy = (User) query.getSingleResult();
358 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
359 } catch (final NoResultException ex) {
361 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
363 // User name does not exist
365 } catch (final PersistenceException ex) {
366 // Something bad happened
367 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
374 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
381 public boolean isEmailAddressReqistered (final User user) {
383 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
385 // user should not be null
388 throw new NullPointerException("user is null"); //NOI18N
392 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
395 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
399 User dummy = (User) query.getSingleResult();
402 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
403 } catch (final NoResultException ex) {
405 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
407 // Email address does not exist
409 } catch (final PersistenceException ex) {
410 // Something bad happened
411 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
422 public boolean isUserNameReqistered (final User user) {
424 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
426 // user should not be null
429 throw new NullPointerException("user is null"); //NOI18N
433 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
436 query.setParameter("param", user.getUserName()); //NOI18N
440 User dummy = (User) query.getSingleResult();
443 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
444 } catch (final NoResultException ex) {
446 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
448 // User name does not exist
450 } catch (final PersistenceException ex) {
451 // Something bad happened
452 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
463 public void updateUserPersonalData (final User user) {
465 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
467 // user should not be null
470 throw new NullPointerException("user is null"); //NOI18N
471 } else if (user.getUserId() == null) {
473 throw new NullPointerException("user.userId is null"); //NOI18N
474 } else if (user.getUserId() < 1) {
476 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
477 } else if (user.getUserAccountStatus() == null) {
479 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
480 } else if (!this.ifUserExists(user)) {
481 // User does not exist
482 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
486 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
489 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
492 User detachedUser = this.getEntityManager().merge(foundUser);
495 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
498 detachedUser.copyAll(user);
501 detachedUser.setUserUpdated(new GregorianCalendar());
502 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
504 // Get contact from it and find it
505 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
508 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
511 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
513 // Merge contact instance
514 Contact detachedContact = this.getEntityManager().merge(foundContact);
517 detachedContact.copyAll(user.getUserContact());
519 // Set it back in user
520 user.setUserContact(detachedContact);
523 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
525 // Get cellphone instance
526 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
528 // Is there a cellphone instance set?
529 if (cellphone instanceof DialableCellphoneNumber) {
531 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
534 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
537 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
539 // Then merge it, too
540 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
543 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
546 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
549 detachedContact.setContactCellphoneNumber(detachedCellphone);
552 // Get cellphone instance
553 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
555 // Is there a fax instance set?
556 if (fax instanceof DialableFaxNumber) {
558 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
561 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
564 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
566 // Then merge it, too
567 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
570 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
573 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
576 detachedContact.setContactFaxNumber(detachedFax);
579 // Get cellphone instance
580 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
582 // Is there a fax instance set?
583 if (landLine instanceof DialableLandLineNumber) {
585 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
588 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
591 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
593 // Then merge it, too
594 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
597 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
600 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
603 detachedContact.setContactLandLineNumber(detachedLandLine);