diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java index 4c3ef8003..c45b823a6 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/reports/CSVReportService.java @@ -622,10 +622,10 @@ public DownloadableReportResponse generateOffshoreSchoolsHeadcounts(UUID collect } } - private Map getLastSeptCollectionSchoolMap(UUID collectionID){ - var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID); + private Map getLastSeptCollectionSchoolMap(CollectionEntity collection){ + var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collection.getCollectionID(), collection.getSnapshotDate()); if(lastSeptCollectionOpt.isEmpty()) { - throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collectionID.toString()); + throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collection.getCollectionID().toString()); } List lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsByCollectionId(lastSeptCollectionOpt.get().getCollectionID()); return lastSeptCollectionRawData.stream().collect(Collectors.toMap(SpecialEdHeadcountResult::getSchoolID, item -> item)); @@ -1040,55 +1040,6 @@ private boolean shouldIncludeSchoolForEnrolledHeadcountsAndFteReport(SchoolTombs return Arrays.stream(invalidSchoolCategories).noneMatch(categoryCode::equals) && Arrays.stream(invalidFacilityTypes).noneMatch(facilityType::equals); } - // Enroled Headcounts and FTEs For CE and OL Schools report - public DownloadableReportResponse generateEnrolmentHeadcountsAndFteReportForCEAndOLSchools(UUID collectionID) { - var collectionOpt = collectionRepository.findById(collectionID); - - if(collectionOpt.isEmpty()){ - throw new EntityNotFoundException(Collection.class, COLLECTION_ID, collectionID.toString()); - } - - CollectionEntity collection = collectionOpt.get(); - if(!collection.getCollectionTypeCode().equalsIgnoreCase(CollectionTypeCodes.MAY.getTypeCode()) && !collection.getCollectionTypeCode().equalsIgnoreCase(CollectionTypeCodes.FEBRUARY.getTypeCode())) { - throw new InvalidPayloadException(createError(HEADCOUNTS_INVALID_COLLECTION_TYPE)); - } - - List results = sdcSchoolCollectionStudentRepository.getEnrolmentHeadcountsAndFteWithRefugeeByCollectionId(collectionID); - var mappedSeptData = getEnrolmentHeadcountFteResultForLastSeptCollection(collectionID); - - List headers = Arrays.stream(CEAndOLEnrolmentAndFteHeader.values()).map(CEAndOLEnrolmentAndFteHeader::getCode).toList(); - CSVFormat csvFormat = CSVFormat.DEFAULT.builder() - .setHeader(headers.toArray(String[]::new)) - .build(); - try { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(byteArrayOutputStream)); - CSVPrinter csvPrinter = new CSVPrinter(writer, csvFormat); - - for (EnrolmentHeadcountFteResult result : results) { - var septCollectionRecord = mappedSeptData.get(result.getSchoolID()); - - var schoolOpt = restUtils.getSchoolBySchoolID(result.getSchoolID()); - if(schoolOpt.isPresent() && - (schoolOpt.get().getFacilityTypeCode().equalsIgnoreCase(FacilityTypeCodes.CONT_ED.getCode()) || - schoolOpt.get().getFacilityTypeCode().equalsIgnoreCase(FacilityTypeCodes.DISTONLINE.getCode()) || - schoolOpt.get().getFacilityTypeCode().equalsIgnoreCase(FacilityTypeCodes.DIST_LEARN.getCode()))) { - List csvRowData = prepareEnrolmentFteDataForCEAndOLSchools(result, septCollectionRecord, schoolOpt.get()); - csvPrinter.printRecord(csvRowData); - } - } - csvPrinter.flush(); - - var downloadableReport = new DownloadableReportResponse(); - downloadableReport.setReportType(ENROLMENT_HEADCOUNTS_AND_FTE_REPORT_FOR_OL_AND_CE_SCHOOLS.getCode()); - downloadableReport.setDocumentData(Base64.getEncoder().encodeToString(byteArrayOutputStream.toByteArray())); - - return downloadableReport; - } catch (IOException e) { - throw new StudentDataCollectionAPIRuntimeException(e); - } - } - // Refugee Enroled Headcounts and FTEs report public DownloadableReportResponse generateRefugeeEnrolmentHeadcountsAndFteReport(UUID collectionID) { var collectionOpt = collectionRepository.findById(collectionID); @@ -1136,15 +1087,6 @@ public DownloadableReportResponse generateRefugeeEnrolmentHeadcountsAndFteReport } } - private Map getEnrolmentHeadcountFteResultForLastSeptCollection(UUID collectionID){ - var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID); - if(lastSeptCollectionOpt.isEmpty()) { - throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collectionID.toString()); - } - List lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getEnrolmentHeadcountsAndFteWithRefugeeByCollectionId(lastSeptCollectionOpt.get().getCollectionID()); - return lastSeptCollectionRawData.stream().collect(Collectors.toMap(EnrolmentHeadcountFteResult::getSchoolID, item -> item)); - } - private ApiError createError(String message) { return ApiError.builder().timestamp(LocalDateTime.now()).message(message).status(BAD_REQUEST).build(); } diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java index 00f618d6f..543533115 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/controller/v1/MinistryReportsControllerTest.java @@ -737,10 +737,17 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR final GrantedAuthority grantedAuthority = () -> "SCOPE_READ_SDC_MINISTRY_REPORTS"; final OidcLoginRequestPostProcessor mockAuthority = oidcLogin().authorities(grantedAuthority); - CollectionEntity collectionSept = createMockCollectionEntity(); - collectionSept.setCloseDate(LocalDateTime.now().minusDays(20)); - collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); - collectionRepository.save(collectionSept); + CollectionEntity collection1 = createMockCollectionEntity(); + collection1.setCloseDate(LocalDateTime.now().minusDays(2)); + collection1.setSnapshotDate(LocalDate.now().minusMonths(3)); + collection1.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode()); + collection1 = collectionRepository.save(collection1); + + CollectionEntity collection2 = createMockCollectionEntity(); + collection2.setCloseDate(LocalDateTime.now().plusDays(2)); + collection2.setCollectionTypeCode(CollectionTypeCodes.FEBRUARY.getTypeCode()); + collection2.setSnapshotDate(LocalDate.now().plusDays(2)); + collection2 = collectionRepository.save(collection2); var district = this.createMockDistrict(); var authority = this.createMockAuthority(); @@ -752,23 +759,19 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR when(this.restUtils.getDistrictByDistrictID(any())).thenReturn(Optional.of(district)); when(this.restUtils.getAuthorityByAuthorityID(any())).thenReturn(Optional.of(authority)); - CollectionEntity collection = createMockCollectionEntity(); - collection.setCloseDate(LocalDateTime.now().plusDays(2)); - collection = collectionRepository.save(collection); - - SdcDistrictCollectionEntity sdcMockDistrict = createMockSdcDistrictCollectionEntity(collection, null); + SdcDistrictCollectionEntity sdcMockDistrict = createMockSdcDistrictCollectionEntity(collection1, null); sdcDistrictCollectionRepository.save(sdcMockDistrict).getSdcDistrictCollectionID(); - SdcDistrictCollectionEntity sdcMockDistrict2 = createMockSdcDistrictCollectionEntity(collection, null); + SdcDistrictCollectionEntity sdcMockDistrict2 = createMockSdcDistrictCollectionEntity(collection2, null); sdcDistrictCollectionRepository.save(sdcMockDistrict2).getSdcDistrictCollectionID(); SchoolTombstone school1 = createMockSchool(); school1.setDistrictId(sdcMockDistrict.getDistrictID().toString()); - SdcSchoolCollectionEntity sdcSchoolCollectionEntity1 = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school1.getSchoolId())); + SdcSchoolCollectionEntity sdcSchoolCollectionEntity1 = createMockSdcSchoolCollectionEntity(collection1, UUID.fromString(school1.getSchoolId())); SchoolTombstone school2 = createMockSchool(); school2.setDistrictId(sdcMockDistrict2.getDistrictID().toString()); - SdcSchoolCollectionEntity sdcSchoolCollectionEntity2 = createMockSdcSchoolCollectionEntity(collection, UUID.fromString(school2.getSchoolId())); + SdcSchoolCollectionEntity sdcSchoolCollectionEntity2 = createMockSdcSchoolCollectionEntity(collection2, UUID.fromString(school2.getSchoolId())); sdcSchoolCollectionRepository.saveAll(List.of(sdcSchoolCollectionEntity1, sdcSchoolCollectionEntity2)); @@ -777,7 +780,7 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR sdcSchoolCollectionStudentRepository.saveAll(List.of(sdcSchoolCollectionStudent1, sdcSchoolCollectionStudent2)); var resultActions1 = this.mockMvc.perform( - get(URL.BASE_MINISTRY_HEADCOUNTS + "/" + collection.getCollectionID() + "/indy-inclusive-ed-funding-headcounts/download").with(mockAuthority)) + get(URL.BASE_MINISTRY_HEADCOUNTS + "/" + collection2.getCollectionID() + "/indy-inclusive-ed-funding-headcounts/download").with(mockAuthority)) .andDo(print()).andExpect(status().isOk()); val summary1 = objectMapper.readValue(resultActions1.andReturn().getResponse().getContentAsByteArray(), new TypeReference() {