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 13, 2025
1 parent 57dc139 commit d5ab5ae
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ public void setResultsTableComparisonValuesDynamicNested(HeadcountResultsTable c
public UUID getPreviousCollectionID(SdcSchoolCollectionEntity sdcSchoolCollectionEntity) {
Optional<SdcSchoolCollectionEntity> collection;
String collectionType = sdcSchoolCollectionEntity.getCollectionEntity().getCollectionTypeCode();
if (collectionType.equals(CollectionTypeCodes.JULY.getTypeCode())) collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), collectionType, sdcSchoolCollectionEntity.getSdcSchoolCollectionID());
else collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), sdcSchoolCollectionEntity.getSdcSchoolCollectionID());
if (collectionType.equals(CollectionTypeCodes.JULY.getTypeCode())) {
collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), collectionType, sdcSchoolCollectionEntity.getSdcSchoolCollectionID(), sdcSchoolCollectionEntity.getCollectionEntity().getSnapshotDate());
} else {
collection = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(sdcSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), sdcSchoolCollectionEntity.getSdcSchoolCollectionID(), sdcSchoolCollectionEntity.getCollectionEntity().getSnapshotDate());
}
return collection.map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).orElse(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ public interface SdcSchoolCollectionRepository extends JpaRepository<SdcSchoolCo
WHERE SSC.schoolID=:schoolID
AND C.collectionID = SSC.collectionEntity.collectionID
AND SSC.sdcSchoolCollectionID != :currentSdcCollectionID
AND C.snapshotDate < :snapshotDate
AND C.collectionTypeCode = :collectionTypeCode
ORDER BY C.snapshotDate desc
LIMIT 1""")
Optional<SdcSchoolCollectionEntity> findLastCollectionBySchoolIDAndType(UUID schoolID, String collectionTypeCode, UUID currentSdcCollectionID);
Optional<SdcSchoolCollectionEntity> findLastCollectionBySchoolIDAndType(UUID schoolID, String collectionTypeCode, UUID currentSdcCollectionID, LocalDate snapshotDate);

@Query(value="""
SELECT C FROM CollectionEntity C
WHERE C.collectionID != :currentCollectionID
AND C.collectionTypeCode = :collectionTypeCode
AND C.snapshotDate < :snapshotDate
ORDER BY C.snapshotDate desc
LIMIT 1""")
Optional<CollectionEntity> findLastCollectionByType(String collectionTypeCode, UUID currentCollectionID);
Optional<CollectionEntity> findLastCollectionByType(String collectionTypeCode, UUID currentCollectionID, LocalDate snapshotDate);

List<SdcSchoolCollectionEntity> findAllBySchoolID(UUID schoolID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,30 @@ long countAllByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDIn
"ORDER BY s.enrolledGradeCode")
List<SpecialEdHeadcountResult> getSpecialEdHeadcountsBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID);

@Query("SELECT " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B') AND s.fte > 0 THEN 1 END) AS levelOnes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' AND s.fte > 0 THEN 1 END) AS specialEdACodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'B' AND s.fte > 0 THEN 1 END) AS specialEdBCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('C', 'D', 'E', 'F', 'G') AND s.fte > 0 THEN 1 END) AS levelTwos, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'C' AND s.fte > 0 THEN 1 END) AS specialEdCCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'D' AND s.fte > 0 THEN 1 END) AS specialEdDCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'E' AND s.fte > 0 THEN 1 END) AS specialEdECodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'F' AND s.fte > 0 THEN 1 END) AS specialEdFCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'G' AND s.fte > 0 THEN 1 END) AS specialEdGCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('H') AND s.fte > 0 THEN 1 END) AS levelThrees, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'H' AND s.fte > 0 THEN 1 END) AS specialEdHCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('K', 'P', 'Q', 'R') AND s.fte > 0 THEN 1 END) AS otherLevels, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'K' AND s.fte > 0 THEN 1 END) AS specialEdKCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'P' AND s.fte > 0 THEN 1 END) AS specialEdPCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'Q' AND s.fte > 0 THEN 1 END) AS specialEdQCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'R' AND s.fte > 0 THEN 1 END) AS specialEdRCodes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'P', 'Q', 'R') AND s.fte > 0 THEN 1 END) AS allLevels " +
"FROM SdcSchoolCollectionStudentEntity s " +
"WHERE s.sdcSchoolCollection.sdcDistrictCollectionID = :sdcDistrictCollectionID " +
"AND s.sdcSchoolCollectionStudentStatusCode NOT IN ('ERROR', 'DELETED') " +
"AND s.specialEducationNonEligReasonCode IS NULL ")
SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID);

@Query("SELECT " +
"COUNT(CASE WHEN s.specialEducationCategoryCode IN ('A', 'B') THEN 1 END) AS levelOnes, " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' THEN 1 END) AS specialEdACodes, " +
Expand All @@ -778,7 +802,7 @@ long countAllByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDIn
"WHERE s.sdcSchoolCollection.sdcDistrictCollectionID = :sdcDistrictCollectionID " +
"AND s.sdcSchoolCollectionStudentStatusCode NOT IN ('ERROR', 'DELETED') " +
"AND s.specialEducationNonEligReasonCode IS NULL ")
SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID);
SpecialEdHeadcountResult getSpecialEdHeadcountsSimpleFebruaryBySdcDistrictCollectionId(@Param("sdcDistrictCollectionID") UUID sdcDistrictCollectionID);

@Query("SELECT " +
"COUNT(CASE WHEN s.specialEducationCategoryCode = 'A' AND s.specialEducationNonEligReasonCode IS NULL THEN 1 END) AS totalEligibleA, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public void startSDCCollectionFromLastSDCCollectionDataSet(UUID sdcSchoolCollect
Optional<SdcSchoolCollectionEntity> sdcSchoolCollectionOptional = sdcSchoolCollectionRepository.findById(sdcSchoolCollectionID);
SdcSchoolCollectionEntity currentSchoolCollectionEntity = sdcSchoolCollectionOptional.orElseThrow(() -> new EntityNotFoundException(SdcSchoolCollectionEntity.class, SDC_SCHOOL_COLLECTION_ID_KEY, sdcSchoolCollectionID.toString()));
sdcSchoolCollectionStudentHistoryRepository.deleteAllBySdcSchoolCollectionID(sdcSchoolCollectionID);
var septemberCollectionOptional = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(currentSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), currentSchoolCollectionEntity.getSdcSchoolCollectionID());
var septemberCollectionOptional = sdcSchoolCollectionRepository.findLastCollectionBySchoolIDAndType(currentSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), currentSchoolCollectionEntity.getSdcSchoolCollectionID(), currentSchoolCollectionEntity.getCollectionEntity().getSnapshotDate());
SdcSchoolCollectionEntity septemberCollection = septemberCollectionOptional.orElseThrow(() -> new EntityNotFoundException(SdcSchoolCollectionEntity.class, SDC_SCHOOL_COLLECTION_ID_KEY, sdcSchoolCollectionID.toString()));

currentSchoolCollectionEntity.getSDCSchoolStudentEntities().clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,11 @@ public DownloadableReportResponse generateIndySpecialEducationHeadcounts(UUID co
// Independent School Inclusive Education Funding Headcounts report
public DownloadableReportResponse generateIndySpecialEducationFundingHeadcounts(UUID collectionID) {
List<SpecialEdHeadcountResult> collectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsFebruaryByCollectionId(collectionID);
var mappedSeptData = getLastSeptCollectionSchoolMap(collectionID);

var collection = collectionRepository.findById(collectionID).orElseThrow(() ->
new EntityNotFoundException(Collection.class, COLLECTION_ID, collectionID.toString()));

var mappedSeptData = getLastSeptCollectionSchoolMap(collection);

CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
.setHeader(DISTRICT_NUMBER.getCode(), IndySpecialEducationFundingHeadcountHeader.DISTRICT_NAME.getCode(), IndySpecialEducationFundingHeadcountHeader.AUTHORITY_NUMBER.getCode(), IndySpecialEducationFundingHeadcountHeader.AUTHORITY_NAME.getCode(), MINCODE.getCode(), SCHOOL_NAME.getCode(),
Expand Down Expand Up @@ -618,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 @@ -963,7 +967,7 @@ public DownloadableReportResponse generateInclusiveEducationVarianceReport(UUID
SdcDistrictCollectionEntity septCollection = sdcDistrictCollectionRepository.findLastSdcDistrictCollectionByCollectionTypeBefore(CollectionTypeCodes.SEPTEMBER.getTypeCode(), districtID, febCollection.getCollectionEntity().getSnapshotDate())
.orElseThrow(() -> new RuntimeException("No previous September sdc district collection found relative to the February collection."));

var febCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(febCollection.getSdcDistrictCollectionID());
var febCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsSimpleFebruaryBySdcDistrictCollectionId(febCollection.getSdcDistrictCollectionID());
var septCollectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsSimpleBySdcDistrictCollectionId(septCollection.getSdcDistrictCollectionID());

var district = restUtils.getDistrictByDistrictID(districtID.toString()).orElseThrow(() -> new EntityNotFoundException(District.class, DISTRICT_ID, districtID.toString()));
Expand Down Expand Up @@ -1050,7 +1054,7 @@ public DownloadableReportResponse generateEnrolmentHeadcountsAndFteReportForCEAn
}

List<EnrolmentHeadcountFteResult> results = sdcSchoolCollectionStudentRepository.getEnrolmentHeadcountsAndFteWithRefugeeByCollectionId(collectionID);
var mappedSeptData = getEnrolmentHeadcountFteResultForLastSeptCollection(collectionID);
var mappedSeptData = getEnrolmentHeadcountFteResultForLastSeptCollection(collection);

List<String> headers = Arrays.stream(CEAndOLEnrolmentAndFteHeader.values()).map(CEAndOLEnrolmentAndFteHeader::getCode).toList();
CSVFormat csvFormat = CSVFormat.DEFAULT.builder()
Expand Down Expand Up @@ -1132,10 +1136,10 @@ public DownloadableReportResponse generateRefugeeEnrolmentHeadcountsAndFteReport
}
}

private Map<String, EnrolmentHeadcountFteResult> getEnrolmentHeadcountFteResultForLastSeptCollection(UUID collectionID){
var lastSeptCollectionOpt = sdcSchoolCollectionRepository.findLastCollectionByType(CollectionTypeCodes.SEPTEMBER.getTypeCode(), collectionID);
private Map<String, EnrolmentHeadcountFteResult> getEnrolmentHeadcountFteResultForLastSeptCollection(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<EnrolmentHeadcountFteResult> lastSeptCollectionRawData = sdcSchoolCollectionStudentRepository.getEnrolmentHeadcountsAndFteWithRefugeeByCollectionId(lastSeptCollectionOpt.get().getCollectionID());
return lastSeptCollectionRawData.stream().collect(Collectors.toMap(EnrolmentHeadcountFteResult::getSchoolID, item -> item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ public SimpleHeadcountResultsTable getSpecialEducationHeadcountsForIndependentsB
// Independent School Inclusive Education Funding Headcounts report
public SimpleHeadcountResultsTable getSpecialEducationFundingHeadcountsForIndependentsByCollectionID(UUID collectionID) {
List<SpecialEdHeadcountResult> collectionRawData = sdcSchoolCollectionStudentRepository.getSpecialEdHeadcountsFebruaryByCollectionId(collectionID);
var mappedSeptData = getLastSeptCollectionSchoolMap(collectionID);
var collection = collectionRepository.findById(collectionID).orElseThrow(() ->
new EntityNotFoundException(Collection.class, COLLECTION_ID, collectionID.toString()));
var mappedSeptData = getLastSeptCollectionSchoolMap(collection);

SimpleHeadcountResultsTable resultsTable = new SimpleHeadcountResultsTable();
var headerList = new ArrayList<String>();
Expand Down Expand Up @@ -316,10 +318,10 @@ public SimpleHeadcountResultsTable getSpecialEducationFundingHeadcountsForIndepe
return resultsTable;
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ void testGetMinistryReportSpecialEdFunding_ValidIndySchoolType_ShouldReturnRepor

CollectionEntity collectionSept = createMockCollectionEntity();
collectionSept.setCloseDate(LocalDateTime.now().minusDays(20));
collectionSept.setSnapshotDate(LocalDate.now().minusWeeks(4));
collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode());
var savedSeptColl = collectionRepository.save(collectionSept);

Expand Down Expand Up @@ -738,6 +739,7 @@ void testGetMinistryReportCSV_ValidIndySpecialEducationFundingType_ShouldReturnR

CollectionEntity collectionSept = createMockCollectionEntity();
collectionSept.setCloseDate(LocalDateTime.now().minusDays(20));
collectionSept.setSnapshotDate(LocalDate.now().minusWeeks(4));
collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode());
collectionRepository.save(collectionSept);

Expand Down Expand Up @@ -1163,6 +1165,7 @@ void testGetMinistryReportCSV_TypeENROLMENT_HEADCOUNTS_AND_FTE_REPORT_FOR_OL_AND

CollectionEntity collectionSept = createMockCollectionEntity();
collectionSept.setCloseDate(LocalDateTime.now().minusDays(20));
collectionSept.setSnapshotDate(LocalDate.now().minusWeeks(4));
collectionSept.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode());
var savedSeptColl = collectionRepository.save(collectionSept);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

Expand Down Expand Up @@ -660,6 +661,7 @@ void testStartCollectionFromLastSept_ShouldReturnOk() throws Exception {
CollectionEntity oldCollection = createMockCollectionEntity();
oldCollection.setCollectionTypeCode(CollectionTypeCodes.SEPTEMBER.getTypeCode());
oldCollection.setCloseDate(LocalDateTime.now().minusDays(2));
oldCollection.setSnapshotDate(LocalDate.now().minusDays(20));
collectionRepository.save(oldCollection);

District district = createMockDistrict();
Expand Down
Loading

0 comments on commit d5ab5ae

Please sign in to comment.