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