]> 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 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.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 Haeder<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                 }
162
163                 // Update user status and remove confirmation key
164                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
165                 user.setUserConfirmKey(null);
166                 user.setUserUpdated(new GregorianCalendar());
167
168                 // Update user account
169                 User updatedUser = this.updateUserData(user);
170
171                 // Init variable
172                 Address emailAddress;
173
174                 try {
175                         // Create email address and set
176                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
177                 } catch (final AddressException ex) {
178                         // Throw again
179                         throw new EJBException(ex);
180                 }
181
182                 // Send out email
183                 this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
184
185                 // Trace message
186                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
187
188                 // Return updated instance
189                 return updatedUser;
190         }
191
192         @Override
193         public User fillUserData (final User user) {
194                 // Trace message
195                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
196
197                 // user should not be null
198                 if (null == user) {
199                         // Abort here
200                         throw new NullPointerException("user is null"); //NOI18N
201                 }
202
203                 // Try to locate it
204                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
205
206                 // Set parameter
207                 query.setParameter("param", user.getUserName()); //NOI18N
208
209                 // Initialize variable
210                 User foundUser = null;
211
212                 // Try it
213                 try {
214                         // Try to get single result
215                         foundUser = (User) query.getSingleResult();
216                 } catch (final NoResultException ex) {
217                         // Log it
218                         this.getLoggerBeanLocal().logException(ex);
219                 }
220
221                 // Trace message
222                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
223
224                 // Return prepared instance
225                 return foundUser;
226         }
227
228         @Override
229         public User findUserById (final Long userId) throws UserNotFoundException {
230                 // Trace message
231                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
232
233                 // Is the parameter valid?
234                 if (null == userId) {
235                         // Throw NPE
236                         throw new NullPointerException("userId is null"); //NOI18N
237                 } else if (userId < 1) {
238                         // Not valid
239                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
240                 } else if (!this.ifUserIdExists(userId)) {
241                         // Does not exist
242                         throw new UserNotFoundException(userId);
243                 }
244
245                 // Create query instance
246                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
247
248                 // Set user id
249                 query.setParameter("id", userId); //NOI18N
250
251                 // Fetch the result, it should be there by now
252                 User user = (User) query.getSingleResult();
253
254                 // Trace message
255                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
256
257                 // Return found user
258                 return user;
259         }
260
261         @Override
262         public String generateRandomUserName () {
263                 // Trace message
264                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
265
266                 // Get full list
267                 List<String> userList = this.getUserNameList();
268
269                 // Init variable
270                 String userName = null;
271
272                 // Loop until a user name is found
273                 while ((userName == null) || (userList.contains(userName))) {
274                         // Generate random name
275                         userName = UserUtils.generateRandomUserName();
276                 }
277
278                 // Trace message
279                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
280
281                 // Found one, so return it
282                 return userName;
283         }
284
285         @Override
286         @SuppressWarnings ("unchecked")
287         public List<String> getEmailAddressList () {
288                 // Trace message
289                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
290
291                 // Get query
292                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
293
294                 // Get result list
295                 List<String> emailAddressList = query.getResultList();
296
297                 // Trace message
298                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
299
300                 // Return it
301                 return emailAddressList;
302         }
303
304         @Override
305         @SuppressWarnings ("unchecked")
306         public List<String> getUserNameList () {
307                 // Trace message
308                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
309
310                 // Get query
311                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
312
313                 // Get result list
314                 List<String> userNameList = query.getResultList();
315
316                 // Trace message
317                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
318
319                 // Return it
320                 return userNameList;
321         }
322
323         @Override
324         public boolean ifUserExists (final User user) {
325                 // Trace message
326                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
327
328                 // userId should not be null
329                 if (null == user) {
330                         // Abort here
331                         throw new NullPointerException("user is null"); //NOI18N
332                 } else if (user.getUserId() == null) {
333                         // Abort here
334                         throw new NullPointerException("user.userId is null"); //NOI18N
335                 } else if (user.getUserId() < 1) {
336                         // Invalid number
337                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
338                 }
339
340                 // Generate query
341                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
342
343                 // Set parameter
344                 query.setParameter("id", user.getUserId()); //NOI18N
345
346                 // Try this
347                 try {
348                         User dummy = (User) query.getSingleResult();
349
350                         // Debug message
351                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
352                 } catch (final NoResultException ex) {
353                         // Log it
354                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
355
356                         // User name does not exist
357                         return false;
358                 } catch (final PersistenceException ex) {
359                         // Something bad happened
360                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
361
362                         // Throw again
363                         throw ex;
364                 }
365
366                 // Trace message
367                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
368
369                 // Found it
370                 return true;
371         }
372
373         @Override
374         public boolean ifUserIdExists (final Long userId) {
375                 // Trace message
376                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
377
378                 // userId should not be null
379                 if (null == userId) {
380                         // Abort here
381                         throw new NullPointerException("userId is null"); //NOI18N
382                 } else if (userId < 1) {
383                         // Invalid number
384                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
385                 }
386
387                 // Generate query
388                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
389
390                 // Set parameter
391                 query.setParameter("id", userId); //NOI18N
392
393                 // Try this
394                 try {
395                         User dummy = (User) query.getSingleResult();
396
397                         // Debug message
398                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
399                 } catch (final NoResultException ex) {
400                         // Log it
401                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
402
403                         // User name does not exist
404                         return false;
405                 } catch (final PersistenceException ex) {
406                         // Something bad happened
407                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
408
409                         // Throw again
410                         throw ex;
411                 }
412
413                 // Trace message
414                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
415
416                 // Found it
417                 return true;
418         }
419
420         @Override
421         public boolean ifUserNameExists (final String userName) {
422                 // Trace message
423                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
424
425                 // userId should not be null
426                 if (null == userName) {
427                         // Abort here
428                         throw new NullPointerException("userName is null"); //NOI18N
429                 } else if (userName.isEmpty()) {
430                         // Abort here
431                         throw new NullPointerException("userName is empty"); //NOI18N
432                 }
433
434                 // Generate query
435                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
436
437                 // Set parameter
438                 query.setParameter("param", userName); //NOI18N
439
440                 // Try this
441                 try {
442                         User dummy = (User) query.getSingleResult();
443
444                         // Debug message
445                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
446                 } catch (final NoResultException ex) {
447                         // Log it
448                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
449
450                         // User name does not exist
451                         return false;
452                 }
453
454                 // Trace message
455                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
456
457                 // Found it
458                 return true;
459         }
460
461         @Override
462         public boolean isEmailAddressRegistered (final User user) {
463                 // Trace message
464                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
465
466                 // user should not be null
467                 if (null == user) {
468                         // Abort here
469                         throw new NullPointerException("user is null"); //NOI18N
470                 }
471
472                 // Generate query
473                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
474
475                 // Set parameter
476                 query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N
477
478                 // Search for it
479                 try {
480                         User dummy = (User) query.getSingleResult();
481
482                         // Debug message
483                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
484                 } catch (final NoResultException ex) {
485                         // Log it
486                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
487
488                         // Email address does not exist
489                         return false;
490                 } catch (final PersistenceException ex) {
491                         // Something bad happened
492                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
493
494                         // Throw again
495                         throw ex;
496                 }
497
498                 // Trace message
499                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
500
501                 // Found it
502                 return true;
503         }
504
505         @Override
506         public boolean isUserNameRegistered (final User user) {
507                 // Trace message
508                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
509
510                 // user should not be null
511                 if (null == user) {
512                         // Abort here
513                         throw new NullPointerException("user is null"); //NOI18N
514                 }
515
516                 // Generate query
517                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
518
519                 // Set parameter
520                 query.setParameter("param", user.getUserName()); //NOI18N
521
522                 // Try this
523                 try {
524                         User dummy = (User) query.getSingleResult();
525
526                         // Debug message
527                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
528                 } catch (final NoResultException ex) {
529                         // Log it
530                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
531
532                         // User name does not exist
533                         return false;
534                 } catch (final PersistenceException ex) {
535                         // Something bad happened
536                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
537
538                         // Throw again
539                         throw ex;
540                 }
541
542                 // Trace message
543                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
544
545                 // Found it
546                 return true;
547         }
548
549         @Override
550         public User updateUserData (final User user) {
551                 // Trace message
552                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
553
554                 // user should not be null
555                 if (null == user) {
556                         // Abort here
557                         throw new NullPointerException("user is null"); //NOI18N
558                 } else if (user.getUserId() == null) {
559                         // Throw NPE again
560                         throw new NullPointerException("user.userId is null"); //NOI18N
561                 } else if (user.getUserId() < 1) {
562                         // Not valid
563                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
564                 } else if (user.getUserAccountStatus() == null) {
565                         // Throw NPE again
566                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
567                 } else if (!this.ifUserExists(user)) {
568                         // User does not exist
569                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
570                 }
571
572                 // Remove contact instance as this is not updated
573                 user.setUserContact(null);
574
575                 // Find the instance
576                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
577
578                 // Should be found!
579                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
580
581                 // Copy all data
582                 managedUser.copyAll(user);
583
584                 // Set as updated
585                 managedUser.setUserUpdated(new GregorianCalendar());
586
587                 // Trace message
588                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - CALLED!", this.getClass().getSimpleName(), managedUser)); //NOI18N
589
590                 // Return updated instance
591                 return managedUser;
592         }
593
594         @Override
595         public PasswordHistory updateUserPassword (final User user) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
596                 // Trace message
597                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
598
599                 // user should not be null
600                 if (null == user) {
601                         // Abort here
602                         throw new NullPointerException("user is null"); //NOI18N
603                 } else if (user.getUserId() == null) {
604                         // Throw NPE again
605                         throw new NullPointerException("user.userId is null"); //NOI18N
606                 } else if (user.getUserId() < 1) {
607                         // Not valid
608                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
609                 } else if (user.getUserAccountStatus() == null) {
610                         // Throw NPE again
611                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
612                 } else if (!this.ifUserExists(user)) {
613                         // User does not exist
614                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
615                 }
616
617                 // Call other method
618                 User updatedUser = this.updateUserData(user);
619
620                 // Create history entry
621                 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser);
622
623                 // Set created timestamp
624                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
625
626                 // Persist it
627                 this.getEntityManager().persist(entry);
628
629                 // Flush it to get id number back
630                 this.getEntityManager().flush();
631
632                 // Trace message
633                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
634
635                 // Return it
636                 return entry;
637         }
638
639         @Override
640         public User updateUserPersonalData (final User user) {
641                 // Trace message
642                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
643
644                 // user should not be null
645                 if (null == user) {
646                         // Abort here
647                         throw new NullPointerException("user is null"); //NOI18N
648                 } else if (user.getUserId() == null) {
649                         // Throw NPE again
650                         throw new NullPointerException("user.userId is null"); //NOI18N
651                 } else if (user.getUserId() < 1) {
652                         // Not valid
653                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
654                 } else if (user.getUserAccountStatus() == null) {
655                         // Throw NPE again
656                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
657                 } else if (!this.ifUserExists(user)) {
658                         // User does not exist
659                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
660                 }
661
662                 // Find the instance
663                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
664
665                 // Should be found!
666                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
667
668                 // Copy all data
669                 managedUser.copyAll(user);
670
671                 // Set as updated
672                 managedUser.setUserUpdated(new GregorianCalendar());
673                 managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
674
675                 // Get contact from it and find it
676                 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
677
678                 // Should be found
679                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
680
681                 // Debug message
682                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
683
684                 // Copy all
685                 managedContact.copyAll(user.getUserContact());
686
687                 // Set it back in user
688                 user.setUserContact(managedContact);
689
690                 // Should be found!
691                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
692
693                 // Get mobile instance
694                 DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
695
696                 // Is there a  mobile instance set?
697                 if (mobileNumber instanceof DialableMobileNumber) {
698                         // Debug message
699                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobileNumber.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
700
701                         // Set it back
702                         managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
703                 }
704
705                 // Get mobile instance
706                 DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
707
708                 // Is there a  fax instance set?
709                 if (faxNumber instanceof DialableFaxNumber) {
710                         // Debug message
711                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
712
713                         // Set it back
714                         managedContact.setContactFaxNumber(this.getManaged(faxNumber, faxNumber));
715                 }
716
717                 // Get mobile instance
718                 DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
719
720                 // Is there a  fax instance set?
721                 if (landLineNumber instanceof DialableLandLineNumber) {
722                         // Debug message
723                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
724
725                         // Set it back
726                         managedContact.setContactLandLineNumber(this.getManaged(landLineNumber, landLineNumber));
727                 }
728
729                 // Trace message
730                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
731
732                 // Return updated user instance
733                 return managedUser;
734         }
735
736 }