From: Roland Häder Date: Mon, 14 May 2018 23:00:12 +0000 (+0200) Subject: Product-only: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=ece0e2d8992c1797264b81dea1e2c9d1d119214f;p=jfinancials-war.git Product-only: - having this AJAX-pagination-filtering-sorting stuff here is plain wrong, but hey ... what is @ViewScoped again ??? ;-) - added productId backing bean as the edit/delete view require it - also createProductInstance() is setting it, when not null Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java index 8f94c7f6..b4a3c066 100644 --- a/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/FinancialAdminProductWebRequestBean.java @@ -17,9 +17,11 @@ package org.mxchange.jfinancials.beans.generic_product; import java.math.BigDecimal; +import java.text.MessageFormat; import javax.ejb.EJB; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; @@ -29,7 +31,11 @@ import org.mxchange.jfinancials.beans.BaseFinancialsBean; import org.mxchange.jfinancials.beans.generic_product.list.FinancialsProductListWebViewController; import org.mxchange.jproduct.events.product.added.AddedProductEvent; import org.mxchange.jproduct.events.product.added.ProductAddedEvent; +import org.mxchange.jproduct.events.product.created.ObservableCreatedProductEvent; +import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent; +import org.mxchange.jproduct.events.product.updated.ProductUpdatedEvent; import org.mxchange.jproduct.exceptions.product.ProductAlreadyAddedException; +import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; import org.mxchange.jproduct.model.category.Category; import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote; import org.mxchange.jproduct.model.product.GenericProduct; @@ -93,6 +99,11 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl */ private String productI18nKey; + /** + * Product id + */ + private Long productId; + /** * Product list controller */ @@ -134,6 +145,13 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl */ private String productUnitI18nKey; + /** + * Event for updated product + */ + @Inject + @Any + private Event updatedProductEvent; + /** * Default constructor */ @@ -175,6 +193,48 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl this.clear(); } + /** + * Observer for events being fired when a bean helper has successfully + * created a product instance. + *

+ * @param event Event being fired + */ + public void afterCreatedProductEvent (@Observes final ObservableCreatedProductEvent event) { + // The event instance must be valid + if (null == event) { + // Throw NPE again + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getCreatedProduct() == null) { + // Throw NPE again + throw new NullPointerException("event.createdProduct is null"); //NOI18N //NOI18N + } else if (event.getCreatedProduct().getProductId() == null) { + // Throw NPE again + throw new NullPointerException("event.createdProduct.productId is null"); //NOI18N //NOI18N + } else if (event.getCreatedProduct().getProductId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("event.createdProduct.productId={0} is not valid.", event.getCreatedProduct().getProductId())); //NOI18N + } + + // Get product instance from event + final Product product = event.getCreatedProduct(); + + // Set all fields + this.setProductAgeGroup(product.getProductAgeGroup()); + this.setProductAvailability(product.getProductAvailability()); + this.setProductCategory(product.getProductCategory()); + this.setProductCurrencyCode(product.getProductCurrencyCode()); + this.setProductGrossPrice(product.getProductGrossPrice()); + this.setProductI18nKey(product.getProductI18nKey()); + this.setProductId(product.getProductId()); + this.setProductManufacturer(product.getProductManufacturer()); + this.setProductNetPrice(product.getProductNetPrice()); + this.setProductNumber(product.getProductNumber()); + this.setProductSize(product.getProductSize()); + this.setProductTaxRate(product.getProductTaxRate()); + this.setProductUnitAmount(product.getProductUnitAmount()); + this.setProductUnitI18nKey(product.getProductUnitI18nKey()); + } + /** * Getter for product's age group *

@@ -283,6 +343,24 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl this.productI18nKey = productI18nKey; } + /** + * Getter for product id + *

+ * @return Product id + */ + public Long getProductId () { + return this.productId; + } + + /** + * Setter for product id + *

+ * @param productId Product id + */ + public void setProductId (final Long productId) { + this.productId = productId; + } + /** * Getter for product's manufacturing/producing company *

@@ -409,15 +487,56 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl this.productUnitI18nKey = productUnitI18nKey; } + /** + * Updates given product data from request to database + *

+ * @throws FaceletException If something unexpected happened + */ + public void updateProduct () throws FaceletException { + // Get product instance + final Product product = this.createProductInstance(); + // Check if product instance is in helper and valid + + if (null == product) { + // Throw NPE + throw new NullPointerException("productBeanHelper.product is null"); //NOI18N + } else if (product.getProductId() == null) { + // Throw NPE again + throw new NullPointerException("productBeanHelper.product.productId is null"); //NOI18N //NOI18N + } else if (product.getProductId() < 1) { + // Invalid id + throw new IllegalStateException(MessageFormat.format("productBeanHelper.product.productId={0} is invalid", product.getProductId())); //NOI18N + } + + // Init productr + final Product updatedProduct; + + try { + // Call EJB for updating product data + updatedProduct = this.adminProductBean.updateProductData(product); + } catch (final ProductNotFoundException ex) { + // Throw again + throw new FaceletException(ex); + } + + // Fire event + this.updatedProductEvent.fire(new ProductUpdatedEvent(updatedProduct)); + + // Clear bean + this.clear(); + } + /** * Clears this bean (example: product has been added) */ private void clear () { + // Clear all data this.setProductAgeGroup(null); this.setProductAvailability(Boolean.FALSE); this.setProductCategory(null); this.setProductGrossPrice(null); this.setProductI18nKey(null); + this.setProductId(null); this.setProductManufacturer(null); this.setProductNetPrice(null); this.setProductNumber(null); @@ -433,7 +552,7 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl * @return Product instance */ private Product createProductInstance () { - // Create product instance +// Create product instance final Product product = new GenericProduct(this.getProductI18nKey(), this.getProductGrossPrice(), this.getProductCurrencyCode(), this.getProductCategory(), this.getProductAvailability(), this.getProductUnitAmount(), this.getProductUnitI18nKey()); // Set all optional fields @@ -444,6 +563,12 @@ public class FinancialAdminProductWebRequestBean extends BaseFinancialsBean impl product.setProductSize(this.getProductSize()); product.setProductTaxRate(this.getProductTaxRate()); + // Is product id set? + if (this.getProductId() instanceof Long) { + // Set it, too + product.setProductId(this.getProductId()); + } + // Return it return product; } diff --git a/src/java/org/mxchange/jfinancials/beans/generic_product/list/FinancialsProductListWebViewBean.java b/src/java/org/mxchange/jfinancials/beans/generic_product/list/FinancialsProductListWebViewBean.java index 5e7290c6..a9491206 100644 --- a/src/java/org/mxchange/jfinancials/beans/generic_product/list/FinancialsProductListWebViewBean.java +++ b/src/java/org/mxchange/jfinancials/beans/generic_product/list/FinancialsProductListWebViewBean.java @@ -27,12 +27,12 @@ import javax.annotation.PostConstruct; import javax.cache.Cache; import javax.ejb.EJB; import javax.enterprise.event.Observes; -import javax.faces.FacesException; import javax.faces.view.ViewScoped; import javax.inject.Inject; import javax.inject.Named; import org.mxchange.jfinancials.beans.BaseFinancialsBean; import org.mxchange.jproduct.events.product.added.AddedProductEvent; +import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent; import org.mxchange.jproduct.exceptions.product.ProductNotFoundException; import org.mxchange.jproduct.model.product.Product; import org.mxchange.jproduct.model.product.ProductSessionBeanRemote; @@ -94,8 +94,6 @@ public class FinancialsProductListWebViewBean extends BaseFinancialsBean impleme * Observes events fired after a new product has been added *

* @param event Event to be observed - *

- * @todo Move this to own controller */ public void afterProductAddedEvent (@Observes final AddedProductEvent event) { // Is all valid? @@ -114,8 +112,32 @@ public class FinancialsProductListWebViewBean extends BaseFinancialsBean impleme } // Add it - this.productCache.put(event.getAddedProduct().getProductId(), event.getAddedProduct()); - this.getAllProducts().add(event.getAddedProduct()); + this.addProduct(event.getAddedProduct()); + } + + /** + * Event observer for updated product data by administrators + *

+ * @param event Updated product data event + */ + public void afterProductUpdatedEvent (@Observes final ObservableProductUpdatedEvent event) { + // event should not be null + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getUpdatedProduct() == null) { + // Throw NPE again + throw new NullPointerException("event.updatedProduct is null"); //NOI18N + } else if (event.getUpdatedProduct().getProductId() == null) { + // userId is null + throw new NullPointerException("event.updatedProduct.productId is null"); //NOI18N + } else if (event.getUpdatedProduct().getProductId() < 1) { + // Not avalid id + throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedProduct(), event.getUpdatedProduct().getProductId())); //NOI18N + } + + // Add product instance only once + this.addProduct(event.getUpdatedProduct()); } @Override @@ -143,11 +165,9 @@ public class FinancialsProductListWebViewBean extends BaseFinancialsBean impleme * Some "getter" for a linked list of only available products *

* @return Only available products - *

- * @throws javax.faces.view.facelets.FaceletException If anything went wrong */ @SuppressWarnings ("ReturnOfCollectionOrArrayField") - public List getAllProducts () throws FacesException { + public List getAllProducts () { // Return it return this.allProducts; } @@ -263,4 +283,35 @@ public class FinancialsProductListWebViewBean extends BaseFinancialsBean impleme return isFound; } + /** + * Adds unique instance to product list. First any existing instance is + * being removed, then the new instance is added. It also updates the + * cache regardless if found or not. + *

+ * @param product Product instance to add uniquely + */ + private void addProduct (final Product product) { + // Add product to list + this.productCache.put(product.getProductId(), product); + + // Does the list contain it? + if (this.getAllProducts().contains(product)) { + // Yes, then remove it + this.getAllProducts().remove(product); + } + + // (Re-)add product + this.getAllProducts().add(product); + } + + /** + * Removes given product from all lists + *

+ * @param product Product instance to remove + */ + private void removeProduct (final Product product) { + // Remove from general list + this.productCache.remove(product.getProductId()); + } + }