]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/JobsUserSessionBean.java
Continued a bit:
[jjobs-ejb.git] / src / java / org / mxchange / jusercore / model / user / JobsUserSessionBean.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.jjobs.database.BaseJobsDatabaseBean;
30 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
31 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
32 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
33 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
34 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
37 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
38 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
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 JobsUserSessionBean extends BaseJobsDatabaseBean 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 JobsUserSessionBean () {
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                 // Update cellphone, land-line and fax instance
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", LoginUser.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", LoginUser.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", LoginUser.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", LoginUser.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", LoginUser.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 isEmailAddressRegistered (final User user) {
406                 // Trace message
407                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: 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", LoginUser.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("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
427                 } catch (final NoResultException ex) {
428                         // Log it
429                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: 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 isUserNameRegistered (final User user) {
447                 // Trace message
448                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: 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", LoginUser.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("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
468                 } catch (final NoResultException ex) {
469                         // Log it
470                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: 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                 // Trace message
489                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
490
491                 // user should not be null
492                 // @TODO Add check for email address
493                 if (null == user) {
494                         // Abort here
495                         throw new NullPointerException("user is null"); //NOI18N
496                 } else if (user.getUserId() instanceof Long) {
497                         // Id is set
498                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
499                 } else if (user.getUserContact() == null) {
500                         // Throw NPE again
501                         throw new NullPointerException("user.userContact is null"); //NOI18N
502                 } else if (user.getUserContact().getContactId() == null) {
503                         // Throw NPE again
504                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
505                 } else if (user.getUserContact().getContactId() < 1) {
506                         // Not valid id number
507                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
508                 } else if (user.getUserAccountStatus() == null) {
509                         // Throw NPE again
510                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
511                 } else if (this.ifUserNameExists(user.getUserName())) {
512                         // Name already found
513                         throw new UserNameAlreadyRegisteredException(user.getUserName());
514                 } else if (this.ifUserExists(user)) {
515                         // User does not exist
516                         throw new IllegalStateException("User does already exist."); //NOI18N
517                 }
518
519                 // Check if user is registered
520                 if (this.registerBean.isUserNameRegistered(user)) {
521                         // Abort here
522                         throw new UserNameAlreadyRegisteredException(user);
523                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
524                         // Abort here
525                         throw new EmailAddressAlreadyRegisteredException(user);
526                 }
527
528                 // Set created timestamp
529                 user.setUserCreated(new GregorianCalendar());
530
531                 // Try to find the contact instance
532                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
533
534                 // Try to merge it
535                 Contact detachedContact = this.getEntityManager().merge(foundContact);
536
537                 // Set it in user
538                 user.setUserContact(detachedContact);
539
540                 // Persist user
541                 this.getEntityManager().persist(user);
542
543                 // Flush it to get id
544                 this.getEntityManager().flush();
545
546                 // Trace message
547                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N
548
549                 // Return updated instanc
550                 return user;
551         }
552
553         @Override
554         public User updateUserData (final User user) {
555                 // Trace message
556                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
557
558                 // user should not be null
559                 if (null == user) {
560                         // Abort here
561                         throw new NullPointerException("user is null"); //NOI18N
562                 } else if (user.getUserId() == null) {
563                         // Throw NPE again
564                         throw new NullPointerException("user.userId is null"); //NOI18N
565                 } else if (user.getUserId() < 1) {
566                         // Not valid
567                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
568                 } else if (user.getUserAccountStatus() == null) {
569                         // Throw NPE again
570                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
571                 } else if (!this.ifUserExists(user)) {
572                         // User does not exist
573                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
574                 }
575
576                 // Remove contact instance as this is not updatedf
577                 user.setUserContact(null);
578
579                 // Find the instance
580                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
581
582                 // Should be found!
583                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
584
585                 // Merge user
586                 User detachedUser = this.getEntityManager().merge(foundUser);
587
588                 // Should be found!
589                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
590
591                 // Copy all data
592                 detachedUser.copyAll(user);
593
594                 // Set as updated
595                 detachedUser.setUserUpdated(new GregorianCalendar());
596
597                 // Return updated instance
598                 return detachedUser;
599         }
600
601         @Override
602         public User updateUserPersonalData (final User user) {
603                 // Trace message
604                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
605
606                 // user should not be null
607                 if (null == user) {
608                         // Abort here
609                         throw new NullPointerException("user is null"); //NOI18N
610                 } else if (user.getUserId() == null) {
611                         // Throw NPE again
612                         throw new NullPointerException("user.userId is null"); //NOI18N
613                 } else if (user.getUserId() < 1) {
614                         // Not valid
615                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
616                 } else if (user.getUserAccountStatus() == null) {
617                         // Throw NPE again
618                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
619                 } else if (!this.ifUserExists(user)) {
620                         // User does not exist
621                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
622                 }
623
624                 // Find the instance
625                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
626
627                 // Should be found!
628                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
629
630                 // Merge user
631                 User detachedUser = this.getEntityManager().merge(foundUser);
632
633                 // Should be found!
634                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
635
636                 // Copy all data
637                 detachedUser.copyAll(user);
638
639                 // Set as updated
640                 detachedUser.setUserUpdated(new GregorianCalendar());
641                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
642
643                 // Get contact from it and find it
644                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
645
646                 // Should be found
647                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
648
649                 // Debug message
650                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
651
652                 // Merge contact instance
653                 Contact detachedContact = this.getEntityManager().merge(foundContact);
654
655                 // Copy all
656                 detachedContact.copyAll(user.getUserContact());
657
658                 // Set it back in user
659                 user.setUserContact(detachedContact);
660
661                 // Should be found!
662                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
663
664                 // Get cellphone instance
665                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
666
667                 // Is there a  cellphone instance set?
668                 if (cellphone instanceof DialableCellphoneNumber) {
669                         // Debug message
670                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
671
672                         // Then find it, too
673                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
674
675                         // Should be there
676                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
677
678                         // Then merge it, too
679                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
680
681                         // Should be there
682                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
683
684                         // Copy all
685                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
686
687                         // Set it back
688                         detachedContact.setContactCellphoneNumber(detachedCellphone);
689                 }
690
691                 // Get cellphone instance
692                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
693
694                 // Is there a  fax instance set?
695                 if (fax instanceof DialableFaxNumber) {
696                         // Debug message
697                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
698
699                         // Then find it, too
700                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
701
702                         // Should be there
703                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
704
705                         // Then merge it, too
706                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
707
708                         // Should be there
709                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
710
711                         // Copy all
712                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
713
714                         // Set it back
715                         detachedContact.setContactFaxNumber(detachedFax);
716                 }
717
718                 // Get cellphone instance
719                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
720
721                 // Is there a  fax instance set?
722                 if (landLine instanceof DialableLandLineNumber) {
723                         // Debug message
724                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
725
726                         // Then find it, too
727                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
728
729                         // Should be there
730                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
731
732                         // Then merge it, too
733                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
734
735                         // Should be there
736                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
737
738                         // Copy all
739                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
740
741                         // Set it back
742                         detachedContact.setContactLandLineNumber(detachedLandLine);
743                 }
744
745                 // Return updated user instance
746                 return detachedUser;
747         }
748
749 }