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