From e5e755279b8c861f6fede8e34cf0fc5126707bf8 Mon Sep 17 00:00:00 2001 From: Kamal Mohammed Date: Sat, 7 Sep 2024 05:12:29 +0530 Subject: [PATCH] GRAD2-2822: School Report Regeneration Process - Backend Changes --- .../config/BatchJobConfig.java | 60 +++++++++++++++ .../controller/JobLauncherController.java | 44 ++++++++--- ...ReportsCompletionNotificationListener.java | 55 ++++++++++++++ .../batchgraduation/model/JobProperName.java | 3 +- .../batchgraduation/model/JobSelection.java | 3 +- .../SchoolReportsRegenerationRequest.java | 19 +++++ .../batchgraduation/model/TaskSelection.java | 3 +- .../RegenerateSchoolReportsProcessor.java | 45 ++++++++++++ .../RegenerateSchoolReportsPartitioner.java | 73 +++++++++++++++++++ .../reader/RegenerateSchoolReportsReader.java | 31 ++++++++ .../api/batchgraduation/rest/RestUtils.java | 35 +++++++++ .../writer/RegenerateSchoolReportsWriter.java | 17 +++++ 12 files changed, 374 insertions(+), 14 deletions(-) create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/listener/RegenSchoolReportsCompletionNotificationListener.java create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/SchoolReportsRegenerationRequest.java create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/processor/RegenerateSchoolReportsProcessor.java create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsPartitioner.java create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsReader.java create mode 100644 api/src/main/java/ca/bc/gov/educ/api/batchgraduation/writer/RegenerateSchoolReportsWriter.java diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/config/BatchJobConfig.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/config/BatchJobConfig.java index 181c826d..f9e0da7c 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/config/BatchJobConfig.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/config/BatchJobConfig.java @@ -1045,6 +1045,66 @@ public Job certRegenBatchJob(JobRepository jobRepository, PlatformTransactionMan .build(); } + /** + * Regenerate School Reports + */ + + @Bean + @StepScope + public RegenerateSchoolReportsPartitioner partitionerSchoolReportsRegen() { + return new RegenerateSchoolReportsPartitioner(); + } + + @Bean + @StepScope + public RegenerateSchoolReportsReader itemReaderSchoolReportsRegen() { + return new RegenerateSchoolReportsReader(); + } + + @Bean + @StepScope + public RegenerateSchoolReportsProcessor itemProcessorSchoolReportsRegen() { return new RegenerateSchoolReportsProcessor(); } + + @Bean + @StepScope + public RegenerateSchoolReportsWriter itemWriterSchoolReportsRegen() { + return new RegenerateSchoolReportsWriter(); + } + + @Bean + public Step schoolReportsRegenJobStep(JobRepository jobRepository, PlatformTransactionManager transactionManager, SkipSQLTransactionExceptionsListener skipListener) { + return new StepBuilder("schoolReportsRegenJobStep", jobRepository) + ., List>chunk(1, transactionManager) + .reader(itemReaderSchoolReportsRegen()) + .processor(itemProcessorSchoolReportsRegen()) + .writer(itemWriterSchoolReportsRegen()) + .faultTolerant() + .listener(skipListener) + .skip(Exception.class) + .build(); + } + + @Bean + public Step masterStepSchoolReportsRegen(JobRepository jobRepository, PlatformTransactionManager transactionManager, EducGradBatchGraduationApiConstants constants, SkipSQLTransactionExceptionsListener skipListener) { + return new StepBuilder("masterStepSchoolReportsRegen", jobRepository) + .partitioner(schoolReportsRegenJobStep(jobRepository, transactionManager, skipListener).getName(), partitionerSchoolReportsRegen()) + .step(schoolReportsRegenJobStep(jobRepository, transactionManager, skipListener)) + .gridSize(constants.getNumberOfPartitions()) + .taskExecutor(taskExecutor()) + .build(); + } + + @Bean(name="schoolReportsRegenBatchJob") + public Job schoolReportsRegenBatchJob(JobRepository jobRepository, PlatformTransactionManager transactionManager, RegenSchoolReportsCompletionNotificationListener listener, SkipSQLTransactionExceptionsListener skipListener, EducGradBatchGraduationApiConstants constants) { + return new JobBuilder("schoolReportsRegenBatchJob", jobRepository) + .incrementer(new RunIdIncrementer()) + .listener(listener) + .start(masterStepSchoolReportsRegen(jobRepository, transactionManager,constants, skipListener)) + .on("*") + .end().build() + .build(); + } + /** * Create Graduation Status snapshot for EDW */ diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/controller/JobLauncherController.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/controller/JobLauncherController.java index b71a1289..264e2681 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/controller/JobLauncherController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/controller/JobLauncherController.java @@ -15,7 +15,6 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.security.SecurityRequirement; -import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,14 +57,11 @@ public class JobLauncherController { private static final String TVRRUN = "TVRRUN"; private static final String REGALG = "REGALG"; private static final String CERT_REGEN = "CERT_REGEN"; - + private static final String SCHL_RPT_REGEN = "SCHL_RPT_REGEN"; private static final String EDW_SNAPSHOT = "EDW_SNAPSHOT"; - private static final String ARCHIVE_SCHOOL_REPORTS = "ARC_SCH_REPORTS"; private static final String DELETE_STUDENT_REPORTS = "TVR_DELETE"; - private static final String ARCHIVE_STUDENTS = "ARC_STUDENTS"; - private static final String RERUN_ALL = "RERUN_ALL"; private static final String RERUN_FAILED = "RERUN_FAILED"; private static final String DISTRUN = "DISTRUN"; @@ -83,16 +79,12 @@ public class JobLauncherController { private static final String TVR_BATCH_JOB = "tvrBatchJob"; private static final String SPECIAL_GRADUATION_BATCH_JOB = "SpecialGraduationBatchJob"; private static final String SPECIAL_TVR_RUN_BATCH_JOB = "SpecialTvrRunBatchJob"; - private static final String CERTIFICATE_REGENERATION_BATCH_JOB = "certRegenBatchJob"; - private static final String EDW_SNAPSHOT_BATCH_JOB = "edwSnapshotBatchJob"; - private static final String ARCHIVE_SCHOOL_REPORTS_BATCH_JOB = "archiveSchoolReportsBatchJob"; - private static final String DELETE_STUDENT_REPORTS_BATCH_JOB = "deleteStudentReportsBatchJob"; - private static final String ARCHIVE_STUDENTS_BATCH_JOB = "archiveStudentsBatchJob"; + private static final String REGENERATE_SCHOOL_REPORTS_BATCH_JOB = "schoolReportsRegenBatchJob"; private final JobLauncher jobLauncher; private final JobLauncher asyncJobLauncher; @@ -490,7 +482,7 @@ public ResponseEntity launchRegenerateSchoolReports(@PathVariable Long return ResponseEntity.status(500).body(Boolean.FALSE); } - @PostMapping(EducGradBatchGraduationApiConstants.EXECUTE_REGEN_SCHOOL_REPORTS_BY_REQUEST) + /*@PostMapping(EducGradBatchGraduationApiConstants.EXECUTE_REGEN_SCHOOL_REPORTS_BY_REQUEST) @PreAuthorize(PermissionsConstants.RUN_GRAD_ALGORITHM) @Operation(summary = "Re-Generate School Reports for the given batchJobId", description = "Re-Generate School Reports for the given batchJobId", tags = { "RE-RUN" }) @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),@ApiResponse(responseCode = "500", description = "Internal Server Error")}) @@ -505,6 +497,36 @@ public ResponseEntity launchRegenerateSchoolReports(@RequestBody Student } catch (Exception e) { return ResponseEntity.status(500).body(e.getLocalizedMessage()); } + }*/ + + @PostMapping(EducGradBatchGraduationApiConstants.EXECUTE_REGEN_SCHOOL_REPORTS_BY_REQUEST) + @PreAuthorize(PermissionsConstants.RUN_GRAD_ALGORITHM) + @Operation(summary = "Re-Generate School Reports for the given batchJobId", description = "Re-Generate School Reports for the given batchJobId", tags = { "RE-RUN" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"),@ApiResponse(responseCode = "500", description = "Internal Server Error")}) + public ResponseEntity launchRegenerateSchoolReportsBatch(@RequestBody StudentSearchRequest searchRequest, @RequestParam(required = false) String type) { + logger.debug("launchSchoolReportRegenJob"); + BatchJobResponse response = new BatchJobResponse(); + JobParametersBuilder builder = new JobParametersBuilder(); + builder.addLong(TIME, System.currentTimeMillis()).toJobParameters(); + builder.addString(RUN_BY, ThreadLocalStateUtil.getCurrentUser()); + builder.addString(JOB_TRIGGER, MANUAL); + builder.addString(JOB_TYPE, SCHL_RPT_REGEN); + response.setJobType(SCHL_RPT_REGEN); + response.setTriggerBy(MANUAL); + response.setStartTime(LocalDateTime.now()); + response.setStatus(BatchStatusEnum.STARTED.name()); + + try { + String studentSearchData = jsonTransformer.marshall(searchRequest); + builder.addString(SEARCH_REQUEST, studentSearchData); + response.setJobParameters(studentSearchData); + JobExecution jobExecution = asyncJobLauncher.run(jobRegistry.getJob(REGENERATE_SCHOOL_REPORTS_BATCH_JOB), builder.toJobParameters()); + response.setBatchId(jobExecution.getId()); + return ResponseEntity.ok(response); + } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException | NoSuchJobException e) { + response.setException(e.getLocalizedMessage()); + return ResponseEntity.status(500).body(response); + } } @PostMapping(EducGradBatchGraduationApiConstants.EXECUTE_REGEN_STUDENT_REPORTS_BY_REQUEST) diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/listener/RegenSchoolReportsCompletionNotificationListener.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/listener/RegenSchoolReportsCompletionNotificationListener.java new file mode 100644 index 00000000..d748f962 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/listener/RegenSchoolReportsCompletionNotificationListener.java @@ -0,0 +1,55 @@ +package ca.bc.gov.educ.api.batchgraduation.listener; + +import ca.bc.gov.educ.api.batchgraduation.model.DistributionSummaryDTO; +import ca.bc.gov.educ.api.batchgraduation.util.DateUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.stereotype.Component; + +import java.util.Date; + +import static ca.bc.gov.educ.api.batchgraduation.util.EducGradBatchGraduationApiConstants.SEARCH_REQUEST; + +@Slf4j +@Component +public class RegenSchoolReportsCompletionNotificationListener extends BaseDistributionRunCompletionNotificationListener { + + @Override + public void afterJob(JobExecution jobExecution) { + if (jobExecution.getStatus() == BatchStatus.COMPLETED) { + long elapsedTimeMillis = getElapsedTimeMillis(jobExecution); + log.info("======================================================================================="); + JobParameters jobParameters = jobExecution.getJobParameters(); + ExecutionContext jobContext = jobExecution.getExecutionContext(); + Long jobExecutionId = jobExecution.getId(); + String jobType = jobParameters.getString("jobType"); + log.info("{} Regen School Reports Job {} completed in {} s with jobExecution status {}", jobType, jobExecutionId, elapsedTimeMillis / 1000, jobExecution.getStatus()); + + String status = jobExecution.getStatus().toString(); + Date startTime = DateUtils.toDate(jobExecution.getStartTime()); + Date endTime = DateUtils.toDate(jobExecution.getEndTime()); + String jobTrigger = jobParameters.getString("jobTrigger"); + + DistributionSummaryDTO summaryDTO = (DistributionSummaryDTO)jobContext.get("distributionSummaryDTO"); + + String studentSearchRequest = jobParameters.getString(SEARCH_REQUEST, "{}"); + // display Summary Details + log.info("Records read : {}", summaryDTO.getReadCount()); + log.info("Processed count: {}", summaryDTO.getProcessedCount()); + log.info(" --------------------------------------------------------------------------------------"); + log.info("Errors:{}", summaryDTO.getErrors().size()); + + updateUserSchedulingJobs(jobParameters); + + String jobParametersDTO = buildJobParametersDTO(jobType, studentSearchRequest, null, null); + // save batch job & error history + processBatchJobHistory(summaryDTO, jobExecutionId, status, jobTrigger, jobType, startTime, endTime, jobParametersDTO); + log.info(" --------------------------------------------------------------------------------------"); + summaryDTO.getSchools().forEach((value) -> log.info("School {} number of Regen School Reports : {}", value.getMincode(), value.getNumberOfSchoolReports())); + + } + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobProperName.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobProperName.java index eb473e49..20e0f8c6 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobProperName.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobProperName.java @@ -13,7 +13,8 @@ public enum JobProperName { RCBJ ("Regenerate Certificates Batch Job"), DSRBJ ("Delete Student Report Batch Job"), ASBJ("Archive Students Batch Job"), - ASRBJ("Archive School Reports Batch Job"); + ASRBJ("Archive School Reports Batch Job"), + SRRBJ("User Request School Report Regeneration") ; private final String value; diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobSelection.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobSelection.java index 3147cf8a..55005249 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobSelection.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/JobSelection.java @@ -13,7 +13,8 @@ public enum JobSelection { RCBJ ("CERT_REGEN"), DSRBJ ("TVR_DELETE"), ASBJ("ARC_STUDENTS"), - ASRBJ("ARC_SCH_REPORTS"); + ASRBJ("ARC_SCH_REPORTS"), + SRRBJ("SCHL_RPT_REGEN"); private final String value; diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/SchoolReportsRegenerationRequest.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/SchoolReportsRegenerationRequest.java new file mode 100644 index 00000000..8f1b754f --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/SchoolReportsRegenerationRequest.java @@ -0,0 +1,19 @@ +package ca.bc.gov.educ.api.batchgraduation.model; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.stereotype.Component; + +@Data +@Component +@NoArgsConstructor +@AllArgsConstructor +public class SchoolReportsRegenerationRequest extends StudentSearchRequest { + private String runMode; // "Y" or "N" + + public boolean runForAll () { + return (getSchoolOfRecords() == null || getSchoolOfRecords().isEmpty()) && + (getSchoolCategoryCodes() == null || getSchoolCategoryCodes().isEmpty()); + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/TaskSelection.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/TaskSelection.java index 48123d91..cd59d381 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/TaskSelection.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/model/TaskSelection.java @@ -13,7 +13,8 @@ public enum TaskSelection { RCBJ ("certRegenBatchJob"), DSRBJ ("deleteStudentReportsBatchJob"), ASBJ("archiveStudentsBatchJob"), - ASRBJ("archiveSchoolReportsBatchJob"); + ASRBJ("archiveSchoolReportsBatchJob"), + SRRBJ("schoolReportsRegenBatchJob"); private final String value; diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/processor/RegenerateSchoolReportsProcessor.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/processor/RegenerateSchoolReportsProcessor.java new file mode 100644 index 00000000..350282f4 --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/processor/RegenerateSchoolReportsProcessor.java @@ -0,0 +1,45 @@ +package ca.bc.gov.educ.api.batchgraduation.processor; + +import ca.bc.gov.educ.api.batchgraduation.model.*; +import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; + +import java.util.List; +import java.util.stream.Collectors; + +@Slf4j +public class RegenerateSchoolReportsProcessor implements ItemProcessor, List> { + + @Autowired + RestUtils restUtils; + + @Value("#{stepExecutionContext['summary']}") + DistributionSummaryDTO summaryDTO; + + @Override + public List process(List minCodes) throws Exception { + Long batchId = summaryDTO.getBatchId(); + StudentSearchRequest searchRequest = summaryDTO.getStudentSearchRequest(); + long countRegeneratedSchoolReports = 0l; + List reportTypes = searchRequest.getReportTypes(); + if(log.isDebugEnabled()) { + log.debug("Process Schools: {}", !minCodes.isEmpty() ? String.join(",", minCodes) : summaryDTO.getSchools().stream().map(School::getMincode).collect(Collectors.joining(","))); + } + + String reportType; + if(reportTypes != null && !reportTypes.isEmpty() && "NONGRADPRJ".compareToIgnoreCase(reportTypes.get(0)) == 0) + reportType = "TVRRUN"; + else + reportType = "GRADREG and NONGRADREG"; + + for (String mincode : minCodes) { + countRegeneratedSchoolReports += restUtils.createAndStoreSchoolReports(batchId, minCodes, reportType, summaryDTO); + } + + summaryDTO.setProcessedCount(countRegeneratedSchoolReports); + return minCodes; + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsPartitioner.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsPartitioner.java new file mode 100644 index 00000000..7dfab5ff --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsPartitioner.java @@ -0,0 +1,73 @@ +package ca.bc.gov.educ.api.batchgraduation.reader; + +import ca.bc.gov.educ.api.batchgraduation.model.*; +import ca.bc.gov.educ.api.batchgraduation.util.GradSchoolOfRecordFilter; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; + +import java.util.*; + +import static ca.bc.gov.educ.api.batchgraduation.util.EducGradBatchGraduationApiConstants.SEARCH_REQUEST; + +@Slf4j +public class RegenerateSchoolReportsPartitioner extends BasePartitioner { + + @Value("#{stepExecution.jobExecution}") + JobExecution jobExecution; + + @Autowired + GradSchoolOfRecordFilter gradSchoolOfRecordFilter; + + public RegenerateSchoolReportsPartitioner() { + super(); + } + + @Override + public JobExecution getJobExecution() { + return jobExecution; + } + + @Override + public Map partition(int gridSize) { + DistributionSummaryDTO summaryDTO = (DistributionSummaryDTO)jobExecution.getExecutionContext().get("distributionSummaryDTO"); + if(summaryDTO == null) { + summaryDTO = new DistributionSummaryDTO(); + jobExecution.getExecutionContext().put("distributionSummaryDTO", summaryDTO); + } + + StudentSearchRequest searchRequest = getStudentSearchRequest(); + long startTime = System.currentTimeMillis(); + log.debug("Filter Schools for school reports regeneration"); + boolean processAllStudents = "ALL".equalsIgnoreCase(searchRequest.getActivityCode()); + List eligibleStudentSchoolDistricts = gradSchoolOfRecordFilter.filterSchoolOfRecords(searchRequest); + List finalSchoolDistricts = eligibleStudentSchoolDistricts.stream().sorted().toList(); + if(log.isDebugEnabled()) { + log.debug("Final list of eligible District / School codes {}", String.join(", ", finalSchoolDistricts)); + } + + summaryDTO.setBatchId(jobExecution.getId()); + summaryDTO.setStudentSearchRequest(searchRequest); + + long endTime = System.currentTimeMillis(); + long diff = (endTime - startTime)/1000; + log.debug("Total {} schools after filters in {} sec", finalSchoolDistricts.size(), diff); + + updateBatchJobHistory(createBatchJobHistory(), (long)finalSchoolDistricts.size()); + summaryDTO.setReadCount((long)finalSchoolDistricts.size()); + summaryDTO.setProcessedCount(0); + + Map map = new HashMap<>(); + ExecutionContext executionContext = new ExecutionContext(); + executionContext.put(SEARCH_REQUEST, searchRequest); + executionContext.put("data", finalSchoolDistricts); + executionContext.put("summary", summaryDTO); + executionContext.put("readCount", 0); + map.put("partition0", executionContext); + + log.info("Found {} in total running on 1 partitions", finalSchoolDistricts); + return map; + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsReader.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsReader.java new file mode 100644 index 00000000..794aa43a --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/reader/RegenerateSchoolReportsReader.java @@ -0,0 +1,31 @@ +package ca.bc.gov.educ.api.batchgraduation.reader; + +import ca.bc.gov.educ.api.batchgraduation.model.DistributionSummaryDTO; +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.item.ItemReader; +import org.springframework.beans.factory.annotation.Value; + +import java.util.List; + +@Slf4j +public class RegenerateSchoolReportsReader implements ItemReader> { + + @Value("#{stepExecutionContext['data']}") + List schools; + + @Value("#{stepExecutionContext['summary']}") + DistributionSummaryDTO summaryDTO; + + @Value("#{stepExecutionContext['readCount']}") + Long readCount; + + @Override + public List read() throws Exception { + if(readCount > 0) return null; + readCount++; + if(log.isDebugEnabled()) { + log.info("Read schools Codes -> {} of {} schools", schools.size(), String.join(",", schools)); + } + return schools; + } +} diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java index 7500563d..78ac6fef 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/rest/RestUtils.java @@ -556,6 +556,41 @@ public Integer createAndStoreSchoolReports(List uniqueSchools, String ty return result; } + public Integer createAndStoreSchoolReports(Long batchId, List uniqueSchools, String reportType, DistributionSummaryDTO summaryDTO) { + UUID correlationID = UUID.randomUUID(); + Integer result = 0; + try { + if (uniqueSchools == null || uniqueSchools.isEmpty()) { + LOGGER.info("{} Schools selected for School Reports Regeneration", result); + return result; + } + + for (String minCode : uniqueSchools) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Creating School Reports for school {}", minCode); + } + result += webClient.post() + .uri(String.format(constants.getCreateAndStoreSchoolReports(), reportType)) + .headers(h -> { + h.setBearerAuth(getAccessToken()); + h.set(EducGradBatchGraduationApiConstants.CORRELATION_ID, correlationID.toString()); + }) + .body(BodyInserters.fromValue(List.of(minCode))) + .retrieve() + .bodyToMono(Integer.class) + .block(); + } + LOGGER.info("Created and Stored {} School Reports", result); + } catch(Exception e) { + LOGGER.error("Unable to Regenerate School Reports", e); + summaryDTO.setErroredCount(summaryDTO.getErroredCount() + 1); + summaryDTO.getErrors().add(new ProcessError(null,"Unable to Regenerate School Reports", e.getLocalizedMessage())); + summaryDTO.setException(e.getLocalizedMessage()); + return 0; + } + return result; + } + public Integer processStudentReports(List uuidList, String studentReportType) { UUID correlationID = UUID.randomUUID(); Integer result = webClient.post() diff --git a/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/writer/RegenerateSchoolReportsWriter.java b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/writer/RegenerateSchoolReportsWriter.java new file mode 100644 index 00000000..9e4d076f --- /dev/null +++ b/api/src/main/java/ca/bc/gov/educ/api/batchgraduation/writer/RegenerateSchoolReportsWriter.java @@ -0,0 +1,17 @@ +package ca.bc.gov.educ.api.batchgraduation.writer; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.batch.item.Chunk; +import org.springframework.batch.item.ItemWriter; + +import java.util.List; + +@Slf4j +public class RegenerateSchoolReportsWriter implements ItemWriter> { + + @Override + public void write(Chunk> chunk) throws Exception { + log.info("Regenerate School Reports Writer"); + } + +}