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.Stateless;
23 import javax.persistence.NoResultException;
24 import javax.persistence.PersistenceException;
25 import javax.persistence.Query;
26 import org.mxchange.jcontacts.contact.Contact;
27 import org.mxchange.jcoreee.database.BaseDatabaseBean;
28 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
29 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
30 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
31 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
32 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
37 * @author Roland Haeder<roland@mxchange.org>
39 @Stateless (name = "user", mappedName = "ejb/stateless-jjobs-user", description = "A bean handling the user data")
40 public class UserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
45 private static final long serialVersionUID = 542_145_347_916L;
50 public UserSessionBean () {
54 @SuppressWarnings ("unchecked")
55 public List<User> allMemberPublicVisibleUsers () {
57 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
60 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
63 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
64 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
65 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
68 List<User> users = query.getResultList();
71 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
78 @SuppressWarnings ("unchecked")
79 public List<User> allPublicUsers () {
81 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
84 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
87 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
88 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
91 List<User> users = query.getResultList();
94 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
101 public User fillUserData (final User user) {
103 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
105 // user should not be null
108 throw new NullPointerException("user is null"); //NOI18N
112 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
115 query.setParameter("param", user.getUserName()); //NOI18N
117 // Initialize variable
118 User foundUser = null;
122 // Try to get single result
123 foundUser = (User) query.getSingleResult();
124 } catch (final NoResultException ex) {
126 this.getLoggerBeanLocal().logException(ex);
130 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
132 // Return prepared instance
137 @SuppressWarnings ("unchecked")
138 public List<String> getEmailAddressList () {
140 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
143 List<String> emailAddressList = query.getResultList();
146 return emailAddressList;
150 @SuppressWarnings ("unchecked")
151 public List<String> getUserNameList () {
153 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
156 List<String> userNameList = query.getResultList();
163 public boolean ifUserIdExists (final Long userId) {
165 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
167 // userId should not be null
168 if (null == userId) {
170 throw new NullPointerException("userId is null"); //NOI18N
171 } else if (userId < 1) {
173 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
177 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
180 query.setParameter("id", userId); //NOI18N
184 User dummy = (User) query.getSingleResult();
187 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
188 } catch (final NoResultException ex) {
190 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
192 // User name does not exist
194 } catch (final PersistenceException ex) {
195 // Something bad happened
196 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
203 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
210 public boolean isEmailAddressReqistered (final User user) {
212 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
214 // user should not be null
217 throw new NullPointerException("user is null"); //NOI18N
221 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
224 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
228 User dummy = (User) query.getSingleResult();
231 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
232 } catch (final NoResultException ex) {
234 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
236 // Email address does not exist
238 } catch (final PersistenceException ex) {
239 // Something bad happened
240 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
251 public boolean isUserNameReqistered (final User user) {
253 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
255 // user should not be null
258 throw new NullPointerException("user is null"); //NOI18N
262 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
265 query.setParameter("param", user.getUserName()); //NOI18N
269 User dummy = (User) query.getSingleResult();
272 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
273 } catch (final NoResultException ex) {
275 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
277 // User name does not exist
279 } catch (final PersistenceException ex) {
280 // Something bad happened
281 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
292 public void updateEmailAddress (final User user) {
294 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: user={0} - CALLED!", user));
296 // user should not be null
299 throw new NullPointerException("user is null"); //NOI18N
300 } else if (user.getUserId() == null) {
302 throw new NullPointerException("user.userId is null"); //NOI18N
303 } else if (user.getUserId() < 1) {
305 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
306 } else if (user.getUserAccountStatus() == null) {
308 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
309 } else if (!this.ifUserIdExists(user.getUserId())) {
310 // User does not exist
311 throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
314 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
318 public void updateUserPersonalData (final User user) {
320 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
322 // user should not be null
325 throw new NullPointerException("user is null"); //NOI18N
326 } else if (user.getUserId() == null) {
328 throw new NullPointerException("user.userId is null"); //NOI18N
329 } else if (user.getUserId() < 1) {
331 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
332 } else if (user.getUserAccountStatus() == null) {
334 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
335 } else if (!this.ifUserIdExists(user.getUserId())) {
336 // User does not exist
337 throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
341 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
344 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
347 User detachedUser = this.getEntityManager().merge(foundUser);
350 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
353 detachedUser.copyAll(user);
356 detachedUser.setUserUpdated(new GregorianCalendar());
357 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
359 // Get contact from it and find it
360 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
363 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
366 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
368 // Merge contact instance
369 Contact detachedContact = this.getEntityManager().merge(foundContact);
372 detachedContact.copyAll(user.getUserContact());
374 // Set it back in user
375 user.setUserContact(detachedContact);
378 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
380 // Get cellphone instance
381 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
383 // Is there a cellphone instance set?
384 if (cellphone instanceof DialableCellphoneNumber) {
386 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
389 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
392 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
394 // Then merge it, too
395 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
398 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
401 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
404 detachedContact.setContactCellphoneNumber(detachedCellphone);
407 // Get cellphone instance
408 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
410 // Is there a fax instance set?
411 if (fax instanceof DialableFaxNumber) {
413 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
416 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
419 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
421 // Then merge it, too
422 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
425 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
428 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
431 detachedContact.setContactFaxNumber(detachedFax);
434 // Get cellphone instance
435 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
437 // Is there a fax instance set?
438 if (landLine instanceof DialableLandLineNumber) {
440 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
443 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
446 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
448 // Then merge it, too
449 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
452 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
455 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
458 detachedContact.setContactLandLineNumber(detachedLandLine);