]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
Continued a bit: (please cherry-pick)
[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.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
33 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
34 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
35 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
36 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
37 import org.mxchange.jusercore.exceptions.UserNotFoundException;
38 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
39 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
40 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
41 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
42 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
43 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
44
45 /**
46  * A user EJB
47  * <p>
48  * @author Roland Haeder<roland@mxchange.org>
49  */
50 @Stateless (name = "user", description = "A bean handling the user data")
51 public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserSessionBeanRemote {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 542_145_347_916L;
57
58         /**
59          * Registration EJB
60          */
61         @EJB
62         private UserRegistrationSessionBeanRemote registerBean;
63
64         /**
65          * Default constructor
66          */
67         public PizzaUserSessionBean () {
68         }
69
70         @Override
71         public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
72                 // Trace message
73                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N
74
75                 // userId should not be null
76                 if (null == user) {
77                         // Abort here
78                         throw new NullPointerException("user is null"); //NOI18N
79                 } else if (user.getUserId() == null) {
80                         // Abort here
81                         throw new NullPointerException("user.userId is null"); //NOI18N
82                 } else if (user.getUserId() < 1) {
83                         // Invalid number
84                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
85                 }
86
87                 // Check if user is registered
88                 if (this.registerBean.isUserNameRegistered(user)) {
89                         // Abort here
90                         throw new UserNameAlreadyRegisteredException(user);
91                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
92                         // Abort here
93                         throw new EmailAddressAlreadyRegisteredException(user);
94                 }
95
96                 // Set created timestamp
97                 user.setUserCreated(new GregorianCalendar());
98                 user.getUserContact().setContactCreated(new GregorianCalendar());
99
100                 // Update cellphone, land-line and fax instance
101                 this.setAllContactPhoneEntriesCreated(user.getUserContact());
102
103                 // Persist it
104                 this.getEntityManager().persist(user);
105
106                 // Flush to get id back
107                 this.getEntityManager().flush();
108
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.userId={1} - EXIT!", user, user.getUserId())); //NOI18N
111
112                 // Return it
113                 return user;
114         }
115
116         @Override
117         @SuppressWarnings ("unchecked")
118         public List<User> allMemberPublicVisibleUsers () {
119                 // Trace message
120                 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
121
122                 // Get named query
123                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N
124
125                 // Set parameters
126                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
127                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
128                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
129
130                 // Get result
131                 List<User> users = query.getResultList();
132
133                 // Trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
135
136                 // Return full list
137                 return users;
138         }
139
140         @Override
141         @SuppressWarnings ("unchecked")
142         public List<User> allPublicUsers () {
143                 // Trace message
144                 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
145
146                 // Get named query
147                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N
148
149                 // Set parameters
150                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
151                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
152
153                 // Get result
154                 List<User> users = query.getResultList();
155
156                 // Trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
158
159                 // Return full list
160                 return users;
161         }
162
163         @Override
164         @SuppressWarnings ("unchecked")
165         public List<User> allUsers () {
166                 // Trace message
167                 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
168
169                 // Get named query
170                 Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
171
172                 // Get result
173                 List<User> users = query.getResultList();
174
175                 // Trace message
176                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
177
178                 // Return full list
179                 return users;
180         }
181
182         @Override
183         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
184                 // Trace message
185                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("confirmAccount: user={0},baseUrl={1} - CALLED!", user, baseUrl)); //NOI18N
186
187                 // Parameter must be valid
188                 if (null == user) {
189                         // Abort here
190                         throw new NullPointerException("user is null"); //NOI18N
191                 } else if (user.getUserId() == null) {
192                         // Abort here
193                         throw new NullPointerException("user.userId is null"); //NOI18N
194                 } else if (user.getUserId() < 1) {
195                         // Invalid number
196                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
197                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
198                         // Account is already confirmed
199                         throw new UserStatusConfirmedException(user);
200                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
201                         // Account is already confirmed
202                         throw new UserStatusLockedException(user);
203                 } else if (user.getUserConfirmKey() == null) {
204                         // Throw NPE
205                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
206                 }
207
208                 // Update user status and remove confirmation key
209                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
210                 user.setUserConfirmKey(null);
211                 user.setUserUpdated(new GregorianCalendar());
212
213                 // Update user account
214                 User updatedUser = this.updateUserData(user);
215
216                 // Init variable
217                 Address emailAddress;
218
219                 try {
220                         // Create email address and set
221                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
222                 } catch (final AddressException ex) {
223                         // Throw again
224                         throw new EJBException(ex);
225                 }
226
227                 // Send out email
228                 this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
229
230                 // Trace message
231                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("confirmAccount: updatedUser={0} - EXIT!", updatedUser)); //NOI18N
232
233                 // Return updated instance
234                 return updatedUser;
235         }
236
237         @Override
238         public User fillUserData (final User user) {
239                 // Trace message
240                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
241
242                 // user should not be null
243                 if (null == user) {
244                         // Abort here
245                         throw new NullPointerException("user is null"); //NOI18N
246                 }
247
248                 // Try to locate it
249                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
250
251                 // Set parameter
252                 query.setParameter("userName", user.getUserName()); //NOI18N
253
254                 // Initialize variable
255                 User foundUser = null;
256
257                 // Try it
258                 try {
259                         // Try to get single result
260                         foundUser = (User) query.getSingleResult();
261                 } catch (final NoResultException ex) {
262                         // Log it
263                         this.getLoggerBeanLocal().logException(ex);
264                 }
265
266                 // Trace message
267                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
268
269                 // Return prepared instance
270                 return foundUser;
271         }
272
273         @Override
274         public User findUserById (final Long userId) throws UserNotFoundException {
275                 // Trace message
276                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: userId={0} - CALLED!", userId)); //NOI18N
277
278                 // Is the parameter valid?
279                 if (null == userId) {
280                         // Throw NPE
281                         throw new NullPointerException("userId is null"); //NOI18N
282                 } else if (userId < 1) {
283                         // Not valid
284                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
285                 } else if (!this.ifUserIdExists(userId)) {
286                         // Does not exist
287                         throw new UserNotFoundException(userId);
288                 }
289
290                 // Create query instance
291                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
292
293                 // Set user id
294                 query.setParameter("id", userId); //NOI18N
295
296                 // Fetch the result, it should be there by now
297                 User user = (User) query.getSingleResult();
298
299                 // Trace message
300                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: user={0} - EXIT!", user)); //NOI18N
301
302                 // Return found user
303                 return user;
304         }
305
306         @Override
307         public String generateRandomUserName () {
308                 // Trace message
309                 this.getLoggerBeanLocal().logTrace("generateRandomUserName - CALLED!"); //NOI18N
310
311                 // Get full list
312                 List<String> userList = this.getUserNameList();
313
314                 // Init variable
315                 String userName = null;
316
317                 // Loop until a user name is found
318                 while ((userName == null) || (userList.contains(userName))) {
319                         // Generate random name
320                         userName = UserUtils.generateRandomUserName();
321                 }
322
323                 // Trace message
324                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateRandomUserName: userName={0} - EXIT!", userName)); //NOI18N
325
326                 // Found one, so return it
327                 return userName;
328         }
329
330         @Override
331         @SuppressWarnings ("unchecked")
332         public List<String> getEmailAddressList () {
333                 // Get query
334                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
335
336                 // Get result list
337                 List<String> emailAddressList = query.getResultList();
338
339                 // Return it
340                 return emailAddressList;
341         }
342
343         @Override
344         @SuppressWarnings ("unchecked")
345         public List<String> getUserNameList () {
346                 // Get query
347                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
348
349                 // Get result list
350                 List<String> userNameList = query.getResultList();
351
352                 // Return it
353                 return userNameList;
354         }
355
356         @Override
357         public boolean ifUserExists (final User user) {
358                 // Trace message
359                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
360
361                 // userId should not be null
362                 if (null == user) {
363                         // Abort here
364                         throw new NullPointerException("user is null"); //NOI18N
365                 } else if (user.getUserId() == null) {
366                         // Abort here
367                         throw new NullPointerException("user.userId is null"); //NOI18N
368                 } else if (user.getUserId() < 1) {
369                         // Invalid number
370                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
371                 }
372
373                 // Generate query
374                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
375
376                 // Set parameter
377                 query.setParameter("id", user.getUserId()); //NOI18N
378
379                 // Try this
380                 try {
381                         User dummy = (User) query.getSingleResult();
382
383                         // Debug message
384                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
385                 } catch (final NoResultException ex) {
386                         // Log it
387                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
388
389                         // User name does not exist
390                         return false;
391                 } catch (final PersistenceException ex) {
392                         // Something bad happened
393                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
394
395                         // Throw again
396                         throw ex;
397                 }
398
399                 // Trace message
400                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
401
402                 // Found it
403                 return true;
404         }
405
406         @Override
407         public boolean ifUserIdExists (final Long userId) {
408                 // Trace message
409                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
410
411                 // userId should not be null
412                 if (null == userId) {
413                         // Abort here
414                         throw new NullPointerException("userId is null"); //NOI18N
415                 } else if (userId < 1) {
416                         // Invalid number
417                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
418                 }
419
420                 // Generate query
421                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
422
423                 // Set parameter
424                 query.setParameter("id", userId); //NOI18N
425
426                 // Try this
427                 try {
428                         User dummy = (User) query.getSingleResult();
429
430                         // Debug message
431                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
432                 } catch (final NoResultException ex) {
433                         // Log it
434                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
435
436                         // User name does not exist
437                         return false;
438                 } catch (final PersistenceException ex) {
439                         // Something bad happened
440                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
441
442                         // Throw again
443                         throw ex;
444                 }
445
446                 // Trace message
447                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
448
449                 // Found it
450                 return true;
451         }
452
453         @Override
454         public boolean ifUserNameExists (final String userName) {
455                 // Trace message
456                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
457
458                 // userId should not be null
459                 if (null == userName) {
460                         // Abort here
461                         throw new NullPointerException("userName is null"); //NOI18N
462                 } else if (userName.isEmpty()) {
463                         // Abort here
464                         throw new NullPointerException("userName is empty"); //NOI18N
465                 }
466
467                 // Generate query
468                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
469
470                 // Set parameter
471                 query.setParameter("userName", userName); //NOI18N
472
473                 // Try this
474                 try {
475                         User dummy = (User) query.getSingleResult();
476
477                         // Debug message
478                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
479                 } catch (final NoResultException ex) {
480                         // Log it
481                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
482
483                         // User name does not exist
484                         return false;
485                 }
486
487                 // Trace message
488                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
489
490                 // Found it
491                 return true;
492         }
493
494         @Override
495         public boolean isEmailAddressRegistered (final User user) {
496                 // Trace message
497                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
498
499                 // user should not be null
500                 if (null == user) {
501                         // Abort here
502                         throw new NullPointerException("user is null"); //NOI18N
503                 }
504
505                 // Generate query
506                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
507
508                 // Set parameter
509                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
510
511                 // Search for it
512                 try {
513                         User dummy = (User) query.getSingleResult();
514
515                         // Debug message
516                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
517                 } catch (final NoResultException ex) {
518                         // Log it
519                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
520
521                         // Email address does not exist
522                         return false;
523                 } catch (final PersistenceException ex) {
524                         // Something bad happened
525                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
526
527                         // Throw again
528                         throw ex;
529                 }
530
531                 // Found it
532                 return true;
533         }
534
535         @Override
536         public boolean isUserNameRegistered (final User user) {
537                 // Trace message
538                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
539
540                 // user should not be null
541                 if (null == user) {
542                         // Abort here
543                         throw new NullPointerException("user is null"); //NOI18N
544                 }
545
546                 // Generate query
547                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
548
549                 // Set parameter
550                 query.setParameter("userName", user.getUserName()); //NOI18N
551
552                 // Try this
553                 try {
554                         User dummy = (User) query.getSingleResult();
555
556                         // Debug message
557                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
558                 } catch (final NoResultException ex) {
559                         // Log it
560                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
561
562                         // User name does not exist
563                         return false;
564                 } catch (final PersistenceException ex) {
565                         // Something bad happened
566                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
567
568                         // Throw again
569                         throw ex;
570                 }
571
572                 // Found it
573                 return true;
574         }
575
576         @Override
577         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
578                 // Trace message
579                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
580
581                 // user should not be null
582                 if (null == user) {
583                         // Abort here
584                         throw new NullPointerException("user is null"); //NOI18N
585                 } else if (user.getUserId() instanceof Long) {
586                         // Id is set
587                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
588                 } else if (user.getUserContact() == null) {
589                         // Throw NPE again
590                         throw new NullPointerException("user.userContact is null"); //NOI18N
591                 } else if (user.getUserContact().getContactId() == null) {
592                         // Throw NPE again
593                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
594                 } else if (user.getUserContact().getContactId() < 1) {
595                         // Not valid id number
596                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
597                 } else if (user.getUserAccountStatus() == null) {
598                         // Throw NPE again
599                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
600                 } else if (this.ifUserNameExists(user.getUserName())) {
601                         // Name already found
602                         throw new UserNameAlreadyRegisteredException(user.getUserName());
603                 }
604
605                 // Try to find the contact
606                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
607
608                 // Set detached object in rexcruiter instance
609                 user.setUserContact(foundContact);
610
611                 // Set timestamp
612                 user.setUserCreated(new GregorianCalendar());
613
614                 // Perist it
615                 this.getEntityManager().persist(user);
616
617                 // Flush it to get updated instance back
618                 this.getEntityManager().flush();
619
620                 // Log trace message
621                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - EXIT!", user)); //NOI18N
622
623                 // Return updated instanc
624                 return user;
625         }
626
627         @Override
628         public User updateUserData (final User user) {
629                 // Trace message
630                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N
631
632                 // user should not be null
633                 if (null == user) {
634                         // Abort here
635                         throw new NullPointerException("user is null"); //NOI18N
636                 } else if (user.getUserId() == null) {
637                         // Throw NPE again
638                         throw new NullPointerException("user.userId is null"); //NOI18N
639                 } else if (user.getUserId() < 1) {
640                         // Not valid
641                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
642                 } else if (user.getUserAccountStatus() == null) {
643                         // Throw NPE again
644                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
645                 } else if (!this.ifUserExists(user)) {
646                         // User does not exist
647                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
648                 }
649
650                 // Remove contact instance as this is not updatedf
651                 user.setUserContact(null);
652
653                 // Find the instance
654                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
655
656                 // Should be found!
657                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
658
659                 // Merge user
660                 User detachedUser = this.getEntityManager().merge(foundUser);
661
662                 // Should be found!
663                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
664
665                 // Copy all data
666                 detachedUser.copyAll(user);
667
668                 // Set as updated
669                 detachedUser.setUserUpdated(new GregorianCalendar());
670
671                 // Return updated instance
672                 return detachedUser;
673         }
674
675         @Override
676         public User updateUserPersonalData (final User user) {
677                 // Trace message
678                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N
679
680                 // user should not be null
681                 if (null == user) {
682                         // Abort here
683                         throw new NullPointerException("user is null"); //NOI18N
684                 } else if (user.getUserId() == null) {
685                         // Throw NPE again
686                         throw new NullPointerException("user.userId is null"); //NOI18N
687                 } else if (user.getUserId() < 1) {
688                         // Not valid
689                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
690                 } else if (user.getUserAccountStatus() == null) {
691                         // Throw NPE again
692                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
693                 } else if (!this.ifUserExists(user)) {
694                         // User does not exist
695                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
696                 }
697
698                 // Find the instance
699                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
700
701                 // Should be found!
702                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
703
704                 // Merge user
705                 User detachedUser = this.getEntityManager().merge(foundUser);
706
707                 // Should be found!
708                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
709
710                 // Copy all data
711                 detachedUser.copyAll(user);
712
713                 // Set as updated
714                 detachedUser.setUserUpdated(new GregorianCalendar());
715                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
716
717                 // Get contact from it and find it
718                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
719
720                 // Should be found
721                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
722
723                 // Debug message
724                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
725
726                 // Merge contact instance
727                 Contact detachedContact = this.getEntityManager().merge(foundContact);
728
729                 // Copy all
730                 detachedContact.copyAll(user.getUserContact());
731
732                 // Set it back in user
733                 user.setUserContact(detachedContact);
734
735                 // Should be found!
736                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
737
738                 // Get cellphone instance
739                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
740
741                 // Is there a  cellphone instance set?
742                 if (cellphone instanceof DialableCellphoneNumber) {
743                         // Debug message
744                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
745
746                         // Then find it, too
747                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
748
749                         // Should be there
750                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
751
752                         // Then merge it, too
753                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
754
755                         // Should be there
756                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
757
758                         // Copy all
759                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
760
761                         // Set it back
762                         detachedContact.setContactCellphoneNumber(detachedCellphone);
763                 }
764
765                 // Get cellphone instance
766                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
767
768                 // Is there a  fax instance set?
769                 if (fax instanceof DialableFaxNumber) {
770                         // Debug message
771                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
772
773                         // Then find it, too
774                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
775
776                         // Should be there
777                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
778
779                         // Then merge it, too
780                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
781
782                         // Should be there
783                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
784
785                         // Copy all
786                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
787
788                         // Set it back
789                         detachedContact.setContactFaxNumber(detachedFax);
790                 }
791
792                 // Get cellphone instance
793                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
794
795                 // Is there a  fax instance set?
796                 if (landLine instanceof DialableLandLineNumber) {
797                         // Debug message
798                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
799
800                         // Then find it, too
801                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
802
803                         // Should be there
804                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
805
806                         // Then merge it, too
807                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
808
809                         // Should be there
810                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
811
812                         // Copy all
813                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
814
815                         // Set it back
816                         detachedContact.setContactLandLineNumber(detachedLandLine);
817                 }
818
819                 // Return updated user instance
820                 return detachedUser;
821         }
822
823 }