2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
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;
51 * @author Roland Haeder<roland@mxchange.org>
53 @Stateless (name = "user", description = "A bean handling the user data")
54 public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserSessionBeanRemote {
59 private static final long serialVersionUID = 542_145_347_916L;
65 private UserRegistrationSessionBeanRemote registerBean;
70 public PizzaUserSessionBean () {
74 public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
76 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
78 // userId should not be null
81 throw new NullPointerException("user is null"); //NOI18N
82 } else if (user.getUserId() == null) {
84 throw new NullPointerException("user.userId is null"); //NOI18N
85 } else if (user.getUserId() < 1) {
87 throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
90 // Check if user is registered
91 if (this.registerBean.isUserNameRegistered(user)) {
93 throw new UserNameAlreadyRegisteredException(user);
94 } else if (this.registerBean.isEmailAddressRegistered(user)) {
96 throw new EmailAddressAlreadyRegisteredException(user);
99 // Set created timestamp
100 user.setUserCreated(new GregorianCalendar());
101 user.getUserContact().setContactCreated(new GregorianCalendar());
103 // Update cellphone, land-line and fax instance
104 this.setAllContactPhoneEntriesCreated(user.getUserContact());
107 this.getEntityManager().persist(user);
109 // Flush to get id back
110 this.getEntityManager().flush();
113 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
120 @SuppressWarnings ("unchecked")
121 public List<User> allMemberPublicVisibleUsers () {
123 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
126 Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N
129 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
130 query.setParameter("members", ProfileMode.MEMBERS); //NOI18N
131 query.setParameter("public", ProfileMode.PUBLIC); //NOI18N
134 List<User> users = query.getResultList();
137 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
144 @SuppressWarnings ("unchecked")
145 public List<User> allPublicUsers () {
147 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
150 Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N
153 query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N
154 query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N
157 List<User> users = query.getResultList();
160 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
167 @SuppressWarnings ("unchecked")
168 public List<User> allUsers () {
170 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N
173 Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N
176 List<User> users = query.getResultList();
179 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N
186 public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException {
188 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
190 // Parameter must be valid
193 throw new NullPointerException("user is null"); //NOI18N
194 } else if (user.getUserId() == null) {
196 throw new NullPointerException("user.userId is null"); //NOI18N
197 } else if (user.getUserId() < 1) {
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) {
208 throw new NullPointerException("user.userConfirmKey is null"); //NOI18N
211 // Update user status and remove confirmation key
212 user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
213 user.setUserConfirmKey(null);
214 user.setUserUpdated(new GregorianCalendar());
216 // Update user account
217 User updatedUser = this.updateUserData(user);
220 Address emailAddress;
223 // Create email address and set
224 emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress());
225 } catch (final AddressException ex) {
227 throw new EJBException(ex);
231 this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N
234 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N
236 // Return updated instance
241 public User fillUserData (final User user) {
243 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
245 // user should not be null
248 throw new NullPointerException("user is null"); //NOI18N
252 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
255 query.setParameter("userName", user.getUserName()); //NOI18N
257 // Initialize variable
258 User foundUser = null;
262 // Try to get single result
263 foundUser = (User) query.getSingleResult();
264 } catch (final NoResultException ex) {
266 this.getLoggerBeanLocal().logException(ex);
270 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N
272 // Return prepared instance
277 public User findUserById (final Long userId) throws UserNotFoundException {
279 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
281 // Is the parameter valid?
282 if (null == userId) {
284 throw new NullPointerException("userId is null"); //NOI18N
285 } else if (userId < 1) {
287 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
288 } else if (!this.ifUserIdExists(userId)) {
290 throw new UserNotFoundException(userId);
293 // Create query instance
294 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
297 query.setParameter("id", userId); //NOI18N
299 // Fetch the result, it should be there by now
300 User user = (User) query.getSingleResult();
303 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
310 public String generateRandomUserName () {
312 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N
315 List<String> userList = this.getUserNameList();
318 String userName = null;
320 // Loop until a user name is found
321 while ((userName == null) || (userList.contains(userName))) {
322 // Generate random name
323 userName = UserUtils.generateRandomUserName();
327 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
329 // Found one, so return it
334 @SuppressWarnings ("unchecked")
335 public List<String> getEmailAddressList () {
337 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N
340 Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N
343 List<String> emailAddressList = query.getResultList();
346 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N
349 return emailAddressList;
353 @SuppressWarnings ("unchecked")
354 public List<String> getUserNameList () {
356 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N
359 Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N
362 List<String> userNameList = query.getResultList();
365 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N
372 public boolean ifUserExists (final User user) {
374 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
376 // userId should not be null
379 throw new NullPointerException("user is null"); //NOI18N
380 } else if (user.getUserId() == null) {
382 throw new NullPointerException("user.userId is null"); //NOI18N
383 } else if (user.getUserId() < 1) {
385 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
389 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
392 query.setParameter("id", user.getUserId()); //NOI18N
396 User dummy = (User) query.getSingleResult();
399 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.userId={0} found.", dummy.getUserId())); //NOI18N
400 } catch (final NoResultException ex) {
402 this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
404 // User name does not exist
406 } catch (final PersistenceException ex) {
407 // Something bad happened
408 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
415 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
422 public boolean ifUserIdExists (final Long userId) {
424 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N
426 // userId should not be null
427 if (null == userId) {
429 throw new NullPointerException("userId is null"); //NOI18N
430 } else if (userId < 1) {
432 throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid", userId)); //NOI18N
436 Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N
439 query.setParameter("id", userId); //NOI18N
443 User dummy = (User) query.getSingleResult();
446 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
447 } catch (final NoResultException ex) {
449 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
451 // User name does not exist
453 } catch (final PersistenceException ex) {
454 // Something bad happened
455 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N
462 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N
469 public boolean ifUserNameExists (final String userName) {
471 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N
473 // userId should not be null
474 if (null == userName) {
476 throw new NullPointerException("userName is null"); //NOI18N
477 } else if (userName.isEmpty()) {
479 throw new NullPointerException("userName is empty"); //NOI18N
483 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
486 query.setParameter("userName", userName); //NOI18N
490 User dummy = (User) query.getSingleResult();
493 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
494 } catch (final NoResultException ex) {
496 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
498 // User name does not exist
503 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N
510 public boolean isEmailAddressRegistered (final User user) {
512 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
514 // user should not be null
517 throw new NullPointerException("user is null"); //NOI18N
521 Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N
524 query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N
528 User dummy = (User) query.getSingleResult();
531 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
532 } catch (final NoResultException ex) {
534 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
536 // Email address does not exist
538 } catch (final PersistenceException ex) {
539 // Something bad happened
540 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
547 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
554 public boolean isUserNameRegistered (final User user) {
556 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
558 // user should not be null
561 throw new NullPointerException("user is null"); //NOI18N
565 Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N
568 query.setParameter("userName", user.getUserName()); //NOI18N
572 User dummy = (User) query.getSingleResult();
575 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N
576 } catch (final NoResultException ex) {
578 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N
580 // User name does not exist
582 } catch (final PersistenceException ex) {
583 // Something bad happened
584 this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N
591 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N
598 public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
600 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
602 // user should not be null
605 throw new NullPointerException("user is null"); //NOI18N
606 } else if (user.getUserId() instanceof Long) {
608 throw new IllegalArgumentException("user.userId is not null"); //NOI18N
609 } else if (user.getUserContact() == null) {
611 throw new NullPointerException("user.userContact is null"); //NOI18N
612 } else if (user.getUserContact().getContactId() == null) {
614 throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
615 } else if (user.getUserContact().getContactId() < 1) {
616 // Not valid id number
617 throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
618 } else if (user.getUserAccountStatus() == null) {
620 throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
621 } else if (this.ifUserNameExists(user.getUserName())) {
622 // Name already found
623 throw new UserNameAlreadyRegisteredException(user.getUserName());
626 // Try to find the contact
627 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
629 // Set detached object in rexcruiter instance
630 user.setUserContact(foundContact);
633 user.setUserCreated(new GregorianCalendar());
636 this.getEntityManager().persist(user);
638 // Flush it to get updated instance back
639 this.getEntityManager().flush();
642 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
644 // Return updated instanc
649 public User updateUserData (final User user) {
651 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
653 // user should not be null
656 throw new NullPointerException("user is null"); //NOI18N
657 } else if (user.getUserId() == null) {
659 throw new NullPointerException("user.userId is null"); //NOI18N
660 } else if (user.getUserId() < 1) {
662 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
663 } else if (user.getUserAccountStatus() == null) {
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
671 // Remove contact instance as this is not updated
672 user.setUserContact(null);
675 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
678 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
681 User detachedUser = this.getEntityManager().merge(foundUser);
684 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
687 detachedUser.copyAll(user);
690 detachedUser.setUserUpdated(new GregorianCalendar());
693 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
695 // Return updated instance
700 public PasswordHistory updateUserPassword (final User user) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException {
702 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
704 // user should not be null
707 throw new NullPointerException("user is null"); //NOI18N
708 } else if (user.getUserId() == null) {
710 throw new NullPointerException("user.userId is null"); //NOI18N
711 } else if (user.getUserId() < 1) {
713 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
714 } else if (user.getUserAccountStatus() == null) {
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
723 User updatedUser = this.updateUserData(user);
725 // Create history entry
726 PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser);
728 // Set created timestamp
729 entry.setUserPasswordHistoryCreated(new GregorianCalendar());
732 this.getEntityManager().persist(entry);
734 // Flush it to get id number back
735 this.getEntityManager().flush();
738 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N
745 public User updateUserPersonalData (final User user) {
747 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
749 // user should not be null
752 throw new NullPointerException("user is null"); //NOI18N
753 } else if (user.getUserId() == null) {
755 throw new NullPointerException("user.userId is null"); //NOI18N
756 } else if (user.getUserId() < 1) {
758 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
759 } else if (user.getUserAccountStatus() == null) {
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
768 User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId());
771 assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
774 User detachedUser = this.getEntityManager().merge(foundUser);
777 assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
780 detachedUser.copyAll(user);
783 detachedUser.setUserUpdated(new GregorianCalendar());
784 detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
786 // Get contact from it and find it
787 Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
790 assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
793 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N
795 // Merge contact instance
796 Contact detachedContact = this.getEntityManager().merge(foundContact);
799 detachedContact.copyAll(user.getUserContact());
801 // Set it back in user
802 user.setUserContact(detachedContact);
805 assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
807 // Get cellphone instance
808 DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
810 // Is there a cellphone instance set?
811 if (cellphone instanceof DialableCellphoneNumber) {
813 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N
816 DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
819 assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N
821 // Then merge it, too
822 DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
825 assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N
828 detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
831 detachedContact.setContactCellphoneNumber(detachedCellphone);
834 // Get cellphone instance
835 DialableFaxNumber fax = detachedContact.getContactFaxNumber();
837 // Is there a fax instance set?
838 if (fax instanceof DialableFaxNumber) {
840 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N
843 DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
846 assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N
848 // Then merge it, too
849 DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
852 assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N
855 detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
858 detachedContact.setContactFaxNumber(detachedFax);
861 // Get cellphone instance
862 DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
864 // Is there a fax instance set?
865 if (landLine instanceof DialableLandLineNumber) {
867 this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N
870 DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
873 assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N
875 // Then merge it, too
876 DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
879 assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N
882 detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
885 detachedContact.setContactLandLineNumber(detachedLandLine);
889 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N
891 // Return updated user instance