]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
Cleanup: (don't cherry-pick, repeat it all)
[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.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
30 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
31 import org.mxchange.jusercore.exceptions.UserNotFoundException;
32 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
33 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
34 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
35 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
36
37 /**
38  * A user EJB
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  */
42 @Stateless (name = "user", description = "A bean handling the user data")
43 public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserSessionBeanRemote {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 542_145_347_916L;
49
50         /**
51          * Registration EJB
52          */
53         @EJB
54         private UserRegistrationSessionBeanRemote registerBean;
55
56         /**
57          * Default constructor
58          */
59         public PizzaUserSessionBean () {
60         }
61
62         @Override
63         public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
64                 // Trace message
65                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
66
67                 // userId should not be null
68                 if (null == user) {
69                         // Abort here
70                         throw new NullPointerException("user is null"); //NOI18N
71                 } else if (user.getUserId() == null) {
72                         // Abort here
73                         throw new NullPointerException("user.userId is null"); //NOI18N
74                 } else if (user.getUserId() < 1) {
75                         // Invalid number
76                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {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                 // Set all created timestamps
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", User.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                 // Is the parameter valid?
213                 if (null == userId) {
214                         // Throw NPE
215                         throw new NullPointerException("userId is null"); //NOI18N
216                 } else if (userId < 1) {
217                         // Not valid
218                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
219                 } else if (!this.ifUserIdExists(userId)) {
220                         // Does not exist
221                         throw new UserNotFoundException(userId);
222                 }
223
224                 // Create query instance
225                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
226
227                 // Set user id
228                 query.setParameter("id", userId); //NOI18N
229
230                 // Fetch the result, it should be there by now
231                 User user = (User) query.getSingleResult();
232
233                 // Should be there
234                 assert(user instanceof User) : "user is null"; //NOI18N
235
236                 // Return found user
237                 return user;
238         }
239
240         @Override
241         @SuppressWarnings ("unchecked")
242         public List<String> getEmailAddressList () {
243                 // Get query
244                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
245
246                 // Get result list
247                 List<String> emailAddressList = query.getResultList();
248
249                 // Return it
250                 return emailAddressList;
251         }
252
253         @Override
254         @SuppressWarnings ("unchecked")
255         public List<String> getUserNameList () {
256                 // Get query
257                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
258
259                 // Get result list
260                 List<String> userNameList = query.getResultList();
261
262                 // Return it
263                 return userNameList;
264         }
265
266         @Override
267         public boolean ifUserExists (final User user) {
268                 // Trace message
269                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
270
271                 // userId should not be null
272                 if (null == user) {
273                         // Abort here
274                         throw new NullPointerException("user is null"); //NOI18N
275                 } else if (user.getUserId() == null) {
276                         // Abort here
277                         throw new NullPointerException("user.userId is null"); //NOI18N
278                 } else if (user.getUserId() < 1) {
279                         // Invalid number
280                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
281                 }
282
283                 // Generate query
284                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
285
286                 // Set parameter
287                 query.setParameter("id", user.getUserId()); //NOI18N
288
289                 // Try this
290                 try {
291                         User dummy = (User) query.getSingleResult();
292
293                         // Debug message
294                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
295                 } catch (final NoResultException ex) {
296                         // Log it
297                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
298
299                         // User name does not exist
300                         return false;
301                 } catch (final PersistenceException ex) {
302                         // Something bad happened
303                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
304
305                         // Throw again
306                         throw ex;
307                 }
308
309                 // Trace message
310                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
311
312                 // Found it
313                 return true;
314         }
315
316         @Override
317         public boolean ifUserIdExists (final Long userId) {
318                 // Trace message
319                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
320
321                 // userId should not be null
322                 if (null == userId) {
323                         // Abort here
324                         throw new NullPointerException("userId is null"); //NOI18N
325                 } else if (userId < 1) {
326                         // Invalid number
327                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
328                 }
329
330                 // Generate query
331                 Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N
332
333                 // Set parameter
334                 query.setParameter("id", userId); //NOI18N
335
336                 // Try this
337                 try {
338                         User dummy = (User) query.getSingleResult();
339
340                         // Debug message
341                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
342                 } catch (final NoResultException ex) {
343                         // Log it
344                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
345
346                         // User name does not exist
347                         return false;
348                 } catch (final PersistenceException ex) {
349                         // Something bad happened
350                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
351
352                         // Throw again
353                         throw ex;
354                 }
355
356                 // Trace message
357                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
358
359                 // Found it
360                 return true;
361         }
362
363         @Override
364         public boolean ifUserNameExists (final String userName) {
365                 // Trace message
366                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
367
368                 // userId should not be null
369                 if (null == userName) {
370                         // Abort here
371                         throw new NullPointerException("userName is null"); //NOI18N
372                 } else if (userName.isEmpty()) {
373                         // Abort here
374                         throw new NullPointerException("userName is empty"); //NOI18N
375                 }
376
377                 // Generate query
378                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
379
380                 // Set parameter
381                 query.setParameter("param", userName); //NOI18N
382
383                 // Try this
384                 try {
385                         User dummy = (User) query.getSingleResult();
386
387                         // Debug message
388                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
389                 } catch (final NoResultException ex) {
390                         // Log it
391                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
392
393                         // User name does not exist
394                         return false;
395                 }
396
397                 // Trace message
398                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
399
400                 // Found it
401                 return true;
402         }
403
404         @Override
405         public boolean isEmailAddressReqistered (final User user) {
406                 // Trace message
407                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressReqistered: user={0} - CALLED!", user)); //NOI18N
408
409                 // user should not be null
410                 if (null == user) {
411                         // Abort here
412                         throw new NullPointerException("user is null"); //NOI18N
413                 }
414
415                 // Generate query
416                 Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N
417
418                 // Set parameter
419                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
420
421                 // Search for it
422                 try {
423                         User dummy = (User) query.getSingleResult();
424
425                         // Debug message
426                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
427                 } catch (final NoResultException ex) {
428                         // Log it
429                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
430
431                         // Email address does not exist
432                         return false;
433                 } catch (final PersistenceException ex) {
434                         // Something bad happened
435                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
436
437                         // Throw again
438                         throw ex;
439                 }
440
441                 // Found it
442                 return true;
443         }
444
445         @Override
446         public boolean isUserNameReqistered (final User user) {
447                 // Trace message
448                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameReqistered: user={0} - CALLED!", user)); //NOI18N
449
450                 // user should not be null
451                 if (null == user) {
452                         // Abort here
453                         throw new NullPointerException("user is null"); //NOI18N
454                 }
455
456                 // Generate query
457                 Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N
458
459                 // Set parameter
460                 query.setParameter("param", user.getUserName()); //NOI18N
461
462                 // Try this
463                 try {
464                         User dummy = (User) query.getSingleResult();
465
466                         // Debug message
467                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
468                 } catch (final NoResultException ex) {
469                         // Log it
470                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameReqistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
471
472                         // User name does not exist
473                         return false;
474                 } catch (final PersistenceException ex) {
475                         // Something bad happened
476                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
477
478                         // Throw again
479                         throw ex;
480                 }
481
482                 // Found it
483                 return true;
484         }
485
486         @Override
487         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
488                 // userId should not be null
489                 if (null == user) {
490                         // Abort here
491                         throw new NullPointerException("user is null"); //NOI18N
492                 } else if (user.getUserContact() == null) {
493                         // Throw NPE again
494                         throw new NullPointerException("user.userContact is null"); //NOI18N
495                 } else if (user.getUserContact().getContactId() < 1) {
496                         // Not valid id
497                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
498                 }
499
500                 // Check if user is registered
501                 if (this.registerBean.isUserNameRegistered(user)) {
502                         // Abort here
503                         throw new UserNameAlreadyRegisteredException(user);
504                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
505                         // Abort here
506                         throw new EmailAddressAlreadyRegisteredException(user);
507                 }
508
509                 // Set created timestamp
510                 user.setUserCreated(new GregorianCalendar());
511
512                 // Try to find the contact instance
513                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
514
515                 // Try to merge it
516                 Contact detachedContact = this.getEntityManager().merge(foundContact);
517
518                 // Set it in user
519                 user.setUserContact(detachedContact);
520
521                 // Persist user
522                 this.getEntityManager().persist(user);
523
524                 // Flush it to get id
525                 this.getEntityManager().flush();
526
527                 // Trace message
528                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N
529
530                 // Return updated instance
531                 return user;
532         }
533
534         @Override
535         public User updateUserData (final User user) {
536                 // Trace message
537                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
538
539                 // user should not be null
540                 if (null == user) {
541                         // Abort here
542                         throw new NullPointerException("user is null"); //NOI18N
543                 } else if (user.getUserId() == null) {
544                         // Throw NPE again
545                         throw new NullPointerException("user.userId is null"); //NOI18N
546                 } else if (user.getUserId() < 1) {
547                         // Not valid
548                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
549                 } else if (user.getUserAccountStatus() == null) {
550                         // Throw NPE again
551                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
552                 } else if (!this.ifUserExists(user)) {
553                         // User does not exist
554                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
555                 }
556
557                 // Remove contact instance as this is not updatedf
558                 user.setUserContact(null);
559
560                 // Find the instance
561                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
562
563                 // Should be found!
564                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
565
566                 // Merge user
567                 User detachedUser = this.getEntityManager().merge(foundUser);
568
569                 // Should be found!
570                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
571
572                 // Copy all data
573                 detachedUser.copyAll(user);
574
575                 // Set as updated
576                 detachedUser.setUserUpdated(new GregorianCalendar());
577
578                 // Return updated instance
579                 return detachedUser;
580         }
581
582         @Override
583         public User updateUserPersonalData (final User user) {
584                 // Trace message
585                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
586
587                 // user should not be null
588                 if (null == user) {
589                         // Abort here
590                         throw new NullPointerException("user is null"); //NOI18N
591                 } else if (user.getUserId() == null) {
592                         // Throw NPE again
593                         throw new NullPointerException("user.userId is null"); //NOI18N
594                 } else if (user.getUserId() < 1) {
595                         // Not valid
596                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
597                 } else if (user.getUserAccountStatus() == null) {
598                         // Throw NPE again
599                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
600                 } else if (!this.ifUserExists(user)) {
601                         // User does not exist
602                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
603                 }
604
605                 // Find the instance
606                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
607
608                 // Should be found!
609                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
610
611                 // Merge user
612                 User detachedUser = this.getEntityManager().merge(foundUser);
613
614                 // Should be found!
615                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
616
617                 // Copy all data
618                 detachedUser.copyAll(user);
619
620                 // Set as updated
621                 detachedUser.setUserUpdated(new GregorianCalendar());
622                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
623
624                 // Get contact from it and find it
625                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
626
627                 // Should be found
628                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
629
630                 // Debug message
631                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
632
633                 // Merge contact instance
634                 Contact detachedContact = this.getEntityManager().merge(foundContact);
635
636                 // Copy all
637                 detachedContact.copyAll(user.getUserContact());
638
639                 // Set it back in user
640                 user.setUserContact(detachedContact);
641
642                 // Should be found!
643                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
644
645                 // Get cellphone instance
646                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
647
648                 // Is there a  cellphone instance set?
649                 if (cellphone instanceof DialableCellphoneNumber) {
650                         // Debug message
651                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
652
653                         // Then find it, too
654                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
655
656                         // Should be there
657                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
658
659                         // Then merge it, too
660                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
661
662                         // Should be there
663                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
664
665                         // Copy all
666                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
667
668                         // Set it back
669                         detachedContact.setContactCellphoneNumber(detachedCellphone);
670                 }
671
672                 // Get cellphone instance
673                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
674
675                 // Is there a  fax instance set?
676                 if (fax instanceof DialableFaxNumber) {
677                         // Debug message
678                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
679
680                         // Then find it, too
681                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
682
683                         // Should be there
684                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
685
686                         // Then merge it, too
687                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
688
689                         // Should be there
690                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
691
692                         // Copy all
693                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
694
695                         // Set it back
696                         detachedContact.setContactFaxNumber(detachedFax);
697                 }
698
699                 // Get cellphone instance
700                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
701
702                 // Is there a  fax instance set?
703                 if (landLine instanceof DialableLandLineNumber) {
704                         // Debug message
705                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
706
707                         // Then find it, too
708                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
709
710                         // Should be there
711                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
712
713                         // Then merge it, too
714                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
715
716                         // Should be there
717                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
718
719                         // Copy all
720                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
721
722                         // Set it back
723                         detachedContact.setContactLandLineNumber(detachedLandLine);
724                 }
725
726                 // Return updated user instance
727                 return detachedUser;
728         }
729
730 }