ptuomola commented on a change in pull request #2002:
URL: https://github.com/apache/fineract/pull/2002#discussion_r776548392



##########
File path: 
fineract-provider/src/main/java/org/apache/fineract/portfolio/client/domain/AccountNumberGenerator.java
##########
@@ -143,17 +166,60 @@ private String generateAccountNumber(Map<String, String> 
propertyMap, AccountNum
             }
             if 
(accountNumberPrefixType.getValue().equals(AccountNumberPrefixType.PREFIX_SHORT_NAME.getValue()))
 {
                 Integer prefixLength = prefix.length();
-                Integer numberLength = accountMaxLength - prefixLength;
-                accountNumber = StringUtils.leftPad(propertyMap.get(ID), 
numberLength, '0');
+
+                if (randomAccountNumber.isEnabled()) {
+                    accountNumber = accountNumber.substring(prefixLength);
+                } else {
+                    Integer numberLength = accountMaxLength - prefixLength;
+                    accountNumber = StringUtils.leftPad(propertyMap.get(ID), 
numberLength, '0');
+                }
             } else {
                 accountNumber = StringUtils.leftPad(accountNumber, 
Integer.valueOf(propertyMap.get(ID).length()), '0');
             }
 
             accountNumber = StringUtils.overlay(accountNumber, prefix, 0, 0);
+
+            if (randomAccountNumber.isEnabled()) {
+                randomNumberValidation(propertyMap, accountNumberFormat, 
accountNumber);
+            }
+        }
+        return accountNumber;
+    }
+
+    private String randomNumberGenerator(int accountMaxLength, Map<String, 
String> propertyMap) {
+        String randomNumber = RandomStringUtils.random(accountMaxLength, 
false, true);
+
+        BigInteger accNumber = new BigInteger(randomNumber);
+        if (accNumber.equals(BigInteger.ZERO)) { // to avoid account no. 00 in 
randomisation
+            randomNumber = randomNumberGenerator(accountMaxLength, 
propertyMap);
         }
+
+        String accountNumber = randomNumber.substring(0, accountMaxLength);
         return accountNumber;
     }
 
+    private void randomNumberValidation(Map<String, String> propertyMap, 
AccountNumberFormat accountNumberFormat, String accountNumber) {
+
+        String entityType = propertyMap.get(ENTITY_TYPE);
+        if (entityType.equals("client")) { // avoid duplication it will loop 
until it finds new random account no.
+
+            Client client = 
this.clientRepository.getClientByAccountNumber(accountNumber);
+            if (client != null) {
+                accountNumber = generateAccountNumber(propertyMap, 
accountNumberFormat);

Review comment:
       Did you test this? I don't think this will work... in case of a 
conflict, you're assigning a new value to accountNumber here. But this does not 
change the value returned by the calling function - the calling function never 
sees the new value of accountNumber and therefore will return the originally 
generated number. Or am I missing something here? 
   
   Maybe easiest solution is to change this function to e.g. 
checkAccountNumberConflict() and return true if conflict exists. Then the 
calling function can check the return value and recursively call itself in case 
there's a conflict. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to