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