diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/DataValidatorBuilder.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/DataValidatorBuilder.java index f829c6746..ed6a28724 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/DataValidatorBuilder.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/core/data/DataValidatorBuilder.java @@ -9,6 +9,8 @@ import java.math.BigDecimal; import java.text.ParseException; import java.util.Arrays; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import net.fortuna.ical4j.model.ValidationException; import net.fortuna.ical4j.model.property.RRule; @@ -620,4 +622,30 @@ public class DataValidatorBuilder { public DataValidatorBuilder validateForBooleanValue() { return validateStringFor("TRUE" + VALID_INPUT_SEPERATOR + "FALSE"); } + + public DataValidatorBuilder validatePhoneNumber() { + if (this.value == null && this.ignoreNullValue) { return this; } + boolean validationErr = true; + String regex = "^\\+?[0-9. ()-]{0,25}$"; // supports numbers, + // parentheses(), hyphens and + // may contain + sign in the + // beginning and can contain + // whitespaces in between and + // length allowed is 0-25 chars. + Pattern pattern = Pattern.compile(regex); + Matcher matcher = pattern.matcher(this.value.toString()); + if (matcher.matches()) { + validationErr = false; + } + if (validationErr) { + final StringBuilder validationErrorCode = new StringBuilder("validation.msg.").append(this.resource).append(".") + .append(this.parameter).append(".format.is.invalid"); + final StringBuilder defaultEnglishMessage = new StringBuilder("The ").append(this.resource).append(this.parameter) + .append(" is in invalid format, should contain '-','+','()' and numbers only."); + final ApiParameterError error = ApiParameterError.parameterError(validationErrorCode.toString(), + defaultEnglishMessage.toString(), this.parameter, this.value); + this.dataValidationErrors.add(error); + } + return this; + } } \ No newline at end of file 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 32e45056a..afbfdd149 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 @@ -158,9 +158,9 @@ public class GuarantorCommand { baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.COUNTRY.getValue()).value(this.country).ignoreIfNull() .notExceedingLengthOf(50); baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.MOBILE_NUMBER.getValue()).value(this.mobileNumber).ignoreIfNull() - .notExceedingLengthOf(20); + .notExceedingLengthOf(20).validatePhoneNumber(); baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.PHONE_NUMBER.getValue()).value(this.housePhoneNumber) - .ignoreIfNull().notExceedingLengthOf(20); + .ignoreIfNull().notExceedingLengthOf(20).validatePhoneNumber(); baseDataValidator.reset().parameter(GUARANTOR_JSON_INPUT_PARAMS.COMMENT.getValue()).value(this.comment).ignoreIfNull() .notExceedingLengthOf(500); } 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 946550eb3..a752dbf61 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 @@ -144,7 +144,6 @@ public class Guarantor extends AbstractPersistable { 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()) { handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.FIRSTNAME.getValue(), this.firstname); @@ -159,6 +158,10 @@ public class Guarantor extends AbstractPersistable { handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.PHONE_NUMBER.getValue(), this.housePhoneNumber); handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.MOBILE_NUMBER.getValue(), this.mobilePhoneNumber); handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.COMMENT.getValue(), this.comment); + updateExistingEntityToNull(); + } else { + handlePropertyUpdate(command, actualChanges, GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID.getValue(), this.entityId); + updateExternalGuarantorFieldsToNull(); } return actualChanges; @@ -277,8 +280,26 @@ public class Guarantor extends AbstractPersistable { return this.clientRelationshipType; } - public void setClientRelationshipType(CodeValue clientRelationshipType) { + public void updateClientRelationshipType(CodeValue clientRelationshipType) { this.clientRelationshipType = clientRelationshipType; } + private void updateExternalGuarantorFieldsToNull() { + this.firstname = null; + this.lastname = null; + this.dateOfBirth = null; + this.addressLine1 = null; + this.addressLine2 = null; + this.city = null; + this.state = null; + this.country = null; + this.zip = null; + this.housePhoneNumber = null; + this.mobilePhoneNumber = null; + this.comment = null; + } + + private void updateExistingEntityToNull() { + this.entityId = null; + } } \ 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 b2e9e6781..a863e918c 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 @@ -116,26 +116,14 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto final Map changesOnly = guarantorForUpdate.update(command); - if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID)) { + if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.CLIENT_RELATIONSHIP_TYPE_ID.getValue())) { Long clientRelationshipId = guarantorCommand.getClientRelationshipTypeId(); CodeValue clientRelationshipType = null; if (clientRelationshipId != null) { clientRelationshipType = this.codeValueRepositoryWrapper.findOneByCodeNameAndIdWithNotFoundDetection( GuarantorConstants.GUARANTOR_RELATIONSHIP_CODE_NAME, clientRelationshipId); } - 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); - } + guarantorForUpdate.updateClientRelationshipType(clientRelationshipType); } if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID)