]> git.mxchange.org Git - jfinancials-ejb.git/commitdiff
Product-only:
authorRoland Häder <roland@mxchange.org>
Sat, 16 Jun 2018 13:17:55 +0000 (15:17 +0200)
committerRoland Häder <roland@mxchange.org>
Sat, 16 Jun 2018 13:17:55 +0000 (15:17 +0200)
- added mergeProductData()
- implemented updateProductData()

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

index bf5b5d0e55ebf9def34ca4d43e152da930de4401..f78f1d276a3a8db509ef56547e6672258e902c61 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
+ * Copyright (C) 2017, 2018 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
@@ -20,6 +20,7 @@ import java.text.MessageFormat;
 import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean;
 import org.mxchange.jproduct.model.category.Category;
 import org.mxchange.jproduct.model.product.Product;
+import org.mxchange.jproduct.model.product.Products;
 
 /**
  * A general bean for product-related methods that can be generalized.
@@ -118,4 +119,51 @@ public abstract class BaseFinancialsProductDatabaseBean extends BaseFinancialsDa
                return managedProduct;
        }
 
+       /**
+        * Merges given product's data
+        * <p>
+        * @param detachedProduct Product instance to merge
+        * <p>
+        * @return Detached product instance
+        */
+       protected Product mergeProductData (final Product detachedProduct) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: detachedProduct={0} - CALLED!", detachedProduct)); //NOI18N
+
+               // The product instance must be valid
+               if (null == detachedProduct) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedProduct is null"); //NOI18N
+               } else if (detachedProduct.getProductId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("detachedProduct.productId is null"); //NOI18N //NOI18N
+               } else if (detachedProduct.getProductId() < 1) {
+                       // Not valid
+                       throw new IllegalStateException(MessageFormat.format("detachedProduct.productId={0} is not valid.", detachedProduct.getProductId())); //NOI18N
+               }
+
+               // Set updated timestamp
+               // @TODO detachedProduct.setProductUpdated(new Date());
+               // Get product from it and find it
+               final Product foundProduct = this.getEntityManager().find(detachedProduct.getClass(), detachedProduct.getProductId());
+
+               // Should be found
+               assert (foundProduct instanceof Product) : MessageFormat.format("Product with id {0} not found, but should be.", detachedProduct.getProductId()); //NOI18N
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: foundProduct.productId={0}", foundProduct.getProductId())); //NOI18N
+
+               // Merge product instance
+               final Product managedProduct = this.getEntityManager().merge(foundProduct);
+
+               // Copy all
+               Products.copyAll(detachedProduct, managedProduct);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: detachedProduct={0} - EXIT!", managedProduct)); //NOI18N
+
+               // Return detached product
+               return managedProduct;
+       }
+
 }
index 0287935ac0123ca010b5f21fc35373c6d5db1b98..1e2723c0fc5f8eecf2a769b526c8e412ccca42a2 100644 (file)
@@ -25,6 +25,7 @@ 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.exceptions.product.ProductNotFoundException;
 import org.mxchange.jproduct.model.category.Category;
 
 /**
@@ -112,6 +113,36 @@ 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 detachedProduct = this.mergeProductData(product);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: detachedProduct={1} - EXIT!", this.getClass().getSimpleName(), detachedProduct)); //NOI18N
+
+               // Return it
+               return detachedProduct;
+       }
+
        /**
         * Checks if given product is already added by it's title.
         * <p>
@@ -120,21 +151,6 @@ 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.getProductI18nKey() == null) {
-                       // Throw it again
-                       throw new NullPointerException("product.productI18nKey is null"); //NOI18N
-               } else if (product.getProductI18nKey().isEmpty()) {
-                       // Throw it again
-                       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
-               }
-
                // Default is not found
                boolean isFound = false;
 
@@ -143,9 +159,13 @@ public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProd
 
                // Check each entry
                for (final Product createdProduct : list) {
-                       // Is same name?
-                       if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
-                               // 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;
                        }