Skip to content

Commit

Permalink
Fixes for report counts
Browse files Browse the repository at this point in the history
  • Loading branch information
arcshiftsolutions committed Jan 14, 2025
1 parent 21c0d7c commit b9cdf25
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ public DownloadableReportResponse generateOffshoreSchoolsHeadcounts(UUID collect
}
}

private Map<String, SpecialEdHeadcountResult> getLastSeptCollectionSchoolMap(UUID collectionID){
var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID);
private Map<String, SpecialEdHeadcountResult> 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<SpecialEdHeadcountResult> lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsByCollectionId(lastSeptCollectionOpt.get().getCollectionID());
return lastSeptCollectionRawData.stream().collect(Collectors.toMap(SpecialEdHeadcountResult::getSchoolID, item -> item));
Expand Down Expand Up @@ -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<EnrolmentHeadcountFteResult> results = sdcSchoolCollectionStudentRepository.getEnrolmentHeadcountsAndFteWithRefugeeByCollectionId(collectionID);
var mappedSeptData = getEnrolmentHeadcountFteResultForLastSeptCollection(collectionID);

List<String> 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<String> 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);
Expand Down Expand Up @@ -1136,15 +1087,6 @@ public DownloadableReportResponse generateRefugeeEnrolmentHeadcountsAndFteReport
}
}

private Map<String, EnrolmentHeadcountFteResult> getEnrolmentHeadcountFteResultForLastSeptCollection(UUID collectionID){
var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID);
if(lastSeptCollectionOpt.isEmpty()) {
throw new EntityNotFoundException(CollectionEntity.class, COLLECTION_ID, collectionID.toString());
}
List<EnrolmentHeadcountFteResult> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));

Expand All @@ -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<DownloadableReportResponse>() {
Expand Down

0 comments on commit b9cdf25

Please sign in to comment.