|
6 | 6 | import ca.bc.gov.educ.api.batchgraduation.rest.RestUtils; |
7 | 7 | import ca.bc.gov.educ.api.batchgraduation.service.DistributionService; |
8 | 8 | import ca.bc.gov.educ.api.batchgraduation.util.JsonTransformer; |
| 9 | +import ca.bc.gov.educ.api.batchgraduation.util.ThreadLocalStateUtil; |
| 10 | +import com.google.common.collect.Lists; |
9 | 11 | import org.apache.commons.lang3.StringUtils; |
10 | 12 | import org.slf4j.Logger; |
11 | 13 | import org.slf4j.LoggerFactory; |
@@ -71,38 +73,41 @@ private Map<String, ServiceException> updateBackStudentRecords(List<StudentCrede |
71 | 73 | final int totalCredentialCount = cList.size(); |
72 | 74 | List<UUID> studentIDs = cList.stream().map(StudentCredentialDistribution::getStudentID).distinct().toList(); |
73 | 75 | final int totalStudentCount = studentIDs.size(); |
| 76 | + final int partitionSize = 999; |
74 | 77 |
|
75 | | - final int[] processedCount = {1}; |
| 78 | + final int[] processedCount = {0}; |
76 | 79 | // Credential Records |
77 | | - cList.forEach(scd-> { |
78 | | - try { |
| 80 | + if(!cList.isEmpty()) { |
| 81 | + List<List<StudentCredentialDistribution>> studentCredPartitions = Lists.partition(cList, partitionSize); |
| 82 | + studentCredPartitions.forEach(studentCredPartition -> { |
| 83 | + if ("NONGRADYERUN".equalsIgnoreCase(activityCode)) { studentCredPartition.forEach(entry -> entry.setDocumentStatusCode("IP")); } |
79 | 84 | final String accessToken = restUtils.getAccessToken(); |
80 | | - restUtils.updateStudentCredentialRecord(scd.getStudentID(),scd.getCredentialTypeCode(),scd.getPaperType(), |
81 | | - "NONGRADYERUN".equalsIgnoreCase(activityCode)? "IP" : scd.getDocumentStatusCode(),activityCode,accessToken); |
82 | | - LOGGER.debug("Dist Job [{}] / [{}] - update {} of {} student credential record: studentID, credentials, document status [{}, {}, {}]", batchId, activityCode, processedCount[0], totalCredentialCount, scd.getStudentID(), scd.getCredentialTypeCode(), scd.getDocumentStatusCode()); |
83 | | - processedCount[0]++; |
84 | | - } catch (Exception e) { |
85 | | - unprocessedStudents.put(scd.getStudentID().toString(), new ServiceException(e)); |
86 | | - LOGGER.error("Unexpected Error on update {} of {} student credential record: studentID [{}] \n {}", |
87 | | - processedCount[0], totalCredentialCount, scd.getStudentID(), e.getMessage()); |
| 85 | + Integer processed = restUtils.updateStudentCredentialRecords(studentCredPartition, activityCode, accessToken); |
| 86 | + if(studentCredPartition.size() != processed) { |
| 87 | + studentCredPartition.stream().forEach(scd -> unprocessedStudents.put(scd.getStudentID().toString(), new ServiceException("Updating student credential failed."))); |
| 88 | + } |
| 89 | + processedCount[0] = processedCount[0] + processed; |
| 90 | + }); |
| 91 | + LOGGER.debug("Dist Job [{}] / [{}] - updated {} of {} student credential record", batchId, activityCode, processedCount[0], totalCredentialCount); |
| 92 | + if (cList.size() != processedCount[0]) { |
| 93 | + LOGGER.error("Dist Job [{}] / [{}] - Unexpected Error on updating student credential record {} of {} ", |
| 94 | + batchId, activityCode, (totalCredentialCount - processedCount[0]), totalCredentialCount); |
88 | 95 | } |
89 | | - }); |
| 96 | + } |
90 | 97 |
|
91 | | - processedCount[0] = 1; |
92 | 98 | // Unique Students |
93 | | - studentIDs.forEach(uuid-> { |
94 | | - try { |
95 | | - if(!StringUtils.equalsAnyIgnoreCase(jobType, "REGALG", "TVRRUN")) { |
96 | | - restUtils.updateStudentGradRecord(uuid, batchId, activityCode); |
97 | | - LOGGER.debug("Dist Job [{}] / [{}] - update {} of {} student grad record: studentID [{}]", batchId, activityCode, processedCount[0], totalStudentCount, uuid); |
98 | | - } |
99 | | - processedCount[0]++; |
100 | | - } catch (Exception e) { |
101 | | - unprocessedStudents.put(uuid.toString(), new ServiceException(e)); |
102 | | - LOGGER.error("Unexpected Error on create {} of {} audit history: studentID [{}] \n {}", |
103 | | - processedCount[0], totalStudentCount, uuid, e.getMessage()); |
| 99 | + processedCount[0] = 0; |
| 100 | + if(!StringUtils.equalsAnyIgnoreCase(jobType, "REGALG", "TVRRUN") && !studentIDs.isEmpty()) { |
| 101 | + List<List<UUID>> studentIDPartitions = Lists.partition(studentIDs, partitionSize); |
| 102 | + studentIDPartitions.forEach(studentIDPartition -> { |
| 103 | + processedCount[0] = processedCount[0] + restUtils.updateStudentGradRecordHistory(studentIDPartition, batchId, ThreadLocalStateUtil.getCurrentUser(), activityCode); |
| 104 | + }); |
| 105 | + LOGGER.debug("Dist Job [{}] / [{}] - updated {} of {} student grad record & history", batchId, activityCode, processedCount[0], totalStudentCount); |
| 106 | + if(studentIDs.size() != processedCount[0]) { |
| 107 | + LOGGER.error("Dist Job [{}] / [{}] - Unexpected Error on updating {} of {} student grad record & history", |
| 108 | + batchId, activityCode, (totalStudentCount - processedCount[0]), totalStudentCount); |
104 | 109 | } |
105 | | - }); |
| 110 | + } |
106 | 111 | return unprocessedStudents; |
107 | 112 | } |
108 | 113 |
|
|
0 commit comments