From 9bc90800591f0953dba9cb3d63363f791cdad2e0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 23 Oct 2017 23:11:35 +0200 Subject: [PATCH] Maybe cherry-pick: - added EJBs for product categories and generic products, each with general and administrative purposes - implemented all methods - added generic jproduct-core specific, abstract class with first method "createManaged(category)" MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../BaseFinancialsProductDatabaseBean.java | 81 ++++++++++ ...ncialsAdminProductCategorySessionBean.java | 143 ++++++++++++++++++ .../FinancialsProductCategorySessionBean.java | 65 ++++++++ ...ancialsAdminGenericProductSessionBean.java | 136 +++++++++++++++++ .../FinancialsGenericProductSessionBean.java | 76 ++++++++++ 5 files changed, 501 insertions(+) create mode 100644 src/java/org/mxchange/jfinancials/database/product/BaseFinancialsProductDatabaseBean.java create mode 100644 src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java create mode 100644 src/java/org/mxchange/jproduct/model/category/FinancialsProductCategorySessionBean.java create mode 100644 src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java create mode 100644 src/java/org/mxchange/jproduct/model/product/FinancialsGenericProductSessionBean.java diff --git a/src/java/org/mxchange/jfinancials/database/product/BaseFinancialsProductDatabaseBean.java b/src/java/org/mxchange/jfinancials/database/product/BaseFinancialsProductDatabaseBean.java new file mode 100644 index 0000000..df1f961 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/database/product/BaseFinancialsProductDatabaseBean.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jfinancials.database.product; + +import java.text.MessageFormat; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jproduct.model.category.Category; + +/** + * A general bean for product-related methods that can be generalized. + *

+ * @author Roland Haeder + */ +public abstract class BaseFinancialsProductDatabaseBean extends BaseFinancialsDatabaseBean { + + /** + * Serial number + */ + private static final long serialVersionUID = 523_676_481_092_175_621L; + + /** + * Protected constructor, no instance from this class. + */ + protected BaseFinancialsProductDatabaseBean () { + super(); + } + + /** + * Creates a managed instance from given category instance + *

+ * @param category Unmanaged category instance + *

+ * @return Managed instance + */ + protected Category createManaged (final Category category) { + // Validate parameter + if (null == category) { + // Throw NPE + throw new NullPointerException("category is null"); + } else if(category.getCategoryTitle() == null) { + // Throw it again + throw new NullPointerException("category.categoryTitle is null"); + } else if (category.getCategoryTitle().isEmpty()) { + // Throw it again + throw new IllegalArgumentException("category.categoryTitle is empty"); + } else if(category.getCategoryId() == null) { + // Throw it again + throw new NullPointerException("category.categoryId is null"); + } else if(category.getCategoryId() < 1) { + // Throw it again + throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not valid.", category.getCategoryId())); + } + + // Try to find it + final Category managedCategory = this.getEntityManager().find(category.getClass(), category.getCategoryId()); + + // Should be there + assert (managedCategory instanceof Category) : "managedCategory is null"; //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.createManaged: managedCategory={1} - EXIT!", this.getClass().getSimpleName(), managedCategory)); //NOI18N + + // Return it + return managedCategory; + } + +} diff --git a/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java b/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java new file mode 100644 index 0000000..8d5cbc0 --- /dev/null +++ b/src/java/org/mxchange/jproduct/model/category/FinancialsAdminProductCategorySessionBean.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jproduct.model.category; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.product.BaseFinancialsProductDatabaseBean; +import org.mxchange.jproduct.exceptions.category.CategoryAlreadyAddedException; + +/** + * An administrative stateless session bean for product categories. + *

+ * @author Roland Häder + */ +@Stateless (name = "adminCategory", description = "An administrative stateless session bean for product categories.") +public class FinancialsAdminProductCategorySessionBean extends BaseFinancialsProductDatabaseBean implements AdminCategorySessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 23_865_767_165_437_802L; + + /** + * Regular user bean + */ + @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote") + private CategorySessionBeanRemote categoryBean; + + /** + * Default constructor + */ + public FinancialsAdminProductCategorySessionBean () { + // Call super constructor + super(); + } + + @Override + public Category addProductCategory (final Category category) throws CategoryAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.adminAddProductCategory: category={1} - CALLED!", this.getClass().getSimpleName(), category)); //NOI18N + + // Validate parameter + if (null == category) { + // Throw NPE + throw new NullPointerException("category is null"); //NOI18N + } else if (category.getCategoryTitle() == null) { + // Throw it again + throw new NullPointerException("category.categoryTitle is null"); //NOI18N + } else if (category.getCategoryTitle().isEmpty()) { + // Throw it again + throw new IllegalArgumentException("category.categoryTitle is empty"); //NOI18N + } else if (category.getCategoryId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not expected.", category.getCategoryId())); //NOI18N + } else if (this.isCategoryCreated(category)) { + // Is already created (by name) + throw new CategoryAlreadyAddedException(category); + } + + // Is a parent category set? + if (category.getParentCategory() instanceof Category) { + // Then make it managed + final Category managedCategory = this.createManaged(category.getParentCategory()); + + // Set it back + category.setParentCategory(managedCategory); + } + + // Set created instance + category.setCategoryCreated(new Date()); + + // Persist it + this.getEntityManager().persist(category); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.adminAddProductCategory: category.categoryId={1} - EXIT!", this.getClass().getSimpleName(), category.getCategoryId())); //NOI18N + + // Return it + return category; + } + + /** + * Checks if given category is already added by it's title. + *

+ * @param category Category to check + *

+ * @return Whether it has been found + */ + private boolean isCategoryCreated (final Category category) { + // Validate parameter + if (null == category) { + // Throw NPE + throw new NullPointerException("category is null"); //NOI18N + } else if (category.getCategoryTitle() == null) { + // Throw it again + throw new NullPointerException("category.categoryTitle is null"); //NOI18N + } else if (category.getCategoryTitle().isEmpty()) { + // Throw it again + throw new IllegalArgumentException("category.categoryTitle is empty"); //NOI18N + } else if (category.getCategoryId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("category.categoryId={0} is not expected.", category.getCategoryId())); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Get full list from other EJB + final List list = this.categoryBean.allCategories(); + + // Check each entry + for (final Category createdCategory : list) { + // Is same name? + if (Objects.equals(createdCategory.getCategoryTitle(), category.getCategoryTitle())) { + // Found it, then stop here + isFound = true; + break; + } + } + + // Return result + return isFound; + } + +} diff --git a/src/java/org/mxchange/jproduct/model/category/FinancialsProductCategorySessionBean.java b/src/java/org/mxchange/jproduct/model/category/FinancialsProductCategorySessionBean.java new file mode 100644 index 0000000..b33479d --- /dev/null +++ b/src/java/org/mxchange/jproduct/model/category/FinancialsProductCategorySessionBean.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jproduct.model.category; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; + +/** + * A general stateless session bean for product categories. + *

+ * @author Roland Häder + */ +@Stateless (name = "category", description = "A general stateless session bean for product categories.") +public class FinancialsProductCategorySessionBean extends BaseFinancialsDatabaseBean implements CategorySessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 23_865_767_165_437_801L; + + /** + * Default constructor + */ + public FinancialsProductCategorySessionBean () { + // Call super constructor + super(); + } + + @Override + @SuppressWarnings ("unchecked") + public List allCategories () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCategories: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + final Query query = this.getEntityManager().createNamedQuery("AllProductCategories", ProductCategory.class); //NOI18N + + // Get list from it + final List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCategories: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + +} diff --git a/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java new file mode 100644 index 0000000..847a33e --- /dev/null +++ b/src/java/org/mxchange/jproduct/model/product/FinancialsAdminGenericProductSessionBean.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jproduct.model.product; + +import java.text.MessageFormat; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.product.BaseFinancialsProductDatabaseBean; +import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException; +import org.mxchange.jproduct.model.category.Category; + +/** + * An administrative stateless session bean for generic products. + *

+ * @author Roland Häder + */ +@Stateless (name = "adminProduct", description = "A stateless session bean for general purposes for generic products.") +public class FinancialsAdminGenericProductSessionBean extends BaseFinancialsProductDatabaseBean implements AdminProductSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 184_573_667_215_549_811L; + + /** + * Regular user bean + */ + @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote") + private ProductSessionBeanRemote productBean; + + @Override + public Product addGenericProduct (final Product product) throws ProductAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product={1} - CALLED!", this.getClass().getSimpleName(), product)); //NOI18N + + // Validate parameter + if (null == product) { + // Throw NPE + throw new NullPointerException("product is null"); //NOI18N + } else if (product.getProductTitle() == null) { + // Throw it again + throw new NullPointerException("product.productTitle is null"); //NOI18N + } else if (product.getProductTitle().isEmpty()) { + // Throw it again + throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N + } else if (product.getProductId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N + } else if (this.isProductCreated(product)) { + // Is already created (by name) + throw new ProductAlreadyAddedException(product); + } + + // Is a category set? + if (product.getProductCategory()instanceof Category) { + // Then make it managed + final Category managedCategory = this.createManaged(product.getProductCategory()); + + // Set it back + product.setProductCategory(managedCategory); + } + + // Set created instance + product.setProductCreated(new Date()); + + // Persist it + this.getEntityManager().persist(product); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addGenericProduct: product.productId={1} - EXIT!", this.getClass().getSimpleName(), product.getProductId())); //NOI18N + + // Return it + return product; + } + + /** + * Checks if given product is already added by it's title. + *

+ * @param product Product to check + *

+ * @return Whether it has been found + */ + private boolean isProductCreated (final Product product) { + // Validate parameter + if (null == product) { + // Throw NPE + throw new NullPointerException("product is null"); //NOI18N + } else if (product.getProductTitle() == null) { + // Throw it again + throw new NullPointerException("product.productTitle is null"); //NOI18N + } else if (product.getProductTitle().isEmpty()) { + // Throw it again + throw new IllegalArgumentException("product.productTitle is empty"); //NOI18N + } else if (product.getProductId() != null) { + // Throw IAE + throw new IllegalArgumentException(MessageFormat.format("product.productId={0} is not expected.", product.getProductId())); //NOI18N + } + + // Default is not found + boolean isFound = false; + + // Get full list from other EJB + final List list = this.productBean.allProducts(); + + // Check each entry + for (final Product createdProduct : list) { + // Is same name? + if (Objects.equals(createdProduct.getProductTitle(), product.getProductTitle())) { + // Found it, then stop here + isFound = true; + break; + } + } + + // Return result + return isFound; + } + +} diff --git a/src/java/org/mxchange/jproduct/model/product/FinancialsGenericProductSessionBean.java b/src/java/org/mxchange/jproduct/model/product/FinancialsGenericProductSessionBean.java new file mode 100644 index 0000000..12c50bf --- /dev/null +++ b/src/java/org/mxchange/jproduct/model/product/FinancialsGenericProductSessionBean.java @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jproduct.model.product; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; + +/** + * A general stateless session bean for generic products. + *

+ * @author Roland Häder + */ +@Stateless (name = "product", description = "A general stateless session bean for generic products.") +public class FinancialsGenericProductSessionBean extends BaseFinancialsDatabaseBean implements ProductSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 184_573_667_215_549_810L; + + @Override + @SuppressWarnings ("unchecked") + public List allAvailableProducts () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allAvailableProducts: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + final Query query = this.getEntityManager().createNamedQuery("AllAvailableProducts", GenericProduct.class); //NOI18N + + // Get list from it + final List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allAvailableProducts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @Override + @SuppressWarnings ("unchecked") + public List allProducts () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allProducts: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + final Query query = this.getEntityManager().createNamedQuery("AllProducts", GenericProduct.class); //NOI18N + + // Get list from it + final List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allProducts: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + +} -- 2.39.5