2 * Copyright (C) 2017 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jproduct.model.product;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
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;
32 * An administrative stateless session bean for generic products.
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.")
37 public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductEnterpriseBean implements AdminProductSessionBeanRemote {
42 private static final long serialVersionUID = 184_573_667_215_549_811L;
47 @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote")
48 private ProductSessionBeanRemote productBean;
51 public Product addGenericProduct (final Product product) throws ProductAlreadyAddedException {
53 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
56 if (null == product) {
58 throw new NullPointerException("product is null"); //NOI18N
59 } else if (product.getProductI18nKey() == null) {
61 throw new NullPointerException("product.productI18nKey is null"); //NOI18N
62 } else if (product.getProductI18nKey().isEmpty()) {
64 throw new IllegalArgumentException("product.productI18nKey is empty"); //NOI18N
65 } else if (product.getProductId() != null) {
67 throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
68 } else if (product.getProductCategory() == null) {
70 throw new NullPointerException("product.productCategory is null"); //NOI18N
71 } else if (product.getProductCategory().getCategoryId() == null) {
73 throw new NullPointerException("product.productCategory.categoryId is null"); //NOI18N
74 } else if (product.getProductCategory().getCategoryId() < 1) {
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)) {
79 throw new NullPointerException("product.productManufacturer.basicDataId is null"); //NOI18N
80 } else if ((product.getProductManufacturer() instanceof BasicData) && (product.getProductManufacturer().getBasicDataId() < 1)) {
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);
88 // Created a managed instance from product's category
89 final Category managedCategory = this.createManaged(product.getProductCategory());
92 product.setProductCategory(managedCategory);
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());
100 product.setProductManufacturer(managedBasicData);
103 // Set created instance
104 product.setProductEntryCreated(new Date());
107 this.getEntityManager().persist(product);
110 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product.productId={1} - EXIT!", this.getClass().getSimpleName(), product.getProductId())); //NOI18N
117 public Product updateProductData (final Product product) throws ProductNotFoundException {
119 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
121 // The product instance must be valid
122 if (null == product) {
124 throw new NullPointerException("product is null"); //NOI18N
125 } else if (product.getProductId() == null) {
127 throw new NullPointerException("product.productId is null"); //NOI18N
128 } else if (product.getProductId() < 1) {
130 throw new IllegalStateException(MessageFormat.format("product.productId={0} is not valid.", product.getProductId())); //NOI18N
131 } else if (!this.isProductCreated(product)) {
133 throw new ProductNotFoundException(product.getProductId());
137 final Product managedProduct = this.mergeGenericProductData(product);
139 // Set updated instance
140 managedProduct.setProductEntryUpdated(new Date());
143 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateProductData: managedProduct={1} - EXIT!", this.getClass().getSimpleName(), managedProduct)); //NOI18N
146 return managedProduct;
150 * Checks if given product is already added by it's title.
152 * @param product Product to check
154 * @return Whether it has been found
156 private boolean isProductCreated (final Product product) {
157 // Default is not found
158 boolean isFound = false;
160 // Get full list from other EJB
161 final List<Product> list = this.productBean.fetchAllProducts();
164 for (final Product createdProduct : list) {
165 // Is same id or name?
166 if (Objects.equals(product.getProductId(), createdProduct.getProductId())) {
170 } else if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
171 // Found by i18n key, then stop here