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