2 * Copyright (C) 2017 - 2020 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.enterprise.product;
19 import java.text.MessageFormat;
20 import org.mxchange.jproduct.model.category.Category;
21 import org.mxchange.jproduct.model.product.Product;
22 import org.mxchange.jproduct.model.product.Products;
23 import org.mxchange.pizzaapplication.enterprise.BasePizzaEnterpriseBean;
26 * A general bean for product-related methods that can be generalized.
28 * @author Roland Haeder<roland@mxchange.org>
30 public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterpriseBean {
35 private static final long serialVersionUID = 523_676_481_092_175_621L;
38 * Protected constructor, no instance from this class.
40 protected BasePizzaProductEnterpriseBean () {
45 * Creates a managed instance from given category instance
47 * @param category Unmanaged category instance
49 * @return Managed instance
51 protected Category createManaged (final Category category) {
53 if (null == category) {
55 throw new NullPointerException("category is null");
56 } else if (category.getCategoryI18nKey() == null) {
58 throw new NullPointerException("category.categoryTitle is null");
59 } else if (category.getCategoryI18nKey().isEmpty()) {
61 throw new IllegalArgumentException("category.categoryTitle is empty");
62 } else if (category.getCategoryId() == null) {
64 throw new NullPointerException("category.categoryId is null");
65 } else if (category.getCategoryId() < 1) {
67 throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not valid.", category.getCategoryId()));
71 final Category managedCategory = this.getEntityManager().find(category.getClass(), category.getCategoryId());
74 assert (managedCategory instanceof Category) : "managedCategory is null"; //NOI18N
77 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCategory={1} - EXIT!", this.getClass().getSimpleName(), managedCategory)); //NOI18N
80 return managedCategory;
84 * Creates a managed instance from given product instance
86 * @param product Unmanaged product instance
88 * @return Managed instance
90 protected Product createManaged (final Product product) {
92 if (null == product) {
94 throw new NullPointerException("product is null");
95 } else if (product.getProductI18nKey() == null) {
97 throw new NullPointerException("product.productTitle is null");
98 } else if (product.getProductI18nKey().isEmpty()) {
100 throw new IllegalArgumentException("product.productTitle is empty");
101 } else if (product.getProductId() == null) {
103 throw new NullPointerException("product.productId is null");
104 } else if (product.getProductId() < 1) {
106 throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId()));
110 final Product managedProduct = this.getEntityManager().find(product.getClass(), product.getProductId());
113 assert (managedProduct instanceof Product) : "managedProduct is null"; //NOI18N
116 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
119 return managedProduct;
123 * Merges given product's data
125 * @param detachedProduct Product instance to merge
127 * @return Managed product instance
129 protected Product mergeProductData (final Product detachedProduct) {
131 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: detachedProduct={0} - CALLED!", detachedProduct)); //NOI18N
133 // The product instance must be valid
134 if (null == detachedProduct) {
136 throw new NullPointerException("detachedProduct is null"); //NOI18N
137 } else if (detachedProduct.getProductId() == null) {
139 throw new NullPointerException("detachedProduct.productId is null"); //NOI18N //NOI18N
140 } else if (detachedProduct.getProductId() < 1) {
142 throw new IllegalStateException(MessageFormat.format("detachedProduct.productId={0} is not valid.", detachedProduct.getProductId())); //NOI18N
145 // Set updated timestamp
146 detachedProduct.setProductEntryUpdated(new Date());
148 // Get product from it and find it
149 final Product foundProduct = this.getEntityManager().find(detachedProduct.getClass(), detachedProduct.getProductId());
152 assert (foundProduct instanceof Product) : MessageFormat.format("Product with id {0} not found, but should be.", detachedProduct.getProductId()); //NOI18N
155 this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeProductData: foundProduct.productId={0}", foundProduct.getProductId())); //NOI18N
158 Products.copyProductData(detachedProduct, foundProduct);
160 // Merge product instance
161 final Product managedProduct = this.getEntityManager().merge(foundProduct);
164 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: managedProduct={0} - EXIT!", managedProduct)); //NOI18N
166 // Return detached product
167 return managedProduct;