]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
Continued a bit:
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookUserSessionBean.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.addressbook.database.BaseAddressbookDatabaseBean;
32 import org.mxchange.jcontacts.contact.Contact;
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 AddressbookUserSessionBean extends BaseAddressbookDatabaseBean 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 AddressbookUserSessionBean () {
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("userName", 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("userName", 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("emailAddress", 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("userName", 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 foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
577
578                 // Should be found!
579                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
580
581                 // Merge user
582                 User detachedUser = this.getEntityManager().merge(foundUser);
583
584                 // Should be found!
585                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
586
587                 // Copy all data
588                 detachedUser.copyAll(user);
589
590                 // Set as updated
591                 detachedUser.setUserUpdated(new GregorianCalendar());
592
593                 // Trace message
594                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
595
596                 // Return updated instance
597                 return detachedUser;
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                         // Abort here
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
614                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
615                 } else if (user.getUserAccountStatus() == null) {
616                         // Throw NPE again
617                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
618                 } else if (!this.ifUserExists(user)) {
619                         // User does not exist
620                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
621                 } else if (null == baseUrl) {
622                         // Abort here
623                         throw new NullPointerException("password is null"); //NOI18N
624                 } else if (baseUrl.isEmpty()) {
625                         // Abort here
626                         throw new IllegalArgumentException("password is empty"); //NOI18N
627                 }
628
629                 // Call other method
630                 User updatedUser = this.updateUserData(user);
631
632                 // Create history entry
633                 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser);
634
635                 // Set created timestamp
636                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
637
638                 // Persist it
639                 this.getEntityManager().persist(entry);
640
641                 // Flush it to get id number back
642                 this.getEntityManager().flush();
643
644                 // Trace message
645                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
646
647                 // Return it
648                 return entry;
649         }
650
651         @Override
652         public User updateUserPersonalData (final User user) {
653                 // Trace message
654                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
655
656                 // user should not be null
657                 if (null == user) {
658                         // Abort here
659                         throw new NullPointerException("user is null"); //NOI18N
660                 } else if (user.getUserId() == null) {
661                         // Throw NPE again
662                         throw new NullPointerException("user.userId is null"); //NOI18N
663                 } else if (user.getUserId() < 1) {
664                         // Not valid
665                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
666                 } else if (user.getUserAccountStatus() == null) {
667                         // Throw NPE again
668                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
669                 } else if (!this.ifUserExists(user)) {
670                         // User does not exist
671                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
672                 }
673
674                 // Find the instance
675                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
676
677                 // Should be found!
678                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
679
680                 // Merge user
681                 User detachedUser = this.getEntityManager().merge(foundUser);
682
683                 // Should be found!
684                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
685
686                 // Copy all data
687                 detachedUser.copyAll(user);
688
689                 // Set as updated
690                 detachedUser.setUserUpdated(new GregorianCalendar());
691                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
692
693                 // Get contact from it and find it
694                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
695
696                 // Should be found
697                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
698
699                 // Debug message
700                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
701
702                 // Merge contact instance
703                 Contact detachedContact = this.getEntityManager().merge(foundContact);
704
705                 // Copy all
706                 detachedContact.copyAll(user.getUserContact());
707
708                 // Set it back in user
709                 user.setUserContact(detachedContact);
710
711                 // Should be found!
712                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
713
714                 // Get mobile instance
715                 DialableMobileNumber mobileNumber = detachedContact.getContactMobileNumber();
716
717                 // Is there a  mobile instance set?
718                 if (mobileNumber instanceof DialableMobileNumber) {
719                         // Debug message
720                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
721
722                         // Then find it, too
723                         DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId());
724
725                         // Should be there
726                         assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N
727
728                         // Then merge it, too
729                         DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile);
730
731                         // Should be there
732                         assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N
733
734                         // Copy all
735                         detachedMobile.copyAll(user.getUserContact().getContactMobileNumber());
736
737                         // Set it back
738                         detachedContact.setContactMobileNumber(detachedMobile);
739                 }
740
741                 // Get mobile instance
742                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
743
744                 // Is there a  fax instance set?
745                 if (fax instanceof DialableFaxNumber) {
746                         // Debug message
747                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
748
749                         // Then find it, too
750                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
751
752                         // Should be there
753                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
754
755                         // Then merge it, too
756                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
757
758                         // Should be there
759                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
760
761                         // Copy all
762                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
763
764                         // Set it back
765                         detachedContact.setContactFaxNumber(detachedFax);
766                 }
767
768                 // Get mobile instance
769                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
770
771                 // Is there a  fax instance set?
772                 if (landLine instanceof DialableLandLineNumber) {
773                         // Debug message
774                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
775
776                         // Then find it, too
777                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
778
779                         // Should be there
780                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
781
782                         // Then merge it, too
783                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
784
785                         // Should be there
786                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
787
788                         // Copy all
789                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
790
791                         // Set it back
792                         detachedContact.setContactLandLineNumber(detachedLandLine);
793                 }
794
795                 // Trace message
796                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
797
798                 // Return updated user instance
799                 return detachedUser;
800         }
801
802 }