Skip to content

Commit

Permalink
Starting updates for rest utils
Browse files Browse the repository at this point in the history
  • Loading branch information
arcshiftsolutions committed Jan 29, 2024
1 parent 749edd7 commit 1f6b2d1
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ca.bc.gov.educ.penreg.api.properties.ApplicationProperties;
import ca.bc.gov.educ.penreg.api.rest.RestUtils;
import ca.bc.gov.educ.penreg.api.service.NotificationService;
import ca.bc.gov.educ.penreg.api.service.PenCoordinatorService;
import ca.bc.gov.educ.penreg.api.service.StudentRegistrationContactService;
import ca.bc.gov.educ.penreg.api.struct.School;
import com.google.common.base.Stopwatch;
import lombok.Getter;
Expand All @@ -36,7 +36,6 @@

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -97,7 +96,7 @@ public class PenRegBatchProcessor {
/**
* The Pen coordinator service.
*/
private final PenCoordinatorService penCoordinatorService;
private final StudentRegistrationContactService penCoordinatorService;

/**
* The Duplicate file check service map.
Expand All @@ -120,7 +119,7 @@ public class PenRegBatchProcessor {
* @param penRequestBatchFileValidator the pen request batch file validator
*/
@Autowired
public PenRegBatchProcessor(final PenRegBatchStudentRecordsProcessor penRegBatchStudentRecordsProcessor, final PenRequestBatchFileService penRequestBatchFileService, final ApplicationProperties applicationProperties, final NotificationService notificationService, final PenCoordinatorService penCoordinatorService, final List<DuplicateFileCheckService> duplicateFileCheckServiceList, final PenRequestBatchFileValidator penRequestBatchFileValidator, final RestUtils restUtils) {
public PenRegBatchProcessor(final PenRegBatchStudentRecordsProcessor penRegBatchStudentRecordsProcessor, final PenRequestBatchFileService penRequestBatchFileService, final ApplicationProperties applicationProperties, final NotificationService notificationService, final StudentRegistrationContactService penCoordinatorService, final List<DuplicateFileCheckService> duplicateFileCheckServiceList, final PenRequestBatchFileValidator penRequestBatchFileValidator, final RestUtils restUtils) {
this.penRegBatchStudentRecordsProcessor = penRegBatchStudentRecordsProcessor;
this.penRequestBatchFileService = penRequestBatchFileService;
this.applicationProperties = applicationProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public abstract class BaseReturnFilesOrchestrator<T> extends BaseOrchestrator<T>
@Getter(PROTECTED)
private final PenRequestBatchService penRequestBatchService;
@Getter(PROTECTED)
private final PenCoordinatorService penCoordinatorService;
private final StudentRegistrationContactService penCoordinatorService;
@Getter(PROTECTED)
private final ResponseFileGeneratorService responseFileGeneratorService;
@Getter(PROTECTED)
Expand All @@ -83,7 +83,7 @@ public abstract class BaseReturnFilesOrchestrator<T> extends BaseOrchestrator<T>
protected BaseReturnFilesOrchestrator(final SagaService sagaService, final MessagePublisher messagePublisher,
final Class<T> clazz, final String sagaName, final String topicToSubscribe,
final PenRequestBatchService penRequestBatchService,
final PenCoordinatorService penCoordinatorService,
final StudentRegistrationContactService penCoordinatorService,
final PenCoordinatorProperties penCoordinatorProperties,
final ResponseFileGeneratorService responseFileGeneratorService,
final PenRequestBatchStudentValidationIssueService penRequestBatchStudentValidationIssueService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class PenRequestBatchArchiveAndReturnOrchestrator extends BaseReturnFiles
*/
public PenRequestBatchArchiveAndReturnOrchestrator(SagaService sagaService, MessagePublisher messagePublisher,
PenRequestBatchService penRequestBatchService,
PenCoordinatorService penCoordinatorService,
StudentRegistrationContactService penCoordinatorService,
PenCoordinatorProperties penCoordinatorProperties,
ResponseFileGeneratorService responseFileGeneratorService,
PenRequestBatchStudentValidationIssueService penRequestBatchStudentValidationIssueService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PenRequestBatchRepostReportsOrchestrator extends BaseReturnFilesOrc
*/
public PenRequestBatchRepostReportsOrchestrator(SagaService sagaService, MessagePublisher messagePublisher,
PenRequestBatchService penRequestBatchService,
PenCoordinatorService penCoordinatorService,
StudentRegistrationContactService penCoordinatorService,
PenCoordinatorProperties penCoordinatorProperties,
ResponseFileGeneratorService responseFileGeneratorService,
PenRequestBatchStudentValidationIssueService penRequestBatchStudentValidationIssueService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class ApplicationProperties {
@Value("${url.api.student}")
private String studentApiURL;

@Value("${url.api.institute}")
private String instituteApiUrl;

/**
* Amount of time that can elapse before a duplicate request is not considered a repeat
*/
Expand Down
114 changes: 73 additions & 41 deletions api/src/main/java/ca/bc/gov/educ/penreg/api/rest/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import ca.bc.gov.educ.penreg.api.constants.EventOutcome;
import ca.bc.gov.educ.penreg.api.constants.EventType;
import ca.bc.gov.educ.penreg.api.constants.SagaTopicsEnum;
import ca.bc.gov.educ.penreg.api.exception.PenRegAPIRuntimeException;
import ca.bc.gov.educ.penreg.api.messaging.MessagePublisher;
import ca.bc.gov.educ.penreg.api.properties.ApplicationProperties;
import ca.bc.gov.educ.penreg.api.struct.Event;
import ca.bc.gov.educ.penreg.api.struct.School;
import ca.bc.gov.educ.penreg.api.struct.Student;
import ca.bc.gov.educ.penreg.api.struct.*;
import ca.bc.gov.educ.penreg.api.struct.v1.GradeCode;
import ca.bc.gov.educ.penreg.api.struct.v1.PenCoordinator;
import ca.bc.gov.educ.penreg.api.struct.v1.PenRequestBatchStudentValidationIssueFieldCode;
Expand All @@ -27,10 +26,12 @@
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import org.springframework.web.util.UriComponentsBuilder;
import reactor.core.publisher.Mono;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -144,7 +145,7 @@ public void populateSchoolMap() {
try {
writeLock.lock();
for (val school : this.getSchools()) {
this.schoolMap.put(school.getDistNo() + school.getSchlNo(), school);
this.schoolMap.put(school.getMincode(), school);
}
}
catch (Exception ex) {
Expand Down Expand Up @@ -188,14 +189,14 @@ public void populatePenRequestBatchStudentValidationIssueFieldCodeMap() {
* @return the schools
*/
public List<School> getSchools() {
log.info("calling school api to load schools to memory");
log.info("Calling Institute api to get list of schools");
return this.webClient.get()
.uri(this.props.getSchoolApiURL())
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(School.class)
.collectList()
.block();
.uri(this.props.getInstituteApiUrl() + "/school")
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(School.class)
.collectList()
.block();
}

/**
Expand All @@ -206,12 +207,12 @@ public List<School> getSchools() {
public List<PenRequestBatchStudentValidationIssueTypeCode> getPenRequestBatchStudentValidationIssueTypeCodes() {
log.info("calling pen service api to load penRequestBatchStudentValidationIssueTypeCodes to memory");
return this.webClient.get()
.uri(this.props.getPenServicesApiURL() + "/api/v1/pen-services/validation/issue-type-code")
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(PenRequestBatchStudentValidationIssueTypeCode.class)
.collectList()
.block();
.uri(this.props.getPenServicesApiURL() + "/api/v1/pen-services/validation/issue-type-code")
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(PenRequestBatchStudentValidationIssueTypeCode.class)
.collectList()
.block();
}

/**
Expand Down Expand Up @@ -252,11 +253,11 @@ public void scheduled() {
*/
public Student getStudentByStudentID(final String studentID) {
return this.webClient.get()
.uri(this.props.getStudentApiURL(), uri -> uri.path("/{studentID}").build(studentID))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToMono(Student.class)
.block();
.uri(this.props.getStudentApiURL(), uri -> uri.path("/{studentID}").build(studentID))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToMono(Student.class)
.block();
}

/**
Expand All @@ -266,12 +267,12 @@ public Student getStudentByStudentID(final String studentID) {
*/
public void updateStudent(final Student studentFromStudentAPI) {
this.webClient.put()
.uri(this.props.getStudentApiURL(), uri -> uri.path("/{studentID}").build(studentFromStudentAPI.getStudentID()))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just(studentFromStudentAPI), Student.class)
.retrieve()
.bodyToMono(Student.class)
.block();
.uri(this.props.getStudentApiURL(), uri -> uri.path("/{studentID}").build(studentFromStudentAPI.getStudentID()))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just(studentFromStudentAPI), Student.class)
.retrieve()
.bodyToMono(Student.class)
.block();
}


Expand All @@ -283,12 +284,12 @@ public void updateStudent(final Student studentFromStudentAPI) {
*/
public Student createStudent(final Student student) {
return this.webClient.post()
.uri(this.props.getStudentApiURL())
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just(student), Student.class)
.retrieve()
.bodyToMono(Student.class)
.block();
.uri(this.props.getStudentApiURL())
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.body(Mono.just(student), Student.class)
.retrieve()
.bodyToMono(Student.class)
.block();
}

/**
Expand Down Expand Up @@ -369,7 +370,7 @@ public Optional<PenRequestBatchStudentValidationIssueFieldCode> getPenRequestBat
public List<Student> getStudentsByStudentIDs(final List<UUID> studentIDs) throws IOException, ExecutionException, InterruptedException, TimeoutException {
final var event = Event.builder().sagaId(UUID.randomUUID()).eventType(EventType.GET_STUDENTS).eventPayload(JsonUtil.getJsonStringFromObject(studentIDs)).build();
val responseEvent = JsonUtil.getJsonObjectFromByteArray(Event.class,
this.messagePublisher.requestMessage(STUDENT_API_TOPIC.toString(), JsonUtil.getJsonString(event).orElseThrow().getBytes(StandardCharsets.UTF_8)).get(30, TimeUnit.SECONDS).getData());
this.messagePublisher.requestMessage(STUDENT_API_TOPIC.toString(), JsonUtil.getJsonString(event).orElseThrow().getBytes(StandardCharsets.UTF_8)).get(30, TimeUnit.SECONDS).getData());
if (responseEvent.getEventOutcome() == EventOutcome.STUDENT_NOT_FOUND) {
return Collections.emptyList();
}
Expand All @@ -380,11 +381,11 @@ public List<Student> getStudentsByStudentIDs(final List<UUID> studentIDs) throws
public Optional<PenCoordinator> getPenCoordinator(final String mincode) {
try {
final var response = this.webClient.get()
.uri(this.props.getSchoolApiURL(), uri -> uri.path("/{mincode}/pen-coordinator").build(mincode))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToMono(PenCoordinator.class)
.block();
.uri(this.props.getSchoolApiURL(), uri -> uri.path("/{mincode}/pen-coordinator").build(mincode))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToMono(PenCoordinator.class)
.block();
log.info("record found for :: {}", mincode);
return Optional.ofNullable(response);
} catch (final WebClientResponseException ex) {
Expand All @@ -397,6 +398,37 @@ public Optional<PenCoordinator> getPenCoordinator(final String mincode) {
}
}

public List<SchoolContact> getStudentRegistrationContactList(final String mincode) {
try {
var school = schoolMap.get(mincode);
if(school == null){
return new ArrayList<>();
}
log.info("Calling Institute api to get list of school student registration contacts");
String criterion = "[{\"searchCriteriaList\":[{\"key\":\"schoolContactTypeCode\",\"operation\":\"eq\",\"value\":\"STUDREGIS\",\"valueType\":\"STRING\",\"condition\":\"AND\"}]}," +
" {\"key\":\"schoolId\",\"operation\":\"eq\",\"value\":\"" + school.getSchoolId() + "\",\"valueType\":\"UUID\",\"condition\":\"AND\"}]}," +
" {\"key\":\"email\",\"operation\":\"neq\",\"value\":\"null\",\"valueType\":\"UUID\",\"condition\":\"AND\"}]}";
SchoolContactSearchWrapper schoolContactSearchWrapper = this.webClient.get()
.uri(getSchoolContactURI(criterion))
.header(CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.retrieve()
.bodyToFlux(SchoolContactSearchWrapper.class)
.blockFirst();

return schoolContactSearchWrapper.getContent();
}catch(Exception e){
log.error("API call to Institute API failure getting student registration contacts :: {}", e.getMessage());
throw new PenRegAPIRuntimeException("API call to Institute API failure getting student registration contacts, contact the Ministry for more info.");
}
}

private URI getSchoolContactURI(String criterion){
return UriComponentsBuilder.fromHttpUrl(this.props.getInstituteApiUrl() + "/school/contact/paginated")
.queryParam("pageNumber", "0")
.queryParam("pageSize", "10000")
.queryParam("searchCriteriaList", criterion).build().toUri();
}

public Optional<Event> requestEventResponseFromServicesAPI(final Event event) {
return this.requestEventResponseFromAPI(event, PEN_SERVICES_API_TOPIC, "Exception while calling services api via nats");
}
Expand All @@ -416,7 +448,7 @@ private Optional<Event> requestEventResponseFromAPI(final Event event, final Sag
try {
log.info("calling :: {} via NATS", topic);
val response = JsonUtil.getJsonObjectFromByteArray(Event.class,
this.messagePublisher.requestMessage(topic.toString(), JsonUtil.getJsonString(event).orElseThrow().getBytes(StandardCharsets.UTF_8)).get(30, TimeUnit.SECONDS).getData());
this.messagePublisher.requestMessage(topic.toString(), JsonUtil.getJsonString(event).orElseThrow().getBytes(StandardCharsets.UTF_8)).get(30, TimeUnit.SECONDS).getData());
log.info("got response from NATS :: {}", response.getEventOutcome());
return Optional.of(response);
} catch (final Exception e) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ResponseFileGeneratorService {
* the pen coordinator service
*/
@Getter(PRIVATE)
private final PenCoordinatorService penCoordinatorService;
private final StudentRegistrationContactService penCoordinatorService;

/**
* the pen request batch student repository
Expand All @@ -75,7 +75,7 @@ public class ResponseFileGeneratorService {
private SpringTemplateEngine templateEngine;

@Autowired
public ResponseFileGeneratorService(final PenCoordinatorService penCoordinatorService, final PenWebBlobRepository penWebBlobRepository, final PenRequestBatchStudentRepository penRequestBatchStudentRepository, final RestUtils restUtils, final SpringTemplateEngine templateEngine) {
public ResponseFileGeneratorService(final StudentRegistrationContactService penCoordinatorService, final PenWebBlobRepository penWebBlobRepository, final PenRequestBatchStudentRepository penRequestBatchStudentRepository, final RestUtils restUtils, final SpringTemplateEngine templateEngine) {
this.penCoordinatorService = penCoordinatorService;
this.penWebBlobRepository = penWebBlobRepository;
this.penRequestBatchStudentRepository = penRequestBatchStudentRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ca.bc.gov.educ.penreg.api.service;

import ca.bc.gov.educ.penreg.api.rest.RestUtils;
import ca.bc.gov.educ.penreg.api.struct.SchoolContact;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class StudentRegistrationContactService {
private final RestUtils restUtils;

@Autowired
public StudentRegistrationContactService(final RestUtils restUtils) {
this.restUtils = restUtils;
}

public List<SchoolContact> getStudentRegistrationContactsByMincode(final String mincode) {
if (StringUtils.length(mincode) != 8 || !StringUtils.isNumeric(mincode)) {
return new ArrayList<>();
}

return this.restUtils.getStudentRegistrationContactList(mincode);
}

public List<String> getStudentRegistrationContactEmailsByMincode(final String mincode) {
log.debug("getting pen coordinator email for mincode :: {}", mincode);
if (StringUtils.length(mincode) != 8 || !StringUtils.isNumeric(mincode)) {
return new ArrayList<>();
}
var contacts = this.restUtils.getStudentRegistrationContactList(mincode);
return contacts.stream()
.map(contact -> String.valueOf(contact.getEmail()))
.toList();
}
}
Loading

0 comments on commit 1f6b2d1

Please sign in to comment.