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