]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
addressbook-user-ejb has been merged into addressbook-ejb:
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / UserSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
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;
29
30 /**
31  * A user bean
32  * <p>
33  * @author Roland Haeder<roland@mxchange.org>
34  */
35 @Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data")
36 public class UserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 542_145_347_916L;
42
43         /**
44          * Default constructor
45          */
46         public UserSessionBean () {
47         }
48
49         @Override
50         @SuppressWarnings ("unchecked")
51         public List<User> allMemberPublicVisibleUsers () {
52                 // Trace message
53                 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
54
55                 // Get named query
56                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
57
58                 // Set parameters
59                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
60                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
61                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
62
63                 // Get result
64                 List<User> users = query.getResultList();
65
66                 // Trace message
67                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
68
69                 // Return full list
70                 return users;
71         }
72
73         @Override
74         @SuppressWarnings ("unchecked")
75         public List<User> allPublicUsers () {
76                 // Trace message
77                 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
78
79                 // Get named query
80                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
81
82                 // Set parameters
83                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
84                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
85
86                 // Get result
87                 List<User> users = query.getResultList();
88
89                 // Trace message
90                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
91
92                 // Return full list
93                 return users;
94         }
95
96         @Override
97         public User fillUserData (final User user) {
98                 // Trace message
99                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
100
101                 // user should not be null
102                 if (null == user) {
103                         // Abort here
104                         throw new NullPointerException("user is null"); //NOI18N
105                 }
106
107                 // Try to locate it
108                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
109
110                 // Set parameter
111                 query.setParameter("param", user.getUserName()); //NOI18N
112
113                 // Initialize variable
114                 User foundUser = null;
115
116                 // Try it
117                 try {
118                         // Try to get single result
119                         foundUser = (User) query.getSingleResult();
120                 } catch (final NoResultException ex) {
121                         // Log it
122                         this.getLoggerBeanLocal().logException(ex);
123                 }
124
125                 // Trace message
126                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
127
128                 // Return prepared instance
129                 return foundUser;
130         }
131
132         @Override
133         @SuppressWarnings ("unchecked")
134         public List<String> getEmailAddressList () {
135                 // Get query
136                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
137
138                 // Get result list
139                 List<String> emailAddressList = query.getResultList();
140
141                 // Return it
142                 return emailAddressList;
143         }
144
145         @Override
146         @SuppressWarnings ("unchecked")
147         public List<String> getUserNameList () {
148                 // Get query
149                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
150
151                 // Get result list
152                 List<String> userNameList = query.getResultList();
153
154                 // Return it
155                 return userNameList;
156         }
157
158         @Override
159         public boolean ifUserIdExists (final Long userId) {
160                 // Trace message
161                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
162
163                 // userId should not be null
164                 if (null == userId) {
165                         // Abort here
166                         throw new NullPointerException("userId is null"); //NOI18N
167                 } else if (userId < 1) {
168                         // Invalid number
169                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
170                 }
171
172                 // Generate query
173                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
174
175                 // Set parameter
176                 query.setParameter("id", userId); //NOI18N
177
178                 // Try this
179                 try {
180                         User dummy = (User) query.getSingleResult();
181
182                         // Debug message
183                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
184                 } catch (final NoResultException ex) {
185                         // Log it
186                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
187
188                         // User name does not exist
189                         return false;
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
193
194                         // Throw again
195                         throw ex;
196                 }
197
198                 // Trace message
199                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
200
201                 // Found it
202                 return true;
203         }
204
205         @Override
206         public boolean isEmailAddressReqistered (final User user) {
207                 // Trace message
208                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
209
210                 // user should not be null
211                 if (null == user) {
212                         // Abort here
213                         throw new NullPointerException("user is null"); //NOI18N
214                 }
215
216                 // Generate query
217                 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
218
219                 // Set parameter
220                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
221
222                 // Search for it
223                 try {
224                         User dummy = (User) query.getSingleResult();
225
226                         // Debug message
227                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
228                 } catch (final NoResultException ex) {
229                         // Log it
230                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
231
232                         // Email address does not exist
233                         return false;
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
237
238                         // Throw again
239                         throw ex;
240                 }
241
242                 // Found it
243                 return true;
244         }
245
246         @Override
247         public boolean isUserNameReqistered (final User user) {
248                 // Trace message
249                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
250
251                 // user should not be null
252                 if (null == user) {
253                         // Abort here
254                         throw new NullPointerException("user is null"); //NOI18N
255                 }
256
257                 // Generate query
258                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
259
260                 // Set parameter
261                 query.setParameter("param", user.getUserName()); //NOI18N
262
263                 // Try this
264                 try {
265                         User dummy = (User) query.getSingleResult();
266
267                         // Debug message
268                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
269                 } catch (final NoResultException ex) {
270                         // Log it
271                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
272
273                         // User name does not exist
274                         return false;
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
278
279                         // Throw again
280                         throw ex;
281                 }
282
283                 // Found it
284                 return true;
285         }
286
287         @Override
288         public void updateUserPersonalData (final User user) {
289                 // user should not be null
290                 if (null == user) {
291                         // Abort here
292                         throw new NullPointerException("user is null"); //NOI18N
293                 } else if (user.getUserId() == null) {
294                         // Throw NPE again
295                         throw new NullPointerException("user.userId is null");
296                 } else if (user.getUserId() < 1) {
297                         // Not valid
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()));
302                 }
303
304                 // Find the instance
305                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
306
307                 // Should be found!
308                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
309
310                 // Merge user
311                 User detachedUser = this.getEntityManager().merge(foundUser);
312
313                 // Should be found!
314                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
315
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
318
319                 // Copy all data
320                 detachedUser.copyAll(user);
321
322                 // Set as updated
323                 detachedUser.setUserUpdated(new GregorianCalendar());
324                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
325         }
326 }