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