From 6546899800930b74d87ca26dfea73aede8b4b5f6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 24 Apr 2020 21:49:31 +0200 Subject: [PATCH] Don't cherry-pick: - avoided local copy of List instance - if a receiptItem is updated, no matter if discount or refund, check it. Also then only rely on the primary key MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- ...seFinancialsReceiptItemEnterpriseBean.java | 2 +- .../FinancialAdminReceiptItemSessionBean.java | 23 +++++++++---------- .../FinancialReceiptItemSessionBean.java | 5 +--- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/java/org/mxchange/jfinancials/enterprise/financial/receipt_item/BaseFinancialsReceiptItemEnterpriseBean.java b/src/java/org/mxchange/jfinancials/enterprise/financial/receipt_item/BaseFinancialsReceiptItemEnterpriseBean.java index c09aa9d..ad06ce1 100644 --- a/src/java/org/mxchange/jfinancials/enterprise/financial/receipt_item/BaseFinancialsReceiptItemEnterpriseBean.java +++ b/src/java/org/mxchange/jfinancials/enterprise/financial/receipt_item/BaseFinancialsReceiptItemEnterpriseBean.java @@ -85,7 +85,7 @@ public abstract class BaseFinancialsReceiptItemEnterpriseBean extends BaseFinanc managedReceiptItem.setItemEntryUpdated(new Date()); // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceipt: managedReceiptItem={0} - EXIT!", managedReceiptItem)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("mergeReceiptItem: managedReceiptItem={0} - EXIT!", managedReceiptItem)); //NOI18N // Return detached receipt return managedReceiptItem; diff --git a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java index 3c361f4..61a239c 100644 --- a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java +++ b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialAdminReceiptItemSessionBean.java @@ -18,7 +18,7 @@ package org.mxchange.jfinancials.model.receipt_item; import java.text.MessageFormat; import java.util.Date; -import java.util.List; +import java.util.Objects; import javax.ejb.EJB; import javax.ejb.Stateless; import org.mxchange.jcontactsbusiness.model.basicdata.BasicData; @@ -81,7 +81,7 @@ public class FinancialAdminReceiptItemSessionBean extends BaseFinancialsReceiptI } else if (receiptItem.getItemReceipt().getReceiptId() < 1) { // Throw NPE throw new NullPointerException(MessageFormat.format("receiptItem.itemReceipt.receiptId={0} is invalid.", receiptItem.getItemReceipt().getReceiptId())); //NOI18N - } else if (this.isReceiptItemRegistered(receiptItem)) { + } else if (this.isReceiptItemRegistered(receiptItem, false, false)) { // Is already registered throw new ReceiptItemAlreadyAddedException(receiptItem); } @@ -129,8 +129,8 @@ public class FinancialAdminReceiptItemSessionBean extends BaseFinancialsReceiptI } else if (receiptItem.getItemId() < 1) { // Throw IAE throw new IllegalArgumentException(MessageFormat.format("receiptItem.itemId={0} is invalid", receiptItem.getItemId())); //NOI18N - } else if (!this.isReceiptItemRegistered(receiptItem)) { - // Not found + } else if (!this.isReceiptItemRegistered(receiptItem, true, true)) { + // No discount and not found throw new ReceiptItemNotFoundException(receiptItem); } @@ -147,16 +147,18 @@ public class FinancialAdminReceiptItemSessionBean extends BaseFinancialsReceiptI /** * Checks if given receipt item has already been added to receipt. *

- * @param receiptItem Receipt item + * @param receiptItem Receipt item + * @param checkDiscountRefund Whether to check discounts and refunds + * @param checkOnlyPrimaryKey Whether to check only primary key *

* @return Whether item has already been added to receipt item's receipt */ - private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem) { + private boolean isReceiptItemRegistered (final BillableReceiptItem receiptItem, final boolean checkDiscountRefund, final boolean checkOnlyPrimaryKey) { // Default is not found boolean isFound = false; // Is the item type a discount? - if (receiptItem.getItemIsDiscount()) { + if (!checkDiscountRefund && receiptItem.getItemIsDiscount()) { // Yes, then allow it multiple items return false; } @@ -165,12 +167,9 @@ public class FinancialAdminReceiptItemSessionBean extends BaseFinancialsReceiptI * Get all receipt's receipt items. No need to look global as people may * buy the same item again and again ... ;-) */ - final List receiptItems = this.receiptItemBean.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt()); - - // Loop through all entries - for (final BillableReceiptItem item : receiptItems) { + for (final BillableReceiptItem currentReceiptItem : this.receiptItemBean.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt())) { // Is it the same item? - if (ReceiptItems.isSameReceiptItem(item, receiptItem) && (!item.getItemIsRefund())) { + if (((checkOnlyPrimaryKey && Objects.equals(currentReceiptItem.getItemId(), receiptItem.getItemId())) || ReceiptItems.isSameReceiptItem(currentReceiptItem, receiptItem)) && (checkDiscountRefund || !currentReceiptItem.getItemIsRefund())) { // Found it isFound = true; break; diff --git a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java index 5b5d4e2..b566b84 100644 --- a/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java +++ b/src/java/org/mxchange/jfinancials/model/receipt_item/FinancialReceiptItemSessionBean.java @@ -168,10 +168,7 @@ public class FinancialReceiptItemSessionBean extends BaseFinancialsEnterpriseBea * Get all receipt's receipt items. No need to look global as people may * buy the same item again and again ... ;-) */ - final List receiptItems = this.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt()); - - // Loop through all entries - for (final BillableReceiptItem item : receiptItems) { + for (final BillableReceiptItem item : this.fetchReceiptItemsByReceipt(receiptItem.getItemReceipt())) { // Is it the same item? if (ReceiptItems.isSameReceiptItem(item, receiptItem)) { // Found it -- 2.39.5