]> git.mxchange.org Git - jfinancials-ejb.git/commitdiff
Maybe cherry-pick:
authorRoland Häder <roland@mxchange.org>
Sat, 28 Oct 2017 18:25:39 +0000 (20:25 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 28 Oct 2017 18:25:39 +0000 (20:25 +0200)
- added check for product's manufacturer/producer instance which is a BasicData
  implementation
- if it is set, it will be turned into a managed instance for preventing
  double-persisting

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java

index 847a33eaf6e107d68bf4bd96a9c7a3b879ca7eb5..3ce7eacd59950b32803e5e66e4d869d8fd718720 100644 (file)
@@ -22,6 +22,7 @@ import java.util.List;
 import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
+import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
 import org.mxchange.jfinancials.database.product.BaseFinancialsProductDatabaseBean;
 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
 import org.mxchange.jproduct.model.category.Category;
@@ -63,18 +64,39 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                } else if (product.getProductId() != null) {
                        // Throw IAE
                        throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
+               } else if (product.getProductCategory() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("product.productCategory is null"); //NOI18N
+               } else if (product.getProductCategory().getCategoryId() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("product.productCategory.categoryId is null"); //NOI18N
+               } else if (product.getProductCategory().getCategoryId() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("product.productCategory.categoryId={0} is invalid", product.getProductCategory().getCategoryId())); //NOI18N
+               } else if ((product.getProductManfacturer() instanceof BasicData) && (product.getProductManfacturer().getBasicDataId() == null)) {
+                       // Throw NPE
+                       throw new NullPointerException("product.productManufacturer.basicDataId is null"); //NOI18N
+               } else if ((product.getProductManfacturer() instanceof BasicData) && (product.getProductManfacturer().getBasicDataId() < 1)) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("product.productManufacturer.basicDataId={0} is invalid", product.getProductManfacturer().getBasicDataId())); //NOI18N
                } else if (this.isProductCreated(product)) {
                        // Is already created (by name)
                        throw new ProductAlreadyAddedException(product);
                }
 
-               // Is a category set?
-               if (product.getProductCategory()instanceof Category) {
-                       // Then make it managed
-                       final Category managedCategory = this.createManaged(product.getProductCategory());
+               // Created a managed instance from product's category
+               final Category managedCategory = this.createManaged(product.getProductCategory());
+
+               // Set it back
+               product.setProductCategory(managedCategory);
+
+               // Is a manufacturer (basic_data entity) set?
+               if (product.getProductManfacturer() instanceof BasicData) {
+                       // Get managed instance from it
+                       final BasicData managedBasicData = this.createManaged(product.getProductManfacturer());
 
                        // Set it back
-                       product.setProductCategory(managedCategory);
+                       product.setProductManfacturer(managedBasicData);
                }
 
                // Set created instance