]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/product/PizzaAdminProductWebRequestBean.java
Rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / product / PizzaAdminProductWebRequestBean.java
index b329e098aaa13ec95d9b8a738a41097ae5d82e7f..d477617b42439778d73d54c8ec976ed3a5030758 100644 (file)
  */
 package org.mxchange.pizzaapplication.beans.product;
 
+import java.util.Collections;
 import java.util.List;
+import javax.annotation.PostConstruct;
 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 javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
+import org.mxchange.jshopcore.events.product.AddedProductEvent;
+import org.mxchange.jshopcore.events.product.ShopProductAddedEvent;
 import org.mxchange.jshopcore.exceptions.CannotAddProductException;
 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
 import org.mxchange.jshopcore.model.category.Category;
 import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote;
 import org.mxchange.jshopcore.model.product.GenericProduct;
 import org.mxchange.jshopcore.model.product.Product;
-import org.mxchange.pizzaapplication.beans.shop.PizzaShopWebApplicationController;
 
 /**
  * Main application class
@@ -46,7 +51,13 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
         */
        private static final long serialVersionUID = 5_819_375_183_472_871L;
 
-       /////////////////////// Properties /////////////////////
+       /**
+        * Event for added product
+        */
+       @Inject
+       @Any
+       private Event<AddedProductEvent> addedProductEvent;
+
        /**
         * Available
         */
@@ -72,13 +83,10 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
         */
        private String productTitle;
 
-       ////////////////////// Bean injections ///////////////////////
-
        /**
-        * Shop bean
+        * Cache for all products
         */
-       @Inject
-       private PizzaShopWebApplicationController shopController;
+       private List<Product> products;
 
        /**
         * Default constructor
@@ -90,7 +98,7 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
                        Context context = new InitialContext();
 
                        // Try to lookup the bean
-                       this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("ejb/stateless-admin-product"); //NOI18N
+                       this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_product!org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote"); //NOI18N
                } catch (final NamingException e) {
                        // Throw it again
                        throw new FaceletException(e);
@@ -112,14 +120,14 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
                        // Call bean
                        Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
 
-                       // Add to shop controller
-                       this.shopController.addProduct(updatedProduct);
+                       // Add updated product to local list
+                       this.products.add(updatedProduct);
+
+                       // Fire event
+                       this.addedProductEvent.fire(new ShopProductAddedEvent(updatedProduct));
 
                        // Set all to null
-                       this.setProductAvailability(Boolean.FALSE);
-                       this.setProductCategory(null);
-                       this.setProductPrice(null);
-                       this.setProductTitle(null);
+                       this.clear();
                } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
                        // Continue to throw
                        throw new FaceletException(ex);
@@ -129,7 +137,7 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
        @Override
        public List<Product> getAllProducts () throws FaceletException {
                // Call bean
-               return this.productRemoteBean.getAllProducts();
+               return Collections.unmodifiableList(this.products);
        }
 
        @Override
@@ -171,4 +179,24 @@ public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequ
        public void setProductTitle (final String productTitle) {
                this.productTitle = productTitle;
        }
+
+       /**
+        * Initializer method
+        */
+       @PostConstruct
+       public void init () {
+               // Initialize list
+               this.products = this.productRemoteBean.getAllProducts();
+       }
+
+       /**
+        * Clears this bean (example: product has been added)
+        */
+       private void clear () {
+               this.setProductAvailability(Boolean.FALSE);
+               this.setProductCategory(null);
+               this.setProductPrice(null);
+               this.setProductTitle(null);
+       }
+
 }