From: Roland Häder Date: Sat, 28 Oct 2017 18:25:39 +0000 (+0200) Subject: Maybe cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=175777d2f6ada0099b31aba65ce24371cabd73f5;p=jfinancials-ejb.git Maybe cherry-pick: - 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 --- diff --git a/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java index 847a33e..3ce7eac 100644 --- a/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java +++ b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java @@ -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