]> 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.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jcontacts.contact.ContactUtils;
33 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
34 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
35 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
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.register.UserRegistrationSessionBeanRemote;
44 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
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();
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                 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                 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                 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                 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<User> allUsers () {
124                 // Trace message
125                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
126
127                 // Get named query
128                 Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
129
130                 // Get result
131                 List<User> users = query.getResultList();
132
133                 // Trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
135
136                 // Return full list
137                 return users;
138         }
139
140         @Override
141         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
142                 // Trace message
143                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
144
145                 // Parameter must be valid
146                 if (null == user) {
147                         // Abort here
148                         throw new NullPointerException("user is null"); //NOI18N
149                 } else if (user.getUserId() == null) {
150                         // Abort here
151                         throw new NullPointerException("user.userId is null"); //NOI18N
152                 } else if (user.getUserId() < 1) {
153                         // Invalid number
154                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
155                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
156                         // Account is already confirmed
157                         throw new UserStatusConfirmedException(user);
158                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
159                         // Account is already confirmed
160                         throw new UserStatusLockedException(user);
161                 } else if (user.getUserConfirmKey() == null) {
162                         // Throw NPE
163                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
164                 } else if (null == baseUrl) {
165                         // Throw it again
166                         throw new NullPointerException("baseUrl is null"); //NOI18N
167                 } else if (baseUrl.isEmpty()) {
168                         // Invalid parameter
169                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
170                 }
171
172                 // Update user status and remove confirmation key
173                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
174                 user.setUserConfirmKey(null);
175                 user.setUserUpdated(new GregorianCalendar());
176
177                 // Update user account
178                 User updatedUser = this.updateUserData(user);
179
180                 // Init variable
181                 Address emailAddress;
182
183                 try {
184                         // Create email address and set
185                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
186                 } catch (final AddressException ex) {
187                         // Throw again
188                         throw new EJBException(ex);
189                 }
190
191                 // Send out email
192                 this.sendEmail("User account confirmed", "user_account_confirmed", emailAddress, updatedUser, baseUrl, null); //NOI18N
193
194                 // Trace message
195                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
196
197                 // Return updated instance
198                 return updatedUser;
199         }
200
201         @Override
202         public User fillUserData (final User user) {
203                 // Trace message
204                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
205
206                 // user should not be null
207                 if (null == user) {
208                         // Abort here
209                         throw new NullPointerException("user is null"); //NOI18N
210                 }
211
212                 // Try to locate it
213                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
214
215                 // Set parameter
216                 query.setParameter("userName", user.getUserName()); //NOI18N
217
218                 // Initialize variable
219                 User foundUser = null;
220
221                 // Try it
222                 try {
223                         // Try to get single result
224                         foundUser = (User) query.getSingleResult();
225                 } catch (final NoResultException ex) {
226                         // Log it
227                         this.getLoggerBeanLocal().logException(ex);
228                 }
229
230                 // Trace message
231                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
232
233                 // Return prepared instance
234                 return foundUser;
235         }
236
237         @Override
238         public User findUserById (final Long userId) throws UserNotFoundException {
239                 // Trace message
240                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
241
242                 // Is the parameter valid?
243                 if (null == userId) {
244                         // Throw NPE
245                         throw new NullPointerException("userId is null"); //NOI18N
246                 } else if (userId < 1) {
247                         // Not valid
248                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
249                 } else if (!this.ifUserIdExists(userId)) {
250                         // Does not exist
251                         throw new UserNotFoundException(userId);
252                 }
253
254                 // Create query instance
255                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
256
257                 // Set user id
258                 query.setParameter("id", userId); //NOI18N
259
260                 // Fetch the result, it should be there by now
261                 User user = (User) query.getSingleResult();
262
263                 // Trace message
264                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
265
266                 // Return found user
267                 return user;
268         }
269
270         @Override
271         public String generateRandomUserName () {
272                 // Trace message
273                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
274
275                 // Get full list
276                 List<String> userList = this.getUserNameList();
277
278                 // Init variable
279                 String userName = null;
280
281                 // Loop until a user name is found
282                 while ((userName == null) || (userList.contains(userName))) {
283                         // Generate random name
284                         userName = UserUtils.generateRandomUserName();
285                 }
286
287                 // Trace message
288                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
289
290                 // Found one, so return it
291                 return userName;
292         }
293
294         @Override
295         @SuppressWarnings ("unchecked")
296         public List<String> getEmailAddressList () {
297                 // Trace message
298                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
299
300                 // Get query
301                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
302
303                 // Get result list
304                 List<String> emailAddressList = query.getResultList();
305
306                 // Trace message
307                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
308
309                 // Return it
310                 return emailAddressList;
311         }
312
313         @Override
314         @SuppressWarnings ("unchecked")
315         public List<String> getUserNameList () {
316                 // Trace message
317                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
318
319                 // Get query
320                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
321
322                 // Get result list
323                 List<String> userNameList = query.getResultList();
324
325                 // Trace message
326                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
327
328                 // Return it
329                 return userNameList;
330         }
331
332         @Override
333         public boolean ifUserExists (final User user) {
334                 // Trace message
335                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
336
337                 // userId should not be null
338                 if (null == user) {
339                         // Abort here
340                         throw new NullPointerException("user is null"); //NOI18N
341                 } else if (user.getUserId() == null) {
342                         // Abort here
343                         throw new NullPointerException("user.userId is null"); //NOI18N
344                 } else if (user.getUserId() < 1) {
345                         // Invalid number
346                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
347                 }
348
349                 // Generate query
350                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
351
352                 // Set parameter
353                 query.setParameter("id", user.getUserId()); //NOI18N
354
355                 // Try this
356                 try {
357                         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                 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                         User dummy = (User) query.getSingleResult();
405
406                         // Debug message
407                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
408                 } catch (final NoResultException ex) {
409                         // Log it
410                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
411
412                         // User name does not exist
413                         return false;
414                 } catch (final PersistenceException ex) {
415                         // Something bad happened
416                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
417
418                         // Throw again
419                         throw ex;
420                 }
421
422                 // Trace message
423                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
424
425                 // Found it
426                 return true;
427         }
428
429         @Override
430         public boolean ifUserNameExists (final String userName) {
431                 // Trace message
432                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
433
434                 // userId should not be null
435                 if (null == userName) {
436                         // Abort here
437                         throw new NullPointerException("userName is null"); //NOI18N
438                 } else if (userName.isEmpty()) {
439                         // Abort here
440                         throw new NullPointerException("userName is empty"); //NOI18N
441                 }
442
443                 // Generate query
444                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
445
446                 // Set parameter
447                 query.setParameter("userName", userName); //NOI18N
448
449                 // Try this
450                 try {
451                         User dummy = (User) query.getSingleResult();
452
453                         // Debug message
454                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
455                 } catch (final NoResultException ex) {
456                         // Log it
457                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
458
459                         // User name does not exist
460                         return false;
461                 }
462
463                 // Trace message
464                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
465
466                 // Found it
467                 return true;
468         }
469
470         @Override
471         public boolean isEmailAddressRegistered (final User user) {
472                 // Trace message
473                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
474
475                 // user should not be null
476                 if (null == user) {
477                         // Abort here
478                         throw new NullPointerException("user is null"); //NOI18N
479                 }
480
481                 // Generate query
482                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
483
484                 // Set parameter
485                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
486
487                 // Search for it
488                 try {
489                         User dummy = (User) query.getSingleResult();
490
491                         // Debug message
492                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
493                 } catch (final NoResultException ex) {
494                         // Log it
495                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
496
497                         // Email address does not exist
498                         return false;
499                 } catch (final PersistenceException ex) {
500                         // Something bad happened
501                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
502
503                         // Throw again
504                         throw ex;
505                 }
506
507                 // Trace message
508                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
509
510                 // Found it
511                 return true;
512         }
513
514         @Override
515         public boolean isUserNameRegistered (final User user) {
516                 // Trace message
517                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
518
519                 // user should not be null
520                 if (null == user) {
521                         // Abort here
522                         throw new NullPointerException("user is null"); //NOI18N
523                 }
524
525                 // Generate query
526                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
527
528                 // Set parameter
529                 query.setParameter("userName", user.getUserName()); //NOI18N
530
531                 // Try this
532                 try {
533                         User dummy = (User) query.getSingleResult();
534
535                         // Debug message
536                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
537                 } catch (final NoResultException ex) {
538                         // Log it
539                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
540
541                         // User name does not exist
542                         return false;
543                 } catch (final PersistenceException ex) {
544                         // Something bad happened
545                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
546
547                         // Throw again
548                         throw ex;
549                 }
550
551                 // Trace message
552                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
553
554                 // Found it
555                 return true;
556         }
557
558         @Override
559         public User updateUserData (final User user) {
560                 // Trace message
561                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
562
563                 // user should not be null
564                 if (null == user) {
565                         // Abort here
566                         throw new NullPointerException("user is null"); //NOI18N
567                 } else if (user.getUserId() == null) {
568                         // Throw NPE again
569                         throw new NullPointerException("user.userId is null"); //NOI18N
570                 } else if (user.getUserId() < 1) {
571                         // Not valid
572                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
573                 } else if (user.getUserAccountStatus() == null) {
574                         // Throw NPE again
575                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
576                 } else if (!this.ifUserExists(user)) {
577                         // User does not exist
578                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
579                 }
580
581                 // Find the instance
582                 User updatedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
583
584                 // Should be found!
585                 assert (updatedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
586
587                 // Copy all data
588                 UserUtils.copyAll(user, updatedUser);
589
590                 // Set as updated
591                 updatedUser.setUserUpdated(new GregorianCalendar());
592
593                 // Trace message
594                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
595
596                 // Return updated instance
597                 return updatedUser;
598         }
599
600         @Override
601         public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
602                 // Trace message
603                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
604
605                 // user should not be null
606                 if (null == user) {
607                         // Throw NPE
608                         throw new NullPointerException("user is null"); //NOI18N
609                 } else if (user.getUserId() == null) {
610                         // Throw NPE again
611                         throw new NullPointerException("user.userId is null"); //NOI18N
612                 } else if (user.getUserId() < 1) {
613                         // Not valid number
614                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
615                 } else if (user.getUserName() == null) {
616                         // Throw NPE again
617                         throw new NullPointerException("user.userName is null"); //NOI18N
618                 } else if (user.getUserName().isEmpty()) {
619                         // Empty string
620                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
621                 } else if (user.getUserAccountStatus() == null) {
622                         // Throw NPE
623                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
624                 } else if (user.getUserContact() == null) {
625                         // Throw it again
626                         throw new NullPointerException("user.userContact is null"); //NOI18N
627                 } else if (user.getUserContact().getContactId() == null) {
628                         // .. and again
629                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
630                 } else if (user.getUserContact().getContactId() < 1) {
631                         // Invalid id
632                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
633                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
634                         // Throw NPE again
635                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
636                 } else if (!this.ifUserExists(user)) {
637                         // User does not exist
638                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
639                 } else if (null == baseUrl) {
640                         // Throw it again
641                         throw new NullPointerException("baseUrl is null"); //NOI18N
642                 } else if (baseUrl.isEmpty()) {
643                         // Invalid parameter
644                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
645                 }
646
647                 // Call other method
648                 User updatedUser = this.updateUserData(user);
649
650                 // Create history entry
651                 PasswordHistory entry = new UserPasswordHistory(updatedUser.getUserEncryptedPassword(), updatedUser);
652
653                 // Set created timestamp
654                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
655
656                 // Merge user to make sure it is not re-persisted
657                 User mergedUser = this.getEntityManager().merge(updatedUser);
658                 entry.setUserPasswordHistoryUser(mergedUser);
659
660                 // Persist it
661                 this.getEntityManager().persist(entry);
662
663                 // Flush it to get id number back
664                 this.getEntityManager().flush();
665
666                 // Init variable
667                 Address emailAddress;
668
669                 try {
670                         // Create email address and set
671                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
672                 } catch (final AddressException ex) {
673                         // Throw again
674                         throw new EJBException(ex);
675                 }
676
677                 // Send email to user
678                 this.sendEmail("User password change", "user_password_change", emailAddress, updatedUser, baseUrl, null); //NOI18N
679
680                 // Trace message
681                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
682
683                 // Return it
684                 return entry;
685         }
686
687         @Override
688         public User updateUserPersonalData (final User user) {
689                 // Trace message
690                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
691
692                 // user should not be null
693                 if (null == user) {
694                         // Abort here
695                         throw new NullPointerException("user is null"); //NOI18N
696                 } else if (user.getUserId() == null) {
697                         // Throw NPE again
698                         throw new NullPointerException("user.userId is null"); //NOI18N
699                 } else if (user.getUserId() < 1) {
700                         // Not valid
701                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
702                 } else if (user.getUserAccountStatus() == null) {
703                         // Throw NPE again
704                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
705                 } else if (!this.ifUserExists(user)) {
706                         // User does not exist
707                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
708                 }
709
710                 // Find the instance
711                 User updatedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
712
713                 // Should be found!
714                 assert (updatedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
715
716                 // Copy all data
717                 UserUtils.copyAll(user, updatedUser);
718
719                 // Set as updated
720                 updatedUser.setUserUpdated(new GregorianCalendar());
721                 updatedUser.getUserContact().setContactUpdated(new GregorianCalendar());
722
723                 // Get contact from it and find it
724                 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
725
726                 // Should be found
727                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
728
729                 // Debug message
730                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
731
732                 // Copy all
733                 ContactUtils.copyAll(user.getUserContact(), managedContact);
734
735                 // Set it back in user
736                 user.setUserContact(managedContact);
737
738                 // Should be found!
739                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
740
741                 // Get mobile instance
742                 DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
743
744                 // Is there a  mobile instance set?
745                 if (mobileNumber instanceof DialableMobileNumber) {
746                         // Debug message
747                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobileNumber.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
748
749                         // Set it back
750                         managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
751                 }
752
753                 // Get mobile instance
754                 DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
755
756                 // Is there a  fax instance set?
757                 if (faxNumber instanceof DialableFaxNumber) {
758                         // Debug message
759                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
760
761                         // Set it back
762                         managedContact.setContactFaxNumber(this.getManaged(faxNumber, faxNumber));
763                 }
764
765                 // Get mobile instance
766                 DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
767
768                 // Is there a  fax instance set?
769                 if (landLineNumber instanceof DialableLandLineNumber) {
770                         // Debug message
771                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
772
773                         // Set it back
774                         managedContact.setContactLandLineNumber(this.getManaged(landLineNumber, landLineNumber));
775                 }
776
777                 // Trace message
778                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
779
780                 // Return updated user instance
781                 return updatedUser;
782         }
783
784 }