]> git.mxchange.org Git - jfinancials-ejb.git/blob - src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java
Product-only:
[jfinancials-ejb.git] / src / java / org / mxchange / jproduct / model / product / FinancialsAdminGenericProductSessionBean.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.jfinancials.enterprise.product.BaseFinancialsProductEnterpriseBean;
27 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
28 import org.mxchange.jproduct.exceptions.product.ProductNotFoundException;
29 import org.mxchange.jproduct.model.category.Category;
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 FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductEnterpriseBean 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/jfinancials-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.setProductEntryCreated(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 managedProduct = this.mergeProductData(product);
138
139                 // Set updated instance
140                 managedProduct.setProductEntryUpdated(new Date());
141
142                 // Trace message
143                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
144
145                 // Return it
146                 return managedProduct;
147         }
148
149         /**
150          * Checks if given product is already added by it's title.
151          * <p>
152          * @param product Product to check
153          * <p>
154          * @return Whether it has been found
155          */
156         private boolean isProductCreated (final Product product) {
157                 // Default is not found
158                 boolean isFound = false;
159
160                 // Get full list from other EJB
161                 final List<Product> list = this.productBean.fetchAllProducts();
162
163                 // Check each entry
164                 for (final Product createdProduct : list) {
165                         // Is same id or name?
166                         if (Objects.equals(product.getProductId(), createdProduct.getProductId())) {
167                                 // Found by id
168                                 isFound = true;
169                                 break;
170                         } else if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
171                                 // Found by i18n key, then stop here
172                                 isFound = true;
173                                 break;
174                         }
175                 }
176
177                 // Return result
178                 return isFound;
179         }
180
181 }