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