]> git.mxchange.org Git - pizzaservice-ejb.git/commitdiff
Product-only:
authorRoland Häder <roland@mxchange.org>
Sun, 24 Nov 2019 04:38:26 +0000 (05:38 +0100)
committerRoland Häder <roland@mxchange.org>
Wed, 15 Jan 2020 10:03:03 +0000 (11:03 +0100)
- renamed/moved file

Signed-off-by: Roland Häder <roland@mxchange.org>
src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java [new file with mode: 0644]

diff --git a/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java b/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java
new file mode 100644 (file)
index 0000000..ccd705f
--- /dev/null
@@ -0,0 +1,169 @@
+/*
+ * 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
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.pizzaapplication.enterprise.product;
+
+import java.text.MessageFormat;
+import org.mxchange.jproduct.model.category.Category;
+import org.mxchange.jproduct.model.product.Product;
+import org.mxchange.jproduct.model.product.Products;
+import org.mxchange.pizzaapplication.enterprise.BasePizzaEnterpriseBean;
+
+/**
+ * A general bean for product-related methods that can be generalized.
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterpriseBean {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 523_676_481_092_175_621L;
+
+       /**
+        * Protected constructor, no instance from this class.
+        */
+       protected BasePizzaProductEnterpriseBean () {
+               super();
+       }
+
+       /**
+        * Creates a managed instance from given category instance
+        * <p>
+        * @param category Unmanaged category instance
+        * <p>
+        * @return Managed instance
+        */
+       protected Category createManaged (final Category category) {
+               // Validate parameter
+               if (null == category) {
+                       // Throw NPE
+                       throw new NullPointerException("category is null");
+               } else if (category.getCategoryI18nKey() == null) {
+                       // Throw it again
+                       throw new NullPointerException("category.categoryTitle is null");
+               } else if (category.getCategoryI18nKey().isEmpty()) {
+                       // Throw it again
+                       throw new IllegalArgumentException("category.categoryTitle is empty");
+               } else if (category.getCategoryId() == null) {
+                       // Throw it again
+                       throw new NullPointerException("category.categoryId is null");
+               } else if (category.getCategoryId() < 1) {
+                       // Throw it again
+                       throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not valid.", category.getCategoryId()));
+               }
+
+               // Try to find it
+               final Category managedCategory = this.getEntityManager().find(category.getClass(), category.getCategoryId());
+
+               // Should be there
+               assert (managedCategory instanceof Category) : "managedCategory is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCategory={1} - EXIT!", this.getClass().getSimpleName(), managedCategory)); //NOI18N
+
+               // Return it
+               return managedCategory;
+       }
+
+       /**
+        * Creates a managed instance from given product instance
+        * <p>
+        * @param product Unmanaged product instance
+        * <p>
+        * @return Managed instance
+        */
+       protected Product createManaged (final Product product) {
+               // Validate parameter
+               if (null == product) {
+                       // Throw NPE
+                       throw new NullPointerException("product is null");
+               } else if (product.getProductI18nKey() == null) {
+                       // Throw it again
+                       throw new NullPointerException("product.productTitle is null");
+               } else if (product.getProductI18nKey().isEmpty()) {
+                       // Throw it again
+                       throw new IllegalArgumentException("product.productTitle is empty");
+               } else if (product.getProductId() == null) {
+                       // Throw it again
+                       throw new NullPointerException("product.productId is null");
+               } else if (product.getProductId() < 1) {
+                       // Throw it again
+                       throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId()));
+               }
+
+               // Try to find it
+               final Product managedProduct = this.getEntityManager().find(product.getClass(), product.getProductId());
+
+               // Should be there
+               assert (managedProduct instanceof Product) : "managedProduct is null"; //NOI18N
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
+
+               // Return it
+               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("mergeProductData: foundProduct.productId={0}", foundProduct.getProductId())); //NOI18N
+
+               // Copy all
+               Products.copyAll(detachedProduct, foundProduct);
+
+               // Merge product instance
+               final Product managedProduct = this.getEntityManager().merge(foundProduct);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: managedProduct={0} - EXIT!", managedProduct)); //NOI18N
+
+               // Return detached product
+               return managedProduct;
+       }
+
+}