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