]> git.mxchange.org Git - jjobs-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
Continued a bit:
[jjobs-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.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;
33
34 /**
35  * A user bean
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Stateless (name = "user", mappedName = "ejb/stateless-addressbook-user", description = "A bean handling the user data")
40 public class UserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 542_145_347_916L;
46
47         /**
48          * Default constructor
49          */
50         public UserSessionBean () {
51         }
52
53         @Override
54         @SuppressWarnings ("unchecked")
55         public List<User> allMemberPublicVisibleUsers () {
56                 // Trace message
57                 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
58
59                 // Get named query
60                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
61
62                 // Set parameters
63                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
64                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
65                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
66
67                 // Get result
68                 List<User> users = query.getResultList();
69
70                 // Trace message
71                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
72
73                 // Return full list
74                 return users;
75         }
76
77         @Override
78         @SuppressWarnings ("unchecked")
79         public List<User> allPublicUsers () {
80                 // Trace message
81                 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
82
83                 // Get named query
84                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
85
86                 // Set parameters
87                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
88                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
89
90                 // Get result
91                 List<User> users = query.getResultList();
92
93                 // Trace message
94                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
95
96                 // Return full list
97                 return users;
98         }
99
100         @Override
101         public User fillUserData (final User user) {
102                 // Trace message
103                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
104
105                 // user should not be null
106                 if (null == user) {
107                         // Abort here
108                         throw new NullPointerException("user is null"); //NOI18N
109                 }
110
111                 // Try to locate it
112                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
113
114                 // Set parameter
115                 query.setParameter("param", user.getUserName()); //NOI18N
116
117                 // Initialize variable
118                 User foundUser = null;
119
120                 // Try it
121                 try {
122                         // Try to get single result
123                         foundUser = (User) query.getSingleResult();
124                 } catch (final NoResultException ex) {
125                         // Log it
126                         this.getLoggerBeanLocal().logException(ex);
127                 }
128
129                 // Trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
131
132                 // Return prepared instance
133                 return foundUser;
134         }
135
136         @Override
137         @SuppressWarnings ("unchecked")
138         public List<String> getEmailAddressList () {
139                 // Get query
140                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
141
142                 // Get result list
143                 List<String> emailAddressList = query.getResultList();
144
145                 // Return it
146                 return emailAddressList;
147         }
148
149         @Override
150         @SuppressWarnings ("unchecked")
151         public List<String> getUserNameList () {
152                 // Get query
153                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
154
155                 // Get result list
156                 List<String> userNameList = query.getResultList();
157
158                 // Return it
159                 return userNameList;
160         }
161
162         @Override
163         public boolean ifUserExists (final User user) {
164                 // Trace message
165                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
166
167                 // userId should not be null
168                 if (null == user) {
169                         // Abort here
170                         throw new NullPointerException("user is null"); //NOI18N
171                 } else if (user.getUserId() == null) {
172                         // Abort here
173                         throw new NullPointerException("user.userId is null"); //NOI18N
174                 } else if (user.getUserId() < 1) {
175                         // Invalid number
176                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
177                 }
178
179                 // Generate query
180                 Query query = this.getEntityManager().createNamedQuery("SearchUser", LoginUser.class); //NOI18N
181
182                 // Set parameter
183                 query.setParameter("user", user); //NOI18N
184
185                 // Try this
186                 try {
187                         User dummy = (User) query.getSingleResult();
188
189                         // Debug message
190                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
191                 } catch (final NoResultException ex) {
192                         // Log it
193                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
194
195                         // User name does not exist
196                         return false;
197                 } catch (final PersistenceException ex) {
198                         // Something bad happened
199                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
200
201                         // Throw again
202                         throw ex;
203                 }
204
205                 // Trace message
206                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
207
208                 // Found it
209                 return true;
210         }
211
212         @Override
213         public boolean ifUserIdExists (final Long userId) {
214                 // Trace message
215                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
216
217                 // userId should not be null
218                 if (null == userId) {
219                         // Abort here
220                         throw new NullPointerException("userId is null"); //NOI18N
221                 } else if (userId < 1) {
222                         // Invalid number
223                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
224                 }
225
226                 // Generate query
227                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
228
229                 // Set parameter
230                 query.setParameter("id", userId); //NOI18N
231
232                 // Try this
233                 try {
234                         User dummy = (User) query.getSingleResult();
235
236                         // Debug message
237                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
238                 } catch (final NoResultException ex) {
239                         // Log it
240                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
241
242                         // User name does not exist
243                         return false;
244                 } catch (final PersistenceException ex) {
245                         // Something bad happened
246                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
247
248                         // Throw again
249                         throw ex;
250                 }
251
252                 // Trace message
253                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
254
255                 // Found it
256                 return true;
257         }
258
259         @Override
260         public boolean isEmailAddressReqistered (final User user) {
261                 // Trace message
262                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
263
264                 // user should not be null
265                 if (null == user) {
266                         // Abort here
267                         throw new NullPointerException("user is null"); //NOI18N
268                 }
269
270                 // Generate query
271                 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
272
273                 // Set parameter
274                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
275
276                 // Search for it
277                 try {
278                         User dummy = (User) query.getSingleResult();
279
280                         // Debug message
281                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
282                 } catch (final NoResultException ex) {
283                         // Log it
284                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
285
286                         // Email address does not exist
287                         return false;
288                 } catch (final PersistenceException ex) {
289                         // Something bad happened
290                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
291
292                         // Throw again
293                         throw ex;
294                 }
295
296                 // Found it
297                 return true;
298         }
299
300         @Override
301         public boolean isUserNameReqistered (final User user) {
302                 // Trace message
303                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
304
305                 // user should not be null
306                 if (null == user) {
307                         // Abort here
308                         throw new NullPointerException("user is null"); //NOI18N
309                 }
310
311                 // Generate query
312                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
313
314                 // Set parameter
315                 query.setParameter("param", user.getUserName()); //NOI18N
316
317                 // Try this
318                 try {
319                         User dummy = (User) query.getSingleResult();
320
321                         // Debug message
322                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
323                 } catch (final NoResultException ex) {
324                         // Log it
325                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
326
327                         // User name does not exist
328                         return false;
329                 } catch (final PersistenceException ex) {
330                         // Something bad happened
331                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
332
333                         // Throw again
334                         throw ex;
335                 }
336
337                 // Found it
338                 return true;
339         }
340
341         @Override
342         public void updateUserPersonalData (final User user) {
343                 // Trace message
344                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
345
346                 // user should not be null
347                 if (null == user) {
348                         // Abort here
349                         throw new NullPointerException("user is null"); //NOI18N
350                 } else if (user.getUserId() == null) {
351                         // Throw NPE again
352                         throw new NullPointerException("user.userId is null"); //NOI18N
353                 } else if (user.getUserId() < 1) {
354                         // Not valid
355                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
356                 } else if (user.getUserAccountStatus() == null) {
357                         // Throw NPE again
358                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
359                 } else if (!this.ifUserExists(user)) {
360                         // User does not exist
361                         throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
362                 }
363
364                 // Find the instance
365                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
366
367                 // Should be found!
368                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
369
370                 // Merge user
371                 User detachedUser = this.getEntityManager().merge(foundUser);
372
373                 // Should be found!
374                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
375
376                 // Copy all data
377                 detachedUser.copyAll(user);
378
379                 // Set as updated
380                 detachedUser.setUserUpdated(new GregorianCalendar());
381                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
382
383                 // Get contact from it and find it
384                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
385
386                 // Should be found
387                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
388
389                 // Debug message
390                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
391
392                 // Merge contact instance
393                 Contact detachedContact = this.getEntityManager().merge(foundContact);
394
395                 // Copy all
396                 detachedContact.copyAll(user.getUserContact());
397
398                 // Set it back in user
399                 user.setUserContact(detachedContact);
400
401                 // Should be found!
402                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
403
404                 // Get cellphone instance
405                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
406
407                 // Is there a  cellphone instance set?
408                 if (cellphone instanceof DialableCellphoneNumber) {
409                         // Debug message
410                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
411
412                         // Then find it, too
413                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
414
415                         // Should be there
416                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
417
418                         // Then merge it, too
419                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
420
421                         // Should be there
422                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
423
424                         // Copy all
425                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
426
427                         // Set it back
428                         detachedContact.setContactCellphoneNumber(detachedCellphone);
429                 }
430
431                 // Get cellphone instance
432                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
433
434                 // Is there a  fax instance set?
435                 if (fax instanceof DialableFaxNumber) {
436                         // Debug message
437                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
438
439                         // Then find it, too
440                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
441
442                         // Should be there
443                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
444
445                         // Then merge it, too
446                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
447
448                         // Should be there
449                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
450
451                         // Copy all
452                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
453
454                         // Set it back
455                         detachedContact.setContactFaxNumber(detachedFax);
456                 }
457
458                 // Get cellphone instance
459                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
460
461                 // Is there a  fax instance set?
462                 if (landLine instanceof DialableLandLineNumber) {
463                         // Debug message
464                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
465
466                         // Then find it, too
467                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
468
469                         // Should be there
470                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
471
472                         // Then merge it, too
473                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
474
475                         // Should be there
476                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
477
478                         // Copy all
479                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
480
481                         // Set it back
482                         detachedContact.setContactLandLineNumber(detachedLandLine);
483                 }
484         }
485
486 }