-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/grad2 3060 - gdc messaging (#701)
* start of gdc messaging * gdc messaging * use grad student service * clean * extend base integration test * required fields for grad student record payload * clean * test
- Loading branch information
1 parent
b3301c0
commit 698092f
Showing
13 changed files
with
194 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
.../ca/bc/gov/educ/api/gradstudent/messaging/jetstream/FetchGradStudentRecordSubscriber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package ca.bc.gov.educ.api.gradstudent.messaging.jetstream; | ||
|
||
import ca.bc.gov.educ.api.gradstudent.constant.Topics; | ||
import ca.bc.gov.educ.api.gradstudent.exception.EntityNotFoundException; | ||
import ca.bc.gov.educ.api.gradstudent.model.dc.Event; | ||
import ca.bc.gov.educ.api.gradstudent.model.dc.GradStatusPayload; | ||
import ca.bc.gov.educ.api.gradstudent.model.dc.GradStudentRecordPayload; | ||
import ca.bc.gov.educ.api.gradstudent.model.dto.messaging.GradStudentRecord; | ||
import ca.bc.gov.educ.api.gradstudent.service.GradStudentService; | ||
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants; | ||
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiUtils; | ||
import ca.bc.gov.educ.api.gradstudent.util.JsonUtil; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import io.nats.client.*; | ||
import lombok.val; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import java.util.UUID; | ||
|
||
@Component | ||
public class FetchGradStudentRecordSubscriber implements MessageHandler { | ||
|
||
private final Connection natsConnection; | ||
private Dispatcher dispatcher; | ||
private final GradStudentService gradStudentService; | ||
public static final String RESPONDING_BACK_TO_NATS_ON_CHANNEL = "responding back to NATS on {} channel "; | ||
public static final String PAYLOAD_LOG = "payload is :: {}"; | ||
|
||
|
||
|
||
private static final String TOPIC = Topics.GRAD_STUDENT_API_FETCH_GRAD_STUDENT_TOPIC.toString(); | ||
private static final Logger log = LoggerFactory.getLogger(FetchGradStudentRecordSubscriber.class); | ||
|
||
@Autowired | ||
public FetchGradStudentRecordSubscriber(final Connection natsConnection, GradStudentService gradStudentService, EducGradStudentApiConstants constants) { | ||
this.natsConnection = natsConnection; | ||
this.gradStudentService = gradStudentService; | ||
} | ||
|
||
@PostConstruct | ||
public void subscribe() { | ||
this.dispatcher = this.natsConnection.createDispatcher(this); | ||
this.dispatcher.subscribe(TOPIC); | ||
} | ||
|
||
@Override | ||
public void onMessage(Message message) { | ||
val eventString = new String(message.getData()); | ||
log.debug("Received message: {}", eventString); | ||
String response; | ||
|
||
try { | ||
Event event = JsonUtil.getJsonObjectFromString(Event.class, eventString); | ||
log.info("received GET_STUDENT event :: {}", event.getSagaId()); | ||
log.trace(PAYLOAD_LOG, event.getEventPayload()); | ||
UUID studentId = JsonUtil.getJsonObjectFromString(UUID.class, event.getEventPayload()); | ||
GradStudentRecord studentRecord = gradStudentService.getGraduationStudentRecord(studentId); | ||
response = getResponse(studentRecord); | ||
log.info(RESPONDING_BACK_TO_NATS_ON_CHANNEL, message.getReplyTo() != null ? message.getReplyTo() : event.getReplyTo()); | ||
this.natsConnection.publish(message.getReplyTo(), response.getBytes()); | ||
} catch (Exception e) { | ||
log.error("Error while processing GET_STUDENT event", e); | ||
} | ||
} | ||
|
||
private String getResponse(GradStudentRecord studentRecord) throws JsonProcessingException { | ||
GradStudentRecordPayload gradStudentRecordPayload = GradStudentRecordPayload.builder() | ||
.program(studentRecord.getProgram()) | ||
.programCompletionDate(studentRecord.getProgramCompletionDate() != null ? EducGradStudentApiUtils.formatDate(studentRecord.getProgramCompletionDate()) : null) | ||
.schoolOfRecord(studentRecord.getSchoolOfRecord()) | ||
.build(); | ||
return JsonUtil.getJsonStringFromObject(gradStudentRecordPayload); | ||
} | ||
|
||
private String getErrorResponse(Exception e) { | ||
String ex = (e instanceof EntityNotFoundException) ? "not found" : "error"; | ||
GradStatusPayload gradStatusPayload = GradStatusPayload.builder() | ||
.exception(ex) | ||
.build(); | ||
try { | ||
return JsonUtil.getJsonStringFromObject(gradStatusPayload); | ||
} catch (JsonProcessingException exc) { | ||
log.error("Error while serializing error response", exc); | ||
return "{\"program\": \"\", \"programCompletionDate\": \"\", \"schoolOfRecord\": \"\", \"exception\": \"JSON Parsing exception\"}"; | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dc/GradStudentRecordPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package ca.bc.gov.educ.api.gradstudent.model.dc; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class GradStudentRecordPayload { | ||
|
||
private String exception; | ||
private String program; | ||
private String programCompletionDate; | ||
private String schoolOfRecord; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
api/src/main/java/ca/bc/gov/educ/api/gradstudent/model/dto/messaging/GradStudentRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ca.bc.gov.educ.api.gradstudent.model.dto.messaging; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@EqualsAndHashCode(callSuper = false) | ||
public class GradStudentRecord { | ||
|
||
private UUID studentID; | ||
private String program; | ||
private Date programCompletionDate; | ||
private String schoolOfRecord; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters