Skip to content

Commit

Permalink
Interim commit for student differences
Browse files Browse the repository at this point in the history
  • Loading branch information
arcshiftsolutions committed Jul 18, 2024
1 parent b42b264 commit 19a1683
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public SdcSchoolCollectionStudentEntity toSdcSchoolStudentEntity(final SdcStuden
entity.setNumberOfCourses(StringMapper.trimAndUppercase(studentDetails.getNumberOfCourses()));
entity.setBandCode(StringMapper.trimAndUppercase(studentDetails.getBandCode()));

var hash = entity.getUniqueObjectHash();
entity.setOriginalDemogHash(hash);
entity.setCurrentDemogHash(hash);

if(StringUtils.isNotBlank(studentDetails.getPen()) && studentDetails.getPen().length() == 9) {
entity.setStudentPen(StringMapper.trimAndUppercase(studentDetails.getPen()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,27 @@
import ca.bc.gov.educ.studentdatacollection.api.constants.v1.ReportTypeCode;
import ca.bc.gov.educ.studentdatacollection.api.endpoint.v1.ReportGenerationEndpoint;
import ca.bc.gov.educ.studentdatacollection.api.exception.InvalidPayloadException;
import ca.bc.gov.educ.studentdatacollection.api.exception.StudentDataCollectionAPIRuntimeException;
import ca.bc.gov.educ.studentdatacollection.api.exception.errors.ApiError;
import ca.bc.gov.educ.studentdatacollection.api.mappers.v1.SdcSchoolCollectionStudentMapper;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentHistoryEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentPaginationEntity;
import ca.bc.gov.educ.studentdatacollection.api.reports.*;
import ca.bc.gov.educ.studentdatacollection.api.service.v1.SdcSchoolCollectionHistoryService;
import ca.bc.gov.educ.studentdatacollection.api.service.v1.SdcSchoolCollectionStudentSearchService;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.reports.DownloadableReportResponse;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.summary.StudentDifference;
import ca.bc.gov.educ.studentdatacollection.api.util.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

Expand All @@ -32,6 +44,9 @@ public class ReportGenerationController implements ReportGenerationEndpoint {
private final GradeEnrollmentHeadcountPerSchoolReportService gradeEnrollmentHeadcountPerSchoolReportService;
private final CareerProgramHeadcountPerSchoolReportService careerProgramHeadcountPerSchoolReportService;
private final RefugeeHeadcountPerSchoolReportService refugeeHeadcountPerSchoolReportService;
private final SdcSchoolCollectionStudentSearchService sdcSchoolCollectionStudentSearchService;
private final SdcSchoolCollectionHistoryService sdcSchoolCollectionHistoryService;
private static final SdcSchoolCollectionStudentMapper sdcSchoolCollectionStudentMapper = SdcSchoolCollectionStudentMapper.mapper;

@Override
public DownloadableReportResponse generateSDCReport(UUID collectionID, String reportTypeCode) {
Expand Down Expand Up @@ -63,5 +78,42 @@ public DownloadableReportResponse generateSDCReport(UUID collectionID, String re
};
}

@Override
public List<StudentDifference> getStudentDifferences(Integer pageNumber, Integer pageSize, String sortCriteriaJson, String searchCriteriaListJson) {
final List<Sort.Order> sorts = new ArrayList<>();
Specification<SdcSchoolCollectionStudentPaginationEntity> studentSpecs = sdcSchoolCollectionStudentSearchService
.setSpecificationAndSortCriteria(
sortCriteriaJson,
searchCriteriaListJson,
JsonUtil.mapper,
sorts
);
try {
var studentsWithDiffAndCriteria = this.sdcSchoolCollectionStudentSearchService.findAll(studentSpecs, pageNumber, pageSize, sorts).get();
var currentStudentsMap = studentsWithDiffAndCriteria.stream().collect(Collectors.toMap(
stud -> stud.getSdcSchoolCollectionStudentID(),
stud -> stud
));
var historyRecords = sdcSchoolCollectionHistoryService.getFirstHistoryRecordsForStudentIDs(currentStudentsMap.keySet());
var historyRecordsMap = historyRecords.stream().collect(Collectors.toMap(
stud -> stud.getSdcSchoolCollectionStudentID(),
stud -> stud
));
return getDifferencesList(currentStudentsMap, historyRecordsMap);
} catch (Exception e) {
throw new StudentDataCollectionAPIRuntimeException("Error occurred making pagination call: " + e.getMessage());
}
}

private List<StudentDifference> getDifferencesList(Map<UUID, SdcSchoolCollectionStudentPaginationEntity> currentStudents, Map<UUID, SdcSchoolCollectionStudentHistoryEntity> originalStudents){
List<StudentDifference> differences = new ArrayList<>();
originalStudents.values().stream().forEach(stud -> {
StudentDifference diff = new StudentDifference();
diff.setOriginalStudent(sdcSchoolCollectionStudentMapper.toSdcSchoolStudentHistory(stud));
diff.setCurrentStudent(sdcSchoolCollectionStudentMapper.toSdcSchoolStudent(currentStudents.get(stud.getSdcSchoolCollectionStudentID())));
differences.add(diff);
});
return differences;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import ca.bc.gov.educ.studentdatacollection.api.constants.v1.URL;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.reports.DownloadableReportResponse;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.summary.StudentDifference;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@RequestMapping(URL.BASE_URL_REPORT_GENERATION)
Expand All @@ -21,4 +24,13 @@ public interface ReportGenerationEndpoint {
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "NOT FOUND")})
DownloadableReportResponse generateSDCReport(@PathVariable("sdcSchoolCollectionID") UUID sdcSchoolCollectionID, @PathVariable("reportTypeCode") String reportTypeCode);

@GetMapping("/differences")
@PreAuthorize("hasAuthority('SCOPE_READ_SDC_COLLECTION')")
@Transactional(readOnly = true)
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR.")})
List<StudentDifference> getStudentDifferences(@RequestParam(name = "pageNumber", defaultValue = "0") Integer pageNumber,
@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
@RequestParam(name = "sort", defaultValue = "") String sortCriteriaJson,
@RequestParam(name = "searchCriteriaList", required = false) String searchCriteriaListJson);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public enum FilterOperation {
* Equal filter operation.
*/
EQUAL("eq"),
/**
* Equal Other Field filter operation.
*/
NOT_EQUAL_OTHER_COLUMN("eqf"),
/**
* Not equal filter operation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.studentdatacollection.api.filter;

import ca.bc.gov.educ.studentdatacollection.api.exception.StudentDataCollectionAPIRuntimeException;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.criteria.*;
import org.springframework.data.jpa.domain.Specification;
Expand Down Expand Up @@ -42,6 +43,16 @@ public void initSpecifications() {
return criteriaBuilder.equal(root.get(filterCriteria.getFieldName()), filterCriteria.getConvertedSingleValue());
});

map.put(FilterOperation.NOT_EQUAL_OTHER_COLUMN, filterCriteria -> (root, criteriaQuery, criteriaBuilder) -> {
if (filterCriteria.getFieldName().contains(",")) {
String[] splits = filterCriteria.getFieldName().split(",");
if(splits.length == 2) {
return criteriaBuilder.notEqual(root.get(splits[0]), root.get(splits[1]));
}
}
throw new StudentDataCollectionAPIRuntimeException("Invalid search criteria provided");
});

map.put(FilterOperation.NOT_EQUAL, filterCriteria -> (root, criteriaQuery, criteriaBuilder) -> {
if (filterCriteria.getFieldName().contains(".")) {
String[] splits = filterCriteria.getFieldName().split("\\.");
Expand Down Expand Up @@ -140,4 +151,4 @@ public void initSpecifications() {
return root.get(filterCriteria.getFieldName()).in(filterCriteria.getConvertedValues());
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import ca.bc.gov.educ.studentdatacollection.api.mappers.UUIDMapper;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentHistoryEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentPaginationEntity;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.SdcSchoolCollectionStudent;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.SdcSchoolCollectionStudentHistory;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -30,6 +32,8 @@ public interface SdcSchoolCollectionStudentMapper {
@Mapping(target = "schoolID", source = "sdcSchoolCollection.schoolID")
SdcSchoolCollectionStudent toSdcSchoolStudent(SdcSchoolCollectionStudentPaginationEntity sdcSchoolStudentEntity);

SdcSchoolCollectionStudentHistory toSdcSchoolStudentHistory(SdcSchoolCollectionStudentHistoryEntity sdcSchoolStudentEntity);

@Mapping(target = "sdcSchoolCollectionID", source = "sdcSchoolCollection.sdcSchoolCollectionID")
@Mapping(target = "sdcDistrictCollectionID", source = "sdcSchoolCollection.sdcDistrictCollectionID")
@Mapping(target = "schoolID", source = "sdcSchoolCollection.schoolID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import ca.bc.gov.educ.studentdatacollection.api.util.UpperCase;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import jakarta.validation.constraints.PastOrPresent;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -160,6 +159,12 @@ public class SdcSchoolCollectionStudentEntity {
@Column(name = "YEARS_IN_ELL")
private Integer yearsInEll;

@Column(name = "ORIGINAL_DEMOG_HASH")
private Integer originalDemogHash;

@Column(name = "CURRENT_DEMOG_HASH")
private Integer currentDemogHash;

@Column(name = "CREATE_USER", updatable = false , length = 32)
private String createUser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import jakarta.validation.constraints.PastOrPresent;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

Expand Down Expand Up @@ -154,6 +157,12 @@ public class SdcSchoolCollectionStudentHistoryEntity extends AbstractPersistable
@Column(name = "PEN_MATCH_RESULT")
private String penMatchResult;

@Column(name = "ORIGINAL_DEMOG_HASH")
private Integer originalDemogHash;

@Column(name = "CURRENT_DEMOG_HASH")
private Integer currentDemogHash;

@Column(name = "CREATE_USER", updatable = false , length = 32)
private String createUser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import lombok.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.*;

Expand Down Expand Up @@ -145,6 +147,12 @@ public class SdcSchoolCollectionStudentLightEntity {
@Column(name = "YEARS_IN_ELL")
private Integer yearsInEll;

@Column(name = "ORIGINAL_DEMOG_HASH")
private Integer originalDemogHash;

@Column(name = "CURRENT_DEMOG_HASH")
private Integer currentDemogHash;

@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "SDC_SCHOOL_COLLECTION_ID", insertable = false, updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import ca.bc.gov.educ.studentdatacollection.api.util.UpperCase;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import jakarta.validation.constraints.PastOrPresent;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -159,6 +158,12 @@ public class SdcSchoolCollectionStudentPaginationEntity {
@Column(name = "YEARS_IN_ELL")
private Integer yearsInEll;

@Column(name = "ORIGINAL_DEMOG_HASH")
private Integer originalDemogHash;

@Column(name = "CURRENT_DEMOG_HASH")
private Integer currentDemogHash;

@Column(name = "CREATE_USER", updatable = false , length = 32)
private String createUser;

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

import java.util.List;
import java.util.Set;
import java.util.UUID;

@Repository
Expand All @@ -16,4 +17,19 @@ public interface SdcSchoolCollectionStudentHistoryRepository extends JpaReposito
@Modifying
@Query(value = "DELETE FROM SdcSchoolCollectionStudentHistoryEntity WHERE sdcSchoolCollectionStudentID in (:sdcSchoolCollectionStudentIDs)")
void deleteBySdcSchoolCollectionStudentIDs(List<UUID> sdcSchoolCollectionStudentIDs);


@Query(value = """
SELECT *
FROM (SELECT ROW_NUMBER() OVER (
PARTITION BY sdc_school_collection_student_id
ORDER BY create_date ASC) AS rnk,
*
FROM sdc_school_collection_student_history
WHERE sdc_school_collection_student_id IN (:sdcSchoolCollectionStudentIDs)
) sub
WHERE
sub.rnk = 1"""
, nativeQuery = true)
List<SdcSchoolCollectionStudentHistoryEntity> findOrginalHistoryRecordsForStudentIDList(Set<UUID> sdcSchoolCollectionStudentIDs);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionHistoryEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentHistoryEntity;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class SdcSchoolCollectionHistoryService {

private final SdcSchoolCollectionStudentHistoryRepository sdcSchoolCollectionStudentHistoryRepository;

public SdcSchoolCollectionHistoryEntity createSDCSchoolHistory(SdcSchoolCollectionEntity curSDCSchoolEntity, String updateUser) {
final SdcSchoolCollectionHistoryEntity sdcSchoolHistoryEntity = new SdcSchoolCollectionHistoryEntity();
BeanUtils.copyProperties(curSDCSchoolEntity, sdcSchoolHistoryEntity);
Expand All @@ -22,4 +31,8 @@ public SdcSchoolCollectionHistoryEntity createSDCSchoolHistory(SdcSchoolCollecti

return sdcSchoolHistoryEntity;
}

public List<SdcSchoolCollectionStudentHistoryEntity> getFirstHistoryRecordsForStudentIDs(Set<UUID> sdcSchoolStudentIDs){
return sdcSchoolCollectionStudentHistoryRepository.findOrginalHistoryRecordsForStudentIDList(sdcSchoolStudentIDs);
}
}
Loading

0 comments on commit 19a1683

Please sign in to comment.