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