]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java
Continued with logging: (please cherry-pick)
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / AddressbookUserSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.EJB;
23 import javax.ejb.EJBException;
24 import javax.ejb.Stateless;
25 import javax.mail.Address;
26 import javax.mail.internet.AddressException;
27 import javax.mail.internet.InternetAddress;
28 import javax.persistence.NoResultException;
29 import javax.persistence.PersistenceException;
30 import javax.persistence.Query;
31 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
32 import org.mxchange.jcontacts.contact.Contact;
33 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
34 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
35 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
36 import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
37 import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
38 import org.mxchange.jusercore.exceptions.UserNotFoundException;
39 import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
40 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
41 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
42 import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote;
43 import org.mxchange.jusercore.model.user.password_history.PasswordHistory;
44 import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory;
45 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
46 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
47
48 /**
49  * A user EJB
50  * <p>
51  * @author Roland Haeder<roland@mxchange.org>
52  */
53 @Stateless (name = "user", description = "A bean handling the user data")
54 public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean implements UserSessionBeanRemote {
55
56         /**
57          * Serial number
58          */
59         private static final long serialVersionUID = 542_145_347_916L;
60
61         /**
62          * Registration EJB
63          */
64         @EJB
65         private UserRegistrationSessionBeanRemote registerBean;
66
67         /**
68          * Default constructor
69          */
70         public AddressbookUserSessionBean () {
71         }
72
73         @Override
74         public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
75                 // Trace message
76                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
77
78                 // user should not be null
79                 if (null == user) {
80                         // Abort here
81                         throw new NullPointerException("user is null"); //NOI18N
82                 } else if (user.getUserId() != null) {
83                         // Not allowed here
84                         throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
85                 }
86
87                 // Check if user is registered
88                 if (this.registerBean.isUserNameRegistered(user)) {
89                         // Abort here
90                         throw new UserNameAlreadyRegisteredException(user);
91                 } else if (this.registerBean.isEmailAddressRegistered(user)) {
92                         // Abort here
93                         throw new EmailAddressAlreadyRegisteredException(user);
94                 }
95
96                 // Set created timestamp
97                 user.setUserCreated(new GregorianCalendar());
98                 user.getUserContact().setContactCreated(new GregorianCalendar());
99
100                 // Update cellphone, land-line and fax instance
101                 this.setAllContactPhoneEntriesCreated(user.getUserContact());
102
103                 // Persist it
104                 this.getEntityManager().persist(user);
105
106                 // Flush to get id back
107                 this.getEntityManager().flush();
108
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
111
112                 // Return it
113                 return user;
114         }
115
116         @Override
117         @SuppressWarnings ("unchecked")
118         public List<User> allMemberPublicVisibleUsers () {
119                 // Trace message
120                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
121
122                 // Get named query
123                 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N
124
125                 // Set parameters
126                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
127                 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
128                 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
129
130                 // Get result
131                 List<User> users = query.getResultList();
132
133                 // Trace message
134                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
135
136                 // Return full list
137                 return users;
138         }
139
140         @Override
141         @SuppressWarnings ("unchecked")
142         public List<User> allPublicUsers () {
143                 // Trace message
144                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
145
146                 // Get named query
147                 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N
148
149                 // Set parameters
150                 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
151                 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
152
153                 // Get result
154                 List<User> users = query.getResultList();
155
156                 // Trace message
157                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
158
159                 // Return full list
160                 return users;
161         }
162
163         @Override
164         @SuppressWarnings ("unchecked")
165         public List<User> allUsers () {
166                 // Trace message
167                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
168
169                 // Get named query
170                 Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
171
172                 // Get result
173                 List<User> users = query.getResultList();
174
175                 // Trace message
176                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
177
178                 // Return full list
179                 return users;
180         }
181
182         @Override
183         public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
184                 // Trace message
185                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
186
187                 // Parameter must be valid
188                 if (null == user) {
189                         // Abort here
190                         throw new NullPointerException("user is null"); //NOI18N
191                 } else if (user.getUserId() == null) {
192                         // Abort here
193                         throw new NullPointerException("user.userId is null"); //NOI18N
194                 } else if (user.getUserId() < 1) {
195                         // Invalid number
196                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
197                 } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
198                         // Account is already confirmed
199                         throw new UserStatusConfirmedException(user);
200                 } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
201                         // Account is already confirmed
202                         throw new UserStatusLockedException(user);
203                 } else if (user.getUserConfirmKey() == null) {
204                         // Throw NPE
205                         throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
206                 }
207
208                 // Update user status and remove confirmation key
209                 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
210                 user.setUserConfirmKey(null);
211                 user.setUserUpdated(new GregorianCalendar());
212
213                 // Update user account
214                 User updatedUser = this.updateUserData(user);
215
216                 // Init variable
217                 Address emailAddress;
218
219                 try {
220                         // Create email address and set
221                         emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
222                 } catch (final AddressException ex) {
223                         // Throw again
224                         throw new EJBException(ex);
225                 }
226
227                 // Send out email
228                 this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
229
230                 // Trace message
231                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
232
233                 // Return updated instance
234                 return updatedUser;
235         }
236
237         @Override
238         public User fillUserData (final User user) {
239                 // Trace message
240                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
241
242                 // user should not be null
243                 if (null == user) {
244                         // Abort here
245                         throw new NullPointerException("user is null"); //NOI18N
246                 }
247
248                 // Try to locate it
249                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
250
251                 // Set parameter
252                 query.setParameter("userName", user.getUserName()); //NOI18N
253
254                 // Initialize variable
255                 User foundUser = null;
256
257                 // Try it
258                 try {
259                         // Try to get single result
260                         foundUser = (User) query.getSingleResult();
261                 } catch (final NoResultException ex) {
262                         // Log it
263                         this.getLoggerBeanLocal().logException(ex);
264                 }
265
266                 // Trace message
267                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
268
269                 // Return prepared instance
270                 return foundUser;
271         }
272
273         @Override
274         public User findUserById (final Long userId) throws UserNotFoundException {
275                 // Trace message
276                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
277
278                 // Is the parameter valid?
279                 if (null == userId) {
280                         // Throw NPE
281                         throw new NullPointerException("userId is null"); //NOI18N
282                 } else if (userId < 1) {
283                         // Not valid
284                         throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N
285                 } else if (!this.ifUserIdExists(userId)) {
286                         // Does not exist
287                         throw new UserNotFoundException(userId);
288                 }
289
290                 // Create query instance
291                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
292
293                 // Set user id
294                 query.setParameter("id", userId); //NOI18N
295
296                 // Fetch the result, it should be there by now
297                 User user = (User) query.getSingleResult();
298
299                 // Trace message
300                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
301
302                 // Return found user
303                 return user;
304         }
305
306         @Override
307         public String generateRandomUserName () {
308                 // Trace message
309                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
310
311                 // Get full list
312                 List<String> userList = this.getUserNameList();
313
314                 // Init variable
315                 String userName = null;
316
317                 // Loop until a user name is found
318                 while ((userName == null) || (userList.contains(userName))) {
319                         // Generate random name
320                         userName = UserUtils.generateRandomUserName();
321                 }
322
323                 // Trace message
324                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
325
326                 // Found one, so return it
327                 return userName;
328         }
329
330         @Override
331         @SuppressWarnings ("unchecked")
332         public List<String> getEmailAddressList () {
333                 // Trace message
334                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
335
336                 // Get query
337                 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
338
339                 // Get result list
340                 List<String> emailAddressList = query.getResultList();
341
342                 // Trace message
343                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
344
345                 // Return it
346                 return emailAddressList;
347         }
348
349         @Override
350         @SuppressWarnings ("unchecked")
351         public List<String> getUserNameList () {
352                 // Trace message
353                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
354
355                 // Get query
356                 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
357
358                 // Get result list
359                 List<String> userNameList = query.getResultList();
360
361                 // Trace message
362                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
363
364                 // Return it
365                 return userNameList;
366         }
367
368         @Override
369         public boolean ifUserExists (final User user) {
370                 // Trace message
371                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
372
373                 // userId should not be null
374                 if (null == user) {
375                         // Abort here
376                         throw new NullPointerException("user is null"); //NOI18N
377                 } else if (user.getUserId() == null) {
378                         // Abort here
379                         throw new NullPointerException("user.userId is null"); //NOI18N
380                 } else if (user.getUserId() < 1) {
381                         // Invalid number
382                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
383                 }
384
385                 // Generate query
386                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
387
388                 // Set parameter
389                 query.setParameter("id", user.getUserId()); //NOI18N
390
391                 // Try this
392                 try {
393                         User dummy = (User) query.getSingleResult();
394
395                         // Debug message
396                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
397                 } catch (final NoResultException ex) {
398                         // Log it
399                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", 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 {0} found.", user, ex)); //NOI18N
406
407                         // Throw again
408                         throw ex;
409                 }
410
411                 // Trace message
412                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
413
414                 // Found it
415                 return true;
416         }
417
418         @Override
419         public boolean ifUserIdExists (final Long userId) {
420                 // Trace message
421                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
422
423                 // userId should not be null
424                 if (null == userId) {
425                         // Abort here
426                         throw new NullPointerException("userId is null"); //NOI18N
427                 } else if (userId < 1) {
428                         // Invalid number
429                         throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N
430                 }
431
432                 // Generate query
433                 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
434
435                 // Set parameter
436                 query.setParameter("id", userId); //NOI18N
437
438                 // Try this
439                 try {
440                         User dummy = (User) query.getSingleResult();
441
442                         // Debug message
443                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: 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}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
447
448                         // User name does not exist
449                         return false;
450                 } catch (final PersistenceException ex) {
451                         // Something bad happened
452                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
453
454                         // Throw again
455                         throw ex;
456                 }
457
458                 // Trace message
459                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
460
461                 // Found it
462                 return true;
463         }
464
465         @Override
466         public boolean ifUserNameExists (final String userName) {
467                 // Trace message
468                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
469
470                 // userId should not be null
471                 if (null == userName) {
472                         // Abort here
473                         throw new NullPointerException("userName is null"); //NOI18N
474                 } else if (userName.isEmpty()) {
475                         // Abort here
476                         throw new NullPointerException("userName is empty"); //NOI18N
477                 }
478
479                 // Generate query
480                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
481
482                 // Set parameter
483                 query.setParameter("userName", userName); //NOI18N
484
485                 // Try this
486                 try {
487                         User dummy = (User) query.getSingleResult();
488
489                         // Debug message
490                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
491                 } catch (final NoResultException ex) {
492                         // Log it
493                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
494
495                         // User name does not exist
496                         return false;
497                 }
498
499                 // Trace message
500                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
501
502                 // Found it
503                 return true;
504         }
505
506         @Override
507         public boolean isEmailAddressRegistered (final User user) {
508                 // Trace message
509                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
510
511                 // user should not be null
512                 if (null == user) {
513                         // Abort here
514                         throw new NullPointerException("user is null"); //NOI18N
515                 }
516
517                 // Generate query
518                 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
519
520                 // Set parameter
521                 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
522
523                 // Search for it
524                 try {
525                         User dummy = (User) query.getSingleResult();
526
527                         // Debug message
528                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
529                 } catch (final NoResultException ex) {
530                         // Log it
531                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
532
533                         // Email address does not exist
534                         return false;
535                 } catch (final PersistenceException ex) {
536                         // Something bad happened
537                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
538
539                         // Throw again
540                         throw ex;
541                 }
542
543                 // Trace message
544                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
545
546                 // Found it
547                 return true;
548         }
549
550         @Override
551         public boolean isUserNameRegistered (final User user) {
552                 // Trace message
553                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
554
555                 // user should not be null
556                 if (null == user) {
557                         // Abort here
558                         throw new NullPointerException("user is null"); //NOI18N
559                 }
560
561                 // Generate query
562                 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
563
564                 // Set parameter
565                 query.setParameter("userName", user.getUserName()); //NOI18N
566
567                 // Try this
568                 try {
569                         User dummy = (User) query.getSingleResult();
570
571                         // Debug message
572                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
573                 } catch (final NoResultException ex) {
574                         // Log it
575                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
576
577                         // User name does not exist
578                         return false;
579                 } catch (final PersistenceException ex) {
580                         // Something bad happened
581                         this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
582
583                         // Throw again
584                         throw ex;
585                 }
586
587                 // Trace message
588                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
589
590                 // Found it
591                 return true;
592         }
593
594         @Override
595         public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
596                 // Trace message
597                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
598
599                 // user should not be null
600                 if (null == user) {
601                         // Abort here
602                         throw new NullPointerException("user is null"); //NOI18N
603                 } else if (user.getUserId() instanceof Long) {
604                         // Id is set
605                         throw new IllegalArgumentException("user.userId is not null"); //NOI18N
606                 } else if (user.getUserContact() == null) {
607                         // Throw NPE again
608                         throw new NullPointerException("user.userContact is null"); //NOI18N
609                 } else if (user.getUserContact().getContactId() == null) {
610                         // Throw NPE again
611                         throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
612                 } else if (user.getUserContact().getContactId() < 1) {
613                         // Not valid id number
614                         throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
615                 } else if (user.getUserAccountStatus() == null) {
616                         // Throw NPE again
617                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
618                 } else if (this.ifUserNameExists(user.getUserName())) {
619                         // Name already found
620                         throw new UserNameAlreadyRegisteredException(user.getUserName());
621                 } else if (this.ifUserExists(user)) {
622                         // User does not exist
623                         throw new IllegalStateException("User does already exist."); //NOI18N
624                 }
625
626                 // Try to find the contact
627                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
628
629                 // Set detached object in rexcruiter instance
630                 user.setUserContact(foundContact);
631
632                 // Set timestamp
633                 user.setUserCreated(new GregorianCalendar());
634
635                 // Perist it
636                 this.getEntityManager().persist(user);
637
638                 // Flush it to get updated instance back
639                 this.getEntityManager().flush();
640
641                 // Log trace message
642                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
643
644                 // Return updated instanc
645                 return user;
646         }
647
648         @Override
649         public User updateUserData (final User user) {
650                 // Trace message
651                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
652
653                 // user should not be null
654                 if (null == user) {
655                         // Abort here
656                         throw new NullPointerException("user is null"); //NOI18N
657                 } else if (user.getUserId() == null) {
658                         // Throw NPE again
659                         throw new NullPointerException("user.userId is null"); //NOI18N
660                 } else if (user.getUserId() < 1) {
661                         // Not valid
662                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
663                 } else if (user.getUserAccountStatus() == null) {
664                         // Throw NPE again
665                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
666                 } else if (!this.ifUserExists(user)) {
667                         // User does not exist
668                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
669                 }
670
671                 // Remove contact instance as this is not updated
672                 user.setUserContact(null);
673
674                 // Find the instance
675                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
676
677                 // Should be found!
678                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
679
680                 // Merge user
681                 User detachedUser = this.getEntityManager().merge(foundUser);
682
683                 // Should be found!
684                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
685
686                 // Copy all data
687                 detachedUser.copyAll(user);
688
689                 // Set as updated
690                 detachedUser.setUserUpdated(new GregorianCalendar());
691
692                 // Trace message
693                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
694
695                 // Return updated instance
696                 return detachedUser;
697         }
698
699         @Override
700         public PasswordHistory updateUserPassword (final User user) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
701                 // Trace message
702                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
703
704                 // user should not be null
705                 if (null == user) {
706                         // Abort here
707                         throw new NullPointerException("user is null"); //NOI18N
708                 } else if (user.getUserId() == null) {
709                         // Throw NPE again
710                         throw new NullPointerException("user.userId is null"); //NOI18N
711                 } else if (user.getUserId() < 1) {
712                         // Not valid
713                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
714                 } else if (user.getUserAccountStatus() == null) {
715                         // Throw NPE again
716                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
717                 } else if (!this.ifUserExists(user)) {
718                         // User does not exist
719                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
720                 }
721
722                 // Call other method
723                 User updatedUser = this.updateUserData(user);
724
725                 // Create history entry
726                 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser);
727
728                 // Set created timestamp
729                 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
730
731                 // Persist it
732                 this.getEntityManager().persist(entry);
733
734                 // Flush it to get id number back
735                 this.getEntityManager().flush();
736
737                 // Trace message
738                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
739
740                 // Return it
741                 return entry;
742         }
743
744         @Override
745         public User updateUserPersonalData (final User user) {
746                 // Trace message
747                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
748
749                 // user should not be null
750                 if (null == user) {
751                         // Abort here
752                         throw new NullPointerException("user is null"); //NOI18N
753                 } else if (user.getUserId() == null) {
754                         // Throw NPE again
755                         throw new NullPointerException("user.userId is null"); //NOI18N
756                 } else if (user.getUserId() < 1) {
757                         // Not valid
758                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
759                 } else if (user.getUserAccountStatus() == null) {
760                         // Throw NPE again
761                         throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
762                 } else if (!this.ifUserExists(user)) {
763                         // User does not exist
764                         throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
765                 }
766
767                 // Find the instance
768                 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
769
770                 // Should be found!
771                 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
772
773                 // Merge user
774                 User detachedUser = this.getEntityManager().merge(foundUser);
775
776                 // Should be found!
777                 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
778
779                 // Copy all data
780                 detachedUser.copyAll(user);
781
782                 // Set as updated
783                 detachedUser.setUserUpdated(new GregorianCalendar());
784                 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
785
786                 // Get contact from it and find it
787                 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
788
789                 // Should be found
790                 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
791
792                 // Debug message
793                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
794
795                 // Merge contact instance
796                 Contact detachedContact = this.getEntityManager().merge(foundContact);
797
798                 // Copy all
799                 detachedContact.copyAll(user.getUserContact());
800
801                 // Set it back in user
802                 user.setUserContact(detachedContact);
803
804                 // Should be found!
805                 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
806
807                 // Get cellphone instance
808                 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
809
810                 // Is there a  cellphone instance set?
811                 if (cellphone instanceof DialableCellphoneNumber) {
812                         // Debug message
813                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
814
815                         // Then find it, too
816                         DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
817
818                         // Should be there
819                         assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
820
821                         // Then merge it, too
822                         DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
823
824                         // Should be there
825                         assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
826
827                         // Copy all
828                         detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
829
830                         // Set it back
831                         detachedContact.setContactCellphoneNumber(detachedCellphone);
832                 }
833
834                 // Get cellphone instance
835                 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
836
837                 // Is there a  fax instance set?
838                 if (fax instanceof DialableFaxNumber) {
839                         // Debug message
840                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
841
842                         // Then find it, too
843                         DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
844
845                         // Should be there
846                         assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
847
848                         // Then merge it, too
849                         DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
850
851                         // Should be there
852                         assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
853
854                         // Copy all
855                         detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
856
857                         // Set it back
858                         detachedContact.setContactFaxNumber(detachedFax);
859                 }
860
861                 // Get cellphone instance
862                 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
863
864                 // Is there a  fax instance set?
865                 if (landLine instanceof DialableLandLineNumber) {
866                         // Debug message
867                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
868
869                         // Then find it, too
870                         DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
871
872                         // Should be there
873                         assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
874
875                         // Then merge it, too
876                         DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
877
878                         // Should be there
879                         assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
880
881                         // Copy all
882                         detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
883
884                         // Set it back
885                         detachedContact.setContactLandLineNumber(detachedLandLine);
886                 }
887
888                 // Trace message
889                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
890
891                 // Return updated user instance
892                 return detachedUser;
893         }
894
895 }