]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
added method to enqueue user's email address for changing + updated jar(s)
[addressbook-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 ifUserIdExists (final Long userId) {
164                 // Trace message
165                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
166
167                 // userId should not be null
168                 if (null == userId) {
169                         // Abort here
170                         throw new NullPointerException("userId is null"); //NOI18N
171                 } else if (userId < 1) {
172                         // Invalid number
173                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
174                 }
175
176                 // Generate query
177                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
178
179                 // Set parameter
180                 query.setParameter("id", userId); //NOI18N
181
182                 // Try this
183                 try {
184                         User dummy = (User) query.getSingleResult();
185
186                         // Debug message
187                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
188                 } catch (final NoResultException ex) {
189                         // Log it
190                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
191
192                         // User name does not exist
193                         return false;
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
197
198                         // Throw again
199                         throw ex;
200                 }
201
202                 // Trace message
203                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
204
205                 // Found it
206                 return true;
207         }
208
209         @Override
210         public boolean isEmailAddressReqistered (final User user) {
211                 // Trace message
212                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
213
214                 // user should not be null
215                 if (null == user) {
216                         // Abort here
217                         throw new NullPointerException("user is null"); //NOI18N
218                 }
219
220                 // Generate query
221                 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N
222
223                 // Set parameter
224                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
225
226                 // Search for it
227                 try {
228                         User dummy = (User) query.getSingleResult();
229
230                         // Debug message
231                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
232                 } catch (final NoResultException ex) {
233                         // Log it
234                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
235
236                         // Email address does not exist
237                         return false;
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
241
242                         // Throw again
243                         throw ex;
244                 }
245
246                 // Found it
247                 return true;
248         }
249
250         @Override
251         public boolean isUserNameReqistered (final User user) {
252                 // Trace message
253                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
254
255                 // user should not be null
256                 if (null == user) {
257                         // Abort here
258                         throw new NullPointerException("user is null"); //NOI18N
259                 }
260
261                 // Generate query
262                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N
263
264                 // Set parameter
265                 query.setParameter("param", user.getUserName()); //NOI18N
266
267                 // Try this
268                 try {
269                         User dummy = (User) query.getSingleResult();
270
271                         // Debug message
272                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
273                 } catch (final NoResultException ex) {
274                         // Log it
275                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
276
277                         // User name does not exist
278                         return false;
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
282
283                         // Throw again
284                         throw ex;
285                 }
286
287                 // Found it
288                 return true;
289         }
290
291         @Override
292         public void enqueueEmailAddressForChange (final User user) {
293                 // Trace message
294                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("enqueueEmailAddressForChange: user={0} - CALLED!", user));
295
296                 // user should not be null
297                 if (null == user) {
298                         // Abort here
299                         throw new NullPointerException("user is null"); //NOI18N
300                 } else if (user.getUserId() == null) {
301                         // Throw NPE again
302                         throw new NullPointerException("user.userId is null"); //NOI18N
303                 } else if (user.getUserId() < 1) {
304                         // Not valid
305                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
306                 } else if (user.getUserAccountStatus() == null) {
307                         // Throw NPE again
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
312                 }
313
314                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
315         }
316
317         @Override
318         public void updateEmailAddress (final User user) {
319                 // Trace message
320                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateEmailAddress: user={0} - CALLED!", user));
321
322                 // user should not be null
323                 if (null == user) {
324                         // Abort here
325                         throw new NullPointerException("user is null"); //NOI18N
326                 } else if (user.getUserId() == null) {
327                         // Throw NPE again
328                         throw new NullPointerException("user.userId is null"); //NOI18N
329                 } else if (user.getUserId() < 1) {
330                         // Not valid
331                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
332                 } else if (user.getUserAccountStatus() == null) {
333                         // Throw NPE again
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
338                 }
339
340                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
341         }
342
343         @Override
344         public void updateUserPersonalData (final User user) {
345                 // Trace message
346                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
347
348                 // user should not be null
349                 if (null == user) {
350                         // Abort here
351                         throw new NullPointerException("user is null"); //NOI18N
352                 } else if (user.getUserId() == null) {
353                         // Throw NPE again
354                         throw new NullPointerException("user.userId is null"); //NOI18N
355                 } else if (user.getUserId() < 1) {
356                         // Not valid
357                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
358                 } else if (user.getUserAccountStatus() == null) {
359                         // Throw NPE again
360                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
361                 } else if (!this.ifUserIdExists(user.getUserId())) {
362                         // User does not exist
363                         throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
364                 }
365
366                 // Find the instance
367                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
368
369                 // Should be found!
370                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
371
372                 // Merge user
373                 User detachedUser = this.getEntityManager().merge(foundUser);
374
375                 // Should be found!
376                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
377
378                 // Copy all data
379                 detachedUser.copyAll(user);
380
381                 // Set as updated
382                 detachedUser.setUserUpdated(new GregorianCalendar());
383                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
384
385                 // Get contact from it and find it
386                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
387
388                 // Should be found
389                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
390
391                 // Debug message
392                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
393
394                 // Merge contact instance
395                 Contact detachedContact = this.getEntityManager().merge(foundContact);
396
397                 // Copy all
398                 detachedContact.copyAll(user.getUserContact());
399
400                 // Set it back in user
401                 user.setUserContact(detachedContact);
402
403                 // Should be found!
404                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
405
406                 // Get cellphone instance
407                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
408
409                 // Is there a  cellphone instance set?
410                 if (cellphone instanceof DialableCellphoneNumber) {
411                         // Debug message
412                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
413
414                         // Then find it, too
415                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
416
417                         // Should be there
418                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
419
420                         // Then merge it, too
421                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
422
423                         // Should be there
424                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
425
426                         // Copy all
427                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
428
429                         // Set it back
430                         detachedContact.setContactCellphoneNumber(detachedCellphone);
431                 }
432
433                 // Get cellphone instance
434                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
435
436                 // Is there a  fax instance set?
437                 if (fax instanceof DialableFaxNumber) {
438                         // Debug message
439                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
440
441                         // Then find it, too
442                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
443
444                         // Should be there
445                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
446
447                         // Then merge it, too
448                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
449
450                         // Should be there
451                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
452
453                         // Copy all
454                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
455
456                         // Set it back
457                         detachedContact.setContactFaxNumber(detachedFax);
458                 }
459
460                 // Get cellphone instance
461                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
462
463                 // Is there a  fax instance set?
464                 if (landLine instanceof DialableLandLineNumber) {
465                         // Debug message
466                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
467
468                         // Then find it, too
469                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
470
471                         // Should be there
472                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
473
474                         // Then merge it, too
475                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
476
477                         // Should be there
478                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
479
480                         // Copy all
481                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
482
483                         // Set it back
484                         detachedContact.setContactLandLineNumber(detachedLandLine);
485                 }
486         }
487
488 }