From a3df9afd3f4eb080a468f2c036a72fc731415061 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 24 Nov 2019 05:38:26 +0100 Subject: [PATCH] Product-only: - renamed/moved file MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../BasePizzaProductEnterpriseBean.java | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java 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 index 0000000..ccd705f --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java @@ -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 . + */ +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. + *

+ * @author Roland Haeder + */ +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 + *

+ * @param category Unmanaged category instance + *

+ * @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 + *

+ * @param product Unmanaged product instance + *

+ * @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 + *

+ * @param detachedProduct Product instance to merge + *

+ * @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; + } + +} -- 2.39.5