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()))thrownewCustomException("EG_BT_APP_ERR","tenantId is mandatory for creating birth registration applications"); }); }publicBirthRegistrationApplicationvalidateApplicationExistence(BirthRegistrationApplication birthRegistrationApplication) {returnrepository.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.utils.IdgenUtil;importdigit.utils.UserUtil;importdigit.web.models.*;importlombok.extern.slf4j.Slf4j;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 detailsAuditDetails 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());// application.getFather().setId(application.getId());// application.getMother().setId(application.getId());// Enrich registration Idapplication.getAddress().setRegistrationId(application.getId());// Enrich address UUIDapplication.getAddress().setId(UUID.randomUUID().toString());//Enrich application number from IDgenapplication.setApplicationNumber(birthRegistrationIdList.get(index++)); } }publicvoidenrichBirthApplicationUponUpdate(BirthRegistrationRequest birthRegistrationRequest) {// Enrich lastModifiedTime and lastModifiedBy in case of updatebirthRegistrationRequest.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().getId(),null);User fatherUser =fatherUserResponse.getUser().get(0);log.info(fatherUser.toString());FatherApplicant fatherApplicant =FatherApplicant.builder().aadhaarNumber(fatherUser.getAadhaarNumber()).accountLocked(fatherUser.getAccountLocked()).active(fatherUser.getActive()).altContactNumber(fatherUser.getAltContactNumber()).bloodGroup(fatherUser.getBloodGroup()).correspondenceAddress(fatherUser.getCorrespondenceAddress()).correspondenceCity(fatherUser.getCorrespondenceCity()).correspondencePincode(fatherUser.getCorrespondencePincode()).gender(fatherUser.getGender()).id(fatherUser.getUuid()).name(fatherUser.getName()).type(fatherUser.getType()).roles(fatherUser.getRoles()).build();application.setFather(fatherApplicant); }publicvoidenrichMotherApplicantOnSearch(BirthRegistrationApplication application) {UserDetailResponse motherUserResponse =userService.searchUser(userUtils.getStateLevelTenant(application.getTenantId()),application.getFather().getId(),null);User motherUser =motherUserResponse.getUser().get(0);log.info(motherUser.toString());MotherApplicant motherApplicant =MotherApplicant.builder().aadhaarNumber(motherUser.getAadhaarNumber()).accountLocked(motherUser.getAccountLocked()).active(motherUser.getActive()).altContactNumber(motherUser.getAltContactNumber()).bloodGroup(motherUser.getBloodGroup()).correspondenceAddress(motherUser.getCorrespondenceAddress()).correspondenceCity(motherUser.getCorrespondenceCity()).correspondencePincode(motherUser.getCorrespondencePincode()).gender(motherUser.getGender()).id(motherUser.getUuid()).name(motherUser.getName()).type(motherUser.getType()).roles(motherUser.getRoles()).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.