From e5a7ae0245f38f9e9f9137d2ce8fa45181db0065 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 24 Oct 2022 01:35:43 +0200 Subject: [PATCH] Don't cherry-pick: - also update product reference in item when product has been updated by admin --- .../FinancialsReceiptItemListWebViewBean.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/java/org/mxchange/jfinancials/beans/financial/model/receipt_item/list/FinancialsReceiptItemListWebViewBean.java b/src/java/org/mxchange/jfinancials/beans/financial/model/receipt_item/list/FinancialsReceiptItemListWebViewBean.java index 541dcf05..392f4bed 100644 --- a/src/java/org/mxchange/jfinancials/beans/financial/model/receipt_item/list/FinancialsReceiptItemListWebViewBean.java +++ b/src/java/org/mxchange/jfinancials/beans/financial/model/receipt_item/list/FinancialsReceiptItemListWebViewBean.java @@ -38,6 +38,7 @@ import org.mxchange.jfinancials.exceptions.receipt_item.ReceiptItemNotFoundExcep import org.mxchange.jfinancials.model.receipt_item.BillableReceiptItem; import org.mxchange.jfinancials.model.receipt_item.FinancialReceiptItemSessionBeanRemote; import org.mxchange.jfinancials.model.utils.ReceiptItemUtils; +import org.mxchange.jproduct.events.product.updated.ObservableProductUpdatedEvent; import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; /** @@ -158,6 +159,55 @@ public class FinancialsReceiptItemListWebViewBean extends BaseFinancialsBean imp this.uniqueAddReceiptItem(event.getUpdatedReceiptItem()); } + /** + * Event observer for updated product data by administrators + *

+ * @param event Updated product data event + */ + public void afterProductUpdatedEvent (@Observes final ObservableProductUpdatedEvent event) { + // Event and contained entity instance 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 + } + + // Get iterator from + final Iterator> iterator = this.receiptItemCache.iterator(); + + // Loop through it + while (iterator.hasNext()) { + // Get next item + final Cache.Entry current = iterator.next(); + + // Get current product + final BillableReceiptItem currentItem = current.getValue(); + + // Is the same product id set? + if (event.getUpdatedProduct().getProductId().equals(currentItem.getItemProduct().getProductId())) { + // Yes, found it, get key + final Long itemId = current.getKey(); + + // Set new instance + currentItem.setItemProduct(event.getUpdatedProduct()); + + // Update cache + this.receiptItemCache.put(itemId, currentItem); + + // Stop searching here + break; + } + } + } + /** * Event observer for logged-in user *

-- 2.39.5