From: Roland Häder Date: Tue, 31 Oct 2017 19:04:59 +0000 (+0100) Subject: Maybe cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=df74b3d8a6f2f1402adfbe36d4cc60629adef510;p=jfinancials-war.git Maybe cherry-pick: - renamed "category" path -> "product_category" - renamed "product" path -> "generic_product" Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestBean.java deleted file mode 100644 index 1764c64f..00000000 --- a/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestBean.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.category; - -import javax.ejb.EJB; -import javax.enterprise.context.RequestScoped; -import javax.enterprise.event.Event; -import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.jfinancials.beans.BaseFinancialsBean; -import org.mxchange.jproduct.events.category.AddedCategoryEvent; -import org.mxchange.jproduct.events.category.CategoryAddedEvent; -import org.mxchange.jproduct.exceptions.category.CategoryAlreadyAddedException; -import org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote; -import org.mxchange.jproduct.model.category.Category; -import org.mxchange.jproduct.model.category.ProductCategory; - -/** - * Main application class - *

- * @author Roland Häder - */ -@Named ("adminCategoryController") -@RequestScoped -public class FinancialAdminCategoryWebRequestBean extends BaseFinancialsBean implements FinancialAdminCategoryWebRequestController { - - /** - * Serial number - */ - private static final long serialVersionUID = 5_819_375_183_472_871L; - - /** - * Event for added shop categories - */ - @Inject - @Any - private Event categoryAddedEvent; - - /** - * Remote bean for categories - */ - @EJB (lookup = "java:global/jfinancials-ejb/adminCategory!org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote") - private AdminCategorySessionBeanRemote categoryBean; - - /** - * Whether this category is shown in statistics - */ - private Boolean categoryShownInStatistics; - - /** - * Category categoryI18nKey - */ - private String categoryI18nKey; - - /** - * Parent category - */ - private Category parentCategory; - - /** - * Default constructor - */ - public FinancialAdminCategoryWebRequestBean () { - // Call super constructor - super(); - } - - /** - * Adds given category data from request to database - *

- * @throws javax.faces.view.facelets.FaceletException If something - * unexpected happened - */ - public void addCategory () throws FaceletException { - // Create category - final Category category = this.createCategoryInstance(); - - // Declare updated category instance - final Category updatedCategory; - - try { - // Deligate to remote bean - updatedCategory = this.categoryBean.addProductCategory(category); - } catch (final CategoryAlreadyAddedException ex) { - // Continue to throw - throw new FaceletException(ex); - } - - // Fire event - this.categoryAddedEvent.fire(new CategoryAddedEvent(updatedCategory)); - - // Unset all older values - this.clear(); - } - - /** - * Getter for whether category is shown in statistics - *

- * @return Whether category is shown in statistics - */ - public Boolean getCategoryShownInStatistics () { - return this.categoryShownInStatistics; - } - - /** - * Setter for whether category is shown in statistics - *

- * @param categoryShownInStatistics Whether category is shown in statistics - */ - public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) { - this.categoryShownInStatistics = categoryShownInStatistics; - } - - /** - * Getter for category i18n key - *

- * @return Category i18n key - */ - public String getCategoryI18nKey () { - return this.categoryI18nKey; - } - - /** - * Setter for category i18n key - *

- * @param categoryI18nKey Category i18n key - */ - public void setCategoryI18nKey (final String categoryI18nKey) { - this.categoryI18nKey = categoryI18nKey; - } - - /** - * Getter for parent id - *

- * @return Parent id - */ - public Category getParentCategory () { - return this.parentCategory; - } - - /** - * Setter for parent category - *

- * @param parentCategory Parent category to set - */ - public void setParentCategory (final Category parentCategory) { - this.parentCategory = parentCategory; - } - - /** - * Clears this bean (example: when category has been added) - */ - private void clear () { - // Clear all fields - this.setCategoryI18nKey(null); - this.setParentCategory(null); - } - - /** - * Creates a category instance with all fields (except primary key) - *

- * @return Category instance - */ - private Category createCategoryInstance () { - // Create category - final Category category = new ProductCategory(this.getCategoryI18nKey(), this.getParentCategory(), this.getCategoryShownInStatistics()); - - // Return it - return category; - } - -} diff --git a/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestController.java deleted file mode 100644 index 6a292c20..00000000 --- a/src/java/org/mxchange/jfinancials/beans/category/FinancialAdminCategoryWebRequestController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.category; - -import java.io.Serializable; - -/** - * An interface for product controllers for "ADMIN" role - *

- * @author Roland Häder - */ -public interface FinancialAdminCategoryWebRequestController extends Serializable { - -} diff --git a/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestBean.java deleted file mode 100644 index e45879a9..00000000 --- a/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestBean.java +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.category; - -import fish.payara.cdi.jsr107.impl.NamedCache; -import java.text.MessageFormat; -import java.util.Comparator; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import javax.annotation.PostConstruct; -import javax.cache.Cache; -import javax.ejb.EJB; -import javax.enterprise.context.RequestScoped; -import javax.enterprise.event.Observes; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.jfinancials.beans.BaseFinancialsBean; -import org.mxchange.jproduct.events.category.AddedCategoryEvent; -import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException; -import org.mxchange.jproduct.model.category.Category; -import org.mxchange.jproduct.model.category.CategorySessionBeanRemote; - -/** - * General (product) category controller - *

- * @author Roland Häder - */ -@Named ("categoryController") -@RequestScoped -public class FinancialCategoryWebRequestBean extends BaseFinancialsBean implements FinancialCategoryWebRequestController { - - /** - * Serial number - */ - private static final long serialVersionUID = 58_137_539_530_279L; - - /** - * List of all categories - */ - private final List allCategories; - - /** - * EJB for general category stuff - */ - @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote") - private CategorySessionBeanRemote categoryBean; - - /** - * Cache for all product categories - */ - @Inject - @NamedCache (cacheName = "categoryCache") - private Cache categoryCache; - - /** - * A list of filtered categories - */ - private List filteredCategories; - - /** - * Default constructor - */ - public FinancialCategoryWebRequestBean () { - // Call super constructor - super(); - - // Init list - this.allCategories = new LinkedList<>(); - } - - /** - * Observes events fired after a new product category has been added - *

- * @param event Event to be observed - */ - public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) { - // Is all valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getAddedCategory() == null) { - // Throw again ... - throw new NullPointerException("event.addedCategory is null"); //NOI18N - } else if (event.getAddedCategory().getCategoryId() == null) { - // And again ... - throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N - } else if (event.getAddedCategory().getCategoryId() < 1) { - // Id is invalid - throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N - } - - // Add the category - this.allCategories.add(event.getAddedCategory()); - } - - /** - * Getter for a list of all categories - *

- * @return All categories - */ - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allCategories () { - // Return it - return this.allCategories; - } - - @Override - public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException { - // Validate parameter - if (null == categoryId) { - // Throw NPE - throw new NullPointerException("categoryId is null"); //NOI18N - } else if (categoryId < 1) { - // Throw IAE - throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N - } else if (!this.categoryCache.containsKey(categoryId)) { - // Not found - throw new CategoryNotFoundException(categoryId); - } - - // Get it from cache - final Category category = this.categoryCache.get(categoryId); - - // Return it - return category; - } - - /** - * Getter for filtered category list - *

- * @return Filtered category list - */ - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List getFilteredCategories () { - return this.filteredCategories; - } - - /** - * Setter for filtered category list - *

- * @param filteredCategories Filtered category list - */ - @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") - public void setFilteredCategories (final List filteredCategories) { - this.filteredCategories = filteredCategories; - } - - /** - * Initialization of this bean - */ - @PostConstruct - public void init () { - // Is cache there? - if (!this.categoryCache.iterator().hasNext()) { - // Get whole list - final List categories = this.categoryBean.allCategories(); - - // "Walk" through all entries and add to cache - for (final Category category : categories) { - // Add it by primary key - this.categoryCache.put(category.getCategoryId(), category); - } - } - - // Is the list empty, but filled cache? - if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) { - // Get iterator - final Iterator> iterator = this.categoryCache.iterator(); - - // Build up list - while (iterator.hasNext()) { - // GEt next element - final Cache.Entry next = iterator.next(); - - // Add to list - this.allCategories.add(next.getValue()); - } - - // Sort list - this.allCategories.sort(new Comparator() { - @Override - public int compare (final Category o1, final Category o2) { - return o1.getCategoryId() > o2.getCategoryId() ? 1 : o1.getCategoryId() < o2.getCategoryId() ? -1 : 0; - } - } - ); - } - } - -} diff --git a/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestController.java deleted file mode 100644 index c8caa54e..00000000 --- a/src/java/org/mxchange/jfinancials/beans/category/FinancialCategoryWebRequestController.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.category; - -import java.io.Serializable; -import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException; -import org.mxchange.jproduct.model.category.Category; - -/** - * An interface for (product) categories - *

- * @author Roland Häder - */ -public interface FinancialCategoryWebRequestController extends Serializable { - - /** - * Returns a category instance (entity) for given primary key. If not found, - * a proper exception is thrown. - *

- * @param categoryId Category id (primary key) - *

- * @return Category entity matching to primary key - *

- * @throws CategoryNotFoundException If a category with given primary key - * could not be found - */ - Category findCategoryById (final Long categoryId) throws CategoryNotFoundException; - -} diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java new file mode 100644 index 00000000..14ee0cc4 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java @@ -0,0 +1,337 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.generic_product; + +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.jfinancials.beans.BaseFinancialsBean; +import org.mxchange.jproduct.events.product.AddedProductEvent; +import org.mxchange.jproduct.events.product.ProductAddedEvent; +import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException; +import org.mxchange.jproduct.model.category.Category; +import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote; +import org.mxchange.jproduct.model.product.GenericProduct; +import org.mxchange.jproduct.model.product.Product; + +/** + * Main application class + *

+ * @author Roland Häder + */ +@Named ("adminProductController") +@RequestScoped +public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean implements FinancialAdminProductWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 5_819_375_183_472_871L; + + /** + * Event for added product + */ + @Inject + @Any + private Event addedProductEvent; + + /** + * Available + */ + private Boolean productAvailability; + + /** + * Category instance + */ + private Category productCategory; + + /** + * Product's price currency code like EUR or USD + */ + private String productCurrencyCode; + + /** + * Product's gross price + */ + private Float productGrossPrice; + + /** + * Product's net price + */ + private Float productNetPrice; + + /** + * Remote bean for products + */ + @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote") + private AdminProductSessionBeanRemote adminProductBean; + + /** + * Product's tax rate + */ + private Float productTaxRate; + + /** + * I18n key of product + */ + private String productI18nKey; + + /** + * Product's unit amount + */ + private Float productUnitAmount; + + /** + * Product's unit type + */ + private String productUnitType; + + /** + * Default constructor + */ + public FinancialAdminProductWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Adds given product data from request to database + *

+ * @throws FaceletException If something unexpected happened + */ + public void addProduct () throws FaceletException { + // Create product instance + final Product product = this.createProductInstance(); + + // Declare updated product instance + final Product updatedProduct; + + try { + // Call bean + updatedProduct = this.adminProductBean.addGenericProduct(product); + } catch (final ProductAlreadyAddedException ex) { + // Continue to throw + throw new FaceletException(ex); + } + + // Fire event + this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct)); + + // Set all to null + this.clear(); + } + + /** + * Getter for product's available + *

+ * @return Product's available + */ + public Boolean getProductAvailability () { + return this.productAvailability; + } + + /** + * Setter for product's available + *

+ * @param productAvailability Product's available + */ + public void setProductAvailability (final Boolean productAvailability) { + this.productAvailability = productAvailability; + } + + /** + * Getter for product's category + *

+ * @return Product's category + */ + public Category getProductCategory () { + return this.productCategory; + } + + /** + * Setter for product's category instance + *

+ * @param productCategory Product's category instance + */ + public void setProductCategory (final Category productCategory) { + this.productCategory = productCategory; + } + + /** + * Getter for product's price currency code + *

+ * @return Product's price currency code + */ + public String getProductCurrencyCode () { + return this.productCurrencyCode; + } + + /** + * Setter for product's price currency code + *

+ * @param productCurrencyCode Product's price currency code + */ + public void setProductCurrencyCode (final String productCurrencyCode) { + this.productCurrencyCode = productCurrencyCode; + } + + /** + * Getter for product's gross price + *

+ * @return Product's gross price + */ + public Float getProductGrossPrice () { + return this.productGrossPrice; + } + + /** + * Setter for product's gross price + *

+ * @param productGrossPrice Product's gross price + */ + public void setProductGrossPrice (final Float productGrossPrice) { + this.productGrossPrice = productGrossPrice; + } + + /** + * Getter for product's net price + *

+ * @return Product's net price + */ + public Float getProductNetPrice () { + return this.productNetPrice; + } + + /** + * Setter for product's net price + *

+ * @param productNetPrice Product's net price + */ + public void setProductNetPrice (final Float productNetPrice) { + this.productNetPrice = productNetPrice; + } + + /** + * Getter for product's tax rate + *

+ * @return Product's tax rate + */ + public Float getProductTaxRate () { + return this.productTaxRate; + } + + /** + * Setter for product's tax rate + *

+ * @param productTaxRate Product's tax rate + */ + public void setProductTaxRate (final Float productTaxRate) { + this.productTaxRate = productTaxRate; + } + + /** + * Getter for product's i18n key + *

+ * @return Product's i18n key + */ + public String getProductI18nKey () { + return this.productI18nKey; + } + + /** + * Setter for product's i18n key + *

+ * @param productI18nKey Product's i18n key + */ + public void setProductI18nKey (final String productI18nKey) { + this.productI18nKey = productI18nKey; + } + + /** + * Getter for product's unit amount + *

+ * @return Product's unit amount + */ + public Float getProductUnitAmount () { + return this.productUnitAmount; + } + + /** + * Setter for product's unit amount + *

+ * @param productUnitAmount Product's unit amount + */ + public void setProductUnitAmount (final Float productUnitAmount) { + this.productUnitAmount = productUnitAmount; + } + + /** + * Getter for product's unit type + *

+ * @return Product's unit type + */ + public String getProductUnitType () { + return this.productUnitType; + } + + /** + * Setter for product's unit type + *

+ * @param productUnitType Product's unit type + */ + public void setProductUnitType (final String productUnitType) { + this.productUnitType = productUnitType; + } + + /** + * Clears this bean (example: product has been added) + */ + private void clear () { + this.setProductAvailability(Boolean.FALSE); + this.setProductCategory(null); + this.setProductNetPrice(null); + this.setProductTaxRate(null); + this.setProductGrossPrice(null); + this.setProductI18nKey(null); + this.setProductUnitAmount(null); + this.setProductUnitType(null); + } + + /** + * Creates a product instance with all fields + *

+ * @return Product instance + */ + private Product createProductInstance () { + // Create product instance + final Product product = new GenericProduct(this.getProductI18nKey(), this.getProductGrossPrice(), this.getProductCurrencyCode(), this.getProductCategory(), this.getProductAvailability()); + + // Set all optional fields + product.setProductNetPrice(this.getProductNetPrice()); + product.setProductTaxRate(this.getProductTaxRate()); + product.setProductUnitAmount(this.getProductUnitAmount()); + product.setProductUnitType(this.getProductUnitType()); + + // Return it + return product; + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestController.java new file mode 100644 index 00000000..c419bae0 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.generic_product; + +import java.io.Serializable; + +/** + * An interface for product controllers for "ADMIN" role + *

+ * @author Roland Häder + */ +public interface FinancialAdminProductWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java new file mode 100644 index 00000000..55ec5325 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestBean.java @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.generic_product; + +import fish.payara.cdi.jsr107.impl.NamedCache; +import java.text.MessageFormat; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.cache.Cache; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.faces.FacesException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.jfinancials.beans.BaseFinancialsBean; +import org.mxchange.jproduct.events.product.AddedProductEvent; +import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; +import org.mxchange.jproduct.model.product.Product; +import org.mxchange.jproduct.model.product.ProductSessionBeanRemote; + +/** + * General product controller + *

+ * @author Roland Häder + */ +@Named ("productController") +@RequestScoped +public class FinancialProductWebRequestBean extends BaseFinancialsBean implements FinancialProductWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_137_539_530_279L; + + /** + * List for all products + */ + private List allProducts; + + /** + * List for filtered products + */ + private List filteredProducts; + + /** + * EJB for general product purposes + */ + @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote") + private ProductSessionBeanRemote productBean; + + /** + * Cached products + */ + @Inject + @NamedCache (cacheName = "productCache") + private Cache productCache; + + /** + * Default constructor + */ + public FinancialProductWebRequestBean () { + // Call super constructor + super(); + + // Init list + this.allProducts = new LinkedList<>(); + } + + /** + * Observes events fired after a new product has been added + *

+ * @param event Event to be observed + *

+ * @todo Move this to own controller + */ + public void afterProductAdded (@Observes final AddedProductEvent event) { + // Is all valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedProduct() == null) { + // Throw again ... + throw new NullPointerException("event.addedProduct is null"); //NOI18N + } else if (event.getAddedProduct().getProductId() == null) { + // And again ... + throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N + } else if (event.getAddedProduct().getProductId() < 1) { + // Id is invalid + throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N + } + + // Is the product available? + if (event.getAddedProduct().getProductAvailability()) { + // Add it + this.allProducts.add(event.getAddedProduct()); + } + } + + @Override + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allProducts () throws FacesException { + // Return it + return this.allProducts; + } + + @Override + public Product findProductById (final Long productId) throws ProductNotFoundException { + // Validate parameter + if (null == productId) { + // Throw NPE + throw new NullPointerException("productId is null"); //NOI18N + } else if (productId < 1) { + // Throw IAE + throw new IllegalArgumentException("productId=" + productId + " is invalid"); //NOI18N + } else if (!this.productCache.containsKey(productId)) { + // Not found + throw new ProductNotFoundException(productId); + } + + // Get it from cache + final Product product = this.productCache.get(productId); + + // Return it + return product; + } + + /** + * Getter for filtered product list + *

+ * @return Filtered product list + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List getFilteredProducts () { + return this.filteredProducts; + } + + /** + * Setter for filtered product list + *

+ * @param filteredProducts Filtered product list + */ + @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") + public void setFilteredProducts (final List filteredProducts) { + this.filteredProducts = filteredProducts; + } + + /** + * Initialization of this bean + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.productCache.iterator().hasNext()) { + // Get whole list + final List products = this.productBean.allProducts(); + + // "Walk" through all entries and add to cache + for (final Product product : products) { + // Add it by primary key + this.productCache.put(product.getProductId(), product); + } + } + + // Is the list empty, but filled cache? + if (this.allProducts.isEmpty() && this.productCache.iterator().hasNext()) { + // Get iterator + final Iterator> iterator = this.productCache.iterator(); + + // Build up list + while (iterator.hasNext()) { + // GEt next element + final Cache.Entry next = iterator.next(); + + // Add to list + this.allProducts.add(next.getValue()); + } + + // Sort list + this.allProducts.sort(new Comparator() { + @Override + public int compare (final Product o1, final Product o2) { + return o1.getProductId() > o2.getProductId() ? 1 : o1.getProductId() < o2.getProductId() ? -1 : 0; + } + } + ); + } + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java new file mode 100644 index 00000000..7dbe48cc --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialProductWebRequestController.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.generic_product; + +import java.io.Serializable; +import java.util.List; +import javax.ejb.Local; +import javax.faces.view.facelets.FaceletException; +import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; +import org.mxchange.jproduct.model.product.Product; + +/** + * An interface for products + *

+ * @author Roland Häder + */ +@Local +public interface FinancialProductWebRequestController extends Serializable { + + /** + * Some "getter" for a linked list of only available products + *

+ * @return Only available products + *

+ * @throws javax.faces.view.facelets.FaceletException If anything went wrong + */ + List allProducts () throws FaceletException; + + /** + * Returns a product instance (entity) for given primary key. If not found, + * a proper exception is thrown. + *

+ * @param productId Product id (primary key) + *

+ * @return Product entity matching to primary key + *

+ * @throws ProductNotFoundException If a product with given primary key + * could not be found + */ + Product findProductById (final Long productId) throws ProductNotFoundException; + +} diff --git a/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestBean.java deleted file mode 100644 index 842d3b1f..00000000 --- a/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestBean.java +++ /dev/null @@ -1,337 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.product; - -import javax.ejb.EJB; -import javax.enterprise.context.RequestScoped; -import javax.enterprise.event.Event; -import javax.enterprise.inject.Any; -import javax.faces.view.facelets.FaceletException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.jfinancials.beans.BaseFinancialsBean; -import org.mxchange.jproduct.events.product.AddedProductEvent; -import org.mxchange.jproduct.events.product.ProductAddedEvent; -import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException; -import org.mxchange.jproduct.model.category.Category; -import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote; -import org.mxchange.jproduct.model.product.GenericProduct; -import org.mxchange.jproduct.model.product.Product; - -/** - * Main application class - *

- * @author Roland Häder - */ -@Named ("adminProductController") -@RequestScoped -public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean implements FinancialAdminProductWebRequestController { - - /** - * Serial number - */ - private static final long serialVersionUID = 5_819_375_183_472_871L; - - /** - * Event for added product - */ - @Inject - @Any - private Event addedProductEvent; - - /** - * Available - */ - private Boolean productAvailability; - - /** - * Category instance - */ - private Category productCategory; - - /** - * Product's price currency code like EUR or USD - */ - private String productCurrencyCode; - - /** - * Product's gross price - */ - private Float productGrossPrice; - - /** - * Product's net price - */ - private Float productNetPrice; - - /** - * Remote bean for products - */ - @EJB (lookup = "java:global/jfinancials-ejb/adminProduct!org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote") - private AdminProductSessionBeanRemote adminProductBean; - - /** - * Product's tax rate - */ - private Float productTaxRate; - - /** - * I18n key of product - */ - private String productI18nKey; - - /** - * Product's unit amount - */ - private Float productUnitAmount; - - /** - * Product's unit type - */ - private String productUnitType; - - /** - * Default constructor - */ - public FinancialAdminProductWebRequestBean () { - // Call super constructor - super(); - } - - /** - * Adds given product data from request to database - *

- * @throws FaceletException If something unexpected happened - */ - public void addProduct () throws FaceletException { - // Create product instance - final Product product = this.createProductInstance(); - - // Declare updated product instance - final Product updatedProduct; - - try { - // Call bean - updatedProduct = this.adminProductBean.addGenericProduct(product); - } catch (final ProductAlreadyAddedException ex) { - // Continue to throw - throw new FaceletException(ex); - } - - // Fire event - this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct)); - - // Set all to null - this.clear(); - } - - /** - * Getter for product's available - *

- * @return Product's available - */ - public Boolean getProductAvailability () { - return this.productAvailability; - } - - /** - * Setter for product's available - *

- * @param productAvailability Product's available - */ - public void setProductAvailability (final Boolean productAvailability) { - this.productAvailability = productAvailability; - } - - /** - * Getter for product's category - *

- * @return Product's category - */ - public Category getProductCategory () { - return this.productCategory; - } - - /** - * Setter for product's category instance - *

- * @param productCategory Product's category instance - */ - public void setProductCategory (final Category productCategory) { - this.productCategory = productCategory; - } - - /** - * Getter for product's price currency code - *

- * @return Product's price currency code - */ - public String getProductCurrencyCode () { - return this.productCurrencyCode; - } - - /** - * Setter for product's price currency code - *

- * @param productCurrencyCode Product's price currency code - */ - public void setProductCurrencyCode (final String productCurrencyCode) { - this.productCurrencyCode = productCurrencyCode; - } - - /** - * Getter for product's gross price - *

- * @return Product's gross price - */ - public Float getProductGrossPrice () { - return this.productGrossPrice; - } - - /** - * Setter for product's gross price - *

- * @param productGrossPrice Product's gross price - */ - public void setProductGrossPrice (final Float productGrossPrice) { - this.productGrossPrice = productGrossPrice; - } - - /** - * Getter for product's net price - *

- * @return Product's net price - */ - public Float getProductNetPrice () { - return this.productNetPrice; - } - - /** - * Setter for product's net price - *

- * @param productNetPrice Product's net price - */ - public void setProductNetPrice (final Float productNetPrice) { - this.productNetPrice = productNetPrice; - } - - /** - * Getter for product's tax rate - *

- * @return Product's tax rate - */ - public Float getProductTaxRate () { - return this.productTaxRate; - } - - /** - * Setter for product's tax rate - *

- * @param productTaxRate Product's tax rate - */ - public void setProductTaxRate (final Float productTaxRate) { - this.productTaxRate = productTaxRate; - } - - /** - * Getter for product's i18n key - *

- * @return Product's i18n key - */ - public String getProductI18nKey () { - return this.productI18nKey; - } - - /** - * Setter for product's i18n key - *

- * @param productI18nKey Product's i18n key - */ - public void setProductI18nKey (final String productI18nKey) { - this.productI18nKey = productI18nKey; - } - - /** - * Getter for product's unit amount - *

- * @return Product's unit amount - */ - public Float getProductUnitAmount () { - return this.productUnitAmount; - } - - /** - * Setter for product's unit amount - *

- * @param productUnitAmount Product's unit amount - */ - public void setProductUnitAmount (final Float productUnitAmount) { - this.productUnitAmount = productUnitAmount; - } - - /** - * Getter for product's unit type - *

- * @return Product's unit type - */ - public String getProductUnitType () { - return this.productUnitType; - } - - /** - * Setter for product's unit type - *

- * @param productUnitType Product's unit type - */ - public void setProductUnitType (final String productUnitType) { - this.productUnitType = productUnitType; - } - - /** - * Clears this bean (example: product has been added) - */ - private void clear () { - this.setProductAvailability(Boolean.FALSE); - this.setProductCategory(null); - this.setProductNetPrice(null); - this.setProductTaxRate(null); - this.setProductGrossPrice(null); - this.setProductI18nKey(null); - this.setProductUnitAmount(null); - this.setProductUnitType(null); - } - - /** - * Creates a product instance with all fields - *

- * @return Product instance - */ - private Product createProductInstance () { - // Create product instance - final Product product = new GenericProduct(this.getProductI18nKey(), this.getProductGrossPrice(), this.getProductCurrencyCode(), this.getProductCategory(), this.getProductAvailability()); - - // Set all optional fields - product.setProductNetPrice(this.getProductNetPrice()); - product.setProductTaxRate(this.getProductTaxRate()); - product.setProductUnitAmount(this.getProductUnitAmount()); - product.setProductUnitType(this.getProductUnitType()); - - // Return it - return product; - } - -} diff --git a/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestController.java deleted file mode 100644 index 768ed80d..00000000 --- a/src/java/org/mxchange/jfinancials/beans/product/FinancialAdminProductWebRequestController.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.product; - -import java.io.Serializable; - -/** - * An interface for product controllers for "ADMIN" role - *

- * @author Roland Häder - */ -public interface FinancialAdminProductWebRequestController extends Serializable { - -} diff --git a/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestBean.java deleted file mode 100644 index 635cbe82..00000000 --- a/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestBean.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.product; - -import fish.payara.cdi.jsr107.impl.NamedCache; -import java.text.MessageFormat; -import java.util.Comparator; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import javax.annotation.PostConstruct; -import javax.cache.Cache; -import javax.ejb.EJB; -import javax.enterprise.context.RequestScoped; -import javax.enterprise.event.Observes; -import javax.faces.FacesException; -import javax.inject.Inject; -import javax.inject.Named; -import org.mxchange.jfinancials.beans.BaseFinancialsBean; -import org.mxchange.jproduct.events.product.AddedProductEvent; -import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; -import org.mxchange.jproduct.model.product.Product; -import org.mxchange.jproduct.model.product.ProductSessionBeanRemote; - -/** - * General product controller - *

- * @author Roland Häder - */ -@Named ("productController") -@RequestScoped -public class FinancialProductWebRequestBean extends BaseFinancialsBean implements FinancialProductWebRequestController { - - /** - * Serial number - */ - private static final long serialVersionUID = 58_137_539_530_279L; - - /** - * List for all products - */ - private List allProducts; - - /** - * List for filtered products - */ - private List filteredProducts; - - /** - * EJB for general product purposes - */ - @EJB (lookup = "java:global/jfinancials-ejb/product!org.mxchange.jproduct.model.product.ProductSessionBeanRemote") - private ProductSessionBeanRemote productBean; - - /** - * Cached products - */ - @Inject - @NamedCache (cacheName = "productCache") - private Cache productCache; - - /** - * Default constructor - */ - public FinancialProductWebRequestBean () { - // Call super constructor - super(); - - // Init list - this.allProducts = new LinkedList<>(); - } - - /** - * Observes events fired after a new product has been added - *

- * @param event Event to be observed - *

- * @todo Move this to own controller - */ - public void afterProductAdded (@Observes final AddedProductEvent event) { - // Is all valid? - if (null == event) { - // Throw NPE - throw new NullPointerException("event is null"); //NOI18N - } else if (event.getAddedProduct() == null) { - // Throw again ... - throw new NullPointerException("event.addedProduct is null"); //NOI18N - } else if (event.getAddedProduct().getProductId() == null) { - // And again ... - throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N - } else if (event.getAddedProduct().getProductId() < 1) { - // Id is invalid - throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N - } - - // Is the product available? - if (event.getAddedProduct().getProductAvailability()) { - // Add it - this.allProducts.add(event.getAddedProduct()); - } - } - - @Override - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List allProducts () throws FacesException { - // Return it - return this.allProducts; - } - - @Override - public Product findProductById (final Long productId) throws ProductNotFoundException { - // Validate parameter - if (null == productId) { - // Throw NPE - throw new NullPointerException("productId is null"); //NOI18N - } else if (productId < 1) { - // Throw IAE - throw new IllegalArgumentException("productId=" + productId + " is invalid"); //NOI18N - } else if (!this.productCache.containsKey(productId)) { - // Not found - throw new ProductNotFoundException(productId); - } - - // Get it from cache - final Product product = this.productCache.get(productId); - - // Return it - return product; - } - - /** - * Getter for filtered product list - *

- * @return Filtered product list - */ - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List getFilteredProducts () { - return this.filteredProducts; - } - - /** - * Setter for filtered product list - *

- * @param filteredProducts Filtered product list - */ - @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") - public void setFilteredProducts (final List filteredProducts) { - this.filteredProducts = filteredProducts; - } - - /** - * Initialization of this bean - */ - @PostConstruct - public void init () { - // Is cache there? - if (!this.productCache.iterator().hasNext()) { - // Get whole list - final List products = this.productBean.allProducts(); - - // "Walk" through all entries and add to cache - for (final Product product : products) { - // Add it by primary key - this.productCache.put(product.getProductId(), product); - } - } - - // Is the list empty, but filled cache? - if (this.allProducts.isEmpty() && this.productCache.iterator().hasNext()) { - // Get iterator - final Iterator> iterator = this.productCache.iterator(); - - // Build up list - while (iterator.hasNext()) { - // GEt next element - final Cache.Entry next = iterator.next(); - - // Add to list - this.allProducts.add(next.getValue()); - } - - // Sort list - this.allProducts.sort(new Comparator() { - @Override - public int compare (final Product o1, final Product o2) { - return o1.getProductId() > o2.getProductId() ? 1 : o1.getProductId() < o2.getProductId() ? -1 : 0; - } - } - ); - } - } - -} diff --git a/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestController.java deleted file mode 100644 index 8543d3c1..00000000 --- a/src/java/org/mxchange/jfinancials/beans/product/FinancialProductWebRequestController.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright (C) 2016, 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.jfinancials.beans.product; - -import java.io.Serializable; -import java.util.List; -import javax.ejb.Local; -import javax.faces.view.facelets.FaceletException; -import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; -import org.mxchange.jproduct.model.product.Product; - -/** - * An interface for products - *

- * @author Roland Häder - */ -@Local -public interface FinancialProductWebRequestController extends Serializable { - - /** - * Some "getter" for a linked list of only available products - *

- * @return Only available products - *

- * @throws javax.faces.view.facelets.FaceletException If anything went wrong - */ - List allProducts () throws FaceletException; - - /** - * Returns a product instance (entity) for given primary key. If not found, - * a proper exception is thrown. - *

- * @param productId Product id (primary key) - *

- * @return Product entity matching to primary key - *

- * @throws ProductNotFoundException If a product with given primary key - * could not be found - */ - Product findProductById (final Long productId) throws ProductNotFoundException; - -} diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java new file mode 100644 index 00000000..fb45265a --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestBean.java @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.product_category; + +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; +import javax.faces.view.facelets.FaceletException; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.jfinancials.beans.BaseFinancialsBean; +import org.mxchange.jproduct.events.category.AddedCategoryEvent; +import org.mxchange.jproduct.events.category.CategoryAddedEvent; +import org.mxchange.jproduct.exceptions.category.CategoryAlreadyAddedException; +import org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote; +import org.mxchange.jproduct.model.category.Category; +import org.mxchange.jproduct.model.category.ProductCategory; + +/** + * Main application class + *

+ * @author Roland Häder + */ +@Named ("adminCategoryController") +@RequestScoped +public class FinancialAdminCategoryWebRequestBean extends BaseFinancialsBean implements FinancialAdminCategoryWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 5_819_375_183_472_871L; + + /** + * Event for added shop categories + */ + @Inject + @Any + private Event categoryAddedEvent; + + /** + * Remote bean for categories + */ + @EJB (lookup = "java:global/jfinancials-ejb/adminCategory!org.mxchange.jproduct.model.category.AdminCategorySessionBeanRemote") + private AdminCategorySessionBeanRemote categoryBean; + + /** + * Whether this category is shown in statistics + */ + private Boolean categoryShownInStatistics; + + /** + * Category categoryI18nKey + */ + private String categoryI18nKey; + + /** + * Parent category + */ + private Category parentCategory; + + /** + * Default constructor + */ + public FinancialAdminCategoryWebRequestBean () { + // Call super constructor + super(); + } + + /** + * Adds given category data from request to database + *

+ * @throws javax.faces.view.facelets.FaceletException If something + * unexpected happened + */ + public void addCategory () throws FaceletException { + // Create category + final Category category = this.createCategoryInstance(); + + // Declare updated category instance + final Category updatedCategory; + + try { + // Deligate to remote bean + updatedCategory = this.categoryBean.addProductCategory(category); + } catch (final CategoryAlreadyAddedException ex) { + // Continue to throw + throw new FaceletException(ex); + } + + // Fire event + this.categoryAddedEvent.fire(new CategoryAddedEvent(updatedCategory)); + + // Unset all older values + this.clear(); + } + + /** + * Getter for whether category is shown in statistics + *

+ * @return Whether category is shown in statistics + */ + public Boolean getCategoryShownInStatistics () { + return this.categoryShownInStatistics; + } + + /** + * Setter for whether category is shown in statistics + *

+ * @param categoryShownInStatistics Whether category is shown in statistics + */ + public void setCategoryShownInStatistics (final Boolean categoryShownInStatistics) { + this.categoryShownInStatistics = categoryShownInStatistics; + } + + /** + * Getter for category i18n key + *

+ * @return Category i18n key + */ + public String getCategoryI18nKey () { + return this.categoryI18nKey; + } + + /** + * Setter for category i18n key + *

+ * @param categoryI18nKey Category i18n key + */ + public void setCategoryI18nKey (final String categoryI18nKey) { + this.categoryI18nKey = categoryI18nKey; + } + + /** + * Getter for parent id + *

+ * @return Parent id + */ + public Category getParentCategory () { + return this.parentCategory; + } + + /** + * Setter for parent category + *

+ * @param parentCategory Parent category to set + */ + public void setParentCategory (final Category parentCategory) { + this.parentCategory = parentCategory; + } + + /** + * Clears this bean (example: when category has been added) + */ + private void clear () { + // Clear all fields + this.setCategoryI18nKey(null); + this.setParentCategory(null); + } + + /** + * Creates a category instance with all fields (except primary key) + *

+ * @return Category instance + */ + private Category createCategoryInstance () { + // Create category + final Category category = new ProductCategory(this.getCategoryI18nKey(), this.getParentCategory(), this.getCategoryShownInStatistics()); + + // Return it + return category; + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestController.java new file mode 100644 index 00000000..be189896 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialAdminCategoryWebRequestController.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.product_category; + +import java.io.Serializable; + +/** + * An interface for product controllers for "ADMIN" role + *

+ * @author Roland Häder + */ +public interface FinancialAdminCategoryWebRequestController extends Serializable { + +} diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java new file mode 100644 index 00000000..3dac53be --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestBean.java @@ -0,0 +1,205 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.product_category; + +import fish.payara.cdi.jsr107.impl.NamedCache; +import java.text.MessageFormat; +import java.util.Comparator; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import javax.cache.Cache; +import javax.ejb.EJB; +import javax.enterprise.context.RequestScoped; +import javax.enterprise.event.Observes; +import javax.inject.Inject; +import javax.inject.Named; +import org.mxchange.jfinancials.beans.BaseFinancialsBean; +import org.mxchange.jproduct.events.category.AddedCategoryEvent; +import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException; +import org.mxchange.jproduct.model.category.Category; +import org.mxchange.jproduct.model.category.CategorySessionBeanRemote; + +/** + * General (product) category controller + *

+ * @author Roland Häder + */ +@Named ("categoryController") +@RequestScoped +public class FinancialCategoryWebRequestBean extends BaseFinancialsBean implements FinancialCategoryWebRequestController { + + /** + * Serial number + */ + private static final long serialVersionUID = 58_137_539_530_279L; + + /** + * List of all categories + */ + private final List allCategories; + + /** + * EJB for general category stuff + */ + @EJB (lookup = "java:global/jfinancials-ejb/category!org.mxchange.jproduct.model.category.CategorySessionBeanRemote") + private CategorySessionBeanRemote categoryBean; + + /** + * Cache for all product categories + */ + @Inject + @NamedCache (cacheName = "categoryCache") + private Cache categoryCache; + + /** + * A list of filtered categories + */ + private List filteredCategories; + + /** + * Default constructor + */ + public FinancialCategoryWebRequestBean () { + // Call super constructor + super(); + + // Init list + this.allCategories = new LinkedList<>(); + } + + /** + * Observes events fired after a new product category has been added + *

+ * @param event Event to be observed + */ + public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) { + // Is all valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getAddedCategory() == null) { + // Throw again ... + throw new NullPointerException("event.addedCategory is null"); //NOI18N + } else if (event.getAddedCategory().getCategoryId() == null) { + // And again ... + throw new NullPointerException("event.addedCategory.categoryId is null"); //NOI18N + } else if (event.getAddedCategory().getCategoryId() < 1) { + // Id is invalid + throw new IllegalArgumentException(MessageFormat.format("event.addedCategory.categoryId={0} is not valid.", event.getAddedCategory().getCategoryId())); //NOI18N + } + + // Add the category + this.allCategories.add(event.getAddedCategory()); + } + + /** + * Getter for a list of all categories + *

+ * @return All categories + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List allCategories () { + // Return it + return this.allCategories; + } + + @Override + public Category findCategoryById (final Long categoryId) throws CategoryNotFoundException { + // Validate parameter + if (null == categoryId) { + // Throw NPE + throw new NullPointerException("categoryId is null"); //NOI18N + } else if (categoryId < 1) { + // Throw IAE + throw new IllegalArgumentException("categoryId=" + categoryId + " is invalid"); //NOI18N + } else if (!this.categoryCache.containsKey(categoryId)) { + // Not found + throw new CategoryNotFoundException(categoryId); + } + + // Get it from cache + final Category category = this.categoryCache.get(categoryId); + + // Return it + return category; + } + + /** + * Getter for filtered category list + *

+ * @return Filtered category list + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List getFilteredCategories () { + return this.filteredCategories; + } + + /** + * Setter for filtered category list + *

+ * @param filteredCategories Filtered category list + */ + @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter") + public void setFilteredCategories (final List filteredCategories) { + this.filteredCategories = filteredCategories; + } + + /** + * Initialization of this bean + */ + @PostConstruct + public void init () { + // Is cache there? + if (!this.categoryCache.iterator().hasNext()) { + // Get whole list + final List categories = this.categoryBean.allCategories(); + + // "Walk" through all entries and add to cache + for (final Category category : categories) { + // Add it by primary key + this.categoryCache.put(category.getCategoryId(), category); + } + } + + // Is the list empty, but filled cache? + if (this.allCategories.isEmpty() && this.categoryCache.iterator().hasNext()) { + // Get iterator + final Iterator> iterator = this.categoryCache.iterator(); + + // Build up list + while (iterator.hasNext()) { + // GEt next element + final Cache.Entry next = iterator.next(); + + // Add to list + this.allCategories.add(next.getValue()); + } + + // Sort list + this.allCategories.sort(new Comparator() { + @Override + public int compare (final Category o1, final Category o2) { + return o1.getCategoryId() > o2.getCategoryId() ? 1 : o1.getCategoryId() < o2.getCategoryId() ? -1 : 0; + } + } + ); + } + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java new file mode 100644 index 00000000..69fde5ee --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/product_category/FinancialCategoryWebRequestController.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2016, 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.jfinancials.beans.product_category; + +import java.io.Serializable; +import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException; +import org.mxchange.jproduct.model.category.Category; + +/** + * An interface for (product) categories + *

+ * @author Roland Häder + */ +public interface FinancialCategoryWebRequestController extends Serializable { + + /** + * Returns a category instance (entity) for given primary key. If not found, + * a proper exception is thrown. + *

+ * @param categoryId Category id (primary key) + *

+ * @return Category entity matching to primary key + *

+ * @throws CategoryNotFoundException If a category with given primary key + * could not be found + */ + Category findCategoryById (final Long categoryId) throws CategoryNotFoundException; + +} diff --git a/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java b/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java index 5af8d5fd..4c78bd34 100644 --- a/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java +++ b/src/java/org/mxchange/jfinancials/converter/generic_product/FinancialsGenericProductConverter.java @@ -22,8 +22,8 @@ import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; -import org.mxchange.jfinancials.beans.product.FinancialProductWebRequestBean; -import org.mxchange.jfinancials.beans.product.FinancialProductWebRequestController; +import org.mxchange.jfinancials.beans.generic_product.FinancialProductWebRequestBean; +import org.mxchange.jfinancials.beans.generic_product.FinancialProductWebRequestController; import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; import org.mxchange.jproduct.model.product.Product; diff --git a/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java b/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java index 983e8b3f..4978f233 100644 --- a/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java +++ b/src/java/org/mxchange/jfinancials/converter/product_category/FinancialsProductCategoryConverter.java @@ -22,8 +22,8 @@ import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import javax.faces.convert.FacesConverter; -import org.mxchange.jfinancials.beans.category.FinancialCategoryWebRequestBean; -import org.mxchange.jfinancials.beans.category.FinancialCategoryWebRequestController; +import org.mxchange.jfinancials.beans.product_category.FinancialCategoryWebRequestBean; +import org.mxchange.jfinancials.beans.product_category.FinancialCategoryWebRequestController; import org.mxchange.jproduct.exceptions.category.CategoryNotFoundException; import org.mxchange.jproduct.model.category.Category; diff --git a/web/WEB-INF/faces-config.xml b/web/WEB-INF/faces-config.xml index b6471e27..81a63726 100644 --- a/web/WEB-INF/faces-config.xml +++ b/web/WEB-INF/faces-config.xml @@ -130,11 +130,11 @@ admin_list_category - /admin/category/admin_product_category_list.xhtml + /admin/product_category/admin_product_category_list.xhtml admin_list_product - /admin/product/admin_generic_product_list.xhtml + /admin/generic_product/admin_generic_product_list.xhtml admin_list_user @@ -882,37 +882,37 @@ - /admin/category/admin_product_category_list.xhtml + /admin/product_category/admin_product_category_list.xhtml admin_show_product_category - /admin/category/admin_product_category_show.xhtml + /admin/product_category/admin_product_category_show.xhtml admin_edit_product_category - /admin/category/admin_product_category_edit.xhtml + /admin/product_category/admin_product_category_edit.xhtml admin_delete_product_category - /admin/category/admin_product_category_delete.xhtml + /admin/product_category/admin_product_category_delete.xhtml admin_assign_parent_category - /admin/category/admin_product_category_assign_parent.xhtml + /admin/product_category/admin_product_category_assign_parent.xhtml - /admin/product/admin_generic_product_list.xhtml + /admin/generic_product/admin_generic_product_list.xhtml admin_show_generic_product - /admin/product/admin_generic_product_show.xhtml + /admin/generic_product/admin_generic_product_show.xhtml admin_edit_generic_product - /admin/product/admin_generic_product_edit.xhtml + /admin/generic_product/admin_generic_product_edit.xhtml admin_delete_generic_product - /admin/product/admin_generic_product_delete.xhtml + /admin/generic_product/admin_generic_product_delete.xhtml - - - - - - - - - - - - - - - - - - - diff --git a/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl b/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl new file mode 100644 index 00000000..2587060e --- /dev/null +++ b/web/WEB-INF/templates/admin/generic_product/admin_form_product_data.tpl @@ -0,0 +1,80 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/web/WEB-INF/templates/admin/product/admin_form_product_data.tpl b/web/WEB-INF/templates/admin/product/admin_form_product_data.tpl deleted file mode 100644 index 2587060e..00000000 --- a/web/WEB-INF/templates/admin/product/admin_form_product_data.tpl +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/web/WEB-INF/templates/admin/product_category/admin_form_category_data.tpl b/web/WEB-INF/templates/admin/product_category/admin_form_category_data.tpl new file mode 100644 index 00000000..c4d265cd --- /dev/null +++ b/web/WEB-INF/templates/admin/product_category/admin_form_category_data.tpl @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/web/admin/category/admin_product_category_list.xhtml b/web/admin/category/admin_product_category_list.xhtml deleted file mode 100644 index 1796183a..00000000 --- a/web/admin/category/admin_product_category_list.xhtml +++ /dev/null @@ -1,128 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/web/admin/generic_product/admin_generic_product_list.xhtml b/web/admin/generic_product/admin_generic_product_list.xhtml new file mode 100644 index 00000000..9b47ab69 --- /dev/null +++ b/web/admin/generic_product/admin_generic_product_list.xhtml @@ -0,0 +1,135 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/web/admin/product/admin_generic_product_list.xhtml b/web/admin/product/admin_generic_product_list.xhtml deleted file mode 100644 index bcaa8190..00000000 --- a/web/admin/product/admin_generic_product_list.xhtml +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/web/admin/product_category/admin_product_category_list.xhtml b/web/admin/product_category/admin_product_category_list.xhtml new file mode 100644 index 00000000..884fe1f4 --- /dev/null +++ b/web/admin/product_category/admin_product_category_list.xhtml @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +