]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/jproduct/model/product/PizzaAdminGenericProductSessionBean.java
Updated copyright year
[pizzaservice-ejb.git] / src / java / org / mxchange / jproduct / model / product / PizzaAdminGenericProductSessionBean.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.jproduct.model.product;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.ejb.EJB;
24 import javax.ejb.Stateless;
25 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
26 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
27 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
28 import org.mxchange.jproduct.model.category.Category;
29 import org.mxchange.pizzaapplication.database.product.BasePizzaProductEnterpriseBean;
30
31 /**
32  * An administrative stateless session bean for generic products.
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.")
37 public class PizzaAdminGenericProductSessionBean extends BasePizzaProductEnterpriseBean implements AdminProductSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 184_573_667_215_549_811L;
43
44         /**
45          * Regular user bean
46          */
47         @EJB (lookup = "java:global/pizzaapplication-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote")
48         private ProductSessionBeanRemote productBean;
49
50         @Override
51         public Product addGenericProduct (final Product product) throws ProductAlreadyAddedException {
52                 // Trace message
53                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
54
55                 // Validate parameter
56                 if (null == product) {
57                         // Throw NPE
58                         throw new NullPointerException("product is null"); //NOI18N
59                 } else if (product.getProductI18nKey() == null) {
60                         // Throw it again
61                         throw new NullPointerException("product.productI18nKey is null"); //NOI18N
62                 } else if (product.getProductI18nKey().isEmpty()) {
63                         // Throw it again
64                         throw new IllegalArgumentException("product.productI18nKey is empty"); //NOI18N
65                 } else if (product.getProductId() != null) {
66                         // Throw IAE
67                         throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
68                 } else if (product.getProductCategory() == null) {
69                         // Throw NPE
70                         throw new NullPointerException("product.productCategory is null"); //NOI18N
71                 } else if (product.getProductCategory().getCategoryId() == null) {
72                         // Throw NPE
73                         throw new NullPointerException("product.productCategory.categoryId is null"); //NOI18N
74                 } else if (product.getProductCategory().getCategoryId() < 1) {
75                         // Throw IAE
76                         throw new IllegalArgumentException(MessageFormat.format("product.productCategory.categoryId={0} is invalid", product.getProductCategory().getCategoryId())); //NOI18N
77                 } else if ((product.getProductManufacturer() instanceof BasicData) && (product.getProductManufacturer().getBasicDataId() == null)) {
78                         // Throw NPE
79                         throw new NullPointerException("product.productManufacturer.basicDataId is null"); //NOI18N
80                 } else if ((product.getProductManufacturer() instanceof BasicData) && (product.getProductManufacturer().getBasicDataId() < 1)) {
81                         // Throw IAE
82                         throw new IllegalArgumentException(MessageFormat.format("product.productManufacturer.basicDataId={0} is invalid", product.getProductManufacturer().getBasicDataId())); //NOI18N
83                 } else if (this.isProductCreated(product)) {
84                         // Is already created (by name)
85                         throw new ProductAlreadyAddedException(product);
86                 }
87
88                 // Created a managed instance from product's category
89                 final Category managedCategory = this.createManaged(product.getProductCategory());
90
91                 // Set it back
92                 product.setProductCategory(managedCategory);
93
94                 // Is a manufacturer (basic_data entity) set?
95                 if (product.getProductManufacturer() instanceof BasicData) {
96                         // Get managed instance from it
97                         final BasicData managedBasicData = this.createManaged(product.getProductManufacturer());
98
99                         // Set it back
100                         product.setProductManufacturer(managedBasicData);
101                 }
102
103                 // Set created instance
104                 product.setProductCreated(new Date());
105
106                 // Persist it
107                 this.getEntityManager().persist(product);
108
109                 // Trace message
110                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product.productId={1} - EXIT!", this.getClass().getSimpleName(), product.getProductId())); //NOI18N
111
112                 // Return it
113                 return product;
114         }
115
116         @Override
117         public Product updateProductData (final Product product) throws ProductNotFoundException {
118                 // Log trace message
119                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
120
121                 // The product instance must be valid
122                 if (null == product) {
123                         // Throw NPE again
124                         throw new NullPointerException("product is null"); //NOI18N
125                 } else if (product.getProductId() == null) {
126                         // Throw NPE again
127                         throw new NullPointerException("product.productId is null"); //NOI18N
128                 } else if (product.getProductId() < 1) {
129                         // Not valid
130                         throw new IllegalStateException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId())); //NOI18N
131                 } else if (!this.isProductCreated(product)) {
132                         // No not found
133                         throw new ProductNotFoundException(product.getProductId());
134                 }
135
136                 // Merge data
137                 final Product detachedProduct = this.mergeProductData(product);
138
139                 // Trace message
140                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: detachedProduct={1} - EXIT!", this.getClass().getSimpleName(), detachedProduct)); //NOI18N
141
142                 // Return it
143                 return detachedProduct;
144         }
145
146         /**
147          * Checks if given product is already added by it's title.
148          * <p>
149          * @param product Product to check
150          * <p>
151          * @return Whether it has been found
152          */
153         private boolean isProductCreated (final Product product) {
154                 // Default is not found
155                 boolean isFound = false;
156
157                 // Get full list from other EJB
158                 final List<Product> list = this.productBean.allProducts();
159
160                 // Check each entry
161                 for (final Product createdProduct : list) {
162                         // Is same id or name?
163                         if (Objects.equals(product.getProductId(), createdProduct.getProductId())) {
164                                 // Found by id
165                                 isFound = true;
166                                 break;
167                         } else if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
168                                 // Found by i18n key, then stop here
169                                 isFound = true;
170                                 break;
171                         }
172                 }
173
174                 // Return result
175                 return isFound;
176         }
177
178 }