]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java
Added TODO (testing JPA makes no sense here)
[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                 // Should be there
237                 // @TODO This assertion tests the JPA
238                 assert (user instanceof User) : "user is null"; //NOI18N
239
240                 // Trace message
241                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: user={0} - EXIT!", user)); //NOI18N
242
243                 // Return found user
244                 return user;
245         }
246
247         @Override
248         public String generateRandomUserName () {
249                 // Trace message
250                 this.getLoggerBeanLocal().logTrace("generateRandomUserName - CALLED!"); //NOI18N
251
252                 // Get full list
253                 List<String> userList = this.getUserNameList();
254
255                 // Init variable
256                 String userName = null;
257
258                 // Loop until a user name is found
259                 while ((userName == null) || (userList.contains(userName))) {
260                         // Generate random name
261                         userName = UserUtils.generateRandomUserName();
262                 }
263
264                 // Trace message
265                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateRandomUserName: userName={0} - EXIT!", userName)); //NOI18N
266
267                 // Found one, so return it
268                 return userName;
269         }
270
271         @Override
272         @SuppressWarnings ("unchecked")
273         public List<String> getEmailAddressList () {
274                 // Get query
275                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
276
277                 // Get result list
278                 List<String> emailAddressList = query.getResultList();
279
280                 // Return it
281                 return emailAddressList;
282         }
283
284         @Override
285         @SuppressWarnings ("unchecked")
286         public List<String> getUserNameList () {
287                 // Get query
288                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
289
290                 // Get result list
291                 List<String> userNameList = query.getResultList();
292
293                 // Return it
294                 return userNameList;
295         }
296
297         @Override
298         public boolean ifUserExists (final User user) {
299                 // Trace message
300                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
301
302                 // userId should not be null
303                 if (null == user) {
304                         // Abort here
305                         throw new NullPointerException("user is null"); //NOI18N
306                 } else if (user.getUserId() == null) {
307                         // Abort here
308                         throw new NullPointerException("user.userId is null"); //NOI18N
309                 } else if (user.getUserId() < 1) {
310                         // Invalid number
311                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
312                 }
313
314                 // Generate query
315                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
316
317                 // Set parameter
318                 query.setParameter("id", user.getUserId()); //NOI18N
319
320                 // Try this
321                 try {
322                         User dummy = (User) query.getSingleResult();
323
324                         // Debug message
325                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
326                 } catch (final NoResultException ex) {
327                         // Log it
328                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
329
330                         // User name does not exist
331                         return false;
332                 } catch (final PersistenceException ex) {
333                         // Something bad happened
334                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
335
336                         // Throw again
337                         throw ex;
338                 }
339
340                 // Trace message
341                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
342
343                 // Found it
344                 return true;
345         }
346
347         @Override
348         public boolean ifUserIdExists (final Long userId) {
349                 // Trace message
350                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
351
352                 // userId should not be null
353                 if (null == userId) {
354                         // Abort here
355                         throw new NullPointerException("userId is null"); //NOI18N
356                 } else if (userId < 1) {
357                         // Invalid number
358                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
359                 }
360
361                 // Generate query
362                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
363
364                 // Set parameter
365                 query.setParameter("id", userId); //NOI18N
366
367                 // Try this
368                 try {
369                         User dummy = (User) query.getSingleResult();
370
371                         // Debug message
372                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
373                 } catch (final NoResultException ex) {
374                         // Log it
375                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
376
377                         // User name does not exist
378                         return false;
379                 } catch (final PersistenceException ex) {
380                         // Something bad happened
381                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
382
383                         // Throw again
384                         throw ex;
385                 }
386
387                 // Trace message
388                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
389
390                 // Found it
391                 return true;
392         }
393
394         @Override
395         public boolean ifUserNameExists (final String userName) {
396                 // Trace message
397                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
398
399                 // userId should not be null
400                 if (null == userName) {
401                         // Abort here
402                         throw new NullPointerException("userName is null"); //NOI18N
403                 } else if (userName.isEmpty()) {
404                         // Abort here
405                         throw new NullPointerException("userName is empty"); //NOI18N
406                 }
407
408                 // Generate query
409                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
410
411                 // Set parameter
412                 query.setParameter("param", userName); //NOI18N
413
414                 // Try this
415                 try {
416                         User dummy = (User) query.getSingleResult();
417
418                         // Debug message
419                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
420                 } catch (final NoResultException ex) {
421                         // Log it
422                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
423
424                         // User name does not exist
425                         return false;
426                 }
427
428                 // Trace message
429                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
430
431                 // Found it
432                 return true;
433         }
434
435         @Override
436         public boolean isEmailAddressRegistered (final User user) {
437                 // Trace message
438                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
439
440                 // user should not be null
441                 if (null == user) {
442                         // Abort here
443                         throw new NullPointerException("user is null"); //NOI18N
444                 }
445
446                 // Generate query
447                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
448
449                 // Set parameter
450                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
451
452                 // Search for it
453                 try {
454                         User dummy = (User) query.getSingleResult();
455
456                         // Debug message
457                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
458                 } catch (final NoResultException ex) {
459                         // Log it
460                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
461
462                         // Email address does not exist
463                         return false;
464                 } catch (final PersistenceException ex) {
465                         // Something bad happened
466                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
467
468                         // Throw again
469                         throw ex;
470                 }
471
472                 // Found it
473                 return true;
474         }
475
476         @Override
477         public boolean isUserNameRegistered (final User user) {
478                 // Trace message
479                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
480
481                 // user should not be null
482                 if (null == user) {
483                         // Abort here
484                         throw new NullPointerException("user is null"); //NOI18N
485                 }
486
487                 // Generate query
488                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
489
490                 // Set parameter
491                 query.setParameter("param", user.getUserName()); //NOI18N
492
493                 // Try this
494                 try {
495                         User dummy = (User) query.getSingleResult();
496
497                         // Debug message
498                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
499                 } catch (final NoResultException ex) {
500                         // Log it
501                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
502
503                         // User name does not exist
504                         return false;
505                 } catch (final PersistenceException ex) {
506                         // Something bad happened
507                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
508
509                         // Throw again
510                         throw ex;
511                 }
512
513                 // Found it
514                 return true;
515         }
516
517         @Override
518         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
519                 // Trace message
520                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
521
522                 // user should not be null
523                 // @TODO Add check for email address
524                 if (null == user) {
525                         // Abort here
526                         throw new NullPointerException("user is null"); //NOI18N
527                 } else if (user.getUserId() instanceof Long) {
528                         // Id is set
529                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
530                 } else if (user.getUserContact() == null) {
531                         // Throw NPE again
532                         throw new NullPointerException("user.userContact is null"); //NOI18N
533                 } else if (user.getUserContact().getContactId() == null) {
534                         // Throw NPE again
535                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
536                 } else if (user.getUserContact().getContactId() < 1) {
537                         // Not valid id number
538                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
539                 } else if (user.getUserAccountStatus() == null) {
540                         // Throw NPE again
541                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
542                 } else if (this.ifUserNameExists(user.getUserName())) {
543                         // Name already found
544                         throw new UserNameAlreadyRegisteredException(user.getUserName());
545                 } else if (this.ifUserExists(user)) {
546                         // User does not exist
547                         throw new IllegalStateException("User does already exist."); //NOI18N
548                 }
549
550                 // Check if user is registered
551                 if (this.registerBean.isUserNameRegistered(user)) {
552                         // Abort here
553                         throw new UserNameAlreadyRegisteredException(user);
554                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
555                         // Abort here
556                         throw new EmailAddressAlreadyRegisteredException(user);
557                 }
558
559                 // Set created timestamp
560                 user.setUserCreated(new GregorianCalendar());
561
562                 // Try to find the contact instance
563                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
564
565                 // Set detached object in rexcruiter instance
566                 user.setUserContact(foundContact);
567
568                 // Persist user
569                 this.getEntityManager().persist(user);
570
571                 // Flush it to get id
572                 this.getEntityManager().flush();
573
574                 // Trace message
575                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N
576
577                 // Return updated instanc
578                 return user;
579         }
580
581         @Override
582         public User updateUserData (final User user) {
583                 // Trace message
584                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
585
586                 // user should not be null
587                 if (null == user) {
588                         // Abort here
589                         throw new NullPointerException("user is null"); //NOI18N
590                 } else if (user.getUserId() == null) {
591                         // Throw NPE again
592                         throw new NullPointerException("user.userId is null"); //NOI18N
593                 } else if (user.getUserId() < 1) {
594                         // Not valid
595                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
596                 } else if (user.getUserAccountStatus() == null) {
597                         // Throw NPE again
598                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
599                 } else if (!this.ifUserExists(user)) {
600                         // User does not exist
601                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
602                 }
603
604                 // Remove contact instance as this is not updatedf
605                 user.setUserContact(null);
606
607                 // Find the instance
608                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
609
610                 // Should be found!
611                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
612
613                 // Merge user
614                 User detachedUser = this.getEntityManager().merge(foundUser);
615
616                 // Should be found!
617                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
618
619                 // Copy all data
620                 detachedUser.copyAll(user);
621
622                 // Set as updated
623                 detachedUser.setUserUpdated(new GregorianCalendar());
624
625                 // Return updated instance
626                 return detachedUser;
627         }
628
629         @Override
630         public User updateUserPersonalData (final User user) {
631                 // Trace message
632                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
633
634                 // user should not be null
635                 if (null == user) {
636                         // Abort here
637                         throw new NullPointerException("user is null"); //NOI18N
638                 } else if (user.getUserId() == null) {
639                         // Throw NPE again
640                         throw new NullPointerException("user.userId is null"); //NOI18N
641                 } else if (user.getUserId() < 1) {
642                         // Not valid
643                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
644                 } else if (user.getUserAccountStatus() == null) {
645                         // Throw NPE again
646                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
647                 } else if (!this.ifUserExists(user)) {
648                         // User does not exist
649                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
650                 }
651
652                 // Find the instance
653                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
654
655                 // Should be found!
656                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
657
658                 // Merge user
659                 User detachedUser = this.getEntityManager().merge(foundUser);
660
661                 // Should be found!
662                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
663
664                 // Copy all data
665                 detachedUser.copyAll(user);
666
667                 // Set as updated
668                 detachedUser.setUserUpdated(new GregorianCalendar());
669                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
670
671                 // Get contact from it and find it
672                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
673
674                 // Should be found
675                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
676
677                 // Debug message
678                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
679
680                 // Merge contact instance
681                 Contact detachedContact = this.getEntityManager().merge(foundContact);
682
683                 // Copy all
684                 detachedContact.copyAll(user.getUserContact());
685
686                 // Set it back in user
687                 user.setUserContact(detachedContact);
688
689                 // Should be found!
690                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
691
692                 // Get cellphone instance
693                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
694
695                 // Is there a  cellphone instance set?
696                 if (cellphone instanceof DialableCellphoneNumber) {
697                         // Debug message
698                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
699
700                         // Then find it, too
701                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
702
703                         // Should be there
704                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
705
706                         // Then merge it, too
707                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
708
709                         // Should be there
710                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
711
712                         // Copy all
713                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
714
715                         // Set it back
716                         detachedContact.setContactCellphoneNumber(detachedCellphone);
717                 }
718
719                 // Get cellphone instance
720                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
721
722                 // Is there a  fax instance set?
723                 if (fax instanceof DialableFaxNumber) {
724                         // Debug message
725                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
726
727                         // Then find it, too
728                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
729
730                         // Should be there
731                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
732
733                         // Then merge it, too
734                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
735
736                         // Should be there
737                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
738
739                         // Copy all
740                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
741
742                         // Set it back
743                         detachedContact.setContactFaxNumber(detachedFax);
744                 }
745
746                 // Get cellphone instance
747                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
748
749                 // Is there a  fax instance set?
750                 if (landLine instanceof DialableLandLineNumber) {
751                         // Debug message
752                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
753
754                         // Then find it, too
755                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
756
757                         // Should be there
758                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
759
760                         // Then merge it, too
761                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
762
763                         // Should be there
764                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
765
766                         // Copy all
767                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
768
769                         // Set it back
770                         detachedContact.setContactLandLineNumber(detachedLandLine);
771                 }
772
773                 // Return updated user instance
774                 return detachedUser;
775         }
776
777 }