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.model.register.UserRegistrationSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
37 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
42 * @author Roland Haeder<roland@mxchange.org>
44 @Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data")
45 public class AddressbookUserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
50 private static final long serialVersionUID = 542_145_347_916L;
56 private UserRegistrationSessionBeanRemote registerBean;
61 public AddressbookUserSessionBean () {
65 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
67 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
69 // user should not be null
72 throw new NullPointerException("user is null"); //NOI18N
75 // Check if user is registered
76 if (this.registerBean.isUserNameRegistered(user)) {
78 throw new UserNameAlreadyRegisteredException(user);
79 } else if (this.registerBean.isEmailAddressRegistered(user)) {
81 throw new EmailAddressAlreadyRegisteredException(user);
84 // Set created timestamp
85 user.setUserCreated(new GregorianCalendar());
86 user.getUserContact().setContactCreated(new GregorianCalendar());
89 this.getEntityManager().persist(user);
91 // Flush to get id back
92 this.getEntityManager().flush();
95 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
102 @SuppressWarnings ("unchecked")
103 public List<User> allMemberPublicVisibleUsers () {
105 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
108 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
111 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
112 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
113 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
116 List<User> users = query.getResultList();
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
126 @SuppressWarnings ("unchecked")
127 public List<User> allPublicUsers () {
129 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
132 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
135 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
136 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
139 List<User> users = query.getResultList();
142 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
149 @SuppressWarnings ("unchecked")
150 public List<User> allUsers () {
152 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
155 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
158 List<User> users = query.getResultList();
161 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
168 public User fillUserData (final User user) {
170 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
172 // user should not be null
175 throw new NullPointerException("user is null"); //NOI18N
179 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
182 query.setParameter("param", user.getUserName()); //NOI18N
184 // Initialize variable
185 User foundUser = null;
189 // Try to get single result
190 foundUser = (User) query.getSingleResult();
191 } catch (final NoResultException ex) {
193 this.getLoggerBeanLocal().logException(ex);
197 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
199 // Return prepared instance
204 @SuppressWarnings ("unchecked")
205 public List<String> getEmailAddressList () {
207 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
210 List<String> emailAddressList = query.getResultList();
213 return emailAddressList;
217 @SuppressWarnings ("unchecked")
218 public List<String> getUserNameList () {
220 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
223 List<String> userNameList = query.getResultList();
230 public boolean ifUserExists (final User user) {
232 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
234 // userId should not be null
237 throw new NullPointerException("user is null"); //NOI18N
238 } else if (user.getUserId() == null) {
240 throw new NullPointerException("user.userId is null"); //NOI18N
241 } else if (user.getUserId() < 1) {
243 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
247 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
250 query.setParameter("id", user.getUserId()); //NOI18N
254 User dummy = (User) query.getSingleResult();
257 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
258 } catch (final NoResultException ex) {
260 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
262 // User name does not exist
264 } catch (final PersistenceException ex) {
265 // Something bad happened
266 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
273 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
280 public boolean ifUserIdExists (final Long userId) {
282 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
284 // userId should not be null
285 if (null == userId) {
287 throw new NullPointerException("userId is null"); //NOI18N
288 } else if (userId < 1) {
290 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
294 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
297 query.setParameter("id", userId); //NOI18N
301 User dummy = (User) query.getSingleResult();
304 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
305 } catch (final NoResultException ex) {
307 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
309 // User name does not exist
311 } catch (final PersistenceException ex) {
312 // Something bad happened
313 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
320 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
327 public boolean isEmailAddressReqistered (final User user) {
329 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
331 // user should not be null
334 throw new NullPointerException("user is null"); //NOI18N
338 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
341 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
345 User dummy = (User) query.getSingleResult();
348 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
349 } catch (final NoResultException ex) {
351 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
353 // Email address does not exist
355 } catch (final PersistenceException ex) {
356 // Something bad happened
357 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
368 public boolean isUserNameReqistered (final User user) {
370 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
372 // user should not be null
375 throw new NullPointerException("user is null"); //NOI18N
379 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
382 query.setParameter("param", user.getUserName()); //NOI18N
386 User dummy = (User) query.getSingleResult();
389 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
390 } catch (final NoResultException ex) {
392 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
394 // User name does not exist
396 } catch (final PersistenceException ex) {
397 // Something bad happened
398 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
409 public void updateUserPersonalData (final User user) {
411 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
413 // user should not be null
416 throw new NullPointerException("user is null"); //NOI18N
417 } else if (user.getUserId() == null) {
419 throw new NullPointerException("user.userId is null"); //NOI18N
420 } else if (user.getUserId() < 1) {
422 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
423 } else if (user.getUserAccountStatus() == null) {
425 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
426 } else if (!this.ifUserExists(user)) {
427 // User does not exist
428 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
432 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
435 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
438 User detachedUser = this.getEntityManager().merge(foundUser);
441 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
444 detachedUser.copyAll(user);
447 detachedUser.setUserUpdated(new GregorianCalendar());
448 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
450 // Get contact from it and find it
451 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
454 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
457 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
459 // Merge contact instance
460 Contact detachedContact = this.getEntityManager().merge(foundContact);
463 detachedContact.copyAll(user.getUserContact());
465 // Set it back in user
466 user.setUserContact(detachedContact);
469 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
471 // Get cellphone instance
472 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
474 // Is there a cellphone instance set?
475 if (cellphone instanceof DialableCellphoneNumber) {
477 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
480 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
483 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
485 // Then merge it, too
486 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
489 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
492 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
495 detachedContact.setContactCellphoneNumber(detachedCellphone);
498 // Get cellphone instance
499 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
501 // Is there a fax instance set?
502 if (fax instanceof DialableFaxNumber) {
504 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
507 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
510 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
512 // Then merge it, too
513 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
516 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
519 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
522 detachedContact.setContactFaxNumber(detachedFax);
525 // Get cellphone instance
526 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
528 // Is there a fax instance set?
529 if (landLine instanceof DialableLandLineNumber) {
531 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
534 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
537 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
539 // Then merge it, too
540 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
543 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
546 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
549 detachedContact.setContactLandLineNumber(detachedLandLine);