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