]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
Please 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.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.persistence.NoResultException;
26 import javax.persistence.PersistenceException;
27 import javax.persistence.Query;
28 import org.mxchange.jcontacts.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.database.BasePizzaDatabaseBean;
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 BasePizzaDatabaseBean 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 GregorianCalendar());
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 User findUserById (final Long userId) throws UserNotFoundException {
257                 // Trace message
258                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
259
260                 // Is the parameter valid?
261                 if (null == userId) {
262                         // Throw NPE
263                         throw new NullPointerException("userId is null"); //NOI18N
264                 } else if (userId < 1) {
265                         // Not valid
266                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
267                 } else if (!this.ifUserIdExists(userId)) {
268                         // Does not exist
269                         throw new UserNotFoundException(userId);
270                 }
271
272                 // Create query instance
273                 final Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
274
275                 // Set user id
276                 query.setParameter("id", userId); //NOI18N
277
278                 // Fetch the result, it should be there by now
279                 final User user = (User) query.getSingleResult();
280
281                 // Trace message
282                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
283
284                 // Return found user
285                 return user;
286         }
287
288         @Override
289         public String generateRandomUserName () {
290                 // Trace message
291                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
292
293                 // Get full list
294                 final List<String> userList = this.allUserNames();
295
296                 // Init variable
297                 String userName = null;
298
299                 // Loop until a user name is found
300                 while ((userName == null) || (userList.contains(userName))) {
301                         // Generate random name
302                         userName = UserUtils.generateRandomUserName();
303                 }
304
305                 // Trace message
306                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
307
308                 // Found one, so return it
309                 return userName;
310         }
311
312         @Override
313         @SuppressWarnings ("unchecked")
314         public List<String> getEmailAddressList () {
315                 // Trace message
316                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
317
318                 // Get query
319                 final Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
320
321                 // Get result list
322                 final List<String> emailAddressList = query.getResultList();
323
324                 // Trace message
325                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
326
327                 // Return it
328                 return emailAddressList;
329         }
330
331         @Override
332         public boolean ifUserExists (final User user) {
333                 // Trace message
334                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
335
336                 // userId should not be null
337                 if (null == user) {
338                         // Abort here
339                         throw new NullPointerException("user is null"); //NOI18N
340                 } else if (user.getUserId() == null) {
341                         // Abort here
342                         throw new NullPointerException("user.userId is null"); //NOI18N
343                 } else if (user.getUserId() < 1) {
344                         // Invalid number
345                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
346                 }
347
348                 // Generate query
349                 final Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
350
351                 // Set parameter
352                 query.setParameter("id", user.getUserId()); //NOI18N
353
354                 // Try this
355                 try {
356                         // Try to get single result
357                         final User dummy = (User) query.getSingleResult();
358
359                         // Debug message
360                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
361                 } catch (final NoResultException ex) {
362                         // Log it
363                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
364
365                         // User name does not exist
366                         return false;
367                 } catch (final PersistenceException ex) {
368                         // Something bad happened
369                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
370
371                         // Throw again
372                         throw ex;
373                 }
374
375                 // Trace message
376                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
377
378                 // Found it
379                 return true;
380         }
381
382         @Override
383         public boolean ifUserIdExists (final Long userId) {
384                 // Trace message
385                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
386
387                 // userId should not be null
388                 if (null == userId) {
389                         // Abort here
390                         throw new NullPointerException("userId is null"); //NOI18N
391                 } else if (userId < 1) {
392                         // Invalid number
393                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
394                 }
395
396                 // Generate query
397                 final Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
398
399                 // Set parameter
400                 query.setParameter("id", userId); //NOI18N
401
402                 // Try this
403                 try {
404                         // Try to get single result
405                         final User dummy = (User) query.getSingleResult();
406
407                         // Debug message
408                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
409                 } catch (final NoResultException ex) {
410                         // Log it
411                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
412
413                         // User name does not exist
414                         return false;
415                 } catch (final PersistenceException ex) {
416                         // Something bad happened
417                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
418
419                         // Throw again
420                         throw ex;
421                 }
422
423                 // Trace message
424                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
425
426                 // Found it
427                 return true;
428         }
429
430         @Override
431         public boolean ifUserNameExists (final String userName) {
432                 // Trace message
433                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
434
435                 // userId should not be null
436                 if (null == userName) {
437                         // Abort here
438                         throw new NullPointerException("userName is null"); //NOI18N
439                 } else if (userName.isEmpty()) {
440                         // Abort here
441                         throw new NullPointerException("userName is empty"); //NOI18N
442                 }
443
444                 // Generate query
445                 final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
446
447                 // Set parameter
448                 query.setParameter("userName", userName); //NOI18N
449
450                 // Try this
451                 try {
452                         // Try to get single result
453                         final User dummy = (User) query.getSingleResult();
454
455                         // Debug message
456                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
457                 } catch (final NoResultException ex) {
458                         // Log it
459                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
460
461                         // User name does not exist
462                         return false;
463                 }
464
465                 // Trace message
466                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
467
468                 // Found it
469                 return true;
470         }
471
472         @Override
473         public boolean isEmailAddressRegistered (final User user) {
474                 // Trace message
475                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
476
477                 // user should not be null
478                 if (null == user) {
479                         // Abort here
480                         throw new NullPointerException("user is null"); //NOI18N
481                 }
482
483                 // Generate query
484                 final Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
485
486                 // Set parameter
487                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
488
489                 // Search for it
490                 try {
491                         // Try to get single result
492                         final User dummy = (User) query.getSingleResult();
493
494                         // Debug message
495                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
496                 } catch (final NoResultException ex) {
497                         // Log it
498                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
499
500                         // Email address does not exist
501                         return false;
502                 } catch (final PersistenceException ex) {
503                         // Something bad happened
504                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
505
506                         // Throw again
507                         throw ex;
508                 }
509
510                 // Trace message
511                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
512
513                 // Found it
514                 return true;
515         }
516
517         @Override
518         public boolean isUserNameRegistered (final User user) {
519                 // Trace message
520                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
521
522                 // user should not be null
523                 if (null == user) {
524                         // Abort here
525                         throw new NullPointerException("user is null"); //NOI18N
526                 }
527
528                 // Generate query
529                 final Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
530
531                 // Set parameter
532                 query.setParameter("userName", user.getUserName()); //NOI18N
533
534                 // Try this
535                 try {
536                         // Try to get single result
537                         final User dummy = (User) query.getSingleResult();
538
539                         // Debug message
540                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
541                 } catch (final NoResultException ex) {
542                         // Log it
543                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
544
545                         // User name does not exist
546                         return false;
547                 } catch (final PersistenceException ex) {
548                         // Something bad happened
549                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
550
551                         // Throw again
552                         throw ex;
553                 }
554
555                 // Trace message
556                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
557
558                 // Found it
559                 return true;
560         }
561
562         @Override
563         public User updateUserData (final User user) {
564                 // Trace message
565                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
566
567                 // user should not be null
568                 if (null == user) {
569                         // Abort here
570                         throw new NullPointerException("user is null"); //NOI18N
571                 } else if (user.getUserId() == null) {
572                         // Throw NPE again
573                         throw new NullPointerException("user.userId is null"); //NOI18N
574                 } else if (user.getUserId() < 1) {
575                         // Not valid
576                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
577                 } else if (user.getUserAccountStatus() == null) {
578                         // Throw NPE again
579                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
580                 } else if (!this.ifUserExists(user)) {
581                         // User does not exist
582                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
583                 }
584
585                 // Find the instance
586                 final User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
587
588                 // Should be found!
589                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
590
591                 // Merge user
592                 final User managedUser = this.getEntityManager().merge(foundUser);
593
594                 // Should be found!
595                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", managedUser.getUserId()); //NOI18N
596
597                 // Copy all data
598                 UserUtils.copyAll(user, managedUser);
599
600                 // Set as updated
601                 managedUser.setUserUpdated(new GregorianCalendar());
602
603                 // Trace message
604                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
605
606                 // Return updated instance
607                 return managedUser;
608         }
609
610         @Override
611         public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
612                 // Trace message
613                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
614
615                 // user should not be null
616                 if (null == user) {
617                         // Throw NPE
618                         throw new NullPointerException("user is null"); //NOI18N
619                 } else if (user.getUserId() == null) {
620                         // Throw NPE again
621                         throw new NullPointerException("user.userId is null"); //NOI18N
622                 } else if (user.getUserId() < 1) {
623                         // Not valid number
624                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
625                 } else if (user.getUserName() == null) {
626                         // Throw NPE again
627                         throw new NullPointerException("user.userName is null"); //NOI18N
628                 } else if (user.getUserName().isEmpty()) {
629                         // Empty string
630                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
631                 } else if (user.getUserAccountStatus() == null) {
632                         // Throw NPE
633                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
634                 } else if (user.getUserContact() == null) {
635                         // Throw it again
636                         throw new NullPointerException("user.userContact is null"); //NOI18N
637                 } else if (user.getUserContact().getContactId() == null) {
638                         // .. and again
639                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
640                 } else if (user.getUserContact().getContactId() < 1) {
641                         // Invalid id
642                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
643                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
644                         // Throw NPE again
645                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
646                 } else if (!this.ifUserExists(user)) {
647                         // User does not exist
648                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
649                 } else if (null == baseUrl) {
650                         // Throw it again
651                         throw new NullPointerException("baseUrl is null"); //NOI18N
652                 } else if (baseUrl.isEmpty()) {
653                         // Invalid parameter
654                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
655                 }
656
657                 // Call other method
658                 final User managedUser = this.updateUserData(user);
659
660                 // Update user account
661                 managedUser.setUserUpdated(new GregorianCalendar());
662
663                 // Create history entry
664                 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), managedUser);
665
666                 // Set created timestamp
667                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
668
669                 // Merge user to make sure it is not re-persisted
670                 final User mergedUser = this.getEntityManager().merge(managedUser);
671                 entry.setUserPasswordHistoryUser(mergedUser);
672
673                 // Persist it
674                 this.getEntityManager().persist(entry);
675
676                 // Send email to user
677                 this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
678
679                 // Trace message
680                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
681
682                 // Return it
683                 return entry;
684         }
685
686         @Override
687         public User updateUserPersonalData (final User user) {
688                 // Trace message
689                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
690
691                 // user should not be null
692                 if (null == user) {
693                         // Abort here
694                         throw new NullPointerException("user is null"); //NOI18N
695                 } else if (user.getUserId() == null) {
696                         // Throw NPE again
697                         throw new NullPointerException("user.userId is null"); //NOI18N
698                 } else if (user.getUserId() < 1) {
699                         // Not valid
700                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
701                 } else if (user.getUserAccountStatus() == null) {
702                         // Throw NPE again
703                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
704                 } else if (!this.ifUserExists(user)) {
705                         // User does not exist
706                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
707                 }
708
709                 // Find the instance
710                 final User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
711
712                 // Should be found!
713                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
714
715                 // Copy all data
716                 UserUtils.copyAll(user, managedUser);
717
718                 // Set as updated
719                 managedUser.setUserUpdated(new GregorianCalendar());
720                 managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
721
722                 // Get contact from it and find it
723                 final Contact foundContact = this.getEntityManager().find(managedUser.getUserContact().getClass(), managedUser.getUserContact().getContactId());
724
725                 // Should be found
726                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
727
728                 // Debug message
729                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
730
731                 // Merge contact instance
732                 final Contact managedContact = this.getEntityManager().merge(foundContact);
733
734                 // Copy all
735                 ContactUtils.copyAll(user.getUserContact(), managedContact);
736
737                 // Set it back in user
738                 user.setUserContact(managedContact);
739
740                 // Should be found!
741                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
742
743                 // Get mobile instance
744                 final DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
745
746                 // Is there a  mobile instance set?
747                 if (mobileNumber instanceof DialableMobileNumber) {
748                         // Debug message
749                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
750
751                         // Then find it, too
752                         final DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId());
753
754                         // Should be there
755                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
756
757                         // Then merge it, too
758                         final DialableMobileNumber managedMobile = this.getEntityManager().merge(foundMobile);
759
760                         // Should be there
761                         assert (managedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", managedMobile.getPhoneId()); //NOI18N
762
763                         // Copy all
764                         MobileNumbers.copyAll(managedUser.getUserContact().getContactMobileNumber(), managedMobile);
765
766                         // Set it back
767                         managedContact.setContactMobileNumber(this.createManaged(mobileNumber, mobileNumber));
768                 }
769
770                 // Get mobile instance
771                 final DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
772
773                 // Is there a  fax instance set?
774                 if (faxNumber instanceof DialableFaxNumber) {
775                         // Debug message
776                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
777
778                         // Then find it, too
779                         final DialableFaxNumber foundFax = this.getEntityManager().find(faxNumber.getClass(), faxNumber.getPhoneId());
780
781                         // Should be there
782                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
783
784                         // Then merge it, too
785                         final DialableFaxNumber managedFax = this.getEntityManager().merge(foundFax);
786
787                         // Should be there
788                         assert (managedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", managedFax.getPhoneId()); //NOI18N
789
790                         // Copy all
791                         FaxNumbers.copyAll(managedUser.getUserContact().getContactFaxNumber(), managedFax);
792
793                         // Set it back
794                         managedContact.setContactFaxNumber(managedFax);
795                 }
796
797                 // Get mobile instance
798                 final DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
799
800                 // Is there a  fax instance set?
801                 if (landLineNumber instanceof DialableLandLineNumber) {
802                         // Debug message
803                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
804
805                         // Then find it, too
806                         final DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLineNumber.getClass(), landLineNumber.getPhoneId());
807
808                         // Should be there
809                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
810
811                         // Then merge it, too
812                         final DialableLandLineNumber managedLandLine = this.getEntityManager().merge(foundLandLine);
813
814                         // Should be there
815                         assert (managedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", managedLandLine.getPhoneId()); //NOI18N
816
817                         // Copy all
818                         LandLineNumbers.copyAll(managedUser.getUserContact().getContactLandLineNumber(), managedLandLine);
819
820                         // Set it back
821                         managedContact.setContactLandLineNumber(managedLandLine);
822                 }
823
824                 // Trace message
825                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
826
827                 // Return updated user instance
828                 return managedUser;
829         }
830
831 }