Skip to content

Commit

Permalink
Merge pull request #1504 from bcgov/feature/reportRevisions
Browse files Browse the repository at this point in the history
Fixes for report counts
  • Loading branch information
SodhiA1 authored Jan 14, 2025
2 parents ae99574 + b9cdf25 commit efbbeaf
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 75 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
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 @@ -736,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 @@ -751,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 @@ -776,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
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 efbbeaf

Please sign in to comment.