I have written this logic for logging, but is getting replication for all fields (last name, DOB,city, state, ZIP, etc). Here only validation logic is going to differ, whereas logging functionality will remain the same.
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;
}
if (record.getFirstName().length() <=50) { return false; }
Are you sure you want to fail when a first name is shorter than 50 characters? \$\endgroup\$