]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Sat, 9 Sep 2017 11:35:31 +0000 (13:35 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 9 Sep 2017 11:59:42 +0000 (13:59 +0200)
- removed explicit flush() on entity manager as this hurts performance + may
  cause trouble when other entities (concurrently) are not "ready to be flushed)
- implemented addBranchOffice() + added missing public constructor
- added private method isBranchOfficeFound() which uses the general EJB for
  retrieving whole branch office list
- added protected getManaged() for Contact and Country instances
- renamed companyDataId -> basicDataId

Signed-off-by: Roland Häder <roland@mxchange.org>
12 files changed:
src/java/org/mxchange/jcontacts/contact/PizzaAdminContactSessionBean.java
src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaAdminBusinessDataSessionBean.java
src/java/org/mxchange/jcontactsbusiness/basicdata/PizzaBusinessDataSessionBean.java
src/java/org/mxchange/jcontactsbusiness/branchoffice/PizzaAdminBranchOfficeSessionBean.java
src/java/org/mxchange/jcontactsbusiness/branchoffice/PizzaBranchOfficeSessionBean.java
src/java/org/mxchange/jcountry/data/PizzaCountrySingletonBean.java
src/java/org/mxchange/jphone/phonenumbers/mobileprovider/PizzaAdminMobileProviderSessionBean.java
src/java/org/mxchange/jusercore/model/user/PizzaAdminUserSessionBean.java
src/java/org/mxchange/jusercore/model/user/PizzaUserSessionBean.java
src/java/org/mxchange/jusercore/model/user/activity/PizzaUserActivityLogMessageBean.java
src/java/org/mxchange/jusercore/model/user/email_address/PizzaUserEmailChangeSessionBean.java
src/java/org/mxchange/pizzaaplication/database/BasePizzaDatabaseBean.java

index 1841b15d86a02650eefb78a8d8fc0ab9431bd0b4..fe254f5f94acff3056fc625ad46d61d04b1b27fd 100644 (file)
@@ -72,9 +72,6 @@ public class PizzaAdminContactSessionBean extends BasePizzaDatabaseBean implemen
                // Persist it
                this.getEntityManager().persist(contact);
 
-               // Flush it to get contactId set
-               this.getEntityManager().flush();
-
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N
 
index 4ab3e779f6ac97ab250e43e86b311a95d7dec713..5ad2589b1fd284325305f3a0896fc1bca68291f9 100644 (file)
@@ -61,9 +61,9 @@ public class PizzaAdminBusinessDataSessionBean extends BasePizzaDatabaseBean imp
                if (null == basicData) {
                        // Throw NPE
                        throw new NullPointerException("basicData is null"); //NOI18N
-               } else if (basicData.getCompanyDataId() != null) {
+               } else if (basicData.getBasicDataId() != null) {
                        // Should be null
-                       throw new IllegalArgumentException(MessageFormat.format("basicData.companyDataId={0} - is not null", basicData.getCompanyDataId())); //NOI18N
+                       throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} - is not null", basicData.getBasicDataId())); //NOI18N
                }
 
                // Get all available entries
@@ -87,11 +87,8 @@ public class PizzaAdminBusinessDataSessionBean extends BasePizzaDatabaseBean imp
                // Persist it
                this.getEntityManager().persist(basicData);
 
-               // Flush it (bad performance!)
-               this.getEntityManager().flush();
-
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.companyDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getCompanyDataId())); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBusinessBasicData: basicData.basicDataId={1} - EXIT!", this.getClass().getSimpleName(), basicData.getBasicDataId())); //NOI18N
 
                // Return updated instance
                return basicData;
index 8fe3192939e8ccefd60a6bc5d2055f2cfefc7c97..2e1be008d2c6026789c0ef03b9da2aae551a1136 100644 (file)
@@ -66,7 +66,7 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
        }
 
        @Override
-       public BusinessBasicData findBasicDataById (final Long companyDataId) throws BusinessDataNotFoundException {
+       public BusinessBasicData findBasicDataById (final Long basicDataId) throws BusinessDataNotFoundException {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: CALLED!", this.getClass().getSimpleName())); //NOI18N
 
@@ -74,28 +74,28 @@ public class PizzaBusinessDataSessionBean extends BasePizzaDatabaseBean implemen
                final Query query = this.getEntityManager().createNamedQuery("SearchBusinessDataById", CompanyBasicData.class); //NOI18N
 
                // Set parameter
-               query.setParameter("companyDataId", companyDataId); //NOI18N
+               query.setParameter("basicDataId", basicDataId); //NOI18N
 
                // Get single instance
-               final BusinessBasicData businessData;
+               final BusinessBasicData basicData;
 
                // Try to find a result
                try {
                        // Find a single result
-                       businessData = (BusinessBasicData) query.getSingleResult();
+                       basicData = (BusinessBasicData) query.getSingleResult();
 
                        // Log trace message
-                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found contact={1}", this.getClass().getSimpleName(), businessData)); //NOI18N
+                       this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: Found basicData={1}", this.getClass().getSimpleName(), basicData)); //NOI18N
                } catch (final NoResultException ex) {
                        // No result found
-                       throw new BusinessDataNotFoundException(companyDataId, ex);
+                       throw new BusinessDataNotFoundException(basicDataId, ex);
                }
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: businessData={1} - EXIT!", this.getClass().getSimpleName(), businessData)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findBasicDataById: basicData={1} - EXIT!", this.getClass().getSimpleName(), basicData)); //NOI18N
 
                // Return it
-               return businessData;
+               return basicData;
        }
 
        @Override
index 92444827851d04b105d39cdb2bbe9bd8055cab2c..7b328cfcc01d72dc7e83c36cccbe08ac778824a1 100644 (file)
  */
 package org.mxchange.jcontactsbusiness.branchoffice;
 
+import java.text.MessageFormat;
+import java.util.GregorianCalendar;
+import java.util.List;
+import javax.ejb.EJB;
 import javax.ejb.Stateless;
+import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData;
+import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
+import org.mxchange.jcountry.data.Country;
+import org.mxchange.jusercore.model.user.User;
 import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 
 /**
@@ -32,4 +40,105 @@ public class PizzaAdminBranchOfficeSessionBean extends BasePizzaDatabaseBean imp
         */
        private static final long serialVersionUID = 58_467_386_571_701L;
 
+       /**
+        * General branch office bean
+        */
+       @EJB
+       private BranchOfficeSessionBeanRemote branchOfficeBean;
+
+       /**
+        * Default constructor
+        */
+       public PizzaAdminBranchOfficeSessionBean () {
+               // Call super constructor
+               super();
+       }
+
+       @Override
+       public BranchOffice addBranchOffice (final BranchOffice branchOffice) throws BranchOfficeAlreadyAddedException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
+
+               // Validate parameter
+               if (null == branchOffice) {
+                       // Throw NPE
+                       throw new NullPointerException("branchOffice is null"); //NOI18N
+               } else if (branchOffice.getBranchId() instanceof Long) {
+                       // Should not happen
+                       throw new IllegalArgumentException("branchOffice.branchId should not be set."); //NOI18N
+               } else if (this.isBranchOfficeFound(branchOffice)) {
+                       // Already added, abort here
+                       throw new BranchOfficeAlreadyAddedException(branchOffice);
+               }
+
+               // Add created timestamp
+               branchOffice.setBranchCreated(new GregorianCalendar());
+
+               // Is user instance set?
+               if (branchOffice.getBranchCompany() instanceof BusinessBasicData) {
+                       // Get managed instance back
+                       final BusinessBasicData managedBasicData = this.getManaged(branchOffice.getBranchCompany());
+
+                       // Set it back in branch office
+                       branchOffice.setBranchCompany(managedBasicData);
+               }
+
+               // Is user instance set?
+               if (branchOffice.getBranchUserOwner() instanceof User) {
+                       // Get managed instance back
+                       final User managedUser = this.getManaged(branchOffice.getBranchUserOwner());
+
+                       // Set it back in branch office
+                       branchOffice.setBranchUserOwner(managedUser);
+               }
+
+               // Is user instance set?
+               if (branchOffice.getBranchCountry() instanceof Country) {
+                       // Get managed instance back
+                       final Country managedCountry = this.getManaged(branchOffice.getBranchCountry());
+
+                       // Set it back in branch office
+                       branchOffice.setBranchCountry(managedCountry);
+               }
+
+               // Persist it
+               this.getEntityManager().persist(branchOffice);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchId={1} - EXIT!", this.getClass().getSimpleName(), branchOffice.getBranchId())); //NOI18N
+
+               // Return updated instance
+               return branchOffice;
+       }
+
+       /**
+        * Checks if given branch office's address is already persisted. The whole
+        * (persisted) list is being loaded and each address is being matched
+        * against the given branch office's address.
+        * <p>
+        * @param branchOffice Branch office being checked
+        * <p>
+        * @return Whether it has been found
+        */
+       private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
+               // Get whole list
+               final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
+
+               // Default is not found
+               boolean isFound = false;
+
+               // Check all single addresses
+               for (final BranchOffice bo : branchOffices) {
+                       // Is the same address found?
+                       if (BranchOfficeUtils.isSameAddress(bo, branchOffice)) {
+                               // Found one
+                               isFound = true;
+                               break;
+                       }
+               }
+
+               // Return flag
+               return isFound;
+       }
+
 }
index 5d74c9eac38cc571c15a76f8a9720f68cc5816eb..bc251dfdaafb9c0e3a6808cb13b8f177bbedc3ee 100644 (file)
@@ -20,7 +20,7 @@ import java.text.MessageFormat;
 import java.util.List;
 import javax.ejb.Stateless;
 import javax.persistence.Query;
-import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
+import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean;
 
 /**
  * A stateless session bean for general branch office purposes
@@ -28,7 +28,7 @@ import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
  * @author Roland Häder<roland@mxchange.org>
  */
 @Stateless (name = "branchOffice", description = "A general statless bean for handling branch office data (all)")
-public class PizzaBranchOfficeSessionBean extends BaseFinancialsDatabaseBean implements BranchOfficeSessionBeanRemote {
+public class PizzaBranchOfficeSessionBean extends BasePizzaDatabaseBean implements BranchOfficeSessionBeanRemote {
 
        /**
         * Serial number
index 6dc7bbbdad68c64c71c7b073bce4a0aea9fa75f1..eb2b2b2a99792359471be7bbbd9a5dad3c120b20 100644 (file)
@@ -77,9 +77,6 @@ public class PizzaCountrySingletonBean extends BasePizzaDatabaseBean implements
                // It is not added, so persist it
                this.getEntityManager().persist(country);
 
-               // Flush it to get id number back, maybe it is directly needed?
-               this.getEntityManager().flush();
-
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N
 
index 1db9ace8ab96dcaa4ce51fc839c3aa0f933b2e88..7217458343fcf5f2d1a8e69ea62b1633701a654c 100644 (file)
@@ -84,9 +84,6 @@ public class PizzaAdminMobileProviderSessionBean extends BasePizzaDatabaseBean i
                // Persist it
                this.getEntityManager().persist(mobileProvider);
 
-               // ... and flush it to get id back
-               this.getEntityManager().flush();
-
                // Log trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addMobileProvider: mobileProvider.providerId={1} - EXIT!", this.getClass().getSimpleName(), mobileProvider.getProviderId())); //NOI18N
 
index af908863f9726e4ccb73ea68b252d3f360c4aa66..1b1a159f70c893aebb0e96bd08814656b3245667 100644 (file)
@@ -97,9 +97,6 @@ public class PizzaAdminUserSessionBean extends BasePizzaDatabaseBean implements
                // Persist it
                this.getEntityManager().persist(user);
 
-               // Flush to get id back
-               this.getEntityManager().flush();
-
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
 
@@ -140,7 +137,7 @@ public class PizzaAdminUserSessionBean extends BasePizzaDatabaseBean implements
                }
 
                // Get a managed instance
-               final User managedUser = this.getManagedUser(user);
+               final User managedUser = this.getManaged(user);
 
                // Should be found!
                assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
@@ -193,9 +190,6 @@ public class PizzaAdminUserSessionBean extends BasePizzaDatabaseBean implements
                // Perist it
                this.getEntityManager().persist(user);
 
-               // Flush it to get updated instance back
-               this.getEntityManager().flush();
-
                // Log trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
 
index 567710b807be0162db1f3c7479c6a926dc0563bc..15cc2773febc2b2dec4398585cb1990f0e16e2bd 100644 (file)
@@ -673,9 +673,6 @@ public class PizzaUserSessionBean extends BasePizzaDatabaseBean implements UserS
                // Persist it
                this.getEntityManager().persist(entry);
 
-               // Flush it to get id number back
-               this.getEntityManager().flush();
-
                // Send email to user
                this.sendEmail("User password change", "user_password_change", managedUser, baseUrl, null); //NOI18N
 
index 75f991cacfbafd1a43dccc981b0c1f1eef94724f..3d64e9ca642a5b2723ebe4c8e3f7b8e93aa42417 100644 (file)
@@ -136,7 +136,7 @@ public class PizzaUserActivityLogMessageBean extends BasePizzaDatabaseBean imple
                }
 
                // Make user instance managed
-               final User managedUser = this.getManagedUser(userActivity.getActivityUser());
+               final User managedUser = this.getManaged(userActivity.getActivityUser());
 
                // Set it back
                userActivity.setActivityUser(managedUser);
index 488224e7705a99753c18d9a958907fd8858f5c07..e8c3a59ff4cd785ee7ada813cf063b65a8bad19d 100644 (file)
@@ -110,11 +110,10 @@ public class PizzaUserEmailChangeSessionBean extends BasePizzaDatabaseBean imple
                this.generateSecureHash(emailChange);
 
                // Make user managed
-               emailChange.setEmailChangeUser(this.getManagedUser(emailChange.getEmailChangeUser()));
+               emailChange.setEmailChangeUser(this.getManaged(emailChange.getEmailChangeUser()));
 
                // Persist it
                //@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange);
-
                // Send email
                this.sendEmail("User email change", "user_email_change", emailChange.getEmailChangeUser(), baseUrl, null); //NOI18N
 
index 724108378f1c2a6a7500b23c2e60fc716a9014db..7776ddf26b7b27790cb94a43ec675ee1d5940b7c 100644 (file)
@@ -28,7 +28,12 @@ import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcontacts.contact.ContactUtils;
+import org.mxchange.jcontacts.contact.UserContact;
+import org.mxchange.jcontactsbusiness.basicdata.BusinessBasicData;
+import org.mxchange.jcontactsbusiness.basicdata.CompanyBasicData;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jcountry.data.Country;
+import org.mxchange.jcountry.data.CountryData;
 import org.mxchange.jmailee.model.delivery.wrapper.EmailDeliveryWrapper;
 import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery;
 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
@@ -280,6 +285,114 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
                return detachedNumber;
        }
 
+       /**
+        * Get back a managed instance from given contact
+        * <p>
+        * @param contact Unmanaged/detached contact instance
+        * <p>
+        * @return Managed contact instance
+        */
+       protected Contact getManaged (final Contact contact) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
+
+               // user should not be null
+               if (null == contact) {
+                       // Abort here
+                       throw new NullPointerException("contact is null"); //NOI18N
+               } else if (contact.getContactId() == null) {
+                       // Id is set
+                       throw new NullPointerException("contact.contactId is null"); //NOI18N
+               } else if (contact.getContactId() < 1) {
+                       // Id is set
+                       throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is null", contact.getContactId())); //NOI18N
+               }
+
+               // Try to find it (should be there)
+               final Contact managedContact = this.getEntityManager().find(UserContact.class, contact.getContactId());
+
+               // Should be there
+               assert (managedContact instanceof Contact) : "managedContact is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
+
+               // Return it
+               return managedContact;
+       }
+
+       /**
+        * Get back a managed instance from given country
+        * <p>
+        * @param country Unmanaged/detached country instance
+        * <p>
+        * @return Managed country instance
+        */
+       protected Country getManaged (final Country country) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
+
+               // user should not be null
+               if (null == country) {
+                       // Abort here
+                       throw new NullPointerException("country is null"); //NOI18N
+               } else if (country.getCountryId() == null) {
+                       // Id is set
+                       throw new NullPointerException("country.countryId is null"); //NOI18N
+               } else if (country.getCountryId() < 1) {
+                       // Id is set
+                       throw new IllegalArgumentException(MessageFormat.format("country.countryId={0} is null", country.getCountryId())); //NOI18N
+               }
+
+               // Try to find it (should be there)
+               final Country managedCountry = this.getEntityManager().find(CountryData.class, country.getCountryId());
+
+               // Should be there
+               assert (managedCountry instanceof Country) : "managedCountry is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedCountry={1} - EXIT!", this.getClass().getSimpleName(), managedCountry)); //NOI18N
+
+               // Return it
+               return managedCountry;
+       }
+
+       /**
+        * Get back a managed instance from given contact
+        * <p>
+        * @param basicData Unmanaged/detached contact instance
+        * <p>
+        * @return Managed contact instance
+        */
+       protected BusinessBasicData getManaged (final BusinessBasicData basicData) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
+
+               // user should not be null
+               if (null == basicData) {
+                       // Abort here
+                       throw new NullPointerException("basicData is null"); //NOI18N
+               } else if (basicData.getBasicDataId() == null) {
+                       // Id is set
+                       throw new NullPointerException("basicData.basicDataId is null"); //NOI18N
+               } else if (basicData.getBasicDataId() < 1) {
+                       // Id is set
+                       throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} is null", basicData.getBasicDataId())); //NOI18N
+               }
+
+               // Try to find it (should be there)
+               final BusinessBasicData managedBasicData = this.getEntityManager().find(CompanyBasicData.class, basicData.getBasicDataId());
+
+               // Should be there
+               assert (managedBasicData instanceof BusinessBasicData) : "managedBasicData is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedBasicData={1} - EXIT!", this.getClass().getSimpleName(), managedBasicData)); //NOI18N
+
+               // Return it
+               return managedBasicData;
+       }
+
        /**
         * Get back a managed instance from given user
         * <p>
@@ -287,7 +400,7 @@ public abstract class BasePizzaDatabaseBean extends BaseDatabaseBean {
         * <p>
         * @return Managed user instance
         */
-       protected User getManagedUser (final User user) {
+       protected User getManaged (final User user) {
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N