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.jfinancials.database.product.BaseFinancialsProductDatabaseBean;
26 import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException;
27 import org.mxchange.jproduct.model.category.Category;
30 * An administrative stateless session bean for generic products.
32 * @author Roland Häder<roland@mxchange.org>
34 @Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.")
35 public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductDatabaseBean implements AdminProductSessionBeanRemote {
40 private static final long serialVersionUID = 184_573_667_215_549_811L;
45 @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote")
46 private ProductSessionBeanRemote productBean;
49 public Product addGenericProduct (final Product product) throws ProductAlreadyAddedException {
51 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N
54 if (null == product) {
56 throw new NullPointerException("product is null"); //NOI18N
57 } else if (product.getProductTitle() == null) {
59 throw new NullPointerException("product.productTitle is null"); //NOI18N
60 } else if (product.getProductTitle().isEmpty()) {
62 throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N
63 } else if (product.getProductId() != null) {
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);
72 if (product.getProductCategory()instanceof Category) {
73 // Then make it managed
74 final Category managedCategory = this.createManaged(product.getProductCategory());
77 product.setProductCategory(managedCategory);
80 // Set created instance
81 product.setProductCreated(new Date());
84 this.getEntityManager().persist(product);
87 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product.productId={1} - EXIT!", this.getClass().getSimpleName(), product.getProductId())); //NOI18N
94 * Checks if given product is already added by it's title.
96 * @param product Product to check
98 * @return Whether it has been found
100 private boolean isProductCreated (final Product product) {
101 // Validate parameter
102 if (null == product) {
104 throw new NullPointerException("product is null"); //NOI18N
105 } else if (product.getProductTitle() == null) {
107 throw new NullPointerException("product.productTitle is null"); //NOI18N
108 } else if (product.getProductTitle().isEmpty()) {
110 throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N
111 } else if (product.getProductId() != null) {
113 throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N
116 // Default is not found
117 boolean isFound = false;
119 // Get full list from other EJB
120 final List<Product> list = this.productBean.allProducts();
123 for (final Product createdProduct : list) {
125 if (Objects.equals(createdProduct.getProductTitle(), product.getProductTitle())) {
126 // Found it, then stop here