Skip to content

Commit

Permalink
[PRMT-3744] WIP Start working with ebean to connect to postgres db
Browse files Browse the repository at this point in the history
  • Loading branch information
joefong-nhs committed Aug 10, 2023
1 parent 9f56ea8 commit ed7d3d4
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 49 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ plugins {
id 'org.owasp.dependencycheck' version '7.4.4'
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'io.ebean' version '13.6.5'
}

group 'uk.nhs.prm'
Expand Down Expand Up @@ -53,6 +54,10 @@ dependencies {

// Data Access
implementation 'software.amazon.awssdk:dynamodb-enhanced:2.20.121'
implementation 'org.postgresql:postgresql:42.6.0'
implementation 'io.ebean:ebean:13.6.5'

annotationProcessor 'io.ebean:querybean-generator:13.6.5'

compileOnly 'org.projectlombok:lombok'

Expand All @@ -62,6 +67,7 @@ dependencies {

annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor 'io.ebean:querybean-generator:13.6.5'

testAnnotationProcessor 'org.projectlombok:lombok'

Expand All @@ -71,6 +77,7 @@ dependencies {
testImplementation 'org.xmlunit:xmlunit-core:2.9.1'
testImplementation 'org.xmlunit:xmlunit-matchers:2.9.1'
testImplementation 'com.swiftmq:swiftmq-client:12.5.4'
testImplementation 'io.ebean:ebean-test:13.6.5'
}

tasks.withType(Test).configureEach {
Expand All @@ -87,6 +94,13 @@ configurations {
}

dependencies {


implementation 'io.ebean:ebean-gradle-plugin:11.39.1'
implementation 'io.ebean:ebean-gradle-plugin:11.39.1'
implementation 'io.ebean:ebean-gradle-plugin:11.39.1'
implementation 'io.ebean:ebean-api:13.20.1-jakarta'
implementation 'io.ebean:ebean-postgres:13.20.1-jakarta'
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
groovyScript localGroovy()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package uk.nhs.prm.e2etests.configuration;

import io.ebean.Database;
import io.ebean.DatabaseFactory;
import io.ebean.config.DatabaseConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import uk.nhs.prm.e2etests.property.PostgresDbProperties;

import java.util.Properties;

@Configuration
public class EBeanConfiguration {
private final PostgresDbProperties databaseProperties;

public EBeanConfiguration(
PostgresDbProperties databaseProperties
) {
this.databaseProperties = databaseProperties;
}

// /**
// * Simple Spring bean factory for creating the EbeanServer.
// */
// public class MyEbeanServerFactory implements FactoryBean<EbeanServer> {
//
// public EbeanServer getObject() throws Exception {
//
// return createEbeanServer();
// }
//
// public Class<?> getObjectType() {
// return EbeanServer.class;
// }
//
// public boolean isSingleton() {
// return true;
// }
//
// /**
// * Create a EbeanServer instance.
// */
// private EbeanServer createEbeanServer() {
//
// ServerConfig config = new ServerConfig();
// config.setName("pg");
//
// // load configuration from ebean.properties
// config.loadFromProperties();
// config.setDefaultServer(true);
// ...
// // other programmatic configuration
//
// return EbeanServerFactory.create(config);
// }
// }




@Bean
public Database ehrOutDatabase() {
DatabaseConfig configuration = new DatabaseConfig();

Properties properties = new Properties();
properties.put("ebean.db.ddl.generate", "true");
properties.put("ebean.db.ddl.run", "true");
properties.put("datasource.db.databaseDriver", "org.postgresql.Driver");
properties.put("datasource.db.username", databaseProperties.getEhrOutDatabaseUsername());
properties.put("datasource.db.password", databaseProperties.getEhrOutDatabasePassword());
properties.put("datasource.db.databaseUrl", String.format("jdbc:postgresql://%s/%s",
databaseProperties.getEhrOutDatabaseHost(),
databaseProperties.getEhrOutDatabaseName()));

configuration.loadFromProperties(properties);
return DatabaseFactory.create(configuration);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.nhs.prm.e2etests.exception;

public class NotFoundException extends RuntimeException {
private static final String MESSAGE = "A record could not be found with ID: %s";

public NotFoundException(String id) {
super(String.format(MESSAGE, id));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.nhs.prm.e2etests.model.database;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import java.time.LocalDateTime;
import java.util.UUID;
import lombok.Getter;

@Entity
@Getter
public class Acknowledgement {
@Id
private UUID messageId;
private String acknowledgementTypeCode;
private String acknowledgementDetail;
private String service;
private String referencedMessageId;
private String messageRef;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Getter
@Component
public class DatabaseProperties {
public class DynamoDbProperties {
@Value("${aws.configuration.databaseNames.transferTrackerDb}")
private String transferTrackerDbName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@Component
public class EhrRepositoryProperties extends AbstractSsmRetriever {
public class EhrRepositoryProperties {

@Value("${nhs.services.gp2gp.odsCodes.repoDev}")
private String repoDevOdsCode;
Expand All @@ -28,16 +28,16 @@ public class EhrRepositoryProperties extends AbstractSsmRetriever {
@Value("${aws.configuration.serviceUrls.ehrRepository}")
private String ehrRepositoryUrl;

private final SsmService ssmService;

@Autowired
public EhrRepositoryProperties(SsmService ssmService) {
super(ssmService);
}
public EhrRepositoryProperties(SsmService ssmService) { this.ssmService = ssmService; }

public String getLiveTestApiKey() {
return super.getAwsSsmParameterValue(this.liveTestApiKey);
return this.ssmService.getSsmParameterValue(this.liveTestApiKey);
}

public String getE2eTestApiKey() {
return super.getAwsSsmParameterValue(this.e2eTestApiKey);
return this.ssmService.getSsmParameterValue(this.e2eTestApiKey);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Getter
@Component
public class Gp2gpMessengerProperties extends AbstractSsmRetriever {
public class Gp2gpMessengerProperties {
@Value("${nhs.services.gp2gp.odsCodes.tppPtlInt}")
private String tppPtlIntOdsCode;

Expand All @@ -23,12 +23,14 @@ public class Gp2gpMessengerProperties extends AbstractSsmRetriever {
@Value("${aws.configuration.serviceUrls.gp2GpMessenger}")
private String gp2gpMessengerUrl;

private final SsmService ssmService;

@Autowired
public Gp2gpMessengerProperties(SsmService ssmService) {
super(ssmService);
this.ssmService = ssmService;
}

public String getLiveTestApiKey() {
return super.getAwsSsmParameterValue(this.liveTestApiKey);
return this.ssmService.getSsmParameterValue(this.liveTestApiKey);
}
}
14 changes: 8 additions & 6 deletions src/main/java/uk/nhs/prm/e2etests/property/MeshProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uk.nhs.prm.e2etests.service.SsmService;

@Component
public class MeshProperties extends AbstractSsmRetriever {
public class MeshProperties {
@Value("${aws.configuration.ssm.parameters.mesh.mailboxId}")
private String mailboxId;

Expand All @@ -21,25 +21,27 @@ public class MeshProperties extends AbstractSsmRetriever {

private static final String MAILBOX_SERVICE_OUTBOX_URL = "https://msg.intspineservices.nhs.uk/messageexchange/%s/outbox";

private final SsmService ssmService;

@Autowired
public MeshProperties(SsmService ssmService) {
super(ssmService);
this.ssmService = ssmService;
}

public String getMailboxId() {
return super.getAwsSsmParameterValue(this.mailboxId);
return ssmService.getSsmParameterValue(this.mailboxId);
}

public String getClientCert() {
return super.getAwsSsmParameterValue(this.clientCert);
return ssmService.getSsmParameterValue(this.clientCert);
}

public String getClientKey() {
return super.getAwsSsmParameterValue(this.clientKey);
return ssmService.getSsmParameterValue(this.clientKey);
}

public String getMailboxPassword() {
return super.getAwsSsmParameterValue(this.mailboxPassword);
return ssmService.getSsmParameterValue(this.mailboxPassword);
}

public String getMailboxServiceOutboxUrl() {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/uk/nhs/prm/e2etests/property/NhsProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.nhs.prm.e2etests.property;

import com.amazonaws.services.dynamodbv2.xspec.SS;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -11,7 +12,7 @@

@Getter
@Component
public class NhsProperties extends AbstractSsmRetriever {
public class NhsProperties {
@Value("${nhs.environment}")
private String nhsEnvironment;

Expand All @@ -27,17 +28,19 @@ public class NhsProperties extends AbstractSsmRetriever {
@Value("${nhs.syntheticPatient.nhsNumberPrefix.nonProd}")
private String syntheticPatientNhsNumberPrefixNonProd;

private final SsmService ssmService;

@Autowired
public NhsProperties(SsmService ssmService) {
super(ssmService);
this.ssmService = ssmService;
}

public String getRepoOdsCode() {
return super.getAwsSsmParameterValue(repoOdsCode);
return ssmService.getSsmParameterValue(repoOdsCode);
}

public List<String> getSafeListedPatientList() {
String rawSafeListedPatientListStringFromSsm = super.getAwsSsmParameterValue(safeListedPatients);
String rawSafeListedPatientListStringFromSsm = ssmService.getSsmParameterValue(safeListedPatients);
return Arrays.asList(rawSafeListedPatientListStringFromSsm.split(","));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import uk.nhs.prm.e2etests.service.SsmService;

@Component
public class PdsAdaptorProperties extends AbstractSsmRetriever {
public class PdsAdaptorProperties {
@Value("${aws.configuration.ssm.parameters.pdsAdaptor.performanceApiKey}")
private String performanceApiKey;

Expand All @@ -21,21 +21,22 @@ public class PdsAdaptorProperties extends AbstractSsmRetriever {
@Value("${aws.configuration.serviceUrls.pdsAdaptor}")
private String pdsAdaptorUrl;

private final SsmService ssmService;
@Autowired
public PdsAdaptorProperties(SsmService ssmService) {
super(ssmService);
this.ssmService = ssmService;
}

public String getPerformanceApiKey() {
return super.getAwsSsmParameterValue(this.performanceApiKey);
return ssmService.getSsmParameterValue(this.performanceApiKey);
}

public String getLiveTestApiKey() {
return super.getAwsSsmParameterValue(this.liveTestApiKey);
return ssmService.getSsmParameterValue(this.liveTestApiKey);
}

public String getE2eTestApiKey() {
return super.getAwsSsmParameterValue(this.e2eTestApiKey);
return ssmService.getSsmParameterValue(this.e2eTestApiKey);
}

}
Loading

0 comments on commit ed7d3d4

Please sign in to comment.