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.jcoreee.database.BaseDatabaseBean;
27 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
28 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
33 * @author Roland Haeder<roland@mxchange.org>
35 @Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data")
36 public class UserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
41 private static final long serialVersionUID = 542_145_347_916L;
46 public UserSessionBean () {
50 @SuppressWarnings ("unchecked")
51 public List<User> allMemberPublicVisibleUsers () {
53 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
56 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
59 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
60 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
61 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
64 List<User> users = query.getResultList();
67 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
74 @SuppressWarnings ("unchecked")
75 public List<User> allPublicUsers () {
77 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
80 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
83 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
84 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
87 List<User> users = query.getResultList();
90 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
97 public User fillUserData (final User user) {
99 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
101 // user should not be null
104 throw new NullPointerException("user is null"); //NOI18N
108 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
111 query.setParameter("param", user.getUserName()); //NOI18N
113 // Initialize variable
114 User foundUser = null;
118 // Try to get single result
119 foundUser = (User) query.getSingleResult();
120 } catch (final NoResultException ex) {
122 this.getLoggerBeanLocal().logException(ex);
126 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
128 // Return prepared instance
133 @SuppressWarnings ("unchecked")
134 public List<String> getEmailAddressList () {
136 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
139 List<String> emailAddressList = query.getResultList();
142 return emailAddressList;
146 @SuppressWarnings ("unchecked")
147 public List<String> getUserNameList () {
149 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
152 List<String> userNameList = query.getResultList();
159 public boolean ifUserIdExists (final Long userId) {
161 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
163 // userId should not be null
164 if (null == userId) {
166 throw new NullPointerException("userId is null"); //NOI18N
167 } else if (userId < 1) {
169 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
173 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
176 query.setParameter("id", userId); //NOI18N
180 User dummy = (User) query.getSingleResult();
183 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
184 } catch (final NoResultException ex) {
186 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
188 // User name does not exist
190 } catch (final PersistenceException ex) {
191 // Something bad happened
192 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
199 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
206 public boolean isEmailAddressReqistered (final User user) {
208 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
210 // user should not be null
213 throw new NullPointerException("user is null"); //NOI18N
217 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
220 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
224 User dummy = (User) query.getSingleResult();
227 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
228 } catch (final NoResultException ex) {
230 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
232 // Email address does not exist
234 } catch (final PersistenceException ex) {
235 // Something bad happened
236 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
247 public boolean isUserNameReqistered (final User user) {
249 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
251 // user should not be null
254 throw new NullPointerException("user is null"); //NOI18N
258 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
261 query.setParameter("param", user.getUserName()); //NOI18N
265 User dummy = (User) query.getSingleResult();
268 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
269 } catch (final NoResultException ex) {
271 this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
273 // User name does not exist
275 } catch (final PersistenceException ex) {
276 // Something bad happened
277 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
288 public void updateUserPersonalData (final User user) {
289 // user should not be null
292 throw new NullPointerException("user is null"); //NOI18N
293 } else if (user.getUserId() == null) {
295 throw new NullPointerException("user.userId is null");
296 } else if (user.getUserId() < 1) {
298 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId()));
299 } else if (!this.ifUserIdExists(user.getUserId())) {
300 // User does not exist
301 throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId()));
305 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
308 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
311 User detachedUser = this.getEntityManager().merge(foundUser);
314 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
316 // Strange things needs to be logged
317 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: user.userAccountStatus={0},foundUser.userAccountStatus={1},user.userContact.phoneId={2},detachedUser.userContact.phoneId={3}", user.getUserAccountStatus(), detachedUser.getUserAccountStatus(), user.getUserContact().getContactLandLineNumber().getPhoneId(), detachedUser.getUserContact().getContactLandLineNumber().getPhoneId())); //NOI18N
320 detachedUser.copyAll(user);
323 detachedUser.setUserUpdated(new GregorianCalendar());
324 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());