From: Roland Haeder Date: Tue, 12 Apr 2016 16:54:57 +0000 (+0200) Subject: added business method addUser() and took code from registerUser as both is (at persis... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=255b746e34e1754df3cbf32f3295a51d318cd0f4;p=pizzaservice-ejb.git added business method addUser() and took code from registerUser as both is (at persisting) the same. --- diff --git a/src/java/org/mxchange/jusercore/model/register/PizzaUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/register/PizzaUserRegistrationSessionBean.java index e8bbfca..e63eabe 100644 --- a/src/java/org/mxchange/jusercore/model/register/PizzaUserRegistrationSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/register/PizzaUserRegistrationSessionBean.java @@ -17,7 +17,6 @@ package org.mxchange.jusercore.model.register; import java.text.MessageFormat; -import java.util.GregorianCalendar; import javax.ejb.EJB; import javax.ejb.Stateless; import org.mxchange.jcoreee.database.BaseDatabaseBean; @@ -101,18 +100,11 @@ public class PizzaUserRegistrationSessionBean extends BaseDatabaseBean implement throw new EmailAddressAlreadyRegisteredException(user); } - // Set created timestamp - user.setUserCreated(new GregorianCalendar()); - user.getUserContact().setContactCreated(new GregorianCalendar()); - - // Persist it - this.getEntityManager().persist(user); - - // Flush to get id back - this.getEntityManager().flush(); + // Call other EJB + User addedUser = this.userBean.addUser(user); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: user={0},user.id={1} - EXIT!", user, user.getUserId())); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerUser: addedUser={0},addedUser.userIdd={1} - EXIT!", addedUser, addedUser.getUserId())); //NOI18N // Return it return user; diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java index 76eee4f..3b4ff13 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java @@ -19,6 +19,7 @@ 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; @@ -29,6 +30,9 @@ 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.model.register.PizzaUserRegistrationSessionBean; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; import org.mxchange.jusercore.model.user.status.UserAccountStatus; @@ -45,12 +49,55 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Registration bean + */ + @EJB + private PizzaUserRegistrationSessionBean 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 + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //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()); + + // 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 () {