|
| 1 | +package ca.bc.gov.educ.studentdatacollection.api.reports; |
| 2 | + |
| 3 | +import ca.bc.gov.educ.studentdatacollection.api.constants.v1.DistrictReportTypeCode; |
| 4 | +import ca.bc.gov.educ.studentdatacollection.api.constants.v1.SchoolGradeCodes; |
| 5 | +import ca.bc.gov.educ.studentdatacollection.api.exception.EntityNotFoundException; |
| 6 | +import ca.bc.gov.educ.studentdatacollection.api.exception.StudentDataCollectionAPIRuntimeException; |
| 7 | +import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcDistrictCollectionEntity; |
| 8 | +import ca.bc.gov.educ.studentdatacollection.api.properties.ApplicationProperties; |
| 9 | +import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcDistrictCollectionRepository; |
| 10 | +import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionRepository; |
| 11 | +import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentRepository; |
| 12 | +import ca.bc.gov.educ.studentdatacollection.api.rest.RestUtils; |
| 13 | +import ca.bc.gov.educ.studentdatacollection.api.struct.external.institute.v1.SchoolTombstone; |
| 14 | +import ca.bc.gov.educ.studentdatacollection.api.struct.v1.headcounts.PRPorYouthHeadcountResult; |
| 15 | +import ca.bc.gov.educ.studentdatacollection.api.struct.v1.reports.*; |
| 16 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 17 | +import jakarta.annotation.PostConstruct; |
| 18 | +import lombok.extern.slf4j.Slf4j; |
| 19 | +import net.sf.jasperreports.engine.JRException; |
| 20 | +import net.sf.jasperreports.engine.JasperCompileManager; |
| 21 | +import net.sf.jasperreports.engine.JasperReport; |
| 22 | +import org.springframework.stereotype.Service; |
| 23 | + |
| 24 | +import java.io.InputStream; |
| 25 | +import java.util.*; |
| 26 | + |
| 27 | +@Service |
| 28 | +@Slf4j |
| 29 | +public class PRPorYouthHeadcountReportService extends BaseReportGenerationService<PRPorYouthHeadcountResult>{ |
| 30 | + |
| 31 | + protected static final String ALLPRPORYOUTH = "allPRPorYouth"; |
| 32 | + private final SdcDistrictCollectionRepository sdcDistrictCollectionRepository; |
| 33 | + private final SdcSchoolCollectionStudentRepository sdcSchoolCollectionStudentRepository; |
| 34 | + private final RestUtils restUtils; |
| 35 | + private List<SchoolTombstone> allSchoolsTombstones; |
| 36 | + private List<PRPorYouthHeadcountResult> prpOrYouthHeadcountList; |
| 37 | + private JasperReport prpOrYouthHeadcountReport; |
| 38 | + |
| 39 | + public PRPorYouthHeadcountReportService(SdcDistrictCollectionRepository sdcDistrictCollectionRepository, SdcSchoolCollectionStudentRepository sdcSchoolCollectionStudentRepository, SdcSchoolCollectionRepository sdcSchoolCollectionRepository , RestUtils restUtils) { |
| 40 | + super(restUtils, sdcSchoolCollectionRepository); |
| 41 | + this.sdcDistrictCollectionRepository = sdcDistrictCollectionRepository; |
| 42 | + this.sdcSchoolCollectionStudentRepository = sdcSchoolCollectionStudentRepository; |
| 43 | + this.restUtils = restUtils; |
| 44 | + } |
| 45 | + |
| 46 | + @PostConstruct |
| 47 | + public void init() { |
| 48 | + ApplicationProperties.bgTask.execute(this::initialize); |
| 49 | + } |
| 50 | + |
| 51 | + private void initialize() { |
| 52 | + this.compileJasperReports(); |
| 53 | + } |
| 54 | + |
| 55 | + private void compileJasperReports(){ |
| 56 | + try { |
| 57 | + InputStream inputYouthPRPHeadcount = getClass().getResourceAsStream("/reports/youthPrpHeadcountsPerSchool.jrxml"); |
| 58 | + prpOrYouthHeadcountReport = JasperCompileManager.compileReport(inputYouthPRPHeadcount); |
| 59 | + } catch (JRException e) { |
| 60 | + throw new StudentDataCollectionAPIRuntimeException("Compiling Jasper reports has failed :: " + e.getMessage()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public DownloadableReportResponse generatePerSchoolReport(UUID sdcDistrictCollectionID){ |
| 65 | + try { |
| 66 | + Optional<SdcDistrictCollectionEntity> sdcDistrictCollectionEntityOptional = sdcDistrictCollectionRepository.findById(sdcDistrictCollectionID); |
| 67 | + SdcDistrictCollectionEntity sdcDistrictCollectionEntity = sdcDistrictCollectionEntityOptional.orElseThrow(() -> |
| 68 | + new EntityNotFoundException(SdcDistrictCollectionEntity.class, "sdcDistrictCollectionID", sdcDistrictCollectionID.toString())); |
| 69 | + |
| 70 | + this.allSchoolsTombstones = getAllSchoolTombstonesYouthPRP(sdcDistrictCollectionID); |
| 71 | + List<UUID> youthPRPSchoolIDs = this.allSchoolsTombstones.stream().map(SchoolTombstone::getSchoolId).map(UUID::fromString).toList(); |
| 72 | + var studentList = sdcSchoolCollectionStudentRepository.getYouthPRPHeadcountsBySdcDistrictCollectionIdGroupBySchoolId(sdcDistrictCollectionEntity.getSdcDistrictCollectionID(), youthPRPSchoolIDs); |
| 73 | + this.prpOrYouthHeadcountList = studentList; |
| 74 | + return generateJasperReport(convertToYouthPRPJSONStringDistrict(studentList, sdcDistrictCollectionEntity), prpOrYouthHeadcountReport, DistrictReportTypeCode.DIS_PRP_OR_YOUTH_SUMMARY.getCode()); |
| 75 | + } catch (JsonProcessingException e) { |
| 76 | + log.error("Exception occurred while writing PDF report for dis prp youth :: " + e.getMessage()); |
| 77 | + throw new StudentDataCollectionAPIRuntimeException("Exception occurred while writing PDF report for dis prp youth :: " + e.getMessage()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private String convertToYouthPRPJSONStringDistrict(List<PRPorYouthHeadcountResult> mappedResults, SdcDistrictCollectionEntity sdcDistrictCollection) throws JsonProcessingException { |
| 82 | + HeadcountNode mainNode = new HeadcountNode(); |
| 83 | + HeadcountReportNode reportNode = new HeadcountReportNode(); |
| 84 | + setReportTombstoneValuesDis(sdcDistrictCollection, reportNode); |
| 85 | + |
| 86 | + var nodeMap = generateNodeMap(Boolean.TRUE); |
| 87 | + |
| 88 | + mappedResults.forEach(PRPorYouthHeadcountResult -> setRowValues(nodeMap, PRPorYouthHeadcountResult)); |
| 89 | + |
| 90 | + reportNode.setPrograms(nodeMap.values().stream().sorted(Comparator.comparing(o -> Integer.parseInt(o.getSequence()))).toList()); |
| 91 | + mainNode.setReport(reportNode); |
| 92 | + return objectWriter.writeValueAsString(mainNode); |
| 93 | + } |
| 94 | + |
| 95 | + protected HashMap<String, HeadcountChildNode> generateNodeMap(boolean includeKH) { |
| 96 | + HashMap<String, HeadcountChildNode> nodeMap = new HashMap<>(); |
| 97 | + Set<String> includedSchoolIDs = new HashSet<>(); |
| 98 | + addValuesForSectionToMap(nodeMap, ALLPRPORYOUTH, "All Youth Or PRP Students", "00", includeKH); |
| 99 | + int sequencePrefix = 10; |
| 100 | + |
| 101 | + if (prpOrYouthHeadcountList != null) { |
| 102 | + for (PRPorYouthHeadcountResult result : prpOrYouthHeadcountList) { |
| 103 | + String schoolID = result.getSchoolID(); |
| 104 | + Optional<SchoolTombstone> schoolOptional = restUtils.getSchoolBySchoolID(schoolID); |
| 105 | + int finalSequencePrefix = sequencePrefix; |
| 106 | + schoolOptional.ifPresent(school -> { |
| 107 | + includedSchoolIDs.add(school.getSchoolId()); |
| 108 | + String schoolTitle = school.getMincode() + " - " + school.getDisplayName(); |
| 109 | + addValuesForSectionToMap(nodeMap, schoolID, schoolTitle, String.valueOf(finalSequencePrefix), includeKH); |
| 110 | + }); |
| 111 | + sequencePrefix += 10; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + for (SchoolTombstone school : allSchoolsTombstones) { |
| 116 | + if (!includedSchoolIDs.contains(school.getSchoolId())) { |
| 117 | + String schoolTitle = school.getMincode() + " - " + school.getDisplayName(); |
| 118 | + addValuesForSectionToMap(nodeMap, school.getSchoolId(), schoolTitle, String.valueOf(sequencePrefix), includeKH); |
| 119 | + sequencePrefix += 10; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return nodeMap; |
| 124 | + } |
| 125 | + |
| 126 | + private void addValuesForSectionToMap(HashMap<String, HeadcountChildNode> nodeMap, String sectionPrefix, String sectionTitle, String sequencePrefix, boolean includeKH){ |
| 127 | + if (Objects.equals(sectionPrefix, ALLPRPORYOUTH)) { |
| 128 | + nodeMap.put(sectionPrefix, new GradeHeadcountChildNode(sectionTitle, "true", sequencePrefix + "0", false, true, true, includeKH)); |
| 129 | + } else { |
| 130 | + nodeMap.put(sectionPrefix, new GradeHeadcountChildNode(sectionTitle, "false", sequencePrefix + "0", false, true, true, includeKH)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + protected void setRowValues(HashMap<String, HeadcountChildNode> nodeMap, PRPorYouthHeadcountResult gradeResult) { |
| 135 | + Optional<SchoolGradeCodes> optionalCode = SchoolGradeCodes.findByValue(gradeResult.getEnrolledGradeCode()); |
| 136 | + var code = optionalCode.orElseThrow(() -> |
| 137 | + new EntityNotFoundException(SchoolGradeCodes.class, "Grade Value", gradeResult.getEnrolledGradeCode())); |
| 138 | + |
| 139 | + GradeHeadcountChildNode allYouthPRPNode = (GradeHeadcountChildNode)nodeMap.get(ALLPRPORYOUTH); |
| 140 | + if (allYouthPRPNode.getValueForGrade(code) == null) { |
| 141 | + allYouthPRPNode.setValueForGrade(code, "0"); |
| 142 | + } |
| 143 | + |
| 144 | + String schoolID = gradeResult.getSchoolID(); |
| 145 | + if (nodeMap.containsKey(schoolID)) { |
| 146 | + ((GradeHeadcountChildNode)nodeMap.get(schoolID)).setValueForGrade(code, gradeResult.getYouthPRPTotals()); |
| 147 | + } else { |
| 148 | + log.warn("School ID {} not found in node map", schoolID); |
| 149 | + } |
| 150 | + |
| 151 | + int currentTotal = Integer.parseInt(gradeResult.getYouthPRPTotals()); |
| 152 | + int accumulatedTotal = Integer.parseInt(allYouthPRPNode.getValueForGrade(code)); |
| 153 | + allYouthPRPNode.setValueForGrade(code, String.valueOf(accumulatedTotal + currentTotal)); |
| 154 | + } |
| 155 | +} |
0 commit comments