All business validation logic should be added to this class. For example, verifying the values against the master data, ensuring non-duplication of data etc.
Steps
Follow the steps below to create the validation layer.
Create a package called validators. This ensures the validation logic is separate so that the code is easy to navigate through and readable.
Create a class by the name of BirthApplicationValidator
Annotate the class with @Component annotation and insert the following content in the class -
packagedigit.validators;importdigit.repository.BirthRegistrationRepository;importdigit.web.models.BirthApplicationSearchCriteria;importdigit.web.models.BirthRegistrationApplication;importdigit.web.models.BirthRegistrationRequest;importorg.egov.tracer.model.CustomException;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importorg.springframework.util.ObjectUtils;@ComponentpublicclassBirthApplicationValidator { @AutowiredprivateBirthRegistrationRepository repository;publicvoidvalidateBirthApplication(BirthRegistrationRequest birthRegistrationRequest) {birthRegistrationRequest.getBirthRegistrationApplications().forEach(application -> {if(ObjectUtils.isEmpty(application.getTenantId())) throw new CustomException("EG_BT_APP_ERR", "tenantId is mandatory for creating birth registration applications");
}); } public BirthRegistrationApplication validateApplicationExistence(BirthRegistrationApplication birthRegistrationApplication) {
return repository.getApplications(BirthApplicationSearchCriteria.builder().applicationNumber(birthRegistrationApplication.getApplicationNumber()).build()).get(0);
}}
NOTE: For the sake of simplicity the above-mentioned validations are implemented. Required validations will vary on a case-to-case basis.
Enrichment Layer
This layer enriches the request. System-generated values like id, auditDetails etc. are generated and added to the request. In the case of this module, since the applicant is the parent of a baby and a child cannot be a user of the system directly, both the parents' details are captured in the User table. The user ids of the parents are then enriched in the application.
Steps
Follow the steps below to create the enrichment layer.
Create a package under DIGIT by the name of enrichment. This ensures the enrichment code is separate from business logic so that the codebase is easy to navigate through and readable.
Create a class by the name of BirthApplicationEnrichment
Annotate the class with @Component and add the following methods to the class -
packagedigit.enrichment;importdigit.service.UserService;importdigit.util.IdgenUtil;importdigit.util.UserUtil;importdigit.web.models.*;importlombok.extern.slf4j.Slf4j;importorg.egov.common.contract.models.AuditDetails;importorg.egov.common.contract.request.User;importorg.egov.common.contract.user.UserDetailResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Component;importjava.util.List;importjava.util.UUID;@Component@Slf4jpublicclassBirthApplicationEnrichment { @AutowiredprivateIdgenUtil idgenUtil; @AutowiredprivateUserService userService; @AutowiredprivateUserUtil userUtils;publicvoidenrichBirthApplication(BirthRegistrationRequest birthRegistrationRequest) { List<String> birthRegistrationIdList = idgenUtil.getIdList(birthRegistrationRequest.getRequestInfo(), birthRegistrationRequest.getBirthRegistrationApplications().get(0).getTenantId(), "btr.registrationid", "", birthRegistrationRequest.getBirthRegistrationApplications().size());
Integer index =0;for(BirthRegistrationApplication application :birthRegistrationRequest.getBirthRegistrationApplications()){// Enrich audit details AuditDetails auditDetails = AuditDetails.builder().createdBy(birthRegistrationRequest.getRequestInfo().getUserInfo().getUuid()).createdTime(System.currentTimeMillis()).lastModifiedBy(birthRegistrationRequest.getRequestInfo().getUserInfo().getUuid()).lastModifiedTime(System.currentTimeMillis()).build();
application.setAuditDetails(auditDetails);// Enrich UUIDapplication.setId(UUID.randomUUID().toString());// Enrich application number from IDgenapplication.setApplicationNumber(birthRegistrationIdList.get(index++));// Enrich registration Idapplication.getAddress().setApplicationNumber(application.getId());// Enrich address UUIDapplication.getAddress().setId(UUID.randomUUID().toString()); } }publicvoidenrichBirthApplicationUponUpdate(BirthRegistrationRequest birthRegistrationRequest) {// Enrich lastModifiedTime and lastModifiedBy in case of update birthRegistrationRequest.getBirthRegistrationApplications().get(0).getAuditDetails().setLastModifiedTime(System.currentTimeMillis());
birthRegistrationRequest.getBirthRegistrationApplications().get(0).getAuditDetails().setLastModifiedBy(birthRegistrationRequest.getRequestInfo().getUserInfo().getUuid());
}publicvoidenrichFatherApplicantOnSearch(BirthRegistrationApplication application) { UserDetailResponse fatherUserResponse = userService.searchUser(userUtils.getStateLevelTenant(application.getTenantId()),application.getFather().getUuid(),null);
User fatherUser =fatherUserResponse.getUser().get(0);log.info(fatherUser.toString());User fatherApplicant =User.builder().mobileNumber(fatherUser.getMobileNumber()).id(fatherUser.getId()).name(fatherUser.getName()).userName((fatherUser.getUserName())).type(fatherUser.getType()).roles(fatherUser.getRoles()).uuid(fatherUser.getUuid()).build();application.setFather(fatherApplicant); }publicvoidenrichMotherApplicantOnSearch(BirthRegistrationApplication application) { UserDetailResponse motherUserResponse = userService.searchUser(userUtils.getStateLevelTenant(application.getTenantId()),application.getMother().getUuid(),null);
User motherUser =motherUserResponse.getUser().get(0);log.info(motherUser.toString());User motherApplicant =User.builder().mobileNumber(motherUser.getMobileNumber()).id(motherUser.getId()).name(motherUser.getName()).userName((motherUser.getUserName())).type(motherUser.getType()).roles(motherUser.getRoles()).uuid(motherUser.getUuid()).build();application.setMother(motherApplicant); }}
NOTE: For the sake of simplicity the above-mentioned enrichment methods are implemented. Required enrichment will vary on a case-to-case basis.