I have written this codelogic for logging, but the codeis getting replicatereplication for all fields ie last(last name, DOB,city, state, zipcodeZIP, etc). Here only validation logic is going to differ, whereas logging functionality will remain the same, and i will be logging both success and error message's.Is there any way to reduce the redundant code ?
public boolean validFirstName(UserInputDetails record) {
if (CommonUtil.isNullOrEmpty(record.getFirstName())) {
record.setErrorCode(ErrorCodes.FIRSTNAME_NULL.getErrorCode());
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(5);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("firstName",record.getFirstName());
dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
dtlLogMap.put("validationStatus","Errored");
dtlLogMap.put("errorCode",record.getErrorCode());
DetailedLogger.log(dtlLogMap);
}
return false;
}
if ( record.getFirstName().length() >=50) {
record.setErrorCode(ErrorCodes.FIRSTNAME_INVALID_LENGTH.getErrorCode());
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(6);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("firstName",record.getFirstName());
dtlLogMap.put("firstNameLength",returnStringifNotNull(record.getFirstName().length()));
dtlLogMap.put("validationName","VALID_FIRSTNAME_LENGTH_CHECK");
dtlLogMap.put("validationStatus","Errored");
dtlLogMap.put("errorCode",record.getErrorCode());
DetailedLogger.log(dtlLogMap);
}
return false;
}
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(4);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("firstName",record.getFirstName());
dtlLogMap.put("validationName","VALID_FIRSTNAME_CHECK");
dtlLogMap.put("validationStatus","passed");
DetailedLogger.log(dtlLogMap);
}
return true;
}
public boolean validZipCode(UserInputDetails record) {
if (CommonUtil.isNullOrEmpty(record.getZipCode())) {
record.setErrorCode(ErrorCodes.ZIPCODE_NULL.getErrorCode());
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(5);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("zipcode",record.getZipCode());
dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
dtlLogMap.put("validationStatus","Errored");
dtlLogMap.put("errorCode",record.getErrorCode());
DetailedLogger.log(dtlLogMap);
}
return false;
}
if ( record.getZipCode().length() >=5) {
record.setErrorCode(ErrorCodes.ZIPCODE_INVALID_LENGTH.getErrorCode());
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(6);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("zipcode",record.getZipCode());
dtlLogMap.put("zipcodeLength",returnStringifNotNull(record.getZipCode().length()));
dtlLogMap.put("validationName","VALID_ZIPCODE_LENGTH_CHECK");
dtlLogMap.put("validationStatus","Errored");
dtlLogMap.put("errorCode",record.getErrorCode());
DetailedLogger.log(dtlLogMap);
}
return false;
}
if(record.isDetailedLoggingEnabled()){
Map<String,String> dtlLogMap = new HashMap<>(4);
dtlLogMap.put("transId",String.valueOf(record.getTransactionId()));
dtlLogMap.put("zipcode",record.getZipCode());
dtlLogMap.put("validationName","VALID_ZIPCODE_CHECK");
dtlLogMap.put("validationStatus","passed");
DetailedLogger.log(dtlLogMap);
}
return true;
}
// some more method , but logic / error message /error code will be change
validLastName(UserInputDetails record)
{
}
validateDOB(UserInputDetails record){
}