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;
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 AddressbookUserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
46 private static final long serialVersionUID = 542_145_347_916L;
51 public AddressbookUserSessionBean () {
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 @SuppressWarnings ("unchecked")
103 public List<User> allUsers () {
105 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
108 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
111 List<User> users = query.getResultList();
114 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
121 public User fillUserData (final User user) {
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
125 // user should not be null
128 throw new NullPointerException("user is null"); //NOI18N
132 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
135 query.setParameter("param", user.getUserName()); //NOI18N
137 // Initialize variable
138 User foundUser = null;
142 // Try to get single result
143 foundUser = (User) query.getSingleResult();
144 } catch (final NoResultException ex) {
146 this.getLoggerBeanLocal().logException(ex);
150 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
152 // Return prepared instance
157 @SuppressWarnings ("unchecked")
158 public List<String> getEmailAddressList () {
160 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
163 List<String> emailAddressList = query.getResultList();
166 return emailAddressList;
170 @SuppressWarnings ("unchecked")
171 public List<String> getUserNameList () {
173 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
176 List<String> userNameList = query.getResultList();
183 public boolean ifUserExists (final User user) {
185 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
187 // userId should not be null
190 throw new NullPointerException("user is null"); //NOI18N
191 } else if (user.getUserId() == null) {
193 throw new NullPointerException("user.userId is null"); //NOI18N
194 } else if (user.getUserId() < 1) {
196 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
200 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
203 query.setParameter("id", user.getUserId()); //NOI18N
207 User dummy = (User) query.getSingleResult();
210 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
211 } catch (final NoResultException ex) {
213 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
215 // User name does not exist
217 } catch (final PersistenceException ex) {
218 // Something bad happened
219 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
226 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
233 public boolean ifUserIdExists (final Long userId) {
235 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
237 // userId should not be null
238 if (null == userId) {
240 throw new NullPointerException("userId is null"); //NOI18N
241 } else if (userId < 1) {
243 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
247 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
250 query.setParameter("id", userId); //NOI18N
254 User dummy = (User) query.getSingleResult();
257 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
258 } catch (final NoResultException ex) {
260 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: 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 id {0} found.", userId, ex)); //NOI18N
273 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
280 public boolean isEmailAddressReqistered (final User user) {
282 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
284 // user should not be null
287 throw new NullPointerException("user is null"); //NOI18N
291 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
294 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
298 User dummy = (User) query.getSingleResult();
301 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
302 } catch (final NoResultException ex) {
304 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
306 // Email address does not exist
308 } catch (final PersistenceException ex) {
309 // Something bad happened
310 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
321 public boolean isUserNameReqistered (final User user) {
323 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
325 // user should not be null
328 throw new NullPointerException("user is null"); //NOI18N
332 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
335 query.setParameter("param", user.getUserName()); //NOI18N
339 User dummy = (User) query.getSingleResult();
342 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
343 } catch (final NoResultException ex) {
345 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: 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 email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
362 public void updateUserPersonalData (final User user) {
364 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
366 // user should not be null
369 throw new NullPointerException("user is null"); //NOI18N
370 } else if (user.getUserId() == null) {
372 throw new NullPointerException("user.userId is null"); //NOI18N
373 } else if (user.getUserId() < 1) {
375 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
376 } else if (user.getUserAccountStatus() == null) {
378 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
379 } else if (!this.ifUserExists(user)) {
380 // User does not exist
381 throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
385 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
388 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
391 User detachedUser = this.getEntityManager().merge(foundUser);
394 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
397 detachedUser.copyAll(user);
400 detachedUser.setUserUpdated(new GregorianCalendar());
401 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
403 // Get contact from it and find it
404 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
407 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
410 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
412 // Merge contact instance
413 Contact detachedContact = this.getEntityManager().merge(foundContact);
416 detachedContact.copyAll(user.getUserContact());
418 // Set it back in user
419 user.setUserContact(detachedContact);
422 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
424 // Get cellphone instance
425 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
427 // Is there a cellphone instance set?
428 if (cellphone instanceof DialableCellphoneNumber) {
430 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
433 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
436 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
438 // Then merge it, too
439 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
442 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
445 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
448 detachedContact.setContactCellphoneNumber(detachedCellphone);
451 // Get cellphone instance
452 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
454 // Is there a fax instance set?
455 if (fax instanceof DialableFaxNumber) {
457 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
460 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
463 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
465 // Then merge it, too
466 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
469 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
472 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
475 detachedContact.setContactFaxNumber(detachedFax);
478 // Get cellphone instance
479 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
481 // Is there a fax instance set?
482 if (landLine instanceof DialableLandLineNumber) {
484 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
487 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
490 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
492 // Then merge it, too
493 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
496 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
499 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
502 detachedContact.setContactLandLineNumber(detachedLandLine);