]> git.mxchange.org Git - jfinancials-war.git/commitdiff
Don't cherry-pick:
authorRoland Häder <roland@mxchange.org>
Sun, 23 Oct 2022 23:35:43 +0000 (01:35 +0200)
committerRoland Häder <roland@mxchange.org>
Sun, 23 Oct 2022 23:46:14 +0000 (01:46 +0200)
- also update product reference in item when product has been updated by admin

src/java/org/mxchange/jfinancials/beans/financial/model/receipt_item/list/FinancialsReceiptItemListWebViewBean.java

index 541dcf05ddfadc4e7ea32d6c1102274d0af0f880..392f4bed1c0ebd6ebc1c68b1a1a49862af1f125f 100644 (file)
@@ -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
+        * <p>
+        * @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<Cache.Entry<Long, BillableReceiptItem>> iterator = this.receiptItemCache.iterator();
+
+               // Loop through it
+               while (iterator.hasNext()) {
+                       // Get next item
+                       final Cache.Entry<Long, BillableReceiptItem> 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
         * <p>