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