2 * Copyright (C) 2017 Roland Häder
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.database.product.BaseFinancialsProductDatabaseBean;
27 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
28 import org.mxchange.jproduct.model.category.Category;
31 * An administrative stateless session bean for generic products.
33 * @author Roland Häder<roland@mxchange.org>
35 @Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.")
36 public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductDatabaseBean implements AdminProductSessionBeanRemote {
41 private static final long serialVersionUID = 184_573_667_215_549_811L;
46 @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote")
47 private ProductSessionBeanRemote productBean;
50 public Product addGenericProduct (final Product product) throws ProductAlreadyAddedException {
52 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
55 if (null == product) {
57 throw new NullPointerException("product is null"); //NOI18N
58 } else if (product.getProductI18nKey() == null) {
60 throw new NullPointerException("product.productI18nKey is null"); //NOI18N
61 } else if (product.getProductI18nKey().isEmpty()) {
63 throw new IllegalArgumentException("product.productI18nKey is empty"); //NOI18N
64 } else if (product.getProductId() != null) {
66 throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
67 } else if (product.getProductCategory() == null) {
69 throw new NullPointerException("product.productCategory is null"); //NOI18N
70 } else if (product.getProductCategory().getCategoryId() == null) {
72 throw new NullPointerException("product.productCategory.categoryId is null"); //NOI18N
73 } else if (product.getProductCategory().getCategoryId() < 1) {
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)) {
78 throw new NullPointerException("product.productManufacturer.basicDataId is null"); //NOI18N
79 } else if ((product.getProductManfacturer() instanceof BasicData) && (product.getProductManfacturer().getBasicDataId() < 1)) {
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);
87 // Created a managed instance from product's category
88 final Category managedCategory = this.createManaged(product.getProductCategory());
91 product.setProductCategory(managedCategory);
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());
99 product.setProductManfacturer(managedBasicData);
102 // Set created instance
103 product.setProductCreated(new Date());
106 this.getEntityManager().persist(product);
109 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product.productId={1} - EXIT!", this.getClass().getSimpleName(), product.getProductId())); //NOI18N
116 * Checks if given product is already added by it's title.
118 * @param product Product to check
120 * @return Whether it has been found
122 private boolean isProductCreated (final Product product) {
123 // Validate parameter
124 if (null == product) {
126 throw new NullPointerException("product is null"); //NOI18N
127 } else if (product.getProductI18nKey() == null) {
129 throw new NullPointerException("product.productI18nKey is null"); //NOI18N
130 } else if (product.getProductI18nKey().isEmpty()) {
132 throw new IllegalArgumentException("product.productI18nKey is empty"); //NOI18N
133 } else if (product.getProductId() != null) {
135 throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
138 // Default is not found
139 boolean isFound = false;
141 // Get full list from other EJB
142 final List<Product> list = this.productBean.allProducts();
145 for (final Product createdProduct : list) {
147 if (Objects.equals(createdProduct.getProductI18nKey(), product.getProductI18nKey())) {
148 // Found it, then stop here