Skip to content

Commit

Permalink
Adding new audit tables to track changes in DPA and DSAs
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewLittler committed Aug 7, 2020
1 parent 83e20cd commit ef6ee66
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>org.endeavourhealth.common</groupId>
<artifactId>core</artifactId>
<version>1.708-SNAPSHOT</version>
<version>1.709-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,4 +623,13 @@ public static VisionCodeDalI factoryVisionCodeDal() {
public static TppCtv3SnomedRefDalI factoryTppCtv3SnomedRefDal() {
return new RdbmsTppCtv3SnomedRefDal();
}

public static ServiceSubscriberAuditDalI factoryServiceSubscriberAuditDal() {
return new RdbmsServiceSubscriberAuditDal();
}

public static ServicePublisherAuditDalI factoryServicePublisherAuditDal() {
return new RdbmsPublisherAuditDal();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.endeavourhealth.core.database.dal.audit;

import java.util.UUID;

public interface ServicePublisherAuditDalI {

Boolean getLatestDpaState(UUID serviceId) throws Exception;
void saveDpaState(UUID serviceId, boolean hasDpa) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.endeavourhealth.core.database.dal.audit;

import java.util.List;
import java.util.UUID;

public interface ServiceSubscriberAuditDalI {

List<String> getLatestSubscribers(UUID serviceId) throws Exception;
void saveSubscribers(UUID serviceId, List<String> subscriberConfigNames) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.endeavourhealth.core.database.rdbms.audit;

import org.endeavourhealth.core.database.dal.audit.ServicePublisherAuditDalI;
import org.endeavourhealth.core.database.rdbms.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;
import java.util.UUID;

public class RdbmsPublisherAuditDal implements ServicePublisherAuditDalI {

@Override
public Boolean getLatestDpaState(UUID serviceId) throws Exception {
Connection conn = ConnectionManager.getAuditConnection();
PreparedStatement ps = null;
try {
String sql = "SELECT has_dpa"
+ " FROM service_publisher_audit"
+ " WHERE service_id = ?"
+ " ORDER BY dt_changed DESC"
+ " LIMIT 1";
ps = conn.prepareStatement(sql);

ps.setString(1, serviceId.toString());

ResultSet rs = ps.executeQuery();
if (rs.next()) {
boolean hasDpa = rs.getBoolean(1);
if (rs.wasNull()) {
return null;
} else {
return new Boolean(hasDpa);
}

} else {
return null;
}

} finally {
if (ps != null) {
ps.close();
}
conn.close();
}
}

@Override
public void saveDpaState(UUID serviceId, boolean hasDpa) throws Exception {

Connection conn = ConnectionManager.getAuditConnection();
PreparedStatement ps = null;
try {
String sql = "INSERT INTO service_publisher_audit"
+ " (service_id, dt_changed, has_dpa)"
+ " VALUES"
+ " (?, ?, ?)";
ps = conn.prepareStatement(sql);

ps.setString(1, serviceId.toString());
ps.setTimestamp(2, new java.sql.Timestamp(new Date().getTime()));
ps.setBoolean(3, hasDpa);

ps.executeUpdate();
conn.commit();
} catch (Exception ex) {
conn.rollback();

} finally {
if (ps != null) {
ps.close();
}
conn.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.endeavourhealth.core.database.rdbms.audit;

import org.endeavourhealth.core.database.dal.audit.ServiceSubscriberAuditDalI;
import org.endeavourhealth.core.database.rdbms.ConnectionManager;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.regex.Pattern;

public class RdbmsServiceSubscriberAuditDal implements ServiceSubscriberAuditDalI {

private static final String DELIM = "|";

@Override
public List<String> getLatestSubscribers(UUID serviceId) throws Exception {
Connection conn = ConnectionManager.getAuditConnection();
PreparedStatement ps = null;
try {
String sql = "SELECT subscriber_config_names"
+ " FROM service_subscriber_audit"
+ " WHERE service_id = ?"
+ " ORDER BY dt_changed DESC"
+ " LIMIT 1";
ps = conn.prepareStatement(sql);

ps.setString(1, serviceId.toString());

ResultSet rs = ps.executeQuery();
if (rs.next()) {
String subscriberConfigNames = rs.getString(1);
String[] toks = subscriberConfigNames.split(Pattern.quote(DELIM)); //use the Pattern.quote fn to make it NOT use regex
return Arrays.asList(toks);

} else {
return null;
}

} finally {
if (ps != null) {
ps.close();
}
conn.close();
}
}

@Override
public void saveSubscribers(UUID serviceId, List<String> subscriberConfigNames) throws Exception {

List<String> l = new ArrayList<>(subscriberConfigNames);
//always re-sort so the table is consistent
l.sort((a, b) -> a.compareToIgnoreCase(b));
String subscriberConfigStr = String.join(DELIM, l);

Connection conn = ConnectionManager.getAuditConnection();
PreparedStatement ps = null;
try {
String sql = "INSERT INTO service_subscriber_audit"
+ " (service_id, dt_changed, subscriber_config_names)"
+ " VALUES"
+ " (?, ?, ?)";
ps = conn.prepareStatement(sql);

ps.setString(1, serviceId.toString());
ps.setTimestamp(2, new java.sql.Timestamp(new Date().getTime()));
ps.setString(3, subscriberConfigStr);

ps.executeUpdate();
conn.commit();
} catch (Exception ex) {
conn.rollback();

} finally {
if (ps != null) {
ps.close();
}
conn.close();
}
}
}

0 comments on commit ef6ee66

Please sign in to comment.