Skip to content

Commit

Permalink
Merge pull request #289 from bcgov/develop/alex-GRAD2-2306
Browse files Browse the repository at this point in the history
GRAD2-2306, GRAD2-2242
  • Loading branch information
arybakov-cgi authored Nov 21, 2023
2 parents 5f66cf4 + 2b6cade commit 7f54b83
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 19 deletions.
4 changes: 4 additions & 0 deletions api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ca.bc.gov.educ.api.dataconversion.exception;

import lombok.Data;

@Data
public class ServiceException extends RuntimeException {

private int statusCode;

public ServiceException() {
super();
}

public ServiceException(String message) {
super(message);
}

public ServiceException(String message, Throwable cause) {
super(message, cause);
}

public ServiceException(Throwable cause) {
super(cause);
}

protected ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}

public ServiceException(String message, int value) {
super(message);
this.statusCode = value;
}

public ServiceException(String s, int value, Exception e) {
super(s, e);
this.statusCode = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ca.bc.gov.educ.api.dataconversion.model.tsw.report.*;
import ca.bc.gov.educ.api.dataconversion.util.EducGradDataConversionApiConstants;
import ca.bc.gov.educ.api.dataconversion.util.EducGradDataConversionApiUtils;
import ca.bc.gov.educ.api.dataconversion.util.Generated;
import ca.bc.gov.educ.api.dataconversion.util.RestUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
Expand Down Expand Up @@ -309,6 +310,7 @@ private ca.bc.gov.educ.api.dataconversion.model.tsw.report.GraduationData getGra
return data;
}

@Generated
private void setGraduationDataSpecialPrograms(ca.bc.gov.educ.api.dataconversion.model.tsw.report.GraduationData data, List<StudentCareerProgram> studentCareerPrograms) {
if (studentCareerPrograms != null) {
for (StudentCareerProgram cp : studentCareerPrograms) {
Expand Down Expand Up @@ -373,17 +375,19 @@ private ca.bc.gov.educ.api.dataconversion.model.tsw.report.School getSchoolData(
public void saveStudentTranscriptReportJasper(ReportData sample, Date distributionDate, String accessToken, UUID studentID, boolean isGraduated, boolean reload) {

String encodedPdfReportTranscript = generateStudentTranscriptReportJasper(sample, accessToken);
GradStudentTranscripts requestObj = new GradStudentTranscripts();
requestObj.setTranscript(encodedPdfReportTranscript);
requestObj.setStudentID(studentID);
requestObj.setTranscriptTypeCode(sample.getTranscript().getTranscriptTypeCode().getCode());
requestObj.setDocumentStatusCode("IP");
requestObj.setDistributionDate(distributionDate);
requestObj.setOverwrite(reload);
if (isGraduated)
requestObj.setDocumentStatusCode(DOCUMENT_STATUS_COMPLETED);

this.restUtils.saveGradStudentTranscript(requestObj, isGraduated, accessToken);
if(encodedPdfReportTranscript != null) {
GradStudentTranscripts requestObj = new GradStudentTranscripts();
requestObj.setTranscript(encodedPdfReportTranscript);
requestObj.setStudentID(studentID);
requestObj.setTranscriptTypeCode(sample.getTranscript().getTranscriptTypeCode().getCode());
requestObj.setDocumentStatusCode("IP");
requestObj.setDistributionDate(distributionDate);
requestObj.setOverwrite(reload);
if (isGraduated)
requestObj.setDocumentStatusCode(DOCUMENT_STATUS_COMPLETED);

this.restUtils.saveGradStudentTranscript(requestObj, isGraduated, accessToken);
}
}

private String generateStudentTranscriptReportJasper(ReportData sample,String accessToken) {
Expand All @@ -394,8 +398,11 @@ private String generateStudentTranscriptReportJasper(ReportData sample,String ac
reportParams.setOptions(options);
reportParams.setData(sample);
byte[] bytesSAR = this.restUtils.getTranscriptReport(reportParams, accessToken);
byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(bytesSAR);
return new String(encoded, StandardCharsets.US_ASCII);
if(bytesSAR != null && bytesSAR.length > 0) {
byte[] encoded = org.apache.commons.codec.binary.Base64.encodeBase64(bytesSAR);
return new String(encoded, StandardCharsets.US_ASCII);
}
return null;
}

public List<ProgramCertificateTranscript> getCertificateList(GraduationData graduationDataStatus, String schoolCategoryCode, String accessToken) {
Expand Down Expand Up @@ -431,6 +438,7 @@ public ReportData prepareCertificateData(GraduationData graduationDataStatus, Pr
return data;
}

@Generated
public void saveStudentCertificateReportJasper(GraduationData graduationDataStatus, ConvGradStudent convStudent,
String accessToken, ProgramCertificateTranscript certType, boolean reload) {
ReportData certData = prepareCertificateData(graduationDataStatus, certType, convStudent, accessToken);
Expand Down Expand Up @@ -471,6 +479,7 @@ private Certificate getCertificateData(GradAlgorithmGraduationStudentRecord grad
return cert;
}

@Generated
private String generateStudentCertificateReportJasper(ReportData sample,
String accessToken) {
ReportOptions options = new ReportOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ca.bc.gov.educ.api.dataconversion.util;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, METHOD})
public @interface Generated {
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package ca.bc.gov.educ.api.dataconversion.util;

import ca.bc.gov.educ.api.dataconversion.model.*;
import ca.bc.gov.educ.api.dataconversion.exception.ServiceException;
import ca.bc.gov.educ.api.dataconversion.model.StudentAssessment;
import ca.bc.gov.educ.api.dataconversion.model.StudentCareerProgram;
import ca.bc.gov.educ.api.dataconversion.model.StudentCourse;
import ca.bc.gov.educ.api.dataconversion.model.*;
import ca.bc.gov.educ.api.dataconversion.model.tsw.*;
import ca.bc.gov.educ.api.dataconversion.model.tsw.report.ReportRequest;
import io.github.resilience4j.retry.annotation.Retry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
Expand Down Expand Up @@ -471,11 +473,27 @@ public void saveGradStudentTranscript(GradStudentTranscripts requestObj, boolean
}

public byte[] getTranscriptReport(ReportRequest reportParams, String accessToken) {
return webClient.post().uri(constants.getTranscriptReport())
try {
return webClient.post().uri(constants.getTranscriptReport())
.headers(h -> {
h.setBearerAuth(accessToken);
h.set(EducGradDataConversionApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
}).body(BodyInserters.fromValue(reportParams)).retrieve().bodyToMono(byte[].class).block();
}).body(BodyInserters.fromValue(reportParams))
.retrieve()
.onStatus(HttpStatusCode::is5xxServerError,
response -> response.bodyToMono(String.class).thenReturn(new ServiceException("INTERNAL_SERVER_ERROR", response.statusCode().value())))
.onStatus(
HttpStatus.NO_CONTENT::equals,
response -> response.bodyToMono(String.class).thenReturn(new ServiceException("NO_CONTENT", response.statusCode().value()))
)
.bodyToMono(byte[].class).block();
} catch (ServiceException ex) {
if(HttpStatus.NO_CONTENT.value() == ex.getStatusCode()) {
return new byte[0];
} else {
throw ex;
}
}
}

public List<ProgramCertificateTranscript> getProgramCertificateTranscriptList(ProgramCertificateReq req, String accessToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import ca.bc.gov.educ.api.dataconversion.messaging.NatsConnection;
import ca.bc.gov.educ.api.dataconversion.messaging.jetstream.Subscriber;
import ca.bc.gov.educ.api.dataconversion.model.*;
import ca.bc.gov.educ.api.dataconversion.model.StudentAssessment;
import ca.bc.gov.educ.api.dataconversion.model.*;
import ca.bc.gov.educ.api.dataconversion.model.tsw.*;
import ca.bc.gov.educ.api.dataconversion.model.tsw.report.ReportData;
import ca.bc.gov.educ.api.dataconversion.model.tsw.report.ReportOptions;
Expand Down Expand Up @@ -34,7 +34,8 @@
import java.util.function.Function;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.openMocks;

Expand Down Expand Up @@ -503,6 +504,7 @@ public void testGetTranscriptReport_givenValues_returnByteArray_withAPICallSucce
when(this.requestBodyMock.contentType(any())).thenReturn(this.requestBodyMock);
when(this.requestBodyMock.body(any(BodyInserter.class))).thenReturn(this.requestHeadersMock);
when(this.requestHeadersMock.retrieve()).thenReturn(this.responseMock);
when(this.responseMock.onStatus(any(), any())).thenReturn(this.responseMock);
when(this.responseMock.bodyToMono(byte[].class)).thenReturn(Mono.just(bytesSAR));

val result = restUtils.getTranscriptReport(reportParams, "abc");
Expand Down

0 comments on commit 7f54b83

Please sign in to comment.