]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
6398fad8eca6703bb11e5a2e09ff5f3282b2be29
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookUserSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.EJB;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jcontacts.contact.ContactUtils;
34 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
35 import org.mxchange.jphone.phonenumbers.fax.FaxNumbers;
36 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
37 import org.mxchange.jphone.phonenumbers.landline.LandLineNumbers;
38 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
39 import org.mxchange.jphone.phonenumbers.mobile.MobileNumbers;
40 import org.mxchange.jusercore.exceptions.UserNotFoundException;
41 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
42 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
43 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
44 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
45 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
46 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
47 import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
48 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
49
50 /**
51  * A user EJB
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  */
55 @Stateless (name = "user", description = "A bean handling the user data")
56 public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean implements UserSessionBeanRemote {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 542_145_347_916L;
62
63         /**
64          * Registration EJB
65          */
66         @EJB
67         private UserRegistrationSessionBeanRemote registerBean;
68
69         /**
70          * Default constructor
71          */
72         public AddressbookUserSessionBean () {
73                 // Call super constructor
74                 super();
75         }
76
77         @Override
78         @SuppressWarnings ("unchecked")
79         public List<User> allMemberPublicVisibleUsers () {
80                 // Trace message
81                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
82
83                 // Get named query
84                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N
85
86                 // Set parameters
87                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
88                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
89                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
90
91                 // Get result
92                 List<User> users = query.getResultList();
93
94                 // Trace message
95                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
96
97                 // Return full list
98                 return users;
99         }
100
101         @Override
102         @SuppressWarnings ("unchecked")
103         public List<User> allPublicUsers () {
104                 // Trace message
105                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
106
107                 // Get named query
108                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N
109
110                 // Set parameters
111                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
112                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
113
114                 // Get result
115                 List<User> users = query.getResultList();
116
117                 // Trace message
118                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
119
120                 // Return full list
121                 return users;
122         }
123
124         @Override
125         @SuppressWarnings ("unchecked")
126         public List<User> allUsers () {
127                 // Trace message
128                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
129
130                 // Get named query
131                 Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
132
133                 // Get result
134                 List<User> users = query.getResultList();
135
136                 // Trace message
137                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
138
139                 // Return full list
140                 return users;
141         }
142
143         @Override
144         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
145                 // Trace message
146                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
147
148                 // Parameter must be valid
149                 if (null == user) {
150                         // Abort here
151                         throw new NullPointerException("user is null"); //NOI18N
152                 } else if (user.getUserId() == null) {
153                         // Abort here
154                         throw new NullPointerException("user.userId is null"); //NOI18N
155                 } else if (user.getUserId() < 1) {
156                         // Invalid number
157                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
158                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
159                         // Account is already confirmed
160                         throw new UserStatusConfirmedException(user);
161                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
162                         // Account is already confirmed
163                         throw new UserStatusLockedException(user);
164                 } else if (user.getUserConfirmKey() == null) {
165                         // Throw NPE
166                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
167                 } else if (null == baseUrl) {
168                         // Throw it again
169                         throw new NullPointerException("baseUrl is null"); //NOI18N
170                 } else if (baseUrl.isEmpty()) {
171                         // Invalid parameter
172                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
173                 }
174
175                 // Update user status and remove confirmation key
176                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
177                 user.setUserConfirmKey(null);
178                 user.setUserUpdated(new GregorianCalendar());
179
180                 // Update user account
181                 User updatedUser = this.updateUserData(user);
182
183                 // Init variable
184                 Address emailAddress;
185
186                 try {
187                         // Create email address and set
188                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
189                 } catch (final AddressException ex) {
190                         // Throw again
191                         throw new EJBException(ex);
192                 }
193
194                 // Send out email
195                 this.sendEmail("User account confirmed", "user_account_confirmed", emailAddress, updatedUser, baseUrl, null); //NOI18N
196
197                 // Trace message
198                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
199
200                 // Return updated instance
201                 return updatedUser;
202         }
203
204         @Override
205         public User fillUserData (final User user) {
206                 // Trace message
207                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
208
209                 // user should not be null
210                 if (null == user) {
211                         // Abort here
212                         throw new NullPointerException("user is null"); //NOI18N
213                 }
214
215                 // Try to locate it
216                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
217
218                 // Set parameter
219                 query.setParameter("userName", user.getUserName()); //NOI18N
220
221                 // Initialize variable
222                 User foundUser = null;
223
224                 // Try it
225                 try {
226                         // Try to get single result
227                         foundUser = (User) query.getSingleResult();
228                 } catch (final NoResultException ex) {
229                         // Log it
230                         this.getLoggerBeanLocal().logException(ex);
231                 }
232
233                 // Trace message
234                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
235
236                 // Return prepared instance
237                 return foundUser;
238         }
239
240         @Override
241         public User findUserById (final Long userId) throws UserNotFoundException {
242                 // Trace message
243                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
244
245                 // Is the parameter valid?
246                 if (null == userId) {
247                         // Throw NPE
248                         throw new NullPointerException("userId is null"); //NOI18N
249                 } else if (userId < 1) {
250                         // Not valid
251                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
252                 } else if (!this.ifUserIdExists(userId)) {
253                         // Does not exist
254                         throw new UserNotFoundException(userId);
255                 }
256
257                 // Create query instance
258                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
259
260                 // Set user id
261                 query.setParameter("id", userId); //NOI18N
262
263                 // Fetch the result, it should be there by now
264                 User user = (User) query.getSingleResult();
265
266                 // Trace message
267                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
268
269                 // Return found user
270                 return user;
271         }
272
273         @Override
274         public String generateRandomUserName () {
275                 // Trace message
276                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
277
278                 // Get full list
279                 List<String> userList = this.getUserNameList();
280
281                 // Init variable
282                 String userName = null;
283
284                 // Loop until a user name is found
285                 while ((userName == null) || (userList.contains(userName))) {
286                         // Generate random name
287                         userName = UserUtils.generateRandomUserName();
288                 }
289
290                 // Trace message
291                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
292
293                 // Found one, so return it
294                 return userName;
295         }
296
297         @Override
298         @SuppressWarnings ("unchecked")
299         public List<String> getEmailAddressList () {
300                 // Trace message
301                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
302
303                 // Get query
304                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
305
306                 // Get result list
307                 List<String> emailAddressList = query.getResultList();
308
309                 // Trace message
310                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
311
312                 // Return it
313                 return emailAddressList;
314         }
315
316         @Override
317         @SuppressWarnings ("unchecked")
318         public List<String> getUserNameList () {
319                 // Trace message
320                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
321
322                 // Get query
323                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
324
325                 // Get result list
326                 List<String> userNameList = query.getResultList();
327
328                 // Trace message
329                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
330
331                 // Return it
332                 return userNameList;
333         }
334
335         @Override
336         public boolean ifUserExists (final User user) {
337                 // Trace message
338                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
339
340                 // userId should not be null
341                 if (null == user) {
342                         // Abort here
343                         throw new NullPointerException("user is null"); //NOI18N
344                 } else if (user.getUserId() == null) {
345                         // Abort here
346                         throw new NullPointerException("user.userId is null"); //NOI18N
347                 } else if (user.getUserId() < 1) {
348                         // Invalid number
349                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
350                 }
351
352                 // Generate query
353                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
354
355                 // Set parameter
356                 query.setParameter("id", user.getUserId()); //NOI18N
357
358                 // Try this
359                 try {
360                         User dummy = (User) query.getSingleResult();
361
362                         // Debug message
363                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
364                 } catch (final NoResultException ex) {
365                         // Log it
366                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
367
368                         // User name does not exist
369                         return false;
370                 } catch (final PersistenceException ex) {
371                         // Something bad happened
372                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
373
374                         // Throw again
375                         throw ex;
376                 }
377
378                 // Trace message
379                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
380
381                 // Found it
382                 return true;
383         }
384
385         @Override
386         public boolean ifUserIdExists (final Long userId) {
387                 // Trace message
388                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
389
390                 // userId should not be null
391                 if (null == userId) {
392                         // Abort here
393                         throw new NullPointerException("userId is null"); //NOI18N
394                 } else if (userId < 1) {
395                         // Invalid number
396                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
397                 }
398
399                 // Generate query
400                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
401
402                 // Set parameter
403                 query.setParameter("id", userId); //NOI18N
404
405                 // Try this
406                 try {
407                         User dummy = (User) query.getSingleResult();
408
409                         // Debug message
410                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
411                 } catch (final NoResultException ex) {
412                         // Log it
413                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
414
415                         // User name does not exist
416                         return false;
417                 } catch (final PersistenceException ex) {
418                         // Something bad happened
419                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
420
421                         // Throw again
422                         throw ex;
423                 }
424
425                 // Trace message
426                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
427
428                 // Found it
429                 return true;
430         }
431
432         @Override
433         public boolean ifUserNameExists (final String userName) {
434                 // Trace message
435                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
436
437                 // userId should not be null
438                 if (null == userName) {
439                         // Abort here
440                         throw new NullPointerException("userName is null"); //NOI18N
441                 } else if (userName.isEmpty()) {
442                         // Abort here
443                         throw new NullPointerException("userName is empty"); //NOI18N
444                 }
445
446                 // Generate query
447                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
448
449                 // Set parameter
450                 query.setParameter("userName", userName); //NOI18N
451
452                 // Try this
453                 try {
454                         User dummy = (User) query.getSingleResult();
455
456                         // Debug message
457                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
458                 } catch (final NoResultException ex) {
459                         // Log it
460                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
461
462                         // User name does not exist
463                         return false;
464                 }
465
466                 // Trace message
467                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
468
469                 // Found it
470                 return true;
471         }
472
473         @Override
474         public boolean isEmailAddressRegistered (final User user) {
475                 // Trace message
476                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
477
478                 // user should not be null
479                 if (null == user) {
480                         // Abort here
481                         throw new NullPointerException("user is null"); //NOI18N
482                 }
483
484                 // Generate query
485                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
486
487                 // Set parameter
488                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
489
490                 // Search for it
491                 try {
492                         User dummy = (User) query.getSingleResult();
493
494                         // Debug message
495                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
496                 } catch (final NoResultException ex) {
497                         // Log it
498                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
499
500                         // Email address does not exist
501                         return false;
502                 } catch (final PersistenceException ex) {
503                         // Something bad happened
504                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
505
506                         // Throw again
507                         throw ex;
508                 }
509
510                 // Trace message
511                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
512
513                 // Found it
514                 return true;
515         }
516
517         @Override
518         public boolean isUserNameRegistered (final User user) {
519                 // Trace message
520                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
521
522                 // user should not be null
523                 if (null == user) {
524                         // Abort here
525                         throw new NullPointerException("user is null"); //NOI18N
526                 }
527
528                 // Generate query
529                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
530
531                 // Set parameter
532                 query.setParameter("userName", user.getUserName()); //NOI18N
533
534                 // Try this
535                 try {
536                         User dummy = (User) query.getSingleResult();
537
538                         // Debug message
539                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
540                 } catch (final NoResultException ex) {
541                         // Log it
542                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
543
544                         // User name does not exist
545                         return false;
546                 } catch (final PersistenceException ex) {
547                         // Something bad happened
548                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
549
550                         // Throw again
551                         throw ex;
552                 }
553
554                 // Trace message
555                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
556
557                 // Found it
558                 return true;
559         }
560
561         @Override
562         public User updateUserData (final User user) {
563                 // Trace message
564                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
565
566                 // user should not be null
567                 if (null == user) {
568                         // Abort here
569                         throw new NullPointerException("user is null"); //NOI18N
570                 } else if (user.getUserId() == null) {
571                         // Throw NPE again
572                         throw new NullPointerException("user.userId is null"); //NOI18N
573                 } else if (user.getUserId() < 1) {
574                         // Not valid
575                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
576                 } else if (user.getUserAccountStatus() == null) {
577                         // Throw NPE again
578                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
579                 } else if (!this.ifUserExists(user)) {
580                         // User does not exist
581                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
582                 }
583
584                 // Remove contact instance as this is not updated
585                 user.setUserContact(null);
586
587                 // Find the instance
588                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
589
590                 // Should be found!
591                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
592
593                 // Copy all data
594                 UserUtils.copyAll(user, managedUser);
595
596                 // Set as updated
597                 managedUser.setUserUpdated(new GregorianCalendar());
598
599                 // Trace message
600                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - CALLED!", this.getClass().getSimpleName(), managedUser)); //NOI18N
601
602                 // Return updated instance
603                 return managedUser;
604         }
605
606         @Override
607         public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
608                 // Trace message
609                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
610
611                 // user should not be null
612                 if (null == user) {
613                         // Abort here
614                         throw new NullPointerException("user is null"); //NOI18N
615                 } else if (user.getUserId() == null) {
616                         // Throw NPE again
617                         throw new NullPointerException("user.userId is null"); //NOI18N
618                 } else if (user.getUserId() < 1) {
619                         // Not valid
620                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
621                 } else if (user.getUserAccountStatus() == null) {
622                         // Throw NPE
623                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
624                 } else if (user.getUserContact() == null) {
625                         // Throw it again
626                         throw new NullPointerException("user.userContact is null"); //NOI18N
627                 } else if (user.getUserContact().getContactId() == null) {
628                         // .. and again
629                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
630                 } else if (user.getUserContact().getContactId() < 1) {
631                         // Invalid id
632                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
633                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
634                         // Throw NPE again
635                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
636                 } else if (!this.ifUserExists(user)) {
637                         // User does not exist
638                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
639                 } else if (null == baseUrl) {
640                         // Throw it again
641                         throw new NullPointerException("baseUrl is null"); //NOI18N
642                 } else if (baseUrl.isEmpty()) {
643                         // Invalid parameter
644                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
645                 }
646
647                 // Call other method
648                 User updatedUser = this.updateUserData(user);
649
650                 // Create history entry
651                 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser);
652
653                 // Set created timestamp
654                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
655
656                 // Persist it
657                 this.getEntityManager().persist(entry);
658
659                 // Flush it to get id number back
660                 this.getEntityManager().flush();
661
662                 // Init variable
663                 Address emailAddress;
664
665                 try {
666                         // Create email address and set
667                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
668                 } catch (final AddressException ex) {
669                         // Throw again
670                         throw new EJBException(ex);
671                 }
672
673                 // Send email to user
674                 this.sendEmail("User password change", "user_password_change", emailAddress, user, baseUrl, null); //NOI18N
675
676                 // Trace message
677                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
678
679                 // Return it
680                 return entry;
681         }
682
683         @Override
684         public User updateUserPersonalData (final User user) {
685                 // Trace message
686                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
687
688                 // user should not be null
689                 if (null == user) {
690                         // Abort here
691                         throw new NullPointerException("user is null"); //NOI18N
692                 } else if (user.getUserId() == null) {
693                         // Throw NPE again
694                         throw new NullPointerException("user.userId is null"); //NOI18N
695                 } else if (user.getUserId() < 1) {
696                         // Not valid
697                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
698                 } else if (user.getUserAccountStatus() == null) {
699                         // Throw NPE again
700                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
701                 } else if (!this.ifUserExists(user)) {
702                         // User does not exist
703                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
704                 }
705
706                 // Find the instance
707                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
708
709                 // Should be found!
710                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
711
712                 // Copy all data
713                 UserUtils.copyAll(user, managedUser);
714
715                 // Set as updated
716                 managedUser.setUserUpdated(new GregorianCalendar());
717                 managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
718
719                 // Get contact from it and find it
720                 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
721
722                 // Should be found
723                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
724
725                 // Debug message
726                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
727
728                 // Copy all
729                 ContactUtils.copyAll(user.getUserContact(), managedContact);
730
731                 // Set it back in user
732                 user.setUserContact(managedContact);
733
734                 // Should be found!
735                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
736
737                 // Get mobile instance
738                 DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
739
740                 // Is there a  mobile instance set?
741                 if (mobileNumber instanceof DialableMobileNumber) {
742                         // Debug message
743                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
744
745                         // Then find it, too
746                         DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId());
747
748                         // Should be there
749                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
750
751                         // Then merge it, too
752                         DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
753
754                         // Should be there
755                         assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
756
757                         // Copy all
758                         MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
759
760                         // Set it back
761                         managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
762                 }
763
764                 // Get mobile instance
765                 DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
766
767                 // Is there a  fax instance set?
768                 if (faxNumber instanceof DialableFaxNumber) {
769                         // Debug message
770                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
771
772                         // Then find it, too
773                         DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
774
775                         // Should be there
776                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
777
778                         // Then merge it, too
779                         DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
780
781                         // Should be there
782                         assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
783
784                         // Copy all
785                         FaxNumbers.copyAll(managedUser.getUserContact().getContactFaxNumber(), managedFax);
786
787                         // Set it back
788                         managedContact.setContactFaxNumber(managedFax);
789                 }
790
791                 // Get mobile instance
792                 DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
793
794                 // Is there a  fax instance set?
795                 if (landLineNumber instanceof DialableLandLineNumber) {
796                         // Debug message
797                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
798
799                         // Then find it, too
800                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
801
802                         // Should be there
803                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
804
805                         // Then merge it, too
806                         DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
807
808                         // Should be there
809                         assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
810
811                         // Copy all
812                         LandLineNumbers.copyAll(managedUser.getUserContact().getContactLandLineNumber(), managedLandLine);
813
814                         // Set it back
815                         managedContact.setContactLandLineNumber(managedLandLine);
816                 }
817
818                 // Trace message
819                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
820
821                 // Return updated user instance
822                 return managedUser;
823         }
824
825 }