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