--- /dev/null
+/*
+ * Copyright (C) 2017 Roland Haeder<roland@mxchange.org>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+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
+ * <p>
+ * @param category Unmanaged category instance
+ * <p>
+ * @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;
+ }
+
+}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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.
+ * <p>
+ * @param category Category to check
+ * <p>
+ * @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<Category> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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<Category> 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<Category> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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.
+ * <p>
+ * @param product Product to check
+ * <p>
+ * @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<Product> 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;
+ }
+
+}
--- /dev/null
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+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.
+ * <p>
+ * @author Roland Häder<roland@mxchange.org>
+ */
+@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<Product> 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<Product> 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<Product> 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<Product> 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;
+ }
+
+}