Merge pull request #438 from mifoscontributer/MIFOSX-526

Edit or Update loan guarantor
This commit is contained in:
Vishwas Babu A J
2013-07-11 05:39:18 -07:00
4 changed files with 55 additions and 18 deletions
@@ -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;
}
}
@@ -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);
}
@@ -144,7 +144,6 @@ public class Guarantor extends AbstractPersistable<Long> {
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<Long> {
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<Long> {
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;
}
}
@@ -116,26 +116,14 @@ public class GuarantorWritePlatformServiceJpaRepositoryIImpl implements Guaranto
final Map<String, Object> 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<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);
}
guarantorForUpdate.updateClientRelationshipType(clientRelationshipType);
}
if (changesOnly.containsKey(GUARANTOR_JSON_INPUT_PARAMS.ENTITY_ID)