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=a7a23313b3894940504fbe5157019476f2fbf4b6;p=pizzaservice-mailer-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/AddressbookUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java index 250611c..0cc610f 100644 --- a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.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 AddressbookUserRegistrationSessionBean extends BaseDatabaseBean imp 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/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java index ca8f7db..36fc47d 100644 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.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 AddressbookUserSessionBean extends BaseDatabaseBean implements User */ private static final long serialVersionUID = 542_145_347_916L; + /** + * Registration bean + */ + @EJB + private PizzaUserRegistrationSessionBean registerBean; + /** * Default constructor */ public AddressbookUserSessionBean () { } + @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 () {