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;
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;
*/
private String productI18nKey;
+ /**
+ * Product id
+ */
+ private Long productId;
+
/**
* Product list controller
*/
*/
private String productUnitI18nKey;
+ /**
+ * Event for updated product
+ */
+ @Inject
+ @Any
+ private Event<ObservableProductUpdatedEvent> updatedProductEvent;
+
/**
* Default constructor
*/
this.clear();
}
+ /**
+ * Observer for events being fired when a bean helper has successfully
+ * created a product instance.
+ * <p>
+ * @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
* <p>
this.productI18nKey = productI18nKey;
}
+ /**
+ * Getter for product id
+ * <p>
+ * @return Product id
+ */
+ public Long getProductId () {
+ return this.productId;
+ }
+
+ /**
+ * Setter for product id
+ * <p>
+ * @param productId Product id
+ */
+ public void setProductId (final Long productId) {
+ this.productId = productId;
+ }
+
/**
* Getter for product's manufacturing/producing company
* <p>
this.productUnitI18nKey = productUnitI18nKey;
}
+ /**
+ * Updates given product data from request to database
+ * <p>
+ * @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);
* @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
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;
}
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;
* Observes events fired after a new product has been added
* <p>
* @param event Event to be observed
- * <p>
- * @todo Move this to own controller
*/
public void afterProductAddedEvent (@Observes final AddedProductEvent event) {
// Is all valid?
}
// 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
+ * <p>
+ * @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
* Some "getter" for a linked list of only available products
* <p>
* @return Only available products
- * <p>
- * @throws javax.faces.view.facelets.FaceletException If anything went wrong
*/
@SuppressWarnings ("ReturnOfCollectionOrArrayField")
- public List<Product> getAllProducts () throws FacesException {
+ public List<Product> getAllProducts () {
// Return it
return this.allProducts;
}
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.
+ * <p>
+ * @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
+ * <p>
+ * @param product Product instance to remove
+ */
+ private void removeProduct (final Product product) {
+ // Remove from general list
+ this.productCache.remove(product.getProductId());
+ }
+
}