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