From 4883ce1b63522c1dc3ce4206b11bc0e45716533c Mon Sep 17 00:00:00 2001 From: Vishwas Babu AJ Date: Sat, 30 Mar 2013 03:51:17 +0530 Subject: [PATCH] adding relationship for guarantor + adding wrappers --- .../patches/5-guarantor-add-relationship.sql | 34 +++++++ .../staff/domain/StaffRepositoryWrapper.java | 35 +++++++ .../domain/ClientRepositoryWrapper.java | 34 +++++++ .../domain/LoanRepositoryWrapper.java | 34 +++++++ .../guarantor/GuarantorConstants.java | 9 +- .../guarantor/api/GuarantorsApiResource.java | 25 +++-- .../guarantor/command/GuarantorCommand.java | 24 ++++- .../guarantor/data/GuarantorData.java | 50 ++++++---- .../guarantor/domain/Guarantor.java | 42 ++++++--- .../guarantor/domain/GuarantorRepository.java | 4 +- .../exception/GuarantorNotFoundException.java | 5 + .../CreateGuarantorCommandHandler.java | 2 +- .../DeleteGuarantorCommandHandler.java | 2 +- .../UpdateGuarantorCommandHandler.java | 2 +- ...arantorCommandFromApiJsonDeserializer.java | 6 +- .../service/GuarantorReadPlatformService.java | 2 - .../GuarantorReadPlatformServiceImpl.java | 31 ++++--- .../GuarantorWritePlatformService.java | 6 +- ...ritePlatformServiceJpaRepositoryIImpl.java | 91 ++++++++++--------- 19 files changed, 328 insertions(+), 110 deletions(-) create mode 100755 mifosng-db/patches/5-guarantor-add-relationship.sql create mode 100755 mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/domain/StaffRepositoryWrapper.java create mode 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/domain/ClientRepositoryWrapper.java create mode 100755 mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java diff --git a/mifosng-db/patches/5-guarantor-add-relationship.sql b/mifosng-db/patches/5-guarantor-add-relationship.sql new file mode 100755 index 000000000..b111470f5 --- /dev/null +++ b/mifosng-db/patches/5-guarantor-add-relationship.sql @@ -0,0 +1,34 @@ +-- Adding new system defined code for tracking guarantor relation ship type +insert into m_code (code_name,is_system_defined) values ('GuarantorRelationship',1); + +-- Adding a few Default Guarantor Relationships + +insert into m_code_value (code_id,code_value,order_position) + select id,"Spouse",0 + from m_code + where m_code.code_name="GuarantorRelationship"; + +insert into m_code_value (code_id,code_value,order_position) + select id,"Parent",0 + from m_code + where m_code.code_name="GuarantorRelationship"; + +insert into m_code_value (code_id,code_value,order_position) + select id,"Sibling",0 + from m_code + where m_code.code_name="GuarantorRelationship"; + +insert into m_code_value (code_id,code_value,order_position) + select id,"Business Associate",0 + from m_code + where m_code.code_name="GuarantorRelationship"; + +insert into m_code_value (code_id,code_value,order_position) + select id,"Other",0 + from m_code + where m_code.code_name="GuarantorRelationship"; + +-- Add a column to link Guarantor to Code value +ALTER TABLE `m_guarantor` + ADD COLUMN `client_reln_cv_id` INT(11) NULL DEFAULT NULL AFTER `loan_id`, + ADD CONSTRAINT `FK_m_guarantor_m_code_value` FOREIGN KEY (`client_reln_cv_id`) REFERENCES `m_code_value` (`id`); \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/domain/StaffRepositoryWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/domain/StaffRepositoryWrapper.java new file mode 100755 index 000000000..3acb21d56 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/organisation/staff/domain/StaffRepositoryWrapper.java @@ -0,0 +1,35 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.organisation.staff.domain; + +import org.mifosplatform.organisation.staff.exception.StaffNotFoundException; +import org.mifosplatform.portfolio.client.domain.ClientRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + *

+ * Wrapper for {@link ClientRepository} that adds NULL checking and Error + * handling capabilities + *

+ */ +@Service +public class StaffRepositoryWrapper { + + private final StaffRepository repository; + + @Autowired + public StaffRepositoryWrapper(final StaffRepository repository) { + this.repository = repository; + } + + public Staff findOneWithNotFoundDetection(final Long id) { + final Staff staff = this.repository.findOne(id); + if (staff == null) { throw new StaffNotFoundException(id); } + return staff; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/domain/ClientRepositoryWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/domain/ClientRepositoryWrapper.java new file mode 100755 index 000000000..d485b0b87 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/client/domain/ClientRepositoryWrapper.java @@ -0,0 +1,34 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.portfolio.client.domain; + +import org.mifosplatform.portfolio.client.exception.ClientNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + *

+ * Wrapper for {@link ClientRepository} that adds NULL checking and Error + * handling capabilities + *

+ */ +@Service +public class ClientRepositoryWrapper { + + private final ClientRepository repository; + + @Autowired + public ClientRepositoryWrapper(final ClientRepository repository) { + this.repository = repository; + } + + public Client findOneWithNotFoundDetection(final Long id) { + final Client client = this.repository.findOne(id); + if (client == null) { throw new ClientNotFoundException(id); } + return client; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java new file mode 100755 index 000000000..beb165216 --- /dev/null +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/domain/LoanRepositoryWrapper.java @@ -0,0 +1,34 @@ +/** + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package org.mifosplatform.portfolio.loanaccount.domain; + +import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +/** + *

+ * Wrapper for {@link LoanRepository} that adds NULL checking and Error handling + * capabilities + *

+ */ +@Service +public class LoanRepositoryWrapper { + + private final LoanRepository repository; + + @Autowired + public LoanRepositoryWrapper(final LoanRepository repository) { + this.repository = repository; + } + + public Loan findOneWithNotFoundDetection(final Long id) { + final Loan loan = this.repository.findOne(id); + if (loan == null) { throw new LoanNotFoundException(id); } + return loan; + } + +} \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/GuarantorConstants.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/GuarantorConstants.java index 98f488957..993768a41 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/GuarantorConstants.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/GuarantorConstants.java @@ -10,13 +10,16 @@ import java.util.Set; public class GuarantorConstants { + public static final String GUARANTOR_RELATIONSHIP_CODE_NAME = "GuarantorRelationship"; + /*** * Enum of all parameters passed in while creating/updating a loan product ***/ public static enum GUARANTOR_JSON_INPUT_PARAMS { - LOAN_ID("loanId"), GUARANTOR_TYPE_ID("guarantorTypeId"), ENTITY_ID("entityId"), FIRSTNAME("firstname"), LASTNAME("lastname"), ADDRESS_LINE_1( - "addressLine1"), ADDRESS_LINE_2("addressLine2"), CITY("city"), STATE("state"), ZIP("zip"), COUNTRY("country"), MOBILE_NUMBER( - "mobileNumber"), PHONE_NUMBER("housePhoneNumber"), COMMENT("comment"), DATE_OF_BIRTH("dob"); + LOAN_ID("loanId"), CLIENT_RELATIONSHIP_TYPE_ID("clientRelationshipTypeId"), GUARANTOR_TYPE_ID("guarantorTypeId"), ENTITY_ID( + "entityId"), FIRSTNAME("firstname"), LASTNAME("lastname"), ADDRESS_LINE_1("addressLine1"), ADDRESS_LINE_2("addressLine2"), CITY( + "city"), STATE("state"), ZIP("zip"), COUNTRY("country"), MOBILE_NUMBER("mobileNumber"), PHONE_NUMBER("housePhoneNumber"), COMMENT( + "comment"), DATE_OF_BIRTH("dob"); private final String value; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/api/GuarantorsApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/api/GuarantorsApiResource.java index 0c328a90f..b54f217e9 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/api/GuarantorsApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/api/GuarantorsApiResource.java @@ -6,6 +6,7 @@ package org.mifosplatform.portfolio.loanaccount.guarantor.api; import java.util.Arrays; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -25,12 +26,15 @@ import javax.ws.rs.core.UriInfo; import org.mifosplatform.commands.domain.CommandWrapper; import org.mifosplatform.commands.service.CommandWrapperBuilder; import org.mifosplatform.commands.service.PortfolioCommandSourceWritePlatformService; +import org.mifosplatform.infrastructure.codes.data.CodeValueData; +import org.mifosplatform.infrastructure.codes.service.CodeValueReadPlatformService; import org.mifosplatform.infrastructure.core.api.ApiRequestParameterHelper; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.infrastructure.core.serialization.ApiRequestJsonSerializationSettings; import org.mifosplatform.infrastructure.core.serialization.DefaultToApiJsonSerializer; import org.mifosplatform.infrastructure.security.service.PlatformSecurityContext; +import org.mifosplatform.portfolio.loanaccount.guarantor.GuarantorConstants; import org.mifosplatform.portfolio.loanaccount.guarantor.data.GuarantorData; import org.mifosplatform.portfolio.loanaccount.guarantor.domain.GuarantorType; import org.mifosplatform.portfolio.loanaccount.guarantor.service.GuarantorEnumerations; @@ -44,13 +48,15 @@ import org.springframework.stereotype.Component; @Scope("singleton") public class GuarantorsApiResource { - private static final Set RESPONSE_DATA_PARAMETERS = new HashSet(Arrays.asList("guarantorType", "entityId", - "firstname", "lastname", "addressLine1", "addressLine2", "city", "state", "zip", "country", "mobileNumber", "housePhoneNumber", - "comment", "dob")); + private static final Set RESPONSE_DATA_PARAMETERS = new HashSet(Arrays.asList("id", "loanId", "clientRelationshipType", + "guarantorType", "firstname", "lastname", "entityId", "externalId", "officeName", "joinedDate", "addressLine1", "addressLine2", + "city", "state", "zip", "country", "mobileNumber", "housePhoneNumber", "comment", "dob", "guarantorTypeOptions", + "allowedClientRelationshipTypes")); private final String resourceNameForPermission = "GUARANTOR"; private final GuarantorReadPlatformService guarantorReadPlatformService; + private final CodeValueReadPlatformService codeValueReadPlatformService; private final DefaultToApiJsonSerializer apiJsonSerializerService; private final ApiRequestParameterHelper apiRequestParameterHelper; private final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService; @@ -59,12 +65,14 @@ public class GuarantorsApiResource { @Autowired public GuarantorsApiResource(final PlatformSecurityContext context, final GuarantorReadPlatformService guarantorReadPlatformService, final DefaultToApiJsonSerializer toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, - final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { + final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService, + final CodeValueReadPlatformService codeValueReadPlatformService) { this.context = context; this.apiRequestParameterHelper = apiRequestParameterHelper; this.commandsSourceWritePlatformService = commandsSourceWritePlatformService; this.apiJsonSerializerService = toApiJsonSerializer; this.guarantorReadPlatformService = guarantorReadPlatformService; + this.codeValueReadPlatformService = codeValueReadPlatformService; } @GET @@ -74,7 +82,10 @@ public class GuarantorsApiResource { public String newGuarantorTemplate(@Context final UriInfo uriInfo) { context.authenticatedUser().validateHasReadPermission(resourceNameForPermission); - GuarantorData guarantorData = guarantorReadPlatformService.retrieveNewGuarantorDetails(); + final List guarantorTypeOptions = GuarantorEnumerations.guarantorType(GuarantorType.values()); + final Collection allowedClientRelationshipTypes = this.codeValueReadPlatformService + .retrieveCodeValuesByCode(GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME); + GuarantorData guarantorData = GuarantorData.template(guarantorTypeOptions, allowedClientRelationshipTypes); final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); return this.apiJsonSerializerService.serialize(settings, guarantorData, RESPONSE_DATA_PARAMETERS); @@ -105,8 +116,10 @@ public class GuarantorsApiResource { final ApiRequestJsonSerializationSettings settings = apiRequestParameterHelper.process(uriInfo.getQueryParameters()); if (settings.isTemplate()) { + final Collection allowedClientRelationshipTypes = this.codeValueReadPlatformService + .retrieveCodeValuesByCode(GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME); final List guarantorTypeOptions = GuarantorEnumerations.guarantorType(GuarantorType.values()); - guarantorData = GuarantorData.templateOnTop(guarantorData, guarantorTypeOptions); + guarantorData = GuarantorData.templateOnTop(guarantorData, guarantorTypeOptions, allowedClientRelationshipTypes); } return this.apiJsonSerializerService.serialize(settings, guarantorData, RESPONSE_DATA_PARAMETERS); 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 180fc71ac..afb3ee267 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 @@ -21,6 +21,9 @@ import org.mifosplatform.portfolio.loanaccount.guarantor.domain.GuarantorType; */ public class GuarantorCommand { + /*** Fields for capturing relationship of Guarantor with customer **/ + private final Long clientRelationshipTypeId; + /*** Fields for current customers serving as guarantors **/ private final Integer guarantorTypeId; private final Long entityId; @@ -39,10 +42,13 @@ public class GuarantorCommand { private final String comment; private final LocalDate dob; - public GuarantorCommand(final Integer guarantorTypeId, final Long entityId, final String firstname, - final String lastname, final String addressLine1, final String addressLine2, final String city, final String state, - final String zip, final String country, final String mobileNumber, final String housePhoneNumber, final String comment, - final LocalDate dob) { + public GuarantorCommand(final Long clientRelationshipTypeId, final Integer guarantorTypeId, final Long entityId, + final String firstname, final String lastname, final String addressLine1, final String addressLine2, final String city, + final String state, final String zip, final String country, final String mobileNumber, final String housePhoneNumber, + final String comment, final LocalDate dob) { + + this.clientRelationshipTypeId = clientRelationshipTypeId; + /*** Fields for current entities serving as guarantors **/ this.guarantorTypeId = guarantorTypeId; this.entityId = entityId; @@ -76,6 +82,9 @@ public class GuarantorCommand { DataValidatorBuilder baseDataValidator = getDataValidator(dataValidationErrors); + baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID.getValue()) + .value(this.clientRelationshipTypeId).ignoreIfNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID.getValue()).value(this.guarantorTypeId).notNull() .inMinMaxRange(GuarantorType.getMinValue(), GuarantorType.getMaxValue()); @@ -101,6 +110,9 @@ public class GuarantorCommand { DataValidatorBuilder baseDataValidator = getDataValidator(dataValidationErrors); + baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID.getValue()) + .value(this.clientRelationshipTypeId).ignoreIfNull().integerGreaterThanZero(); + baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID.getValue()).value(this.guarantorTypeId) .ignoreIfNull().inMinMaxRange(GuarantorType.getMinValue(), GuarantorType.getMaxValue()); @@ -161,4 +173,8 @@ public class GuarantorCommand { DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors).resource("Guarantor"); return baseDataValidator; } + + public Long getClientRelationshipTypeId() { + return this.clientRelationshipTypeId; + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/data/GuarantorData.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/data/GuarantorData.java index 8c90e034d..44c91aaae 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/data/GuarantorData.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/data/GuarantorData.java @@ -5,9 +5,11 @@ */ package org.mifosplatform.portfolio.loanaccount.guarantor.data; +import java.util.Collection; import java.util.List; import org.joda.time.LocalDate; +import org.mifosplatform.infrastructure.codes.data.CodeValueData; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.organisation.staff.data.StaffData; import org.mifosplatform.portfolio.client.data.ClientData; @@ -18,6 +20,7 @@ public class GuarantorData { private final Long id; private final Long loanId; + private final CodeValueData clientRelationshipType; private final EnumOptionData guarantorType; private final String firstname; @@ -45,38 +48,46 @@ public class GuarantorData { // template @SuppressWarnings("unused") private final List guarantorTypeOptions; + private final Collection allowedClientRelationshipTypes; - public static GuarantorData template(final List guarantorTypeOptions) { - return new GuarantorData(null, null, null, GuarantorEnumerations.guarantorType(GuarantorType.CUSTOMER), null, null, null, null, - null, null, null, null, null, null, null, null, null, null, null, guarantorTypeOptions); + public static GuarantorData template(final List guarantorTypeOptions, + final Collection allowedClientRelationshipTypes) { + return new GuarantorData(null, null, null, null, GuarantorEnumerations.guarantorType(GuarantorType.CUSTOMER), null, null, null, + null, null, null, null, null, null, null, null, null, null, null, null, guarantorTypeOptions, + allowedClientRelationshipTypes); } - public static GuarantorData templateOnTop(final GuarantorData guarantorData, final List guarantorTypeOptions) { - return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.entityId, guarantorData.guarantorType, - guarantorData.firstname, guarantorData.lastname, guarantorData.dob, guarantorData.addressLine1, guarantorData.addressLine2, - guarantorData.city, guarantorData.state, guarantorData.zip, guarantorData.country, guarantorData.mobileNumber, - guarantorData.housePhoneNumber, guarantorData.comment, guarantorData.officeName, guarantorData.joinedDate, - guarantorData.externalId, guarantorTypeOptions); + public static GuarantorData templateOnTop(final GuarantorData guarantorData, final List guarantorTypeOptions, + final Collection allowedClientRelationshipTypes) { + return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.clientRelationshipType, guarantorData.entityId, + guarantorData.guarantorType, guarantorData.firstname, guarantorData.lastname, guarantorData.dob, + guarantorData.addressLine1, guarantorData.addressLine2, guarantorData.city, guarantorData.state, guarantorData.zip, + guarantorData.country, guarantorData.mobileNumber, guarantorData.housePhoneNumber, guarantorData.comment, + guarantorData.officeName, guarantorData.joinedDate, guarantorData.externalId, guarantorTypeOptions, + allowedClientRelationshipTypes); } public static GuarantorData mergeClientData(ClientData clientData, GuarantorData guarantorData) { - return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.entityId, guarantorData.guarantorType, - clientData.getFirstname(), clientData.getLastname(), null, null, null, null, null, null, null, null, null, null, - clientData.getOfficeName(), clientData.getJoinedDate(), clientData.getExternalId(), null); + return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.clientRelationshipType, guarantorData.entityId, + guarantorData.guarantorType, clientData.getFirstname(), clientData.getLastname(), null, null, null, null, null, null, null, + null, null, null, clientData.getOfficeName(), clientData.getJoinedDate(), clientData.getExternalId(), null, + guarantorData.allowedClientRelationshipTypes); } public static GuarantorData mergeStaffData(StaffData staffData, GuarantorData guarantorData) { - return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.entityId, guarantorData.guarantorType, - staffData.getFirstname(), staffData.getLastname(), null, null, null, null, null, null, null, null, null, null, - staffData.getOfficeName(), null, null, null); + return new GuarantorData(guarantorData.id, guarantorData.loanId, guarantorData.clientRelationshipType, guarantorData.entityId, + guarantorData.guarantorType, staffData.getFirstname(), staffData.getLastname(), null, null, null, null, null, null, null, + null, null, null, staffData.getOfficeName(), null, null, null, guarantorData.allowedClientRelationshipTypes); } - public GuarantorData(Long id, Long loanId, Long entityId, EnumOptionData guarantorType, String firstname, String lastname, - LocalDate dob, String addressLine1, String addressLine2, String city, String state, String zip, String country, - String mobileNumber, String housePhoneNumber, String comment, String officeName, LocalDate joinedDate, String externalId, - List guarantorTypeOptions) { + public GuarantorData(Long id, Long loanId, final CodeValueData clientRelationshipType, Long entityId, EnumOptionData guarantorType, + String firstname, String lastname, LocalDate dob, String addressLine1, String addressLine2, String city, String state, + String zip, String country, String mobileNumber, String housePhoneNumber, String comment, String officeName, + LocalDate joinedDate, String externalId, List guarantorTypeOptions, + Collection allowedClientRelationshipTypes) { this.id = id; this.loanId = loanId; + this.clientRelationshipType = clientRelationshipType; this.guarantorType = guarantorType; this.entityId = entityId; this.firstname = firstname; @@ -95,6 +106,7 @@ public class GuarantorData { this.joinedDate = joinedDate; this.externalId = externalId; this.guarantorTypeOptions = guarantorTypeOptions; + this.allowedClientRelationshipTypes = allowedClientRelationshipTypes; } public boolean isExternalGuarantor() { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/Guarantor.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/Guarantor.java index 9874386be..946550eb3 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/Guarantor.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/domain/Guarantor.java @@ -18,6 +18,7 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import org.apache.commons.lang.StringUtils; +import org.mifosplatform.infrastructure.codes.domain.CodeValue; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.mifosplatform.portfolio.loanaccount.guarantor.GuarantorConstants.GUARANTOR_JSON_INPUT_PARAMS; @@ -31,6 +32,10 @@ public class Guarantor extends AbstractPersistable { @JoinColumn(name = "loan_id", nullable = false) private Loan loan; + @ManyToOne + @JoinColumn(name = "client_reln_cv_id", nullable = false) + private CodeValue clientRelationshipType; + @Column(name = "type_enum", nullable = false) private Integer gurantorType; @@ -78,17 +83,18 @@ public class Guarantor extends AbstractPersistable { } - public static Guarantor createNew(Loan loan, Integer gurantorType, Long entityId, String firstname, String lastname, Date dateOfBirth, - String addressLine1, String addressLine2, String city, String state, String country, String zip, String housePhoneNumber, - String mobilePhoneNumber, String comment) { - return new Guarantor(loan, gurantorType, entityId, firstname, lastname, dateOfBirth, addressLine1, addressLine2, city, state, - country, zip, housePhoneNumber, mobilePhoneNumber, comment); + public static Guarantor createNew(Loan loan, CodeValue clientRelationshipType, Integer gurantorType, Long entityId, String firstname, + String lastname, Date dateOfBirth, String addressLine1, String addressLine2, String city, String state, String country, + String zip, String housePhoneNumber, String mobilePhoneNumber, String comment) { + return new Guarantor(loan, clientRelationshipType, gurantorType, entityId, firstname, lastname, dateOfBirth, addressLine1, + addressLine2, city, state, country, zip, housePhoneNumber, mobilePhoneNumber, comment); } - public Guarantor(Loan loan, Integer gurantorType, Long entityId, String firstname, String lastname, Date dateOfBirth, - String addressLine1, String addressLine2, String city, String state, String country, String zip, String housePhoneNumber, - String mobilePhoneNumber, String comment) { + public Guarantor(Loan loan, CodeValue clientRelationshipType, Integer gurantorType, Long entityId, String firstname, String lastname, + Date dateOfBirth, String addressLine1, String addressLine2, String city, String state, String country, String zip, + String housePhoneNumber, String mobilePhoneNumber, String comment) { this.loan = loan; + this.clientRelationshipType = clientRelationshipType; this.gurantorType = gurantorType; this.entityId = entityId; this.firstname = StringUtils.defaultIfEmpty(firstname, null); @@ -105,7 +111,7 @@ public class Guarantor extends AbstractPersistable { this.comment = StringUtils.defaultIfEmpty(comment, null); } - public static Guarantor fromJson(final Loan loan, final JsonCommand command) { + public static Guarantor fromJson(final Loan loan, final CodeValue clientRelationshipType, final JsonCommand command) { Integer gurantorType = command.integerValueSansLocaleOfParameterNamed(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID.getValue()); Long entityId = command.longValueOfParameterNamed(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID.getValue()); @@ -123,19 +129,21 @@ public class Guarantor extends AbstractPersistable { String mobilePhoneNumber = command.stringValueOfParameterNamed(GUARANTOR_JSON_INPUT_PARAMS.MOBILE_NUMBER.getValue()); String comment = command.stringValueOfParameterNamed(GUARANTOR_JSON_INPUT_PARAMS.COMMENT.getValue()); - return new Guarantor(loan, gurantorType, entityId, firstname, lastname, dateOfBirth, addressLine1, addressLine2, city, state, - country, zip, housePhoneNumber, mobilePhoneNumber, comment); + return new Guarantor(loan, clientRelationshipType, gurantorType, entityId, firstname, lastname, dateOfBirth, addressLine1, + addressLine2, city, state, country, zip, housePhoneNumber, mobilePhoneNumber, comment); } - return new Guarantor(loan, gurantorType, entityId, null, null, null, null, null, null, null, null, null, null, null, null); + return new Guarantor(loan, clientRelationshipType, gurantorType, entityId, null, null, null, null, null, null, null, null, null, + null, null, null); } public Map update(final JsonCommand command) { - final Map actualChanges = new LinkedHashMap(7); + final Map actualChanges = new LinkedHashMap(); handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID.getValue(), this.gurantorType, true); + handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID.getValue(), 0, true); handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID.getValue(), this.entityId); if (this.isExternalGuarantor()) { @@ -265,4 +273,12 @@ public class Guarantor extends AbstractPersistable { return this.loan.getOfficeId(); } + public CodeValue getClientRelationshipType() { + return this.clientRelationshipType; + } + + public void setClientRelationshipType(CodeValue clientRelationshipType) { + this.clientRelationshipType = clientRelationshipType; + } + } \ 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 017319384..1941a7b9e 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,9 +5,11 @@ */ package org.mifosplatform.portfolio.loanaccount.guarantor.domain; +import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; public interface GuarantorRepository extends JpaRepository, JpaSpecificationExecutor { - // no behaviour + + Guarantor findByLoanAndId(Loan loan, Long id); } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/GuarantorNotFoundException.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/GuarantorNotFoundException.java index c352cf784..0ba23a30f 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/GuarantorNotFoundException.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/exception/GuarantorNotFoundException.java @@ -15,4 +15,9 @@ public class GuarantorNotFoundException extends AbstractPlatformResourceNotFound public GuarantorNotFoundException(Long id) { super("error.msg.loan.guarantor.", "Guarantor with identifier " + id + " does not exist", id); } + + public GuarantorNotFoundException(Long loanId, Long guarantorId) { + super("error.msg.loan.guarantor.", "Guarantor with identifier " + guarantorId + " does not exist for loan with Identifier " + + loanId, loanId, guarantorId); + } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/CreateGuarantorCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/CreateGuarantorCommandHandler.java index c28f25ba5..2f0ec3d31 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/CreateGuarantorCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/CreateGuarantorCommandHandler.java @@ -27,6 +27,6 @@ public class CreateGuarantorCommandHandler implements NewCommandSourceHandler { @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.writePlatformService.createGuarantor(command); + return this.writePlatformService.createGuarantor(command.getLoanId(), command); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/DeleteGuarantorCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/DeleteGuarantorCommandHandler.java index ea67bded4..461cdf8c9 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/DeleteGuarantorCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/DeleteGuarantorCommandHandler.java @@ -26,6 +26,6 @@ public class DeleteGuarantorCommandHandler implements NewCommandSourceHandler { @Transactional @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.guarantorWritePlatformService.removeGuarantor(command.entityId()); + return this.guarantorWritePlatformService.removeGuarantor(command.getLoanId(), command.entityId()); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/UpdateGuarantorCommandHandler.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/UpdateGuarantorCommandHandler.java index 81b2f648f..3eea6fb37 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/UpdateGuarantorCommandHandler.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/handler/UpdateGuarantorCommandHandler.java @@ -26,6 +26,6 @@ public class UpdateGuarantorCommandHandler implements NewCommandSourceHandler { @Transactional @Override public CommandProcessingResult processCommand(final JsonCommand command) { - return this.writePlatformService.updateGuarantor(command.entityId(), command); + return this.writePlatformService.updateGuarantor(command.getLoanId(), command.entityId(), command); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/serialization/GuarantorCommandFromApiJsonDeserializer.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/serialization/GuarantorCommandFromApiJsonDeserializer.java index a997728bd..66b4cf11a 100755 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/serialization/GuarantorCommandFromApiJsonDeserializer.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/serialization/GuarantorCommandFromApiJsonDeserializer.java @@ -49,6 +49,8 @@ public final class GuarantorCommandFromApiJsonDeserializer extends AbstractFromA final JsonElement element = fromApiJsonHelper.parse(json); + Long clientRelationshipTypeId = fromApiJsonHelper.extractLongNamed( + GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID.getValue(), element); Integer guarantorTypeId = fromApiJsonHelper.extractIntegerSansLocaleNamed(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID.getValue(), element); Long entityId = fromApiJsonHelper.extractLongNamed(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID.getValue(), element); @@ -65,7 +67,7 @@ public final class GuarantorCommandFromApiJsonDeserializer extends AbstractFromA String comment = fromApiJsonHelper.extractStringNamed(GUARANTOR_JSON_INPUT_PARAMS.COMMENT.getValue(), element); LocalDate dob = fromApiJsonHelper.extractLocalDateNamed(GUARANTOR_JSON_INPUT_PARAMS.DATE_OF_BIRTH.getValue(), element); - return new GuarantorCommand(guarantorTypeId, entityId, firstname, lastname, addressLine1, addressLine2, city, state, zip, - country, mobileNumber, housePhoneNumber, comment, dob); + return new GuarantorCommand(clientRelationshipTypeId, guarantorTypeId, entityId, firstname, lastname, addressLine1, addressLine2, + city, state, zip, country, mobileNumber, housePhoneNumber, comment, dob); } } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformService.java index acc2fd2d7..b46209a5d 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformService.java @@ -30,6 +30,4 @@ public interface GuarantorReadPlatformService { GuarantorData retrieveGuarantor(Long loanId, Long guarantorId); - GuarantorData retrieveNewGuarantorDetails(); - } \ No newline at end of file diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformServiceImpl.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformServiceImpl.java index 4d429208e..aa2307059 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformServiceImpl.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorReadPlatformServiceImpl.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.List; import org.joda.time.LocalDate; +import org.mifosplatform.infrastructure.codes.data.CodeValueData; import org.mifosplatform.infrastructure.core.data.EnumOptionData; import org.mifosplatform.infrastructure.core.domain.JdbcSupport; import org.mifosplatform.infrastructure.core.service.TenantAwareRoutingDataSource; @@ -22,7 +23,6 @@ import org.mifosplatform.portfolio.loanaccount.domain.Loan; import org.mifosplatform.portfolio.loanaccount.domain.LoanRepository; import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException; import org.mifosplatform.portfolio.loanaccount.guarantor.data.GuarantorData; -import org.mifosplatform.portfolio.loanaccount.guarantor.domain.GuarantorType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; @@ -48,17 +48,28 @@ public class GuarantorReadPlatformServiceImpl implements GuarantorReadPlatformSe private static final class GuarantorMapper implements RowMapper { + private StringBuilder sqlBuilder = new StringBuilder( + " g.id as id, g.loan_id as loanId, g.client_reln_cv_id clientRelationshipTypeId, g.entity_id as entityId, g.type_enum guarantorType ,g.firstname as firstname, g.lastname as lastname, g.dob as dateOfBirth, g.address_line_1 as addressLine1, g.address_line_2 as addressLine2, g.city as city, g.state as state, g.country as country, g.zip as zip, g.house_phone_number as housePhoneNumber, g.mobile_number as mobilePhoneNumber, g.comment as comment, ") + .append(" cv.code_value as typeName")// + .append(" FROM m_guarantor g") // + .append(" left JOIN m_code_value cv on g.client_reln_cv_id = cv.id"); + public String schema() { - return " g.id as id, g.loan_id as loanId, g.entity_id as entityId, g.type_enum guarantorType ,g.firstname as firstname," - + " g.lastname as lastname, g.dob as dateOfBirth, g.address_line_1 as addressLine1, g.address_line_2 as addressLine2," - + " g.city as city, g.state as state, g.country as country, g.zip as zip, g.house_phone_number as housePhoneNumber, " - + " g.mobile_number as mobilePhoneNumber, g.comment as comment from m_guarantor g"; + return sqlBuilder.toString(); } @Override public GuarantorData mapRow(final ResultSet rs, @SuppressWarnings("unused") final int rowNum) throws SQLException { Long id = rs.getLong("id"); Long loanId = rs.getLong("loanId"); + Long clientRelationshipTypeId = JdbcSupport.getLong(rs, "clientRelationshipTypeId"); + CodeValueData clientRelationshipType = null; + + if (clientRelationshipTypeId != null) { + final String typeName = rs.getString("typeName"); + clientRelationshipType = CodeValueData.instance(clientRelationshipTypeId, typeName); + } + Integer guarantorTypeId = rs.getInt("guarantorType"); final EnumOptionData guarantorType = GuarantorEnumerations.guarantorType(guarantorTypeId); Long entityId = rs.getLong("entityId"); @@ -74,8 +85,8 @@ public class GuarantorReadPlatformServiceImpl implements GuarantorReadPlatformSe String mobileNumber = rs.getString("mobilePhoneNumber"); String housePhoneNumber = rs.getString("housePhoneNumber"); String comment = rs.getString("comment"); - return new GuarantorData(id, loanId, entityId, guarantorType, firstname, lastname, dob, addressLine1, addressLine2, city, - state, zip, country, mobileNumber, housePhoneNumber, comment, null, null, null, null); + return new GuarantorData(id, loanId, clientRelationshipType, entityId, guarantorType, firstname, lastname, dob, addressLine1, + addressLine2, city, state, zip, country, mobileNumber, housePhoneNumber, comment, null, null, null, null, null); } } @@ -111,12 +122,6 @@ public class GuarantorReadPlatformServiceImpl implements GuarantorReadPlatformSe return mergeDetailsForClientOrStaffGuarantor(guarantorData); } - @Override - public GuarantorData retrieveNewGuarantorDetails() { - final List guarantorTypeOptions = GuarantorEnumerations.guarantorType(GuarantorType.values()); - return GuarantorData.template(guarantorTypeOptions); - } - /** * @param guarantorData */ diff --git a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformService.java b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformService.java index dcc69289a..f62165739 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformService.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/portfolio/loanaccount/guarantor/service/GuarantorWritePlatformService.java @@ -10,10 +10,10 @@ import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; public interface GuarantorWritePlatformService { - CommandProcessingResult createGuarantor(final JsonCommand command); + CommandProcessingResult createGuarantor(final Long loanId, final JsonCommand command); - CommandProcessingResult updateGuarantor(final Long guarantorId, final JsonCommand command); + CommandProcessingResult updateGuarantor(final Long loanId, final Long guarantorId, final JsonCommand command); - CommandProcessingResult removeGuarantor(final Long guarantorId); + CommandProcessingResult removeGuarantor(final Long loanId, final Long guarantorId); } \ No newline at end of file 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 6a4ffd282..35e5cf19d 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 @@ -7,19 +7,17 @@ package org.mifosplatform.portfolio.loanaccount.guarantor.service; import java.util.Map; +import org.mifosplatform.infrastructure.codes.domain.CodeValue; +import org.mifosplatform.infrastructure.codes.domain.CodeValueRepositoryWrapper; import org.mifosplatform.infrastructure.core.api.JsonCommand; import org.mifosplatform.infrastructure.core.data.CommandProcessingResult; import org.mifosplatform.infrastructure.core.data.CommandProcessingResultBuilder; import org.mifosplatform.infrastructure.core.exception.PlatformDataIntegrityException; -import org.mifosplatform.organisation.staff.domain.Staff; -import org.mifosplatform.organisation.staff.domain.StaffRepository; -import org.mifosplatform.organisation.staff.exception.StaffNotFoundException; -import org.mifosplatform.portfolio.client.domain.Client; -import org.mifosplatform.portfolio.client.domain.ClientRepository; -import org.mifosplatform.portfolio.client.exception.ClientNotFoundException; +import org.mifosplatform.organisation.staff.domain.StaffRepositoryWrapper; +import org.mifosplatform.portfolio.client.domain.ClientRepositoryWrapper; import org.mifosplatform.portfolio.loanaccount.domain.Loan; -import org.mifosplatform.portfolio.loanaccount.domain.LoanRepository; -import org.mifosplatform.portfolio.loanaccount.exception.LoanNotFoundException; +import org.mifosplatform.portfolio.loanaccount.domain.LoanRepositoryWrapper; +import org.mifosplatform.portfolio.loanaccount.guarantor.GuarantorConstants; import org.mifosplatform.portfolio.loanaccount.guarantor.GuarantorConstants.GUARANTOR_JSON_INPUT_PARAMS; import org.mifosplatform.portfolio.loanaccount.guarantor.command.GuarantorCommand; import org.mifosplatform.portfolio.loanaccount.guarantor.domain.Guarantor; @@ -39,34 +37,45 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto private final static Logger logger = LoggerFactory.getLogger(GuarantorWritePlatformServiceJpaRepositoryIImpl.class); - private final ClientRepository clientRepository; - private final StaffRepository staffRepository; - private final LoanRepository loanRepository; + private final ClientRepositoryWrapper clientRepositoryWrapper; + private final StaffRepositoryWrapper staffRepositoryWrapper; + private final LoanRepositoryWrapper loanRepositoryWrapper; private final GuarantorRepository guarantorRepository; private final GuarantorCommandFromApiJsonDeserializer fromApiJsonDeserializer; + private final CodeValueRepositoryWrapper codeValueRepositoryWrapper; @Autowired - public GuarantorWritePlatformServiceJpaRepositoryIImpl(final LoanRepository loanRepository, - final GuarantorRepository guarantorRepository, final ClientRepository clientRepository, final StaffRepository staffRepository, - final GuarantorCommandFromApiJsonDeserializer fromApiJsonDeserializer) { - this.loanRepository = loanRepository; - this.clientRepository = clientRepository; + public GuarantorWritePlatformServiceJpaRepositoryIImpl(final LoanRepositoryWrapper loanRepositoryWrapper, + final GuarantorRepository guarantorRepository, final ClientRepositoryWrapper clientRepositoryWrapper, + final StaffRepositoryWrapper staffRepositoryWrapper, final GuarantorCommandFromApiJsonDeserializer fromApiJsonDeserializer, + final CodeValueRepositoryWrapper codeValueRepositoryWrapper) { + this.loanRepositoryWrapper = loanRepositoryWrapper; + this.clientRepositoryWrapper = clientRepositoryWrapper; this.fromApiJsonDeserializer = fromApiJsonDeserializer; this.guarantorRepository = guarantorRepository; - this.staffRepository = staffRepository; + this.staffRepositoryWrapper = staffRepositoryWrapper; + this.codeValueRepositoryWrapper = codeValueRepositoryWrapper; } @Override @Transactional - public CommandProcessingResult createGuarantor(final JsonCommand command) { + public CommandProcessingResult createGuarantor(final Long loanId, final JsonCommand command) { try { final GuarantorCommand guarantorCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json()); guarantorCommand.validateForCreate(); - final Long loanId = command.getLoanId(); - Loan loan = retrieveLoanById(loanId); + Loan loan = this.loanRepositoryWrapper.findOneWithNotFoundDetection(loanId); + + Long clientRelationshipId = guarantorCommand.getClientRelationshipTypeId(); + CodeValue clientRelationshipType = null; + + if (clientRelationshipId != null) { + clientRelationshipType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME, clientRelationshipId); + } + Guarantor guarantor = null; - guarantor = Guarantor.fromJson(loan, command); + guarantor = Guarantor.fromJson(loan, clientRelationshipType, command); validateGuarantorBusinessRules(guarantor); @@ -78,22 +87,31 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto handleGuarantorDataIntegrityIssues(command, dve); return CommandProcessingResult.empty(); } - } @Override @Transactional - public CommandProcessingResult updateGuarantor(final Long guarantorId, final JsonCommand command) { + public CommandProcessingResult updateGuarantor(final Long loanId, final Long guarantorId, final JsonCommand command) { try { final GuarantorCommand guarantorCommand = this.fromApiJsonDeserializer.commandFromApiJson(command.json()); guarantorCommand.validateForUpdate(); - // TODO: Vishwas need to fetch by both loan and Guarantor Id - final Guarantor guarantorForUpdate = this.guarantorRepository.findOne(guarantorId); - if (guarantorForUpdate == null) { throw new GuarantorNotFoundException(guarantorId); } + final Loan loan = this.loanRepositoryWrapper.findOneWithNotFoundDetection(loanId); + final Guarantor guarantorForUpdate = this.guarantorRepository.findByLoanAndId(loan, guarantorId); + if (guarantorForUpdate == null) { throw new GuarantorNotFoundException(loanId, guarantorId); } final Map changesOnly = guarantorForUpdate.update(command); + if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID)) { + Long clientRelationshipId = guarantorCommand.getClientRelationshipTypeId(); + CodeValue clientRelationshipType = null; + if (clientRelationshipId != null) { + clientRelationshipType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( + GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME, clientRelationshipId); + } + guarantorForUpdate.setClientRelationshipType(clientRelationshipType); + } + if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID) || changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.GUARANTOR_TYPE_ID)) { validateGuarantorBusinessRules(guarantorForUpdate); @@ -113,10 +131,10 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto @Override @Transactional - public CommandProcessingResult removeGuarantor(final Long guarantorId) { - // TODO: Vishwas need to fetch by both loan and Guarantor Id - final Guarantor guarantorForDelete = this.guarantorRepository.findOne(guarantorId); - if (guarantorForDelete == null) { throw new GuarantorNotFoundException(guarantorId); } + public CommandProcessingResult removeGuarantor(final Long loanId, final Long guarantorId) { + final Loan loan = this.loanRepositoryWrapper.findOneWithNotFoundDetection(loanId); + final Guarantor guarantorForDelete = this.guarantorRepository.findByLoanAndId(loan, guarantorId); + if (guarantorForDelete == null) { throw new GuarantorNotFoundException(loanId, guarantorId); } this.guarantorRepository.delete(guarantorForDelete); @@ -133,24 +151,15 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto // validate guarantor conditions if (guarantor.isExistingCustomer()) { // check client exists - Client client = clientRepository.findOne(guarantor.getEntityId()); - if (client == null) { throw new ClientNotFoundException(guarantor.getEntityId()); } - + 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()); } } else if (guarantor.isExistingEmployee()) { - Staff staff = staffRepository.findOne(guarantor.getEntityId()); - if (staff == null) { throw new StaffNotFoundException(guarantor.getEntityId()); } + staffRepositoryWrapper.findOneWithNotFoundDetection(guarantor.getEntityId()); } } - private Loan retrieveLoanById(final Long loanId) { - Loan loan = loanRepository.findOne(loanId); - if (loan == null) { throw new LoanNotFoundException(loanId); } - return loan; - } - /** * Throws an exception for any data Integrity issue *