From be4a9a9f692ed457aca32792ceaa33b9d2c4742d Mon Sep 17 00:00:00 2001 From: goutham-M Date: Tue, 2 Jul 2013 20:37:47 +0530 Subject: [PATCH] For same loan same guarantor can be associate more than once(fixed) --- .../guarantor/command/GuarantorCommand.java | 4 +++ .../guarantor/domain/GuarantorRepository.java | 3 ++ .../DuplicateGuarantorException.java | 12 +++++++ ...ritePlatformServiceJpaRepositoryIImpl.java | 36 ++++++++++++++++--- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/DuplicateGuarantorException.java diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/command/GuarantorCommand.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/command/GuarantorCommand.java index afb3ee267..32e45056a 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/command/GuarantorCommand.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/command/GuarantorCommand.java @@ -177,4 +177,8 @@ public class GuarantorCommand { public Long getClientRelationshipTypeId() { return this.clientRelationshipTypeId; } + + public Long getEntityId() { + return this.entityId; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/GuarantorRepository.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/GuarantorRepository.java index 1941a7b9e..296bd4f5d 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/GuarantorRepository.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/GuarantorRepository.java @@ -5,6 +5,8 @@ */ package org.mifosplatform.portfolio.loanaccount.guarantor.domain; +import java.util.List; + import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; @@ -12,4 +14,5 @@ import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface GuarantorRepository extends JpaRepository, JpaSpecificationExecutor { Guarantor findByLoanAndId(Loan loan, Long id); + List findByLoan(Loan loan); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/DuplicateGuarantorException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/DuplicateGuarantorException.java new file mode 100644 index 000000000..87ca9125e --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/DuplicateGuarantorException.java @@ -0,0 +1,12 @@ +package org.mifosplatform.portfolio.loanaccount.guarantor.exception; + +import org.mifosplatform.infrastructure.core.exception.AbstractPlatformDomainRuleException; + + +public class DuplicateGuarantorException extends AbstractPlatformDomainRuleException { + + public DuplicateGuarantorException(final String action, String postFix, String defaultUserMessage, Object... defaultUserMessageArgs) { + super("error.msg." + action + "." + postFix, defaultUserMessage, defaultUserMessageArgs); + // TODO Auto-generated constructor stub + } +} diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformServiceJpaRepositoryIImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformServiceJpaRepositoryIImpl.java index efff482cf..b2e9e6781 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformServiceJpaRepositoryIImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformServiceJpaRepositoryIImpl.java @@ -5,6 +5,7 @@ */ package org.mifosplatform.portfolio.loanaccount.guarantor.service; +import java.util.List; import java.util.Map; import org.mifosplatform.infrastructure.codes.domain.CodeValue; @@ -22,6 +23,7 @@ import org.mifosplatform.portfolio.loanaccount.guarantor.GuarantorConstants.GUAR import org.mifosplatform.portfolio.loanaccount.guarantor.command.GuarantorCommand; import org.mifosplatform.portfolio.loanaccount.guarantor.domain.Guarantor; import org.mifosplatform.portfolio.loanaccount.guarantor.domain.GuarantorRepository; +import org.mifosplatform.portfolio.loanaccount.guarantor.exception.DuplicateGuarantorException; import org.mifosplatform.portfolio.loanaccount.guarantor.exception.GuarantorNotFoundException; import org.mifosplatform.portfolio.loanaccount.guarantor.exception.InvalidGuarantorException; import org.mifosplatform.portfolio.loanaccount.guarantor.serialization.GuarantorCommandFromApiJsonDeserializer; @@ -73,7 +75,19 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto clientRelationshipType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME, clientRelationshipId); } + + List existGuarantorList = this.guarantorRepository.findByLoan(loan); + Long entityId = guarantorCommand.getEntityId(); + for (Guarantor guarantor : existGuarantorList) { + if (guarantor.getLoanId() == loanId && guarantor.getEntityId() == entityId) { + String defaultUserMessage = this.clientRepositoryWrapper.findOneWithNotFoundDetection(entityId).getDisplayName(); + defaultUserMessage = defaultUserMessage + " is already exist as a guarantor for this loan"; + String action = loan.client() != null ? "client.guarantor" : "group.guarantor"; + throw new DuplicateGuarantorException(action, "is.already.exist.same.loan", defaultUserMessage, entityId, loanId); + } + } + Guarantor guarantor = null; guarantor = Guarantor.fromJson(loan, clientRelationshipType, command); @@ -111,12 +125,24 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto } guarantorForUpdate.setClientRelationshipType(clientRelationshipType); } + + List existGuarantorList = this.guarantorRepository.findByLoan(loan); + Long entityId = guarantorCommand.getEntityId(); + for (Guarantor guarantor : existGuarantorList) { + if (guarantor.getLoanId() == loanId && guarantor.getEntityId() == entityId) { + String defaultUserMessage = this.clientRepositoryWrapper.findOneWithNotFoundDetection(entityId).getDisplayName(); + defaultUserMessage = defaultUserMessage + " is already exist as a guarantor for this loan"; + String action = loan.client() != null ? "client.guarantor" : "group.guarantor"; + throw new DuplicateGuarantorException(action, "is.already.exist.same.loan", defaultUserMessage, entityId, loanId); + } + } + if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID) || changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID)) { validateGuarantorBusinessRules(guarantorForUpdate); } - + if (!changesOnly.isEmpty()) { this.guarantorRepository.saveAndFlush(guarantorForUpdate); } @@ -148,9 +174,11 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto // check client exists clientRepositoryWrapper.findOneWithNotFoundDetection(guarantor.getEntityId()); // validate that the client is not set as a self guarantor - if (guarantor.getClientId().equals(guarantor.getEntityId())) { - // - throw new InvalidGuarantorException(guarantor.getEntityId(), guarantor.getLoanId()); + if (guarantor.getClientId() != null) { + if (guarantor.getClientId().equals(guarantor.getEntityId())) { + // + throw new InvalidGuarantorException(guarantor.getEntityId(), guarantor.getLoanId()); + } } } else if (guarantor.isExistingEmployee()) { staffRepositoryWrapper.findOneWithNotFoundDetection(guarantor.getEntityId());