]> git.mxchange.org Git - jfinancials-ejb.git/blobdiff - src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java
Product-only:
[jfinancials-ejb.git] / src / java / org / mxchange / jproduct / model / product / FinancialsAdminGenericProductSessionBean.java
index 3ce7eacd59950b32803e5e66e4d869d8fd718720..26aa418bc6c40d98f43661efb0971ef938bff546 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Roland Häder
+ * Copyright (C) 2017 - 2020 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -23,8 +23,9 @@ 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.jfinancials.enterprise.product.BaseFinancialsProductEnterpriseBean;
 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
+import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
 import org.mxchange.jproduct.model.category.Category;
 
 /**
@@ -33,7 +34,7 @@ import org.mxchange.jproduct.model.category.Category;
  * @author Roland Häder<roland@mxchange.org>
  */
 @Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.")
-public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductDatabaseBean implements AdminProductSessionBeanRemote {
+public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductEnterpriseBean implements AdminProductSessionBeanRemote {
 
        /**
         * Serial number
@@ -55,12 +56,12 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                if (null == product) {
                        // Throw NPE
                        throw new NullPointerException("product is null"); //NOI18N
-               } else if (product.getProductTitle() == null) {
+               } else if (product.getProductI18nKey() == null) {
                        // Throw it again
-                       throw new NullPointerException("product.productTitle is null"); //NOI18N
-               } else if (product.getProductTitle().isEmpty()) {
+                       throw new NullPointerException("product.productI18nKey is null"); //NOI18N
+               } else if (product.getProductI18nKey().isEmpty()) {
                        // Throw it again
-                       throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N
+                       throw new IllegalArgumentException("product.productI18nKey is empty"); //NOI18N
                } else if (product.getProductId() != null) {
                        // Throw IAE
                        throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
@@ -73,12 +74,12 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                } 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)) {
+               } else if ((product.getProductManufacturer() instanceof BasicData) && (product.getProductManufacturer().getBasicDataId() == null)) {
                        // Throw NPE
                        throw new NullPointerException("product.productManufacturer.basicDataId is null"); //NOI18N
-               } else if ((product.getProductManfacturer() instanceof BasicData) && (product.getProductManfacturer().getBasicDataId() < 1)) {
+               } else if ((product.getProductManufacturer() instanceof BasicData) && (product.getProductManufacturer().getBasicDataId() < 1)) {
                        // Throw IAE
-                       throw new IllegalArgumentException(MessageFormat.format("product.productManufacturer.basicDataId={0} is invalid", product.getProductManfacturer().getBasicDataId())); //NOI18N
+                       throw new IllegalArgumentException(MessageFormat.format("product.productManufacturer.basicDataId={0} is invalid", product.getProductManufacturer().getBasicDataId())); //NOI18N
                } else if (this.isProductCreated(product)) {
                        // Is already created (by name)
                        throw new ProductAlreadyAddedException(product);
@@ -91,16 +92,16 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                product.setProductCategory(managedCategory);
 
                // Is a manufacturer (basic_data entity) set?
-               if (product.getProductManfacturer() instanceof BasicData) {
+               if (product.getProductManufacturer() instanceof BasicData) {
                        // Get managed instance from it
-                       final BasicData managedBasicData = this.createManaged(product.getProductManfacturer());
+                       final BasicData managedBasicData = this.createManaged(product.getProductManufacturer());
 
                        // Set it back
-                       product.setProductManfacturer(managedBasicData);
+                       product.setProductManufacturer(managedBasicData);
                }
 
                // Set created instance
-               product.setProductCreated(new Date());
+               product.setProductEntryCreated(new Date());
 
                // Persist it
                this.getEntityManager().persist(product);
@@ -112,6 +113,39 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
                return product;
        }
 
+       @Override
+       public Product updateProductData (final Product product) throws ProductNotFoundException {
+               // Log trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
+
+               // The product instance must be valid
+               if (null == product) {
+                       // Throw NPE again
+                       throw new NullPointerException("product is null"); //NOI18N
+               } else if (product.getProductId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("product.productId is null"); //NOI18N
+               } else if (product.getProductId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId())); //NOI18N
+               } else if (!this.isProductCreated(product)) {
+                       // No not found
+                       throw new ProductNotFoundException(product.getProductId());
+               }
+
+               // Merge data
+               final Product managedProduct = this.mergeGenericProductData(product);
+
+               // Set updated instance
+               managedProduct.setProductEntryUpdated(new Date());
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
+
+               // Return it
+               return managedProduct;
+       }
+
        /**
         * Checks if given product is already added by it's title.
         * <p>
@@ -120,32 +154,21 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
         * @return Whether it has been found
         */
        private boolean isProductCreated (final Product product) {
-               // Validate parameter
-               if (null == product) {
-                       // Throw NPE
-                       throw new NullPointerException("product is null"); //NOI18N
-               } else if (product.getProductTitle() == null) {
-                       // Throw it again
-                       throw new NullPointerException("product.productTitle is null"); //NOI18N
-               } else if (product.getProductTitle().isEmpty()) {
-                       // Throw it again
-                       throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N
-               } else if (product.getProductId() != null) {
-                       // Throw IAE
-                       throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
-               }
-
                // Default is not found
                boolean isFound = false;
 
                // Get full list from other EJB
-               final List<Product> list = this.productBean.allProducts();
+               final List<Product> list = this.productBean.fetchAllProducts();
 
                // Check each entry
                for (final Product createdProduct : list) {
-                       // Is same name?
-                       if (Objects.equals(createdProduct.getProductTitle(), product.getProductTitle())) {
-                               // Found it, then stop here
+                       // Is same id or name?
+                       if (Objects.equals(product.getProductId(), createdProduct.getProductId())) {
+                               // Found by id
+                               isFound = true;
+                               break;
+                       } else if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
+                               // Found by i18n key, then stop here
                                isFound = true;
                                break;
                        }