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