]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
Please cherry-pick:
[pizzaservice-ejb.git] / src / java / org / mxchange / jusercore / model / user / PizzaUserSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
33 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
34 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
35 import org.mxchange.jusercore.exceptions.UserNotFoundException;
36 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
37 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
38 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
39 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
40 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
41 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
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 PizzaUserSessionBean extends BasePizzaDatabaseBean 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 PizzaUserSessionBean () {
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("user.userId={0} is not valid", 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("user.userId={0} is not valid", 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.userId={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={0} is not valid", 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                 // Find the instance
581                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
582
583                 // Should be found!
584                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
585
586                 // Copy all data
587                 managedUser.copyAll(user);
588
589                 // Set as updated
590                 managedUser.setUserUpdated(new GregorianCalendar());
591
592                 // Trace message
593                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
594
595                 // Return updated instance
596                 return managedUser;
597         }
598
599         @Override
600         public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
601                 // Trace message
602                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
603
604                 // user should not be null
605                 if (null == user) {
606                         // Throw NPE
607                         throw new NullPointerException("user is null"); //NOI18N
608                 } else if (user.getUserId() == null) {
609                         // Throw NPE again
610                         throw new NullPointerException("user.userId is null"); //NOI18N
611                 } else if (user.getUserId() < 1) {
612                         // Not valid number
613                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
614                 } else if (user.getUserName() == null) {
615                         // Throw NPE again
616                         throw new NullPointerException("user.userName is null"); //NOI18N
617                 } else if (user.getUserName().isEmpty()) {
618                         // Empty string
619                         throw new IllegalArgumentException("user.userName is empty"); //NOI18N
620                 } else if (user.getUserAccountStatus() == null) {
621                         // Throw NPE
622                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
623                 } else if (user.getUserContact() == null) {
624                         // Throw it again
625                         throw new NullPointerException("user.userContact is null"); //NOI18N
626                 } else if (user.getUserContact().getContactId() == null) {
627                         // .. and again
628                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
629                 } else if (user.getUserContact().getContactId() < 1) {
630                         // Invalid id
631                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N
632                 } else if (user.getUserContact().getContactPersonalTitle() == null) {
633                         // Throw NPE again
634                         throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N
635                 } else if (!this.ifUserExists(user)) {
636                         // User does not exist
637                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
638                 } else if (null == baseUrl) {
639                         // Throw it again
640                         throw new NullPointerException("baseUrl is null"); //NOI18N
641                 } else if (baseUrl.isEmpty()) {
642                         // Invalid parameter
643                         throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
644                 }
645
646                 // Call other method
647                 User managedUser = this.updateUserData(user);
648
649                 // Create history entry
650                 PasswordHistory entry = new UserPasswordHistory(managedUser.getUserEncryptedPassword(), managedUser);
651
652                 // Set created timestamp
653                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
654
655                 // Merge user to make sure it is not re-persisted
656                 User mergedUser = this.getEntityManager().merge(managedUser);
657                 entry.setUserPasswordHistoryUser(mergedUser);
658
659                 // Persist it
660                 this.getEntityManager().persist(entry);
661
662                 // Flush it to get id number back
663                 this.getEntityManager().flush();
664
665                 // Init variable
666                 Address emailAddress;
667
668                 try {
669                         // Create email address and set
670                         emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
671                 } catch (final AddressException ex) {
672                         // Throw again
673                         throw new EJBException(ex);
674                 }
675
676                 // Send email to user
677                 this.sendEmail("User password change", "user_password_change", emailAddress, user, baseUrl); //NOI18N
678
679                 // Trace message
680                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
681
682                 // Return it
683                 return entry;
684         }
685
686         @Override
687         public User updateUserPersonalData (final User user) {
688                 // Trace message
689                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
690
691                 // user should not be null
692                 if (null == user) {
693                         // Abort here
694                         throw new NullPointerException("user is null"); //NOI18N
695                 } else if (user.getUserId() == null) {
696                         // Throw NPE again
697                         throw new NullPointerException("user.userId is null"); //NOI18N
698                 } else if (user.getUserId() < 1) {
699                         // Not valid
700                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
701                 } else if (user.getUserAccountStatus() == null) {
702                         // Throw NPE again
703                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
704                 } else if (!this.ifUserExists(user)) {
705                         // User does not exist
706                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
707                 }
708
709                 // Find the instance
710                 User managedUser = this.getEntityManager().find(user.getClass(), user.getUserId());
711
712                 // Should be found!
713                 assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
714
715                 // Copy all data
716                 managedUser.copyAll(user);
717
718                 // Set as updated
719                 managedUser.setUserUpdated(new GregorianCalendar());
720                 managedUser.getUserContact().setContactUpdated(new GregorianCalendar());
721
722                 // Get contact from it and find it
723                 Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
724
725                 // Should be found
726                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
727
728                 // Debug message
729                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: managedContact.contactId={0}", managedContact.getContactId())); //NOI18N
730
731                 // Copy all
732                 managedContact.copyAll(user.getUserContact());
733
734                 // Set it back in user
735                 user.setUserContact(managedContact);
736
737                 // Should be found!
738                 assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
739
740                 // Get mobile instance
741                 DialableMobileNumber mobileNumber = managedContact.getContactMobileNumber();
742
743                 // Is there a  mobile instance set?
744                 if (mobileNumber instanceof DialableMobileNumber) {
745                         // Debug message
746                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobileNumber.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N
747
748                         // Set it back
749                         managedContact.setContactMobileNumber(this.getManaged(mobileNumber, mobileNumber));
750                 }
751
752                 // Get mobile instance
753                 DialableFaxNumber faxNumber = managedContact.getContactFaxNumber();
754
755                 // Is there a  fax instance set?
756                 if (faxNumber instanceof DialableFaxNumber) {
757                         // Debug message
758                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: faxNumber.phoneId={0} is being updated ...", faxNumber.getPhoneId())); //NOI18N
759
760                         // Set it back
761                         managedContact.setContactFaxNumber(this.getManaged(faxNumber, faxNumber));
762                 }
763
764                 // Get mobile instance
765                 DialableLandLineNumber landLineNumber = managedContact.getContactLandLineNumber();
766
767                 // Is there a  fax instance set?
768                 if (landLineNumber instanceof DialableLandLineNumber) {
769                         // Debug message
770                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLineNumber.phoneId={0} is being updated ...", landLineNumber.getPhoneId())); //NOI18N
771
772                         // Set it back
773                         managedContact.setContactLandLineNumber(this.getManaged(landLineNumber, landLineNumber));
774                 }
775
776                 // Trace message
777                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
778
779                 // Return updated user instance
780                 return managedUser;
781         }
782
783 }