]> git.mxchange.org Git - jfinancials-ejb.git/commitdiff
Please cherry-pick:
authorRoland Häder <roland@mxchange.org>
Sat, 9 Sep 2017 12:18:10 +0000 (14:18 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 9 Sep 2017 12:18:10 +0000 (14:18 +0200)
- introduced isSameCompanyNameAdded() which encapsulates checking for if a
  company name has already been used. This is, together with the thrown checked
  exception a last effort to prevent bad bad SqlException or any other
  "low-level" exception as they are more severage than this.
- thumb of a rule: always pre-validate if all conditions are met (return "okay")
  prior doing risky things where uncontrolled exceptions may be thrown.
- make company-owner (User), founder (Employee) and contact person (dito)
  managed before persisting the whole BasicData instance as this makes sure that
  no duplicates will end up in database

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jcontactsbusiness/basicdata/FinancialsAdminBusinessDataSessionBean.java
src/java/org/mxchange/jcontactsbusiness/branchoffice/FinancialsAdminBranchOfficeSessionBean.java
src/java/org/mxchange/jfinancials/database/BaseFinancialsDatabaseBean.java
src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java
src/java/org/mxchange/jusercore/model/user/activity/FinancialsUserActivityLogMessageBean.java

index c00b268c924d25a0bd52f37e236d88a34ce44777..c786968f5d43375547d8bd2cb96ef2049db5852f 100644 (file)
@@ -22,8 +22,10 @@ import java.util.List;
 import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
+import org.mxchange.jcontactsbusiness.employee.Employee;
 import org.mxchange.jcontactsbusiness.exceptions.basicdata.BusinessDataAlreadyAddedException;
 import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
+import org.mxchange.jusercore.model.user.User;
 
 /**
  * An administrative stateless session bean for business data
@@ -64,25 +66,40 @@ public class FinancialsAdminBusinessDataSessionBean extends BaseFinancialsDataba
                } else if (basicData.getBasicDataId() != null) {
                        // Should be null
                        throw new IllegalArgumentException(MessageFormat.format("basicData.basicDataId={0} - is not null", basicData.getBasicDataId())); //NOI18N
+               } else if (this.isSameCompanyNameAdded(basicData)) {
+                       // Throw exception
+                       throw new BusinessDataAlreadyAddedException(basicData);
                }
 
-               // Get all available entries
-               final List<BusinessBasicData> list = this.businessDataBean.allCompanyBasicData();
+               // Now add current date
+               basicData.setCompanyCreated(new GregorianCalendar());
 
-               // Is the list filled?
-               if (!list.isEmpty()) {
-                       // Then check each entry
-                       for (final BusinessBasicData entry : list) {
-                               // Is the company name matching?
-                               if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {
-                                       // Found match
-                                       throw new BusinessDataAlreadyAddedException(basicData);
-                               }
-                       }
+               // Is there a owner set?
+               if (basicData.getCompanyUserOwner() instanceof User) {
+                       // Get managed instance
+                       final User managedUser = this.createManaged(basicData.getCompanyUserOwner());
+
+                       // Set it back
+                       basicData.setCompanyUserOwner(managedUser);
                }
 
-               // Now add current date
-               basicData.setCompanyCreated(new GregorianCalendar());
+               // Is a founder set?
+               if (basicData.getCompanyFounder() instanceof Employee) {
+                       // Get managed instance
+                       final Employee managedEmployee = this.createManaged(basicData.getCompanyFounder());
+
+                       // Set it back
+                       basicData.setCompanyFounder(managedEmployee);
+               }
+
+               // Is a contact person set?
+               if (basicData.getCompanyContactEmployee() instanceof Employee) {
+                       // Get managed instance
+                       final Employee managedEmployee = this.createManaged(basicData.getCompanyContactEmployee());
+
+                       // Set it back
+                       basicData.setCompanyContactEmployee(managedEmployee);
+               }
 
                // Persist it
                this.getEntityManager().persist(basicData);
@@ -94,4 +111,31 @@ public class FinancialsAdminBusinessDataSessionBean extends BaseFinancialsDataba
                return basicData;
        }
 
+       /**
+        * Checks if given basic data is already added by it's company name
+        * <p>
+        * @param basicData Basic data to be checked
+        *
+        * @return Whether same company name has been used
+        */
+       private boolean isSameCompanyNameAdded (final BusinessBasicData basicData) {
+               // Get all available entries
+               final List<BusinessBasicData> list = this.businessDataBean.allCompanyBasicData();
+
+               // Default is not found
+               boolean isFound = false;
+
+               // Then check each entry
+               for (final BusinessBasicData entry : list) {
+                       // Is the company name matching?
+                       if (Objects.equals(entry.getCompanyName(), basicData.getCompanyName())) {
+                               // Found match
+                               isFound = true;
+                       }
+               }
+
+               // Return flag
+               return isFound;
+       }
+
 }
index c15a831e9b2abce1a9f2dab4089306c9dc00960c..3b3b213134fd1bfce68894bfa6ad5a20556a5b23 100644 (file)
@@ -77,7 +77,7 @@ public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsDataba
                // Is user instance set?
                if (branchOffice.getBranchCompany() instanceof BusinessBasicData) {
                        // Get managed instance back
-                       final BusinessBasicData managedBasicData = this.getManaged(branchOffice.getBranchCompany());
+                       final BusinessBasicData managedBasicData = this.createManaged(branchOffice.getBranchCompany());
 
                        // Set it back in branch office
                        branchOffice.setBranchCompany(managedBasicData);
@@ -86,7 +86,7 @@ public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsDataba
                // Is user instance set?
                if (branchOffice.getBranchUserOwner() instanceof User) {
                        // Get managed instance back
-                       final User managedUser = this.getManaged(branchOffice.getBranchUserOwner());
+                       final User managedUser = this.createManaged(branchOffice.getBranchUserOwner());
 
                        // Set it back in branch office
                        branchOffice.setBranchUserOwner(managedUser);
@@ -95,7 +95,7 @@ public class FinancialsAdminBranchOfficeSessionBean extends BaseFinancialsDataba
                // Is user instance set?
                if (branchOffice.getBranchCountry() instanceof Country) {
                        // Get managed instance back
-                       final Country managedCountry = this.getManaged(branchOffice.getBranchCountry());
+                       final Country managedCountry = this.createManaged(branchOffice.getBranchCountry());
 
                        // Set it back in branch office
                        branchOffice.setBranchCountry(managedCountry);
index 6ecf898c1afd13ad40f39460ac711d7a763fd509..42255e529004cd0b387077905770eabcab72d2f0 100644 (file)
@@ -31,6 +31,8 @@ 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.jcontactsbusiness.employee.CompanyEmployee;
+import org.mxchange.jcontactsbusiness.employee.Employee;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jcountry.data.Country;
 import org.mxchange.jcountry.data.CountryData;
@@ -283,9 +285,9 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
         * <p>
         * @return Managed contact instance
         */
-       protected Contact getManaged (final Contact contact) {
+       protected Contact createManaged (final Contact contact) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N
 
                // user should not be null
                if (null == contact) {
@@ -306,7 +308,7 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
                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
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedContact={1} - EXIT!", this.getClass().getSimpleName(), managedContact)); //NOI18N
 
                // Return it
                return managedContact;
@@ -319,9 +321,9 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
         * <p>
         * @return Managed country instance
         */
-       protected Country getManaged (final Country country) {
+       protected Country createManaged (final Country country) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N
 
                // user should not be null
                if (null == country) {
@@ -342,22 +344,22 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
                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
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCountry={1} - EXIT!", this.getClass().getSimpleName(), managedCountry)); //NOI18N
 
                // Return it
                return managedCountry;
        }
 
        /**
-        * Get back a managed instance from given contact
+        * Get back a managed instance from given basic data
         * <p>
-        * @param basicData Unmanaged/detached contact instance
+        * @param basicData Unmanaged/detached basic data instance
         * <p>
-        * @return Managed contact instance
+        * @return Managed basic data instance
         */
-       protected BusinessBasicData getManaged (final BusinessBasicData basicData) {
+       protected BusinessBasicData createManaged (final BusinessBasicData basicData) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: basicData={1} - CALLED!", this.getClass().getSimpleName(), basicData)); //NOI18N
 
                // user should not be null
                if (null == basicData) {
@@ -378,12 +380,48 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
                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
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedBasicData={1} - EXIT!", this.getClass().getSimpleName(), managedBasicData)); //NOI18N
 
                // Return it
                return managedBasicData;
        }
 
+       /**
+        * Get back a managed instance from given employee
+        * <p>
+        * @param employee Unmanaged/detached employee instance
+        * <p>
+        * @return Managed employee instance
+        */
+       protected Employee createManaged (final Employee employee) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: employee={1} - CALLED!", this.getClass().getSimpleName(), employee)); //NOI18N
+
+               // user should not be null
+               if (null == employee) {
+                       // Abort here
+                       throw new NullPointerException("employee is null"); //NOI18N
+               } else if (employee.getEmployeeId() == null) {
+                       // Id is set
+                       throw new NullPointerException("employee.employeeId is null"); //NOI18N
+               } else if (employee.getEmployeeId() < 1) {
+                       // Id is set
+                       throw new IllegalArgumentException(MessageFormat.format("employee.employeeId={0} is null", employee.getEmployeeId())); //NOI18N
+               }
+
+               // Try to find it (should be there)
+               final Employee managedEmployee = this.getEntityManager().find(CompanyEmployee.class, employee.getEmployeeId());
+
+               // Should be there
+               assert (managedEmployee instanceof Employee) : "managedEmployee is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedEmployee={1} - EXIT!", this.getClass().getSimpleName(), managedEmployee)); //NOI18N
+
+               // Return it
+               return managedEmployee;
+       }
+
        /**
         * Get back a managed instance from given user
         * <p>
@@ -391,9 +429,9 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
         * <p>
         * @return Managed user instance
         */
-       protected User getManaged (final User user) {
+       protected User createManaged (final User user) {
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
 
                // user should not be null
                if (null == user) {
@@ -423,7 +461,7 @@ public abstract class BaseFinancialsDatabaseBean extends BaseDatabaseBean {
                assert (managedUser instanceof User) : "managedUser is null"; //NOI18N
 
                // Trace message
-               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
 
                // Return it
                return managedUser;
index f6e2d1bd406d427923f94ef9117d2730c726c46b..21bdfecbc8ce1b2f52ef2eef2eccafb0bee4cd7a 100644 (file)
@@ -137,7 +137,7 @@ public class FinancialsAdminUserSessionBean extends BaseFinancialsDatabaseBean i
                }
 
                // Get a managed instance
-               final User managedUser = this.getManaged(user);
+               final User managedUser = this.createManaged(user);
 
                // Should be found!
                assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
index 7b2753d7391b659c99d0521a7b8ef5c4c54369ff..dcbf1d88d8bfadd1bd1d3deb8153fd8c30ccb975 100644 (file)
@@ -136,7 +136,7 @@ public class FinancialsUserActivityLogMessageBean extends BaseFinancialsDatabase
                }
 
                // Make user instance managed
-               final User managedUser = this.getManaged(userActivity.getActivityUser());
+               final User managedUser = this.createManaged(userActivity.getActivityUser());
 
                // Set it back
                userActivity.setActivityUser(managedUser);