For same loan same guarantor can be associate more than once(fixed)

This commit is contained in:
goutham-M
2013-07-03 09:50:47 +05:30
parent f00d212e8f
commit be4a9a9f69
4 changed files with 51 additions and 4 deletions
@@ -177,4 +177,8 @@ public class GuarantorCommand {
public Long getClientRelationshipTypeId() {
return this.clientRelationshipTypeId;
}
public Long getEntityId() {
return this.entityId;
}
}
@@ -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<Guarantor, Long>, JpaSpecificationExecutor<Guarantor> {
Guarantor findByLoanAndId(Loan loan, Long id);
List<Guarantor> findByLoan(Loan loan);
}
@@ -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
}
}
@@ -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<Guarantor> 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<Guarantor> 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());