BLasan commented on a change in pull request #1770: URL: https://github.com/apache/fineract/pull/1770#discussion_r701822538
########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/collateralmanagement/service/LoanCollateralAssembler.java ########## @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.portfolio.collateralmanagement.service; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.math.BigDecimal; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; +import org.apache.fineract.infrastructure.codes.domain.CodeValueRepositoryWrapper; +import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper; +import org.apache.fineract.portfolio.collateralmanagement.domain.ClientCollateralManagement; +import org.apache.fineract.portfolio.collateralmanagement.domain.ClientCollateralManagementRepositoryWrapper; +import org.apache.fineract.portfolio.collateralmanagement.exception.LoanCollateralManagementNotFoundException; +import org.apache.fineract.portfolio.loanaccount.domain.LoanCollateralManagement; +import org.apache.fineract.portfolio.loanaccount.domain.LoanCollateralManagementRepository; +import org.apache.fineract.portfolio.loanaccount.exception.InvalidAmountOfCollateralQuantity; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class LoanCollateralAssembler { + + private final FromJsonHelper fromApiJsonHelper; + private final CodeValueRepositoryWrapper codeValueRepository; + private final LoanCollateralManagementRepository loanCollateralRepository; + private final ClientCollateralManagementRepositoryWrapper clientCollateralManagementRepositoryWrapper; + + @Autowired + public LoanCollateralAssembler(final FromJsonHelper fromApiJsonHelper, final CodeValueRepositoryWrapper codeValueRepository, + final LoanCollateralManagementRepository loanCollateralRepository, + final ClientCollateralManagementRepositoryWrapper clientCollateralManagementRepositoryWrapper) { + this.fromApiJsonHelper = fromApiJsonHelper; + this.codeValueRepository = codeValueRepository; + this.loanCollateralRepository = loanCollateralRepository; + this.clientCollateralManagementRepositoryWrapper = clientCollateralManagementRepositoryWrapper; + } + + public Set<LoanCollateralManagement> fromParsedJson(final JsonElement element) { + + final Set<LoanCollateralManagement> collateralItems = new HashSet<>(); + + JsonObject jsonObject = element.getAsJsonObject(); + final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(jsonObject); + + if (jsonObject.has("collateral") && jsonObject.get("collateral").isJsonArray()) { + JsonArray collaterals = jsonObject.get("collateral").getAsJsonArray(); + + for (int i = 0; i < collaterals.size(); i++) { + final JsonObject collateralItemElement = collaterals.get(i).getAsJsonObject(); + final Long id = this.fromApiJsonHelper.extractLongNamed("id", collateralItemElement); + final Long collateralId = this.fromApiJsonHelper.extractLongNamed("clientCollateralId", collateralItemElement); + final ClientCollateralManagement clientCollateral = this.clientCollateralManagementRepositoryWrapper + .getCollateral(collateralId); + final BigDecimal quantity = this.fromApiJsonHelper.extractBigDecimalNamed("quantity", collateralItemElement, locale); + BigDecimal updatedClientQuantity = null; + + if (id == null) { + updatedClientQuantity = clientCollateral.getQuantity().subtract(quantity); + if (BigDecimal.ZERO.compareTo(updatedClientQuantity) > 0) { + throw new InvalidAmountOfCollateralQuantity(quantity); + } + clientCollateral.updateQuantity(updatedClientQuantity); + // this.clientCollateralManagementRepositoryWrapper.saveAndFlush(clientCollateral); + collateralItems.add(LoanCollateralManagement.from(clientCollateral, quantity)); + } else { + LoanCollateralManagement loanCollateralManagement = this.loanCollateralRepository.findById(id) + .orElseThrow(() -> new LoanCollateralManagementNotFoundException(id)); + + if (loanCollateralManagement.getQuantity().compareTo(quantity) != 0) { + updatedClientQuantity = clientCollateral.getQuantity().add(loanCollateralManagement.getQuantity()) + .subtract(quantity); + if (BigDecimal.ZERO.compareTo(updatedClientQuantity) > 0) { + throw new InvalidAmountOfCollateralQuantity(quantity); + } + } else { + updatedClientQuantity = quantity; + } + + // loanCollateralManagement.setQuantity(quantity); + clientCollateral.updateQuantity(updatedClientQuantity); + // loanCollateralManagement.setClientCollateralManagement(clientCollateral); + // this.clientCollateralManagementRepositoryWrapper.saveAndFlush(clientCollateral); + collateralItems Review comment: Fixed ########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/api/LoansApiResource.java ########## @@ -604,12 +612,23 @@ public String retrieveLoan(@PathParam("loanId") @Parameter(description = "loanId if (associationParameters.contains("collateral")) { mandatoryResponseParameters.add("collateral"); - collateral = this.loanCollateralReadPlatformService.retrieveCollaterals(loanId); - if (CollectionUtils.isEmpty(collateral)) { - collateral = null; + loanCollateralManagements = this.loanCollateralManagementReadPlatformService.getLoanCollateralResponseDataList(loanId); + for (LoanCollateralResponseData loanCollateralManagement : loanCollateralManagements) { + loanCollateralManagementData.add(loanCollateralManagement.toCommand()); + } + if (CollectionUtils.isEmpty(loanCollateralManagements)) { + loanCollateralManagements = null; } } + // if (associationParameters.contains("collateral")) { + // mandatoryResponseParameters.add("collateral"); + // collateral = this.loanCollateralReadPlatformService.retrieveCollaterals(loanId); + // if (CollectionUtils.isEmpty(collateral)) { + // collateral = null; + // } + // } Review comment: Fixed ########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationCommandFromApiJsonHelper.java ########## @@ -847,38 +910,67 @@ public void validateForModify(final String json, final LoanProduct loanProduct, } } - // collateral - final String collateralParameterName = "collateral"; - if (element.isJsonObject() && this.fromApiJsonHelper.parameterExists(collateralParameterName, element)) { - final JsonObject topLevelJsonElement = element.getAsJsonObject(); - final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement); - if (topLevelJsonElement.get("collateral").isJsonArray()) { - - final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); - final Set<String> supportedParameters = new HashSet<>(Arrays.asList("id", "type", "value", "description")); - final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray(); - for (int i = 1; i <= array.size(); i++) { - final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject(); - - final String collateralJson = this.fromApiJsonHelper.toJson(collateralItemElement); - this.fromApiJsonHelper.checkForUnsupportedParameters(collateralParameterTypeOfMap, collateralJson, supportedParameters); - - final Long collateralTypeId = this.fromApiJsonHelper.extractLongNamed("type", collateralItemElement); - baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull() - .integerGreaterThanZero(); - - final BigDecimal collateralValue = this.fromApiJsonHelper.extractBigDecimalNamed("value", collateralItemElement, - locale); - baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("value", i).value(collateralValue) - .ignoreIfNull().positiveAmount(); + final String loanTypeParameterName = "loanType"; + final String loanTypeStr = this.fromApiJsonHelper.extractStringNamed(loanTypeParameterName, element); + baseDataValidator.reset().parameter(loanTypeParameterName).value(loanTypeStr).notNull(); - final String description = this.fromApiJsonHelper.extractStringNamed("description", collateralItemElement); - baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", i).value(description).notBlank() - .notExceedingLengthOf(500); + if (!StringUtils.isBlank(loanTypeStr)) { + final AccountType loanType = AccountType.fromName(loanTypeStr); + baseDataValidator.reset().parameter(loanTypeParameterName).value(loanType.getValue()).inMinMaxRange(1, 4); + /** + * TODO: Allow for other loan account types. + */ Review comment: Fixed ########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/serialization/LoanApplicationCommandFromApiJsonHelper.java ########## @@ -414,38 +422,93 @@ public void validateForCreate(final String json, final boolean isMeetingMandator } } - // collateral - final String collateralParameterName = "collateral"; - if (element.isJsonObject() && this.fromApiJsonHelper.parameterExists(collateralParameterName, element)) { - final JsonObject topLevelJsonElement = element.getAsJsonObject(); - final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement); - if (topLevelJsonElement.get("collateral").isJsonArray()) { - - final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); - final Set<String> supportedParameters = new HashSet<>(Arrays.asList("id", "type", "value", "description")); - final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray(); - for (int i = 1; i <= array.size(); i++) { - final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject(); - - final String collateralJson = this.fromApiJsonHelper.toJson(collateralItemElement); - this.fromApiJsonHelper.checkForUnsupportedParameters(collateralParameterTypeOfMap, collateralJson, supportedParameters); - - final Long collateralTypeId = this.fromApiJsonHelper.extractLongNamed("type", collateralItemElement); - baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", i).value(collateralTypeId).notNull() - .integerGreaterThanZero(); + // // collateral + // final String collateralParameterName = "collateral"; + // if (element.isJsonObject() && this.fromApiJsonHelper.parameterExists(collateralParameterName, element)) { + // final JsonObject topLevelJsonElement = element.getAsJsonObject(); + // final Locale locale = this.fromApiJsonHelper.extractLocaleParameter(topLevelJsonElement); + // if (topLevelJsonElement.get("collateral").isJsonArray()) { + // + // final Type collateralParameterTypeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); + // final Set<String> supportedParameters = new HashSet<>(Arrays.asList("id", "type", "value", "description")); + // final JsonArray array = topLevelJsonElement.get("collateral").getAsJsonArray(); + // for (int i = 1; i <= array.size(); i++) { + // final JsonObject collateralItemElement = array.get(i - 1).getAsJsonObject(); + // + // final String collateralJson = this.fromApiJsonHelper.toJson(collateralItemElement); + // this.fromApiJsonHelper.checkForUnsupportedParameters(collateralParameterTypeOfMap, collateralJson, + // supportedParameters); + // + // final Long collateralTypeId = this.fromApiJsonHelper.extractLongNamed("type", collateralItemElement); + // baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("type", + // i).value(collateralTypeId).notNull() + // .integerGreaterThanZero(); + // + // final BigDecimal collateralValue = this.fromApiJsonHelper.extractBigDecimalNamed("value", + // collateralItemElement, + // locale); + // baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("value", i).value(collateralValue) + // .ignoreIfNull().positiveAmount(); + // + // final String description = this.fromApiJsonHelper.extractStringNamed("description", collateralItemElement); + // baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("description", + // i).value(description).notBlank() + // .notExceedingLengthOf(500); + // + // } + // } else { + // baseDataValidator.reset().parameter(collateralParameterName).expectedArrayButIsNot(); + // } + // } - final BigDecimal collateralValue = this.fromApiJsonHelper.extractBigDecimalNamed("value", collateralItemElement, - locale); - baseDataValidator.reset().parameter("collateral").parameterAtIndexArray("value", i).value(collateralValue) - .ignoreIfNull().positiveAmount(); + /** + * TODO: Add collaterals for group loan accounts. FOr now it's only applicable for individual accounts. + * (loanType.isJLG() || loanType.isGLIM()) + */ Review comment: Fixed ########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java ########## @@ -494,10 +498,14 @@ private Loan(final String accountNo, final Client client, final Group group, fin this.charges = null; this.summary = new LoanSummary(); } - if (collateral != null && !collateral.isEmpty()) { - this.collateral = associateWithThisLoan(collateral); + + /** + * TODO: Apply for group loan creation. + */ Review comment: Fixed ########## File path: fineract-provider/src/main/java/org/apache/fineract/portfolio/loanaccount/domain/Loan.java ########## @@ -3008,6 +3021,14 @@ public void makeRefund(final LoanTransaction loanTransaction, final LoanLifecycl final String errorMessage = "Transfer funds is allowed only for loan accounts with overpaid status "; throw new InvalidLoanStateTransitionException("transaction", "is.not.a.overpaid.loan", errorMessage); } + + // // Update the transaction in loan collateral module. + // Set<LoanCollateralManagement> loanCollateralManagements = + // loanTransaction.getLoan().getLoanCollateralManagements(); + // for (LoanCollateralManagement loanCollateralManagement: loanCollateralManagements) { + // loanCollateralManagement.setLoanTransactionData(loanTransaction); + // } + Review comment: Fixed -- 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]
