]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
Implemented business method confirmAccount()
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookUserSessionBean.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.addressbook.database.BaseAddressbookDatabaseBean;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
34 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
35 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
36 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
37 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
38 import org.mxchange.jusercore.exceptions.UserNotFoundException;
39 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
40 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
41 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
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 AddressbookUserSessionBean extends BaseAddressbookDatabaseBean 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 AddressbookUserSessionBean () {
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                 // user 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                         // Not allowed here
81                         throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
82                 }
83
84                 // Check if user is registered
85                 if (this.registerBean.isUserNameRegistered(user)) {
86                         // Abort here
87                         throw new UserNameAlreadyRegisteredException(user);
88                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
89                         // Abort here
90                         throw new EmailAddressAlreadyRegisteredException(user);
91                 }
92
93                 // Set created timestamp
94                 user.setUserCreated(new GregorianCalendar());
95                 user.getUserContact().setContactCreated(new GregorianCalendar());
96
97                 // Update cellphone, land-line and fax instance
98                 this.setAllContactPhoneEntriesCreated(user.getUserContact());
99
100                 // Persist it
101                 this.getEntityManager().persist(user);
102
103                 // Flush to get id back
104                 this.getEntityManager().flush();
105
106                 // Trace message
107                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N
108
109                 // Return it
110                 return user;
111         }
112
113         @Override
114         @SuppressWarnings ("unchecked")
115         public List<User> allMemberPublicVisibleUsers () {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace("allMemberPublicVisibleUsers: CALLED!"); //NOI18N
118
119                 // Get named query
120                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", List.class); //NOI18N
121
122                 // Set parameters
123                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
124                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
125                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
126
127                 // Get result
128                 List<User> users = query.getResultList();
129
130                 // Trace message
131                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allMemberPublicVisibleUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
132
133                 // Return full list
134                 return users;
135         }
136
137         @Override
138         @SuppressWarnings ("unchecked")
139         public List<User> allPublicUsers () {
140                 // Trace message
141                 this.getLoggerBeanLocal().logTrace("allPublicUsers: CALLED!"); //NOI18N
142
143                 // Get named query
144                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", List.class); //NOI18N
145
146                 // Set parameters
147                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
148                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
149
150                 // Get result
151                 List<User> users = query.getResultList();
152
153                 // Trace message
154                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allPublicUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
155
156                 // Return full list
157                 return users;
158         }
159
160         @Override
161         @SuppressWarnings ("unchecked")
162         public List<User> allUsers () {
163                 // Trace message
164                 this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N
165
166                 // Get named query
167                 Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N
168
169                 // Get result
170                 List<User> users = query.getResultList();
171
172                 // Trace message
173                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N
174
175                 // Return full list
176                 return users;
177         }
178
179         @Override
180         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
181                 // Trace message
182                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("confirmAccount: user={0},baseUrl={1} - CALLED!", user, baseUrl)); //NOI18N
183
184                 // Parameter must be valid
185                 if (null == user) {
186                         // Abort here
187                         throw new NullPointerException("user is null"); //NOI18N
188                 } else if (user.getUserId() == null) {
189                         // Abort here
190                         throw new NullPointerException("user.userId is null"); //NOI18N
191                 } else if (user.getUserId() < 1) {
192                         // Invalid number
193                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
194                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
195                         // Account is already confirmed
196                         throw new UserStatusConfirmedException(user);
197                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
198                         // Account is already confirmed
199                         throw new UserStatusLockedException(user);
200                 } else if (user.getUserConfirmKey() == null) {
201                         // Throw NPE
202                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
203                 }
204
205                 // Update user status and remove confirmation key
206                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
207                 user.setUserConfirmKey(null);
208                 user.setUserUpdated(new GregorianCalendar());
209
210                 // Update user account
211                 User updatedUser = this.updateUserData(user);
212
213                 // Init variable
214                 Address emailAddress;
215
216                 try {
217                         // Create email address and set
218                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
219                 } catch (final AddressException ex) {
220                         // Throw again
221                         throw new EJBException(ex);
222                 }
223
224                 // Send out email
225                 this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
226
227                 // Trace message
228                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("confirmAccount: updatedUser={0} - EXIT!", updatedUser)); //NOI18N
229
230                 // Return updated instance
231                 return updatedUser;
232         }
233
234         @Override
235         public User fillUserData (final User user) {
236                 // Trace message
237                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: user={0} - CALLED!", user)); //NOI18N
238
239                 // user should not be null
240                 if (null == user) {
241                         // Abort here
242                         throw new NullPointerException("user is null"); //NOI18N
243                 }
244
245                 // Try to locate it
246                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
247
248                 // Set parameter
249                 query.setParameter("userName", user.getUserName()); //NOI18N
250
251                 // Initialize variable
252                 User foundUser = null;
253
254                 // Try it
255                 try {
256                         // Try to get single result
257                         foundUser = (User) query.getSingleResult();
258                 } catch (final NoResultException ex) {
259                         // Log it
260                         this.getLoggerBeanLocal().logException(ex);
261                 }
262
263                 // Trace message
264                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("fillUserData: foundUser={0} - EXIT!", foundUser)); //NOI18N
265
266                 // Return prepared instance
267                 return foundUser;
268         }
269
270         @Override
271         public User findUserById (final Long userId) throws UserNotFoundException {
272                 // Trace message
273                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: userId={0} - CALLED!", userId)); //NOI18N
274
275                 // Is the parameter valid?
276                 if (null == userId) {
277                         // Throw NPE
278                         throw new NullPointerException("userId is null"); //NOI18N
279                 } else if (userId < 1) {
280                         // Not valid
281                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
282                 } else if (!this.ifUserIdExists(userId)) {
283                         // Does not exist
284                         throw new UserNotFoundException(userId);
285                 }
286
287                 // Create query instance
288                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
289
290                 // Set user id
291                 query.setParameter("id", userId); //NOI18N
292
293                 // Fetch the result, it should be there by now
294                 User user = (User) query.getSingleResult();
295
296                 // Trace message
297                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("findUserById: user={0} - EXIT!", user)); //NOI18N
298
299                 // Return found user
300                 return user;
301         }
302
303         @Override
304         public String generateRandomUserName () {
305                 // Trace message
306                 this.getLoggerBeanLocal().logTrace("generateRandomUserName - CALLED!"); //NOI18N
307
308                 // Get full list
309                 List<String> userList = this.getUserNameList();
310
311                 // Init variable
312                 String userName = null;
313
314                 // Loop until a user name is found
315                 while ((userName == null) || (userList.contains(userName))) {
316                         // Generate random name
317                         userName = UserUtils.generateRandomUserName();
318                 }
319
320                 // Trace message
321                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("generateRandomUserName: userName={0} - EXIT!", userName)); //NOI18N
322
323                 // Found one, so return it
324                 return userName;
325         }
326
327         @Override
328         @SuppressWarnings ("unchecked")
329         public List<String> getEmailAddressList () {
330                 // Get query
331                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
332
333                 // Get result list
334                 List<String> emailAddressList = query.getResultList();
335
336                 // Return it
337                 return emailAddressList;
338         }
339
340         @Override
341         @SuppressWarnings ("unchecked")
342         public List<String> getUserNameList () {
343                 // Get query
344                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
345
346                 // Get result list
347                 List<String> userNameList = query.getResultList();
348
349                 // Return it
350                 return userNameList;
351         }
352
353         @Override
354         public boolean ifUserExists (final User user) {
355                 // Trace message
356                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
357
358                 // userId should not be null
359                 if (null == user) {
360                         // Abort here
361                         throw new NullPointerException("user is null"); //NOI18N
362                 } else if (user.getUserId() == null) {
363                         // Abort here
364                         throw new NullPointerException("user.userId is null"); //NOI18N
365                 } else if (user.getUserId() < 1) {
366                         // Invalid number
367                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
368                 }
369
370                 // Generate query
371                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
372
373                 // Set parameter
374                 query.setParameter("id", user.getUserId()); //NOI18N
375
376                 // Try this
377                 try {
378                         User dummy = (User) query.getSingleResult();
379
380                         // Debug message
381                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
382                 } catch (final NoResultException ex) {
383                         // Log it
384                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
385
386                         // User name does not exist
387                         return false;
388                 } catch (final PersistenceException ex) {
389                         // Something bad happened
390                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
391
392                         // Throw again
393                         throw ex;
394                 }
395
396                 // Trace message
397                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
398
399                 // Found it
400                 return true;
401         }
402
403         @Override
404         public boolean ifUserIdExists (final Long userId) {
405                 // Trace message
406                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: userId={0} - CALLED!", userId)); //NOI18N
407
408                 // userId should not be null
409                 if (null == userId) {
410                         // Abort here
411                         throw new NullPointerException("userId is null"); //NOI18N
412                 } else if (userId < 1) {
413                         // Invalid number
414                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
415                 }
416
417                 // Generate query
418                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
419
420                 // Set parameter
421                 query.setParameter("id", userId); //NOI18N
422
423                 // Try this
424                 try {
425                         User dummy = (User) query.getSingleResult();
426
427                         // Debug message
428                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
429                 } catch (final NoResultException ex) {
430                         // Log it
431                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserIdExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
432
433                         // User name does not exist
434                         return false;
435                 } catch (final PersistenceException ex) {
436                         // Something bad happened
437                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
438
439                         // Throw again
440                         throw ex;
441                 }
442
443                 // Trace message
444                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserIdExists: Found user id {0} - EXIT!", userId)); //NOI18N
445
446                 // Found it
447                 return true;
448         }
449
450         @Override
451         public boolean ifUserNameExists (final String userName) {
452                 // Trace message
453                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N
454
455                 // userId should not be null
456                 if (null == userName) {
457                         // Abort here
458                         throw new NullPointerException("userName is null"); //NOI18N
459                 } else if (userName.isEmpty()) {
460                         // Abort here
461                         throw new NullPointerException("userName is empty"); //NOI18N
462                 }
463
464                 // Generate query
465                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
466
467                 // Set parameter
468                 query.setParameter("userName", userName); //NOI18N
469
470                 // Try this
471                 try {
472                         User dummy = (User) query.getSingleResult();
473
474                         // Debug message
475                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
476                 } catch (final NoResultException ex) {
477                         // Log it
478                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
479
480                         // User name does not exist
481                         return false;
482                 }
483
484                 // Trace message
485                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N
486
487                 // Found it
488                 return true;
489         }
490
491         @Override
492         public boolean isEmailAddressRegistered (final User user) {
493                 // Trace message
494                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isEmailAddressRegistered: user={0} - CALLED!", user)); //NOI18N
495
496                 // user should not be null
497                 if (null == user) {
498                         // Abort here
499                         throw new NullPointerException("user is null"); //NOI18N
500                 }
501
502                 // Generate query
503                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
504
505                 // Set parameter
506                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
507
508                 // Search for it
509                 try {
510                         User dummy = (User) query.getSingleResult();
511
512                         // Debug message
513                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
514                 } catch (final NoResultException ex) {
515                         // Log it
516                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isEmailAddressRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
517
518                         // Email address does not exist
519                         return false;
520                 } catch (final PersistenceException ex) {
521                         // Something bad happened
522                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
523
524                         // Throw again
525                         throw ex;
526                 }
527
528                 // Found it
529                 return true;
530         }
531
532         @Override
533         public boolean isUserNameRegistered (final User user) {
534                 // Trace message
535                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserNameRegistered: user={0} - CALLED!", user)); //NOI18N
536
537                 // user should not be null
538                 if (null == user) {
539                         // Abort here
540                         throw new NullPointerException("user is null"); //NOI18N
541                 }
542
543                 // Generate query
544                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
545
546                 // Set parameter
547                 query.setParameter("userName", user.getUserName()); //NOI18N
548
549                 // Try this
550                 try {
551                         User dummy = (User) query.getSingleResult();
552
553                         // Debug message
554                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: dummy.id={0} found.", dummy.getUserId())); //NOI18N
555                 } catch (final NoResultException ex) {
556                         // Log it
557                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserNameRegistered: getSingleResult() returned no result: {0}", ex)); //NOI18N
558
559                         // User name does not exist
560                         return false;
561                 } catch (final PersistenceException ex) {
562                         // Something bad happened
563                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
564
565                         // Throw again
566                         throw ex;
567                 }
568
569                 // Found it
570                 return true;
571         }
572
573         @Override
574         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
575                 // Trace message
576                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user={0} - CALLED!", user)); //NOI18N
577
578                 // user should not be null
579                 if (null == user) {
580                         // Abort here
581                         throw new NullPointerException("user is null"); //NOI18N
582                 } else if (user.getUserId() instanceof Long) {
583                         // Id is set
584                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
585                 } else if (user.getUserContact() == null) {
586                         // Throw NPE again
587                         throw new NullPointerException("user.userContact is null"); //NOI18N
588                 } else if (user.getUserContact().getContactId() == null) {
589                         // Throw NPE again
590                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
591                 } else if (user.getUserContact().getContactId() < 1) {
592                         // Not valid id number
593                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
594                 } else if (user.getUserAccountStatus() == null) {
595                         // Throw NPE again
596                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
597                 } else if (this.ifUserNameExists(user.getUserName())) {
598                         // Name already found
599                         throw new UserNameAlreadyRegisteredException(user.getUserName());
600                 } else if (this.ifUserExists(user)) {
601                         // User does not exist
602                         throw new IllegalStateException("User does already exist."); //NOI18N
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 }