*/
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
*/
private static final long serialVersionUID = 5_819_375_183_472_871L;
- /////////////////////// Properties /////////////////////
+ /**
+ * Event for added product
+ */
+ @Inject
+ @Any
+ private Event<AddedProductEvent> addedProductEvent;
+
/**
* Available
*/
*/
private String productTitle;
- ////////////////////// Bean injections ///////////////////////
-
/**
- * Shop bean
+ * Cache for all products
*/
- @Inject
- private PizzaShopWebApplicationController shopController;
+ private List<Product> products;
/**
* Default constructor
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);
// 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);
@Override
public List<Product> getAllProducts () throws FaceletException {
// Call bean
- return this.productRemoteBean.getAllProducts();
+ return Collections.unmodifiableList(this.products);
}
@Override
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);
+ }
+
}