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