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