Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package uk.nhs.adaptors.gpccmocks.sds;

import static org.springframework.http.HttpStatus.ACCEPTED;
import static org.springframework.http.HttpStatus.OK;

import static uk.nhs.adaptors.gpccmocks.common.ControllerHelpers.getHostAndPortFromRequest;
Expand All @@ -13,7 +12,6 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;
Expand Down
3 changes: 3 additions & 0 deletions service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ dependencies {
implementation "org.springframework.cloud:spring-cloud-starter-gateway"
implementation "ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:8.4.0"
implementation "com.heroku.sdk:env-keystore:1.1.12"
implementation 'com.github.spullara.mustache.java:compiler:0.9.14'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'

implementation "org.apache.commons:commons-lang3:3.19.0"
implementation 'org.jetbrains:annotations:26.0.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
Expand All @@ -46,6 +47,7 @@
import com.github.tomakehurst.wiremock.client.WireMock;

import reactor.core.publisher.Mono;
import uk.nhs.adaptors.gpc.consumer.common.OperationOutcomes;
import uk.nhs.adaptors.gpc.consumer.common.ResourceReader;
import uk.nhs.adaptors.gpc.consumer.gpc.exception.GpConnectException;
import uk.nhs.adaptors.gpc.consumer.sds.configuration.SdsConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.nhs.adaptors.gpc.consumer.common;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class OperationOutcomeModel {
private String fhirCode;
private String spineCode;
private String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package uk.nhs.adaptors.gpc.consumer.common;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

public class OperationOutcomes {
public static final String OPERATION_OUTCOME = "operationOutcome";

public static ResponseEntity<String> internalServerError(String message) {
var model = OperationOutcomeModel.builder()
.fhirCode("exception")
.spineCode("INTERNAL_SERVER_ERROR")
.message(message)
.build();
var body = TemplateUtils.fillTemplate(OPERATION_OUTCOME, model);
return new ResponseEntity<>(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.nhs.adaptors.gpc.consumer.common;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.StringWriter;

@Slf4j
public class TemplateUtils {
private static final String TEMPLATES_RESOURCE_ROOT = "templates";
private static final String TEMPLATES_EXTENSION = ".mustache";
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(TemplateUtils.class);

@SneakyThrows
public static String fillTemplate(String templateName, Object content) {
var template = loadTemplate(templateName + TEMPLATES_EXTENSION);
StringWriter writer = new StringWriter();
template.execute(writer, content).flush();
var output = writer.toString();

log.debug("Template {} with parameters {} produced output\n{}", template, content, output);
return output;
}

private static Mustache loadTemplate(String templateName) {
MustacheFactory mustacheFactory = new DefaultMustacheFactory(TEMPLATES_RESOURCE_ROOT);
return mustacheFactory.compile(templateName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;

Expand All @@ -17,6 +18,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
import uk.nhs.adaptors.gpc.consumer.common.OperationOutcomes;
import uk.nhs.adaptors.gpc.consumer.sds.builder.SdsRequestBuilder;

@Component
Expand Down Expand Up @@ -150,6 +152,18 @@ private Mono<String> performRequest(RequestHeadersSpec<? extends RequestHeadersS
.bodyToMono(String.class);
}

public ResponseEntity<String> callForMigrateStructuredRecordWithOutcome(String fromOdsCode, String correlationId) {
try {
var sdsDeviceRequest = sdsRequestBuilder.buildMigrateStructuredRecordAsDeviceRequest(fromOdsCode, correlationId);
var sdsEndpointRequest = sdsRequestBuilder.buildMigrateStructuredRecordEndpointRequest(fromOdsCode, correlationId);

var data = retrieveData(sdsDeviceRequest, sdsEndpointRequest).block();
return ResponseEntity.ok("Success");
} catch (Exception ex) {
return OperationOutcomes.internalServerError("Internal error: " + ex.getMessage());
}
}

@Builder
@Getter
@EqualsAndHashCode
Expand Down
Loading