]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/enterprise/product/BasePizzaProductEnterpriseBean.java
Product-only:
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / enterprise / product / BasePizzaProductEnterpriseBean.java
1 /*
2  * Copyright (C) 2017 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.enterprise.product;
18
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;
24
25 /**
26  * A general bean for product-related methods that can be generalized.
27  * <p>
28  * @author Roland Haeder<roland@mxchange.org>
29  */
30 public abstract class BasePizzaProductEnterpriseBean extends BasePizzaEnterpriseBean {
31
32         /**
33          * Serial number
34          */
35         private static final long serialVersionUID = 523_676_481_092_175_621L;
36
37         /**
38          * Protected constructor, no instance from this class.
39          */
40         protected BasePizzaProductEnterpriseBean () {
41                 super();
42         }
43
44         /**
45          * Creates a managed instance from given category instance
46          * <p>
47          * @param category Unmanaged category instance
48          * <p>
49          * @return Managed instance
50          */
51         protected Category createManaged (final Category category) {
52                 // Validate parameter
53                 if (null == category) {
54                         // Throw NPE
55                         throw new NullPointerException("category is null");
56                 } else if (category.getCategoryI18nKey() == null) {
57                         // Throw it again
58                         throw new NullPointerException("category.categoryTitle is null");
59                 } else if (category.getCategoryI18nKey().isEmpty()) {
60                         // Throw it again
61                         throw new IllegalArgumentException("category.categoryTitle is empty");
62                 } else if (category.getCategoryId() == null) {
63                         // Throw it again
64                         throw new NullPointerException("category.categoryId is null");
65                 } else if (category.getCategoryId() < 1) {
66                         // Throw it again
67                         throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not valid.", category.getCategoryId()));
68                 }
69
70                 // Try to find it
71                 final Category managedCategory = this.getEntityManager().find(category.getClass(), category.getCategoryId());
72
73                 // Should be there
74                 assert (managedCategory instanceof Category) : "managedCategory is null"; //NOI18N
75
76                 // Trace message
77                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCategory={1} - EXIT!", this.getClass().getSimpleName(), managedCategory)); //NOI18N
78
79                 // Return it
80                 return managedCategory;
81         }
82
83         /**
84          * Creates a managed instance from given product instance
85          * <p>
86          * @param product Unmanaged product instance
87          * <p>
88          * @return Managed instance
89          */
90         protected Product createManaged (final Product product) {
91                 // Validate parameter
92                 if (null == product) {
93                         // Throw NPE
94                         throw new NullPointerException("product is null");
95                 } else if (product.getProductI18nKey() == null) {
96                         // Throw it again
97                         throw new NullPointerException("product.productTitle is null");
98                 } else if (product.getProductI18nKey().isEmpty()) {
99                         // Throw it again
100                         throw new IllegalArgumentException("product.productTitle is empty");
101                 } else if (product.getProductId() == null) {
102                         // Throw it again
103                         throw new NullPointerException("product.productId is null");
104                 } else if (product.getProductId() < 1) {
105                         // Throw it again
106                         throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId()));
107                 }
108
109                 // Try to find it
110                 final Product managedProduct = this.getEntityManager().find(product.getClass(), product.getProductId());
111
112                 // Should be there
113                 assert (managedProduct instanceof Product) : "managedProduct is null"; //NOI18N
114
115                 // Trace message
116                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
117
118                 // Return it
119                 return managedProduct;
120         }
121
122         /**
123          * Merges given product's data
124          * <p>
125          * @param detachedProduct Product instance to merge
126          * <p>
127          * @return Managed product instance
128          */
129         protected Product mergeProductData (final Product detachedProduct) {
130                 // Trace message
131                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: detachedProduct={0} - CALLED!", detachedProduct)); //NOI18N
132
133                 // The product instance must be valid
134                 if (null == detachedProduct) {
135                         // Throw NPE again
136                         throw new NullPointerException("detachedProduct is null"); //NOI18N
137                 } else if (detachedProduct.getProductId() == null) {
138                         // Throw NPE again
139                         throw new NullPointerException("detachedProduct.productId is null"); //NOI18N //NOI18N
140                 } else if (detachedProduct.getProductId() < 1) {
141                         // Not valid
142                         throw new IllegalStateException(MessageFormat.format("detachedProduct.productId={0} is not valid.", detachedProduct.getProductId())); //NOI18N
143                 }
144
145                 // Set updated timestamp
146                 detachedProduct.setProductEntryUpdated(new Date());
147
148                 // Get product from it and find it
149                 final Product foundProduct = this.getEntityManager().find(detachedProduct.getClass(), detachedProduct.getProductId());
150
151                 // Should be found
152                 assert (foundProduct instanceof Product) : MessageFormat.format("Product with id {0} not found, but should be.", detachedProduct.getProductId()); //NOI18N
153
154                 // Debug message
155                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("mergeProductData: foundProduct.productId={0}", foundProduct.getProductId())); //NOI18N
156
157                 // Copy all
158                 Products.copyProductData(detachedProduct, foundProduct);
159
160                 // Merge product instance
161                 final Product managedProduct = this.getEntityManager().merge(foundProduct);
162
163                 // Trace message
164                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeProductData: managedProduct={0} - EXIT!", managedProduct)); //NOI18N
165
166                 // Return detached product
167                 return managedProduct;
168         }
169
170 }