From 98fa10034deeac8b1fdcb48ee8c349744ae9a5f4 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 15 Apr 2016 14:42:39 +0200 Subject: [PATCH] Implemented new business method findUserById() + ignored strings for i18n --- .../model/user/PizzaUserSessionBean.java | 53 +++++++++++++++---- 1 file changed, 42 insertions(+), 11 deletions(-) diff --git a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java index 956c577..0d49b4c 100644 --- a/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java @@ -32,6 +32,7 @@ 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; @@ -223,6 +224,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", LoginUser.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 () { @@ -431,7 +462,7 @@ public class PizzaUserSessionBean extends BaseDatabaseBean implements UserSessio @Override public void updateUserPersonalData (final User user) { // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); + this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user)); //NOI18N // user should not be null if (null == user) { @@ -477,7 +508,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); @@ -497,19 +528,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()); @@ -524,19 +555,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()); @@ -551,19 +582,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()); -- 2.39.5