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 General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU 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;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.persistence.NoResultException;
25 import javax.persistence.PersistenceException;
26 import javax.persistence.Query;
27 import org.mxchange.jcontacts.contact.Contact;
28 import org.mxchange.jcoreee.database.BaseDatabaseBean;
29 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
30 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
31 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
32 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
33 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
38 * @author Roland Haeder<roland@mxchange.org>
40 @Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data")
41 public class UserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
46 private static final long serialVersionUID = 542_145_347_916L;
51 public UserSessionBean () {
55 @SuppressWarnings ("unchecked")
56 public List<User> allMemberPublicVisibleUsers () {
58 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
61 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
64 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
65 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
66 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
69 List<User> users = query.getResultList();
72 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
79 @SuppressWarnings ("unchecked")
80 public List<User> allPublicUsers () {
82 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
85 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
88 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
89 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
92 List<User> users = query.getResultList();
95 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
102 public User fillUserData (final User user) {
104 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
106 // user should not be null
109 throw new NullPointerException("user is null"); //NOI18N
113 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
116 query.setParameter("param", user.getUserName()); //NOI18N
118 // Initialize variable
119 User foundUser = null;
123 // Try to get single result
124 foundUser = (User) query.getSingleResult();
125 } catch (final NoResultException ex) {
127 this.getLoggerBeanLocal().logException(ex);
131 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
133 // Return prepared instance
138 @SuppressWarnings ("unchecked")
139 public List<String> getEmailAddressList () {
141 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
144 List<String> emailAddressList = query.getResultList();
147 return emailAddressList;
151 @SuppressWarnings ("unchecked")
152 public List<String> getUserNameList () {
154 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
157 List<String> userNameList = query.getResultList();
164 public boolean ifUserExists (final User user) {
166 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
168 // userId should not be null
171 throw new NullPointerException("user is null"); //NOI18N
172 } else if (user.getUserId() == null) {
174 throw new NullPointerException("user.userId is null"); //NOI18N
175 } else if (user.getUserId() < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
181 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
184 query.setParameter("id", user.getUserId()); //NOI18N
188 User dummy = (User) query.getSingleResult();
191 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
192 } catch (final NoResultException ex) {
194 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
196 // User name does not exist
198 } catch (final PersistenceException ex) {
199 // Something bad happened
200 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
207 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
214 public boolean ifUserIdExists (final Long userId) {
216 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
218 // userId should not be null
219 if (null == userId) {
221 throw new NullPointerException("userId is null"); //NOI18N
222 } else if (userId < 1) {
224 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
228 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
231 query.setParameter("id", userId); //NOI18N
235 User dummy = (User) query.getSingleResult();
238 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
239 } catch (final NoResultException ex) {
241 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
243 // User name does not exist
245 } catch (final PersistenceException ex) {
246 // Something bad happened
247 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
254 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
261 public boolean isEmailAddressReqistered (final User user) {
263 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
265 // user should not be null
268 throw new NullPointerException("user is null"); //NOI18N
272 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
275 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
279 User dummy = (User) query.getSingleResult();
282 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
283 } catch (final NoResultException ex) {
285 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
287 // Email address does not exist
289 } catch (final PersistenceException ex) {
290 // Something bad happened
291 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
302 public boolean isUserNameReqistered (final User user) {
304 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
306 // user should not be null
309 throw new NullPointerException("user is null"); //NOI18N
313 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
316 query.setParameter("param", user.getUserName()); //NOI18N
320 User dummy = (User) query.getSingleResult();
323 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
324 } catch (final NoResultException ex) {
326 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
328 // User name does not exist
330 } catch (final PersistenceException ex) {
331 // Something bad happened
332 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
343 public void updateUserPersonalData (final User user) {
345 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
347 // user should not be null
350 throw new NullPointerException("user is null"); //NOI18N
351 } else if (user.getUserId() == null) {
353 throw new NullPointerException("user.userId is null"); //NOI18N
354 } else if (user.getUserId() < 1) {
356 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
357 } else if (user.getUserAccountStatus() == null) {
359 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
360 } else if (!this.ifUserExists(user)) {
361 // User does not exist
362 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
366 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
369 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
372 User detachedUser = this.getEntityManager().merge(foundUser);
375 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
378 detachedUser.copyAll(user);
381 detachedUser.setUserUpdated(new GregorianCalendar());
382 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
384 // Get contact from it and find it
385 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
388 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
391 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
393 // Merge contact instance
394 Contact detachedContact = this.getEntityManager().merge(foundContact);
397 detachedContact.copyAll(user.getUserContact());
399 // Set it back in user
400 user.setUserContact(detachedContact);
403 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
405 // Get cellphone instance
406 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
408 // Is there a cellphone instance set?
409 if (cellphone instanceof DialableCellphoneNumber) {
411 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
414 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
417 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
419 // Then merge it, too
420 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
423 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
426 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
429 detachedContact.setContactCellphoneNumber(detachedCellphone);
432 // Get cellphone instance
433 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
435 // Is there a fax instance set?
436 if (fax instanceof DialableFaxNumber) {
438 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
441 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
444 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
446 // Then merge it, too
447 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
450 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
453 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
456 detachedContact.setContactFaxNumber(detachedFax);
459 // Get cellphone instance
460 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
462 // Is there a fax instance set?
463 if (landLine instanceof DialableLandLineNumber) {
465 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
468 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
471 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
473 // Then merge it, too
474 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
477 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
480 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
483 detachedContact.setContactLandLineNumber(detachedLandLine);