This is an automated email from the ASF dual-hosted git repository.
surajk pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git The following commit(s) were added to refs/heads/trunk by this push: new 73b15e9 Improved: Enforce noninstantiability to FinAccountHelper Class. (#171) 73b15e9 is described below commit 73b15e98677567787e983db5d76df0f296070a44 Author: Suraj Khurana <[hidden email]> AuthorDate: Fri Jun 5 13:13:58 2020 +0530 Improved: Enforce noninstantiability to FinAccountHelper Class. (#171) (OFBIZ-11723) Made class as final, added private constructor, also made data members as private, defined getter methods and changed related occurrences. --- .../finaccount/FinAccountPaymentServices.java | 18 +++--- .../finaccount/FinAccountProductServices.java | 2 +- .../accounting/finaccount/FinAccountServices.java | 14 ++--- .../payment/GiftCertificateServices.java | 26 ++++---- .../ofbiz/order/finaccount/FinAccountHelper.java | 72 ++++++++++++---------- .../ofbiz/order/shoppingcart/CheckOutHelper.java | 2 +- .../ofbiz/order/shoppingcart/ShoppingCart.java | 3 +- 7 files changed, 74 insertions(+), 63 deletions(-) diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountPaymentServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountPaymentServices.java index 3557bf2..dca9894 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountPaymentServices.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountPaymentServices.java @@ -152,14 +152,14 @@ public class FinAccountPaymentServices { Debug.logVerbose("In finAccountPreAuth finAccountSettings=" + finAccountSettings, MODULE); } - BigDecimal minBalance = FinAccountHelper.ZERO; + BigDecimal minBalance = FinAccountHelper.getZero(); String allowAuthToNegative = "N"; if (finAccountSettings != null) { allowAuthToNegative = finAccountSettings.getString("allowAuthToNegative"); minBalance = finAccountSettings.getBigDecimal("minBalance"); if (minBalance == null) { - minBalance = FinAccountHelper.ZERO; + minBalance = FinAccountHelper.getZero(); } // validate the PIN if the store requires it @@ -229,10 +229,10 @@ public class FinAccountPaymentServices { // which includes active authorizations as well as transactions BigDecimal availableBalance = finAccount.getBigDecimal("availableBalance"); if (availableBalance == null) { - availableBalance = FinAccountHelper.ZERO; + availableBalance = FinAccountHelper.getZero(); } else { BigDecimal availableBalanceOriginal = availableBalance; - availableBalance = availableBalance.setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); + availableBalance = availableBalance.setScale(FinAccountHelper.getDecimals(), FinAccountHelper.getRounding()); if (availableBalance.compareTo(availableBalanceOriginal) != 0) { Debug.logWarning("In finAccountPreAuth for finAccountId [" + finAccountId + "] availableBalance [" + availableBalanceOriginal + "] was different after rounding [" + availableBalance @@ -247,7 +247,7 @@ public class FinAccountPaymentServices { String refNum; // make sure to round and scale it to the same as availableBalance - amount = amount.setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); + amount = amount.setScale(FinAccountHelper.getDecimals(), FinAccountHelper.getRounding()); Debug.logInfo("Allow auth to negative: " + allowAuthToNegative + " :: available: " + availableBalance + " comp: " + minBalance + " = " + availableBalance.compareTo(minBalance) + " :: req: " + amount, MODULE); // check the available balance to see if we can auth this tx @@ -594,7 +594,7 @@ public class FinAccountPaymentServices { // transaction if it is sufficient BigDecimal previousBalance = finAccount.getBigDecimal("actualBalance"); if (previousBalance == null) { - previousBalance = FinAccountHelper.ZERO; + previousBalance = FinAccountHelper.getZero(); } BigDecimal balance; @@ -621,7 +621,7 @@ public class FinAccountPaymentServices { // make sure balance is not null if (balance == null) { - balance = FinAccountHelper.ZERO; + balance = FinAccountHelper.getZero(); } Map<String, Object> result = ServiceUtil.returnSuccess(); @@ -686,7 +686,7 @@ public class FinAccountPaymentServices { // get the previous balance BigDecimal previousBalance = finAccount.getBigDecimal("actualBalance"); if (previousBalance == null) { - previousBalance = FinAccountHelper.ZERO; + previousBalance = FinAccountHelper.getZero(); } // create the transaction @@ -704,7 +704,7 @@ public class FinAccountPaymentServices { // make sure balance is not null if (actualBalance == null) { - actualBalance = FinAccountHelper.ZERO; + actualBalance = FinAccountHelper.getZero(); } else { if (actualBalance.compareTo(BigDecimal.ZERO) < 0) { // balance went below zero, set negative pending replenishment status so that no diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountProductServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountProductServices.java index 63ec943..d5c378d 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountProductServices.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountProductServices.java @@ -189,7 +189,7 @@ public class FinAccountProductServices { // price/amount/quantity to create initial deposit amount BigDecimal quantity = orderItem.getBigDecimal("quantity"); BigDecimal price = orderItem.getBigDecimal("unitPrice"); - BigDecimal deposit = price.multiply(quantity).setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); + BigDecimal deposit = price.multiply(quantity).setScale(FinAccountHelper.getDecimals(), FinAccountHelper.getRounding()); // create the financial account Map<String, Object> createCtx = new HashMap<>(); diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountServices.java index 80b37e6..4b246d7 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountServices.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/finaccount/FinAccountServices.java @@ -279,10 +279,10 @@ public class FinAccountServices { BigDecimal availableBalance = finAccount.getBigDecimal("availableBalance"); BigDecimal balance = finAccount.getBigDecimal("actualBalance"); if (availableBalance == null) { - availableBalance = FinAccountHelper.ZERO; + availableBalance = FinAccountHelper.getZero(); } if (balance == null) { - balance = FinAccountHelper.ZERO; + balance = FinAccountHelper.getZero(); } String statusId = finAccount.getString("statusId"); @@ -321,16 +321,16 @@ public class FinAccountServices { BigDecimal balance = finAccount.getBigDecimal("actualBalance"); if (balance == null) { - balance = FinAccountHelper.ZERO; + balance = FinAccountHelper.getZero(); } Debug.logInfo("Account #" + finAccountId + " Balance: " + balance + " Status: " + statusId, MODULE); - if ("FNACT_ACTIVE".equals(statusId) && balance.compareTo(FinAccountHelper.ZERO) < 1) { + if ("FNACT_ACTIVE".equals(statusId) && balance.compareTo(FinAccountHelper.getZero()) < 1) { finAccount.set("statusId", "FNACT_MANFROZEN"); Debug.logInfo("Financial account [" + finAccountId + "] has passed its threshold [" + balance + "] (Frozen)", MODULE); - } else if ("FNACT_MANFROZEN".equals(statusId) && balance.compareTo(FinAccountHelper.ZERO) > 0) { + } else if ("FNACT_MANFROZEN".equals(statusId) && balance.compareTo(FinAccountHelper.getZero()) > 0) { finAccount.set("statusId", "FNACT_ACTIVE"); Debug.logInfo("Financial account [" + finAccountId + "] has been made current [" + balance + "] (Un-Frozen)", MODULE); @@ -391,7 +391,7 @@ public class FinAccountServices { try (EntityListIterator eli = EntityQuery.use(delegator).from("FinAccountTrans").where(condition) .orderBy("-transactionDate").queryIterator()) { GenericValue trans; - while (remainingBalance.compareTo(FinAccountHelper.ZERO) < 0 && (trans = eli.next()) != null) { + while (remainingBalance.compareTo(FinAccountHelper.getZero()) < 0 && (trans = eli.next()) != null) { String orderId = trans.getString("orderId"); String orderItemSeqId = trans.getString("orderItemSeqId"); @@ -497,7 +497,7 @@ public class FinAccountServices { } // check to make sure we balanced out - if (remainingBalance.compareTo(FinAccountHelper.ZERO) == 1) { + if (remainingBalance.compareTo(FinAccountHelper.getZero()) == 1) { result = ServiceUtil.returnSuccess(UtilProperties.getMessage(RES_ERROR, "AccountingFinAccountPartiallyRefunded", locale)); } diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/payment/GiftCertificateServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/payment/GiftCertificateServices.java index 9d22ec6..af6267f 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/payment/GiftCertificateServices.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/payment/GiftCertificateServices.java @@ -84,7 +84,7 @@ public class GiftCertificateServices { final String deposit = "DEPOSIT"; GenericValue giftCertSettings = EntityQuery.use(delegator).from("ProductStoreFinActSetting") - .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId) + .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()) .cache().queryOne(); Map<String, Object> acctResult = null; @@ -106,7 +106,7 @@ public class GiftCertificateServices { // create the FinAccount Map<String, Object> acctCtx = UtilMisc.<String, Object>toMap("finAccountId", finAccountId); - acctCtx.put("finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId); + acctCtx.put("finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()); acctCtx.put("finAccountName", accountName); acctCtx.put("finAccountCode", pinNumber); acctCtx.put("userLogin", userLogin); @@ -118,7 +118,7 @@ public class GiftCertificateServices { } else { Map<String, Object> createAccountCtx = new HashMap<>(); createAccountCtx.put("ownerPartyId", partyId); - createAccountCtx.put("finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId); + createAccountCtx.put("finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()); createAccountCtx.put("productStoreId", productStoreId); createAccountCtx.put("currencyUomId", currency); createAccountCtx.put("finAccountName", accountName + " for party ["+partyId+"]"); @@ -188,7 +188,7 @@ public class GiftCertificateServices { // validate the pin if the store requires it and figure out the finAccountId from card number try { GenericValue giftCertSettings = EntityQuery.use(delegator).from("ProductStoreFinActSetting") - .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId) + .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()) .cache().queryOne(); if ("Y".equals(giftCertSettings.getString("requirePinCode"))) { if (!validatePin(delegator, cardNumber, pinNumber)) { @@ -206,7 +206,7 @@ public class GiftCertificateServices { return ServiceUtil.returnError(UtilProperties.getMessage(RES_ERROR, "AccountingFinAccountSetting", UtilMisc.toMap("productStoreId", productStoreId, - "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId), locale)); + "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()), locale)); } if (finAccountId == null) { @@ -283,7 +283,7 @@ public class GiftCertificateServices { // validate the pin if the store requires it try { GenericValue giftCertSettings = EntityQuery.use(delegator).from("ProductStoreFinActSetting") - .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId) + .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()) .cache().queryOne(); if ("Y".equals(giftCertSettings.getString("requirePinCode")) && !validatePin(delegator, cardNumber, pinNumber)) { return ServiceUtil.returnError(UtilProperties.getMessage(RES_ERROR, @@ -293,7 +293,7 @@ public class GiftCertificateServices { return ServiceUtil.returnError(UtilProperties.getMessage(RES_ERROR, "AccountingFinAccountSetting", UtilMisc.toMap("productStoreId", productStoreId, - "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId), locale)); + "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()), locale)); } Debug.logInfo("Attempting to redeem GC for " + amount, MODULE); @@ -486,7 +486,7 @@ public class GiftCertificateServices { // if the store requires pin codes, then validate pin code against card number, and the gift certificate's finAccountId is the gift card's card number // otherwise, the gift card's card number is an ecrypted string, which must be decoded to find the FinAccount GenericValue giftCertSettings = EntityQuery.use(delegator).from("ProductStoreFinActSetting") - .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId) + .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()) .cache().queryOne(); GenericValue finAccount = null; String finAccountId = null; @@ -509,7 +509,7 @@ public class GiftCertificateServices { return ServiceUtil.returnError(UtilProperties.getMessage(RES_ERROR, "AccountingFinAccountSetting", UtilMisc.toMap("productStoreId", productStoreId, - "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId), locale)); + "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()), locale)); } if (finAccountId == null) { @@ -531,7 +531,7 @@ public class GiftCertificateServices { Map<String, Object> result = ServiceUtil.returnSuccess(); // make sure to round and scale it to the same as availableBalance - amount = amount.setScale(FinAccountHelper.decimals, FinAccountHelper.rounding); + amount = amount.setScale(FinAccountHelper.getDecimals(), FinAccountHelper.getRounding()); // if availableBalance equal to or greater than amount, then auth if (UtilValidate.isNotEmpty(availableBalance) && availableBalance.compareTo(amount) >= 0) { @@ -756,14 +756,14 @@ public class GiftCertificateServices { GenericValue giftCertSettings = null; try { giftCertSettings = EntityQuery.use(delegator).from("ProductStoreFinActSetting") - .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId) + .where("productStoreId", productStoreId, "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()) .cache().queryOne(); } catch (GenericEntityException e) { - Debug.logError(e, "Unable to get Product Store FinAccount settings for " + FinAccountHelper.giftCertFinAccountTypeId, MODULE); + Debug.logError(e, "Unable to get Product Store FinAccount settings for " + FinAccountHelper.getGiftCertFinAccountTypeId(), MODULE); return ServiceUtil.returnError(UtilProperties.getMessage(RES_ERROR, "AccountingFinAccountSetting", UtilMisc.toMap("productStoreId", productStoreId, - "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId), locale) + ": " + e.getMessage()); + "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()), locale) + ": " + e.getMessage()); } // survey information diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/finaccount/FinAccountHelper.java b/applications/order/src/main/java/org/apache/ofbiz/order/finaccount/FinAccountHelper.java index 8f18868..48a5f61 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/finaccount/FinAccountHelper.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/finaccount/FinAccountHelper.java @@ -42,31 +42,41 @@ import org.apache.ofbiz.entity.util.EntityQuery; * A package of methods for improving efficiency of financial accounts services * */ -public class FinAccountHelper { - - private static final String MODULE = FinAccountHelper.class.getName(); - /** - * A word on precision: since we're just adding and subtracting, the interim figures should have one more decimal place of precision than the final numbers. - */ - public static final int decimals = UtilNumber.getBigDecimalScale("finaccount.decimals"); - public static final RoundingMode rounding = UtilNumber.getRoundingMode("finaccount.rounding"); - public static final BigDecimal ZERO = BigDecimal.ZERO.setScale(decimals, rounding); - - public static final String giftCertFinAccountTypeId = "GIFTCERT_ACCOUNT"; - - // pool of available characters for account codes, here numbers plus uppercase characters - static char[] char_pool = new char[10+26]; - static { - int j = 0; - for (int i = "0".charAt(0); i <= "9".charAt(0); i++) { - char_pool[j++] = (char) i; - } - for (int i = "A".charAt(0); i <= "Z".charAt(0); i++) { - char_pool[j++] = (char) i; - } - } - - +public final class FinAccountHelper { + private static final String MODULE = FinAccountHelper.class.getName(); + /** + * Precision: since we're just adding and subtracting, the interim figures should have one more decimal place of precision than the final numbers + */ + private static final int DECIMALS = UtilNumber.getBigDecimalScale("finaccount.decimals"); + private static final RoundingMode ROUNDING = UtilNumber.getRoundingMode("finaccount.rounding"); + private static final BigDecimal ZERO = BigDecimal.ZERO.setScale(DECIMALS, ROUNDING); + private static final String GC_FIN_ACCOUNT_TYPE = "GIFTCERT_ACCOUNT"; + + protected FinAccountHelper() { } + + // pool of available characters for account codes, here numbers plus uppercase characters + private static char[] charPool = new char[10 + 26]; + static { + int j = 0; + for (int i = "0".charAt(0); i <= "9".charAt(0); i++) { + charPool[j++] = (char) i; + } + for (int i = "A".charAt(0); i <= "Z".charAt(0); i++) { + charPool[j++] = (char) i; + } + } + public static int getDecimals() { + return DECIMALS; + } + public static RoundingMode getRounding() { + return ROUNDING; + } + public static BigDecimal getZero() { + return ZERO; + } + public static String getGiftCertFinAccountTypeId() { + return GC_FIN_ACCOUNT_TYPE; + } /** * A convenience method which adds transactions.get(0).get(fieldName) to initialValue, all done in BigDecimal to decimals and rounding * @param initialValue the initial value @@ -109,7 +119,7 @@ public class FinAccountHelper { while (!foundUniqueNewCode) { newAccountCode = new StringBuilder(codeLength); for (int i = 0; i < codeLength; i++) { - newAccountCode.append(char_pool[r.nextInt(char_pool.length)]); + newAccountCode.append(charPool[r.nextInt(charPool.length)]); } GenericValue existingAccountWithCode = EntityQuery.use(delegator).from("FinAccount") @@ -183,7 +193,7 @@ public class FinAccountHelper { EntityCondition.makeCondition("finAccountTransTypeId", EntityOperator.EQUALS, "ADJUSTMENT")), EntityOperator.OR)) .queryList(); - incrementTotal = addFirstEntryAmount(incrementTotal, transSums, "amount", (decimals+1), rounding); + incrementTotal = addFirstEntryAmount(incrementTotal, transSums, "amount", (DECIMALS+1), ROUNDING); // now find sum of all transactions with decrease the value transSums = EntityQuery.use(delegator) @@ -193,10 +203,10 @@ public class FinAccountHelper { EntityCondition.makeCondition("transactionDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime), EntityCondition.makeCondition("finAccountTransTypeId", EntityOperator.EQUALS, "WITHDRAWAL")) .queryList(); - decrementTotal = addFirstEntryAmount(decrementTotal, transSums, "amount", (decimals+1), rounding); + decrementTotal = addFirstEntryAmount(decrementTotal, transSums, "amount", (DECIMALS+1), ROUNDING); // the net balance is just the incrementTotal minus the decrementTotal - return incrementTotal.subtract(decrementTotal).setScale(decimals, rounding); + return incrementTotal.subtract(decrementTotal).setScale(DECIMALS, ROUNDING); } /** @@ -220,10 +230,10 @@ public class FinAccountHelper { EntityCondition.makeCondition("authorizationDate", EntityOperator.LESS_THAN_EQUAL_TO, asOfDateTime)) .queryList(); - BigDecimal authorizationsTotal = addFirstEntryAmount(ZERO, authSums, "amount", (decimals+1), rounding); + BigDecimal authorizationsTotal = addFirstEntryAmount(ZERO, authSums, "amount", (DECIMALS+1), ROUNDING); // the total available balance is transactions total minus authorizations total - return netBalance.subtract(authorizationsTotal).setScale(decimals, rounding); + return netBalance.subtract(authorizationsTotal).setScale(DECIMALS, ROUNDING); } public static boolean validateFinAccount(GenericValue finAccount) { diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/CheckOutHelper.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/CheckOutHelper.java index 863b1eb..f6b0e5c 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/CheckOutHelper.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/CheckOutHelper.java @@ -471,7 +471,7 @@ public class CheckOutHelper { errorMessages.add(errMsg); gcFieldsOkay = false; } else if ((finAccount.getBigDecimal("availableBalance") == null) || - !((finAccount.getBigDecimal("availableBalance")).compareTo(FinAccountHelper.ZERO) > 0)) { + !((finAccount.getBigDecimal("availableBalance")).compareTo(FinAccountHelper.getZero()) > 0)) { // if account's available balance (including authorizations) is not greater than zero, then return an error errMsg = UtilProperties.getMessage(RES_ERROR,"checkhelper.gift_card_has_no_value", cart.getLocale()); errorMessages.add(errMsg); diff --git a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java index c91a056..5131be5 100644 --- a/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java +++ b/applications/order/src/main/java/org/apache/ofbiz/order/shoppingcart/ShoppingCart.java @@ -1951,7 +1951,8 @@ public class ShoppingCart implements Iterable<ShoppingCartItem>, Serializable { * @throws GenericEntityException */ public GenericValue getGiftCertSettingFromStore(Delegator delegator) throws GenericEntityException { - return EntityQuery.use(delegator).from("ProductStoreFinActSetting").where("productStoreId", getProductStoreId(), "finAccountTypeId", FinAccountHelper.giftCertFinAccountTypeId).cache().queryOne(); + return EntityQuery.use(delegator).from("ProductStoreFinActSetting") + .where("productStoreId", getProductStoreId(), "finAccountTypeId", FinAccountHelper.getGiftCertFinAccountTypeId()).cache().queryOne(); } /** |
Free forum by Nabble | Edit this page |