X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjusercore%2Fmodel%2Fuser%2FPizzaUserSessionBean.java;h=2ce897289601945ebb23155c0f6f16dbd7797b28;hb=dc42360d34217de267febabebd8b7f0212aba244;hp=772efa00f00d8371bbedeae94574bde8cb0953d4;hpb=26a7be21e6b7edba3f3535198e6847d2785cb536;p=pizzaservice-ejb.git diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java index 772efa0..2ce8972 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java @@ -19,38 +19,92 @@ package org.mxchange.jusercore.model.user; import java.text.MessageFormat; import java.util.GregorianCalendar; import java.util.List; +import javax.ejb.EJB; import javax.ejb.EJBException; import javax.ejb.Stateless; import javax.persistence.NoResultException; import javax.persistence.PersistenceException; import javax.persistence.Query; import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; import org.mxchange.jusercore.model.user.status.UserAccountStatus; +import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean; /** - * A user bean + * A user EJB *

* @author Roland Haeder */ -@Stateless (name = "user", mappedName = "ejb/stateless-pizza-user", description = "A bean handling the user data") -public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessionBeanRemote { +@Stateless (name = "user", description = "A bean handling the user data") +public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserSessionBeanRemote { /** * Serial number */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + /** * Default constructor */ public PizzaUserSessionBean () { } + @Override + public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0} - CALLED!", user)); //NOI18N + + // userId should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N + } + + // Check if user is registered + if (this.registerBean.isUserNameRegistered(user)) { + // Abort here + throw new UserNameAlreadyRegisteredException(user); + } else if (this.registerBean.isEmailAddressRegistered(user)) { + // Abort here + throw new EmailAddressAlreadyRegisteredException(user); + } + + // Set created timestamp + user.setUserCreated(new GregorianCalendar()); + user.getUserContact().setContactCreated(new GregorianCalendar()); + + // Set all created timestamps + this.setAllContactPhoneEntriesCreated(user.getUserContact()); + + // Persist it + this.getEntityManager().persist(user); + + // Flush to get id back + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("addUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N + + // Return it + return user; + } + @Override @SuppressWarnings ("unchecked") public List allMemberPublicVisibleUsers () { @@ -98,6 +152,25 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio return users; } + @Override + @SuppressWarnings ("unchecked") + public List allUsers () { + // Trace message + this.getLoggerBeanLocal().logTrace("allUsers: CALLED!"); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllUsers", List.class); //NOI18N + + // Get result + List users = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsers: users.size()={0} - EXIT!", users.size())); //NOI18N + + // Return full list + return users; + } + @Override public User fillUserData (final User user) { // Trace message @@ -110,7 +183,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } // Try to locate it - Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N // Set parameter query.setParameter("param", user.getUserName()); //NOI18N @@ -134,6 +207,36 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio return foundUser; } + @Override + public User findUserById (final Long userId) throws UserNotFoundException { + // Is the parameter valid? + if (null == userId) { + // Throw NPE + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N + } else if (!this.ifUserIdExists(userId)) { + // Does not exist + throw new UserNotFoundException(userId); + } + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N + + // Set user id + query.setParameter("id", userId); //NOI18N + + // Fetch the result, it should be there by now + User user = (User) query.getSingleResult(); + + // Should be there + assert(user instanceof User) : "user is null"; //NOI18N + + // Return found user + return user; + } + @Override @SuppressWarnings ("unchecked") public List getEmailAddressList () { @@ -178,7 +281,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N // Set parameter query.setParameter("id", user.getUserId()); //NOI18N @@ -225,7 +328,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserId", User.class); //NOI18N // Set parameter query.setParameter("id", userId); //NOI18N @@ -257,6 +360,47 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio return true; } + @Override + public boolean ifUserNameExists (final String userName) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: userName={0} - CALLED!", userName)); //NOI18N + + // userId should not be null + if (null == userName) { + // Abort here + throw new NullPointerException("userName is null"); //NOI18N + } else if (userName.isEmpty()) { + // Abort here + throw new NullPointerException("userName is empty"); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N + + // Set parameter + query.setParameter("param", userName); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserNameExists: getSingleResult() returned no result: {0}", ex)); //NOI18N + + // User name does not exist + return false; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserNameExists: Found user name {0} - EXIT!", userName)); //NOI18N + + // Found it + return true; + } + @Override public boolean isEmailAddressReqistered (final User user) { // Trace message @@ -269,7 +413,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchEmailAddress", User.class); //NOI18N // Set parameter query.setParameter("param", user.getUserContact().getContactEmailAddress()); //NOI18N @@ -310,7 +454,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserName", LoginUser.class); //NOI18N + Query query = this.getEntityManager().createNamedQuery("SearchUserName", User.class); //NOI18N // Set parameter query.setParameter("param", user.getUserName()); //NOI18N @@ -340,9 +484,105 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio } @Override - public void updateUserPersonalData (final User user) { + public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // userId should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } + + // Check if user is registered + if (this.registerBean.isUserNameRegistered(user)) { + // Abort here + throw new UserNameAlreadyRegisteredException(user); + } else if (this.registerBean.isEmailAddressRegistered(user)) { + // Abort here + throw new EmailAddressAlreadyRegisteredException(user); + } + + // Set created timestamp + user.setUserCreated(new GregorianCalendar()); + + // Try to find the contact instance + Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); + + // Try to merge it + Contact detachedContact = this.getEntityManager().merge(foundContact); + + // Set it in user + user.setUserContact(detachedContact); + + // Persist user + this.getEntityManager().persist(user); + + // Flush it to get id + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("linkUser: user.userId={0} - EXIT!", user.getUserId())); //NOI18N + + // Return updated instance + return user; + } + + @Override + public User updateUserData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); + this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserData: user={0} - CALLED!", user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } + + // Remove contact instance as this is not updatedf + user.setUserContact(null); + + // Find the instance + User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); + + // Should be found! + assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Merge user + User detachedUser = this.getEntityManager().merge(foundUser); + + // Should be found! + assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N + + // Copy all data + detachedUser.copyAll(user); + + // Set as updated + detachedUser.setUserUpdated(new GregorianCalendar()); + + // Return updated instance + return detachedUser; + } + + @Override + public User updateUserPersonalData (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N // user should not be null if (null == user) { @@ -388,7 +628,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N // Merge contact instance Contact detachedContact = this.getEntityManager().merge(foundContact); @@ -408,19 +648,19 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio // Is there a cellphone instance set? if (cellphone instanceof DialableCellphoneNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId())); //NOI18N // Then find it, too DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId()); // Should be there - assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); + assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId()); //NOI18N // Then merge it, too DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone); // Should be there - assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); + assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId()); //NOI18N // Copy all detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber()); @@ -435,19 +675,19 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio // Is there a fax instance set? if (fax instanceof DialableFaxNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N // Then find it, too DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); // Should be there - assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); + assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N // Then merge it, too DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); // Should be there - assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); + assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N // Copy all detachedFax.copyAll(user.getUserContact().getContactFaxNumber()); @@ -462,19 +702,19 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio // Is there a fax instance set? if (landLine instanceof DialableLandLineNumber) { // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N // Then find it, too DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); // Should be there - assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); + assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N // Then merge it, too DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); // Should be there - assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); + assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N // Copy all detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber()); @@ -482,6 +722,9 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio // Set it back detachedContact.setContactLandLineNumber(detachedLandLine); } + + // Return updated user instance + return detachedUser; } }