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