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