Skip to content

Commit

Permalink
Merge pull request #383 from bcgov/feature/lastTwo
Browse files Browse the repository at this point in the history
Fixes for pulling last two years
  • Loading branch information
mightycox authored Jan 5, 2024
2 parents b5405ea + ee29035 commit 98622ac
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -160,8 +159,7 @@ public boolean noCoursesForStudentInLastTwoYears(StudentRuleData studentRuleData
boolean isSchoolAged = Boolean.TRUE.equals(student.getIsSchoolAged());

if (isSchoolAged && isEightPlusGradeCode && reportedByOnlineSchoolWithNoCourses) {
var startOfMonth = student.getCreateDate().with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllBySchoolIDAndCreateDateBetween(UUID.fromString(school.getSchoolId()), startOfMonth.minusYears(2), startOfMonth);
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()), student.getSdcSchoolCollection().getSdcSchoolCollectionID());
return lastTwoYearsOfCollections.isEmpty() || sdcSchoolCollectionStudentRepository.countByAssignedStudentIdAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(student.getAssignedStudentId(), lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(), "0") == 0;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.studentdatacollection.api.helpers;

import ca.bc.gov.educ.studentdatacollection.api.constants.v1.CollectionTypeCodes;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionEntity;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionRepository;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentRepository;
Expand All @@ -11,10 +12,6 @@
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -48,13 +45,9 @@ public void setComparisonValues(List<HeadcountHeader> headcountHeaderList, List<
}

public UUID getPreviousSeptemberCollectionID(SdcSchoolCollectionEntity sdcSchoolCollectionEntity) {
int previousYear = sdcSchoolCollectionEntity.getCreateDate().minusYears(1).getYear();
LocalDateTime startOfCollectionDate = LocalDate.of(previousYear, Month.SEPTEMBER, 1).atTime(LocalTime.MIN);
LocalDateTime endOfCollectionDate = LocalDate.of(previousYear, Month.SEPTEMBER, 30).atTime(LocalTime.MAX);

var septemberCollection = sdcSchoolCollectionRepository.findAllBySchoolIDAndCreateDateBetween(sdcSchoolCollectionEntity.getSchoolID(), startOfCollectionDate, endOfCollectionDate);
if(!septemberCollection.isEmpty()) {
return septemberCollection.get(0).getSdcSchoolCollectionID();
var septemberCollection = sdcSchoolCollectionRepository.findLastCollectionByType(sdcSchoolCollectionEntity.getSchoolID(), CollectionTypeCodes.SEPTEMBER.getTypeCode(), sdcSchoolCollectionEntity.getSdcSchoolCollectionID());
if(septemberCollection.isPresent()) {
return septemberCollection.get().getSdcSchoolCollectionID();
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,30 @@ public interface SdcSchoolCollectionRepository extends JpaRepository<SdcSchoolCo
AND C.openDate <= CURRENT_TIMESTAMP AND C.closeDate >= CURRENT_TIMESTAMP""")
Optional<SdcSchoolCollectionEntity> findActiveCollectionBySchoolId(UUID schoolID);

List<SdcSchoolCollectionEntity> findAllBySchoolIDAndCreateDateBetween(UUID schoolId, LocalDateTime startDate, LocalDateTime endDate);
@Query(value = """
SELECT SSC.*
FROM sdc_school_collection SSC, collection C
WHERE SSC.school_id=:schoolId
AND C.collection_id = ssc.collection_id
AND C.snapshot_date >=
(select (col.snapshot_date - INTERVAL '2' year - INTERVAL '10' day)
from collection col, sdc_school_collection ssoc
where col.collection_id = ssoc.collection_id
and ssoc.sdc_school_collection_id = :sdcCollectionID
)
AND ssc.sdc_school_collection_id != :sdcCollectionID"""
, nativeQuery = true)
List<SdcSchoolCollectionEntity> findAllCollectionsForSchoolInLastTwoYears(UUID schoolId, UUID sdcCollectionID);

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

List<SdcSchoolCollectionEntity> findAllByDistrictIDAndCreateDateBetween(UUID schoolId, LocalDateTime startDate, LocalDateTime endDate);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -67,23 +65,12 @@ public List<ProgramEligibilityIssueCode> executeValidation(StudentRuleData stude
List<ProgramEligibilityIssueCode> errors = new ArrayList<>();
School school = studentRuleData.getSchool();
var student = studentRuleData.getSdcSchoolCollectionStudentEntity();
LocalDateTime startOfMonth = student.getCreateDate()
.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);

List<SdcSchoolCollectionEntity> lastTwoYearsOfCollections = sdcSchoolCollectionRepository
.findAllBySchoolIDAndCreateDateBetween(
UUID.fromString(school.getSchoolId()),
startOfMonth.minusYears(2),
startOfMonth
);
log.debug("In executeValidation of ProgramEligibilityBaseRule - InactiveOnlineAdultStudentsRule: No of collections - {}, for sdcSchoolCollectionStudentID :: {}" ,lastTwoYearsOfCollections.size(), studentRuleData.getSdcSchoolCollectionStudentEntity().getSdcSchoolCollectionStudentID());

if (lastTwoYearsOfCollections.isEmpty()
|| sdcSchoolCollectionStudentRepository.countByAssignedStudentIdAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(
student.getAssignedStudentId(),
lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(),
"0"
) == 0) {

List<SdcSchoolCollectionEntity> lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()),student.getSdcSchoolCollection().getSdcSchoolCollectionID());
log.debug("In executeValidation of ProgramEligibilityBaseRule - InactiveOnlineAdultStudentsRule: No of collections - {}, for school :: {}" ,lastTwoYearsOfCollections.size(), school.getSchoolId());

if (lastTwoYearsOfCollections.isEmpty() || student.getAssignedStudentId() == null
|| sdcSchoolCollectionStudentRepository.countByAssignedStudentIdAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(student.getAssignedStudentId(), lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(), "0") == 0) {
errors.add(ProgramEligibilityIssueCode.INACTIVE_ADULT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
Expand Down Expand Up @@ -66,23 +64,12 @@ public List<ProgramEligibilityIssueCode> executeValidation(StudentRuleData stude
List<ProgramEligibilityIssueCode> errors = new ArrayList<>();
var student = studentRuleData.getSdcSchoolCollectionStudentEntity();
School school = studentRuleData.getSchool();
LocalDateTime startOfMonth = student.getCreateDate()
.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0).withNano(0);

List<SdcSchoolCollectionEntity> lastTwoYearsOfCollections = sdcSchoolCollectionRepository
.findAllBySchoolIDAndCreateDateBetween(
UUID.fromString(school.getSchoolId()),
startOfMonth.minusYears(2),
startOfMonth
);
log.debug("In executeValidation of ProgramEligibilityBaseRule - InactiveOnlineMinorStudentsRule: No of collections - {}, for sdcSchoolCollectionStudentID :: {}" ,lastTwoYearsOfCollections.size(), studentRuleData.getSdcSchoolCollectionStudentEntity().getSdcSchoolCollectionStudentID());
List<SdcSchoolCollectionEntity> lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()),student.getSdcSchoolCollection().getSdcSchoolCollectionID());
log.debug("In executeValidation of ProgramEligibilityBaseRule - InactiveOnlineMinorStudentsRule: No of collections - {}, for school :: {}" ,lastTwoYearsOfCollections.size(), school.getSchoolId());

if (lastTwoYearsOfCollections.isEmpty()
|| sdcSchoolCollectionStudentRepository.countByAssignedStudentIdAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(
student.getAssignedStudentId(),
lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(),
"0"
) == 0) {
if (lastTwoYearsOfCollections.isEmpty() || student.getAssignedStudentId() == null
|| sdcSchoolCollectionStudentRepository.countByAssignedStudentIdAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(student.getAssignedStudentId(), lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(), "0") == 0) {
errors.add(ProgramEligibilityIssueCode.INACTIVE_SCHOOL_AGE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import ca.bc.gov.educ.studentdatacollection.api.exception.EntityNotFoundException;
import ca.bc.gov.educ.studentdatacollection.api.exception.StudentDataCollectionAPIRuntimeException;
import ca.bc.gov.educ.studentdatacollection.api.mappers.v1.CodeTableMapper;
import ca.bc.gov.educ.studentdatacollection.api.mappers.v1.SdcSchoolCollectionStudentMapper;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcStudentEllEntity;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentRepository;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcStudentEllRepository;
import ca.bc.gov.educ.studentdatacollection.api.rest.RestUtils;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.*;
import ca.bc.gov.educ.studentdatacollection.api.struct.StudentRuleData;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.*;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
Expand All @@ -33,30 +32,35 @@ public class ValidationRulesService {
private final SdcSchoolCollectionStudentRepository sdcSchoolStudentRepository;
private final RestUtils restUtils;
private static final CodeTableMapper mapper = CodeTableMapper.mapper;

public ValidationRulesService(CodeTableService codeTableService, SdcStudentEllRepository sdcStudentEllRepository, SdcSchoolCollectionStudentRepository sdcSchoolStudentRepository, RestUtils restUtils) {
this.codeTableService = codeTableService;
this.sdcStudentEllRepository = sdcStudentEllRepository;
this.sdcSchoolStudentRepository = sdcSchoolStudentRepository;
this.restUtils = restUtils;
}

public Long getDuplicatePenCount(UUID sdcSchoolID, String studentPen) {
return sdcSchoolStudentRepository.countForDuplicateStudentPENs(sdcSchoolID, studentPen);
}

public List<EnrolledProgramCode> getActiveEnrolledProgramCodes() {
return codeTableService.getAllEnrolledProgramCodes().stream().filter(code -> code.getExpiryDate().isAfter(LocalDateTime.now())).map(mapper::toStructure).toList();
}

public List<CareerProgramCode> getActiveCareerProgramCodes() {
return codeTableService.getAllCareerProgramCodes().stream().filter(code -> code.getExpiryDate().isAfter(LocalDateTime.now())).map(mapper::toStructure).toList();
}

public List<HomeLanguageSpokenCode> getActiveHomeLanguageSpokenCodes() {
return codeTableService.getAllHomeLanguageSpokenCodes().stream().filter(code -> code.getExpiryDate().isAfter(LocalDateTime.now())).map(mapper::toStructure).toList();
}
public List<BandCode> getActiveBandCodes() {

public List<BandCode> getActiveBandCodes() {
return codeTableService.getAllBandCodes().stream().filter(code -> code.getExpiryDate().isAfter(LocalDateTime.now())).map(mapper::toStructure).toList();
}

public List<SchoolFundingCode> getActiveFundingCodes() {
public List<SchoolFundingCode> getActiveFundingCodes() {
return codeTableService.getAllFundingCodes().stream().filter(code -> code.getExpiryDate().isAfter(LocalDateTime.now())).map(mapper::toStructure).toList();
}

Expand All @@ -75,64 +79,68 @@ public boolean isEnrolledProgramCodeInvalid(String enrolledProgramCode) {
}

public List<String> splitString(String enrolledProgramCode) {
if(StringUtils.isEmpty(enrolledProgramCode)) {
if (StringUtils.isEmpty(enrolledProgramCode)) {
return Collections.<String>emptyList();
}
return Pattern.compile(".{1,2}").matcher(enrolledProgramCode).results().map(MatchResult::group).toList();
}

public Optional<SdcStudentEllEntity> getStudentYearsInEll(String studentID){
public Optional<SdcStudentEllEntity> getStudentYearsInEll(String studentID) {
return sdcStudentEllRepository.findByStudentID(UUID.fromString(studentID));
}

public void updatePenMatchAndGradStatusColumns(SdcSchoolCollectionStudentEntity student, String mincode) throws EntityNotFoundException {
var penMatchResult = this.restUtils.getPenMatchResult(UUID.randomUUID(), student, mincode);
val penMatchResultCode = penMatchResult.getPenStatus();
student.setPenMatchResult(penMatchResultCode);
var validPenMatchResults = Arrays.asList("AA", "B1", "C1", "D1");

if (StringUtils.isNotEmpty(penMatchResultCode) && validPenMatchResults.contains(penMatchResultCode)) {
final var penMatchRecordOptional = penMatchResult.getMatchingRecords().stream().findFirst();
if (penMatchRecordOptional.isPresent()) {
var assignedPEN = penMatchRecordOptional.get().getMatchingPEN();
var assignedStudentID = penMatchRecordOptional.get().getStudentID();

student.setAssignedStudentId(UUID.fromString(assignedStudentID));
student.setAssignedPen(assignedPEN);
} else {
log.error("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
throw new StudentDataCollectionAPIRuntimeException("PenMatchRecord in priority queue is empty for matched status, this should not have happened.");
}
}else{
student.setPenMatchResult("NEW");
}
public void updatePenMatchAndGradStatusColumns(SdcSchoolCollectionStudentEntity student, String mincode) throws EntityNotFoundException {
var penMatchResult = this.restUtils.getPenMatchResult(UUID.randomUUID(), student, mincode);
val penMatchResultCode = penMatchResult.getPenStatus();
student.setPenMatchResult(penMatchResultCode);
var validPenMatchResults = Arrays.asList("AA", "B1", "C1", "D1");
var multiPenMatchResults = Arrays.asList("BM", "CM", "DM");

if (StringUtils.isNotEmpty(penMatchResultCode) && validPenMatchResults.contains(penMatchResultCode)) {
if (penMatchResult.getMatchingRecords() == null || penMatchResult.getMatchingRecords().isEmpty()) {
log.error("PEN Match records list is null or empty - this should not have happened :: ", penMatchResult);
throw new StudentDataCollectionAPIRuntimeException("PEN Match records list is null or empty - this should not have happened");
}
final var penMatchRecordOptional = penMatchResult.getMatchingRecords().stream().findFirst();
if (penMatchRecordOptional.isPresent()) {
var assignedPEN = penMatchRecordOptional.get().getMatchingPEN();
var assignedStudentID = penMatchRecordOptional.get().getStudentID();

student.setAssignedStudentId(UUID.fromString(assignedStudentID));
student.setAssignedPen(assignedPEN);
}
} else if (StringUtils.isNotEmpty(penMatchResultCode) && multiPenMatchResults.contains(penMatchResultCode)) {
student.setPenMatchResult("MULTI");
} else {
student.setPenMatchResult("NEW");
}

if(student.getAssignedStudentId() != null) {
final var gradResult = this.restUtils.getGradStatusResult(UUID.randomUUID(), SdcSchoolCollectionStudentMapper.mapper.toSdcSchoolStudent(student));
log.info("Grad status for SDC student {} :: is {}", student.getSdcSchoolCollectionStudentID(), gradResult);
// if(student.getAssignedStudentId() != null) {
// final var gradResult = this.restUtils.getGradStatusResult(UUID.randomUUID(), SdcSchoolCollectionStudentMapper.mapper.toSdcSchoolStudent(student));
// log.info("Grad status for SDC student {} :: is {}", student.getSdcSchoolCollectionStudentID(), gradResult);
// }
student.setIsGraduated(false);
}
student.setIsGraduated(false);
}

public boolean hasEnrollmentHistory(StudentRuleData studentRuleData){
var student = studentRuleData.getSdcSchoolCollectionStudentEntity();
var school = studentRuleData.getSchool();
var twoYearsAgo = student.getSdcSchoolCollection().getCollectionEntity().getOpenDate().getYear() - 2;
public boolean hasEnrollmentHistory(StudentRuleData studentRuleData) {
var student = studentRuleData.getSdcSchoolCollectionStudentEntity();
var school = studentRuleData.getSchool();
var twoYearsAgo = student.getSdcSchoolCollection().getCollectionEntity().getOpenDate().getYear() - 2;


var listOfNumCoursesLastTwoYears = getSdcSchoolStudentRepository().getCollectionHistory(UUID.fromString(school.getSchoolId()),
student.getStudentPen(), student.getSdcSchoolCollection().getCollectionEntity().getOpenDate(), studentRuleData.getCollectionTypeCode(), twoYearsAgo);
var listOfNumCoursesLastTwoYears = getSdcSchoolStudentRepository().getCollectionHistory(UUID.fromString(school.getSchoolId()),
student.getStudentPen(), student.getSdcSchoolCollection().getCollectionEntity().getOpenDate(), studentRuleData.getCollectionTypeCode(), twoYearsAgo);

for (String numString : listOfNumCoursesLastTwoYears){
try{
if (Integer.parseInt(numString) > 0) {
return true;
}
} catch (Exception e) {
//Do nothing
}
}
for (String numString : listOfNumCoursesLastTwoYears) {
try {
if (Integer.parseInt(numString) > 0) {
return true;
}
} catch (Exception e) {
//Do nothing
}
}

return false;
return false;
}
}
Loading

0 comments on commit 98622ac

Please sign in to comment.