Validation and Enrichment layers
Validation Layer - All business validation logic should be added in this class. For example, verifying the values against the master data, ensuring non duplication of data etc.
In this guide, for creating the validation layer, the following steps should be followed -
a. Create a folder under digit by the name of validators. This is being done so that we keep the validation logic separate so that the code is easy to navigate through and readable.
b. Create a class by the name of VoterApplicationValidator
c. Annotate the class with @Component
annotation and put the following content in the class -
@Component
public class VoterApplicationValidator {
@Autowired
private VoterRegistrationRepository repository;
public void validateVoterApplication(VoterRegistrationRequest voterRegistrationRequest) {
voterRegistrationRequest.getVoterRegistrationApplications().forEach(application -> {
if(ObjectUtils.isEmpty(application.getTenantId()))
throw new CustomException("EG_VT_APP_ERR", "tenantId is mandatory for creating voter registration applications");
});
}
public VoterRegistrationApplication validateApplicationExistence(VoterRegistrationApplication voterRegistrationApplication) {
return repository.getApplications(VoterApplicationSearchCriteria.builder().applicationNumber(voterRegistrationApplication.getApplicationNumber()).build()).get(0);
}
}
*** NOTE: For the sake of simplicity the above mentioned validations have been implemented. Required validations will vary on case by case basis.
2. Enrichment Layer - This layer will enrich the request. System generated values like id, auditDetails etc. will be generated and added to the request.
In this guide, for creating the enrichment layer, the following steps should be followed -
a. Create a folder under digit by the name of enrichment. Again, this is being done so that enrichment code is separate from business logic so that the codebase is easy to navigate through and readable.
b. Create a class by the name of VoterApplicationEnrichment
c. Annotate the class with @Component
and add the following methods to the class -
@Component
public class VoterApplicationEnrichment {
@Autowired
private IdgenUtil idgenUtil;
public void enrichVoterApplication(VoterRegistrationRequest voterRegistrationRequest) {
List<String> voterRegistrationIdList = idgenUtil.getIdList(voterRegistrationRequest.getRequestInfo(), voterRegistrationRequest.getVoterRegistrationApplications().get(0).getTenantId(), "vtr.registrationid", "", voterRegistrationRequest.getVoterRegistrationApplications().size());
Integer index = 0;
for(VoterRegistrationApplication application : voterRegistrationRequest.getVoterRegistrationApplications()){
// Enrich audit details
AuditDetails auditDetails = AuditDetails.builder().createdBy(voterRegistrationRequest.getRequestInfo().getUserInfo().getUuid()).createdTime(System.currentTimeMillis()).lastModifiedBy(voterRegistrationRequest.getRequestInfo().getUserInfo().getUuid()).lastModifiedTime(System.currentTimeMillis()).build();
application.setAuditDetails(auditDetails);
// Enrich UUID
application.setId(UUID.randomUUID().toString());
// Enrich registration Id
application.getAddress().setRegistrationId(application.getId());
// Enrich address UUID
application.getAddress().setId(UUID.randomUUID().toString());
//Enrich application number from IDgen
application.setApplicationNumber(voterRegistrationIdList.get(index++));
}
}
public void enrichVoterApplicationUponUpdate(VoterRegistrationRequest voterRegistrationRequest) {
// Enrich lastModifiedTime and lastModifiedBy in case of update
voterRegistrationRequest.getVoterRegistrationApplications().get(0).getAuditDetails().setLastModifiedTime(System.currentTimeMillis());
voterRegistrationRequest.getVoterRegistrationApplications().get(0).getAuditDetails().setLastModifiedBy(voterRegistrationRequest.getRequestInfo().getUserInfo().getUuid());
}
}
*** NOTE: For the sake of simplicity the above mentioned enrichment methods have been implemented. Required enrichment will vary on case by case basis.