mirror of
https://github.com/pcvolkmer/mv64e-onkostar-data.git
synced 2025-07-03 19:12:55 +00:00
Initial commit
This commit is contained in:
19
src/main/java/dev/pcvolkmer/onco/datamapper/DataMapper.java
Normal file
19
src/main/java/dev/pcvolkmer/onco/datamapper/DataMapper.java
Normal file
@ -0,0 +1,19 @@
|
||||
package dev.pcvolkmer.onco.datamapper;
|
||||
|
||||
/**
|
||||
* General interface for all data mappers
|
||||
*
|
||||
* @since 0.1
|
||||
* @author Paul-Christian Volkmer
|
||||
* @param <T> The destination type
|
||||
*/
|
||||
public interface DataMapper<T> {
|
||||
|
||||
/**
|
||||
* Loads a data set from database and maps it into destination data type
|
||||
* @param id The database id of the root procedure data set
|
||||
* @return The data set to be loaded
|
||||
*/
|
||||
T getById(int id);
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package dev.pcvolkmer.onco.datamapper;
|
||||
|
||||
import dev.pcvolkmer.mv64e.mtb.Coding;
|
||||
import dev.pcvolkmer.mv64e.mtb.Mtb;
|
||||
import dev.pcvolkmer.mv64e.mtb.MtbDiagnosis;
|
||||
import dev.pcvolkmer.onco.datamapper.datacatalogues.Kpa;
|
||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* Mapper class to load and map diagnosis data from database table 'dk_dnpm_kpa'
|
||||
*
|
||||
* @author Paul-Christian Volkmer
|
||||
* @since 0.1
|
||||
*/
|
||||
public class KpaDiagnosisDataMapper implements DataMapper<MtbDiagnosis> {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
KpaDiagnosisDataMapper(final JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create instance of the mapper class
|
||||
*
|
||||
* @param dataSource The datasource to be used
|
||||
* @return The initialized mapper
|
||||
*/
|
||||
public static KpaDiagnosisDataMapper create(final DataSource dataSource) {
|
||||
return new KpaDiagnosisDataMapper(new JdbcTemplate(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and maps a diagnosis using the kpa procedures database id
|
||||
*
|
||||
* @param id The database id of the procedure data set
|
||||
* @return The loaded MtbDiagnosis file
|
||||
*/
|
||||
@Override
|
||||
public MtbDiagnosis getById(int id) {
|
||||
var kpa = Kpa.create(this.jdbcTemplate);
|
||||
var data = kpa.getById(id);
|
||||
|
||||
var builder = MtbDiagnosis.builder();
|
||||
try {
|
||||
builder
|
||||
.id(data.getString("id"))
|
||||
.code(
|
||||
Coding.builder()
|
||||
.code(data.getString("icd10"))
|
||||
.build()
|
||||
);
|
||||
} catch (SQLException e) {
|
||||
throw new DataAccessException(e.getMessage());
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package dev.pcvolkmer.onco.datamapper;
|
||||
|
||||
import dev.pcvolkmer.mv64e.mtb.Mtb;
|
||||
import dev.pcvolkmer.onco.datamapper.datacatalogues.Kpa;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
/**
|
||||
* Mapper class to load and map Mtb files from database
|
||||
*
|
||||
* @author Paul-Christian Volkmer
|
||||
* @since 0.1
|
||||
*/
|
||||
public class MtbDataMapper implements DataMapper<Mtb> {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
MtbDataMapper(final JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create instance of the mapper class
|
||||
*
|
||||
* @param dataSource The datasource to be used
|
||||
* @return The initialized mapper
|
||||
*/
|
||||
public static MtbDataMapper create(final DataSource dataSource) {
|
||||
return new MtbDataMapper(new JdbcTemplate(dataSource));
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and maps a Mtb file using the root procedures database id
|
||||
*
|
||||
* @param id The database id of the root procedure data set
|
||||
* @return The loaded Mtb file
|
||||
*/
|
||||
@Override
|
||||
public Mtb getById(int id) {
|
||||
return Mtb.builder().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads and maps a Mtb file using the case id
|
||||
*
|
||||
* @param caseId The case id
|
||||
* @return The loaded Mtb file
|
||||
*/
|
||||
public Mtb getByCaseId(String caseId) {
|
||||
var kpa = Kpa.create(this.jdbcTemplate);
|
||||
return this.getById(
|
||||
kpa.getProcedureIdByCaseId(caseId)
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
package dev.pcvolkmer.onco.datamapper.datacatalogues;
|
||||
|
||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
|
||||
/**
|
||||
* Load raw result sets from database table 'dk_dnpm_kpa'
|
||||
*
|
||||
* @author Paul-Christian Volkmer
|
||||
* @since 0.1
|
||||
*/
|
||||
public class Kpa {
|
||||
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
private Kpa(JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
}
|
||||
|
||||
public static Kpa create(JdbcTemplate jdbcTemplate) {
|
||||
return new Kpa(jdbcTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get procedure result set by procedure id
|
||||
* @param id The procedure id
|
||||
* @return The procedure id
|
||||
*/
|
||||
public ResultSet getById(int id) {
|
||||
var result = this.jdbcTemplate.query(
|
||||
"SELECT * FROM dk_dnpm_kpa id = ?",
|
||||
(resultSet, i) -> resultSet,
|
||||
id);
|
||||
|
||||
if (result.isEmpty()) {
|
||||
throw new DataAccessException("No record found for id: " + id);
|
||||
} else if (result.size() > 1) {
|
||||
throw new DataAccessException("Multiple records found for id: " + id);
|
||||
}
|
||||
|
||||
return result.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get procedure database id by case id
|
||||
* @param caseId The case id
|
||||
* @return The procedure id
|
||||
*/
|
||||
public int getProcedureIdByCaseId(String caseId) {
|
||||
var result = this.jdbcTemplate.query(
|
||||
"SELECT id FROM dk_dnpm_kpa WHERE fallnummermv = ?",
|
||||
(resultSet, i) -> resultSet.getInt(1),
|
||||
caseId);
|
||||
|
||||
if (result.isEmpty()) {
|
||||
throw new DataAccessException("No record found for case: " + caseId);
|
||||
} else if (result.size() > 1) {
|
||||
throw new DataAccessException("Multiple records found for case: " + caseId);
|
||||
}
|
||||
|
||||
return result.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get patient database id by case id
|
||||
* @param caseId The case id
|
||||
* @return The patients database id
|
||||
*/
|
||||
public int getPatientIdByCaseId(String caseId) {
|
||||
var result = this.jdbcTemplate.query(
|
||||
"SELECT patient_id FROM dk_dnpm_kpa JOIN prozedur ON (prozedur.id = dk_dnpm_kpa.id) WHERE fallnummermv = ?",
|
||||
(resultSet, i) -> resultSet.getInt(1),
|
||||
caseId);
|
||||
|
||||
if (result.isEmpty()) {
|
||||
throw new DataAccessException("No record found for case: " + caseId);
|
||||
} else if (result.size() > 1) {
|
||||
throw new DataAccessException("Multiple records found for case: " + caseId);
|
||||
}
|
||||
|
||||
return result.get(0);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package dev.pcvolkmer.onco.datamapper.exceptions;
|
||||
|
||||
/**
|
||||
* Exception to be thrown if no or unexpected multiple data sets where found
|
||||
*
|
||||
* @author Paul-Christian Volkmer
|
||||
* @since 0.1
|
||||
*/
|
||||
public class DataAccessException extends RuntimeException {
|
||||
public DataAccessException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package dev.pcvolkmer.onco.datamapper;
|
||||
|
||||
import dev.pcvolkmer.mv64e.mtb.MtbDiagnosis;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KpaDiagnosisDataMapperTest {
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
KpaDiagnosisDataMapper dataMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp(@Mock JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.dataMapper = new KpaDiagnosisDataMapper(jdbcTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateDataMapper(@Mock DataSource dataSource) {
|
||||
assertThat(MtbDataMapper.create(dataSource)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUseKpaProcedureId(@Mock ResultSet resultSet) throws SQLException {
|
||||
doAnswer(invocationOnMock -> {
|
||||
var columnName = invocationOnMock.getArgument(0, String.class);
|
||||
switch (columnName) {
|
||||
case "id":
|
||||
return "1";
|
||||
case "icd10":
|
||||
return "F79.9";
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}).when(resultSet).getString(anyString());
|
||||
|
||||
doAnswer(invocationOnMock -> List.of(resultSet))
|
||||
.when(jdbcTemplate)
|
||||
.query(anyString(), any(RowMapper.class), anyInt());
|
||||
|
||||
var actual = this.dataMapper.getById(1);
|
||||
assertThat(actual).isInstanceOf(MtbDiagnosis.class);
|
||||
assertThat(actual.getId()).isEqualTo("1");
|
||||
assertThat(actual.getCode().getCode()).isEqualTo("F79.9");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
package dev.pcvolkmer.onco.datamapper;
|
||||
|
||||
import dev.pcvolkmer.mv64e.mtb.Mtb;
|
||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class MtbDataMapperTest {
|
||||
|
||||
JdbcTemplate jdbcTemplate;
|
||||
|
||||
MtbDataMapper mtbDataMapper;
|
||||
|
||||
@BeforeEach
|
||||
void setUp(@Mock JdbcTemplate jdbcTemplate) {
|
||||
this.jdbcTemplate = jdbcTemplate;
|
||||
this.mtbDataMapper = new MtbDataMapper(jdbcTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateDataMapper(@Mock DataSource dataSource) {
|
||||
assertThat(MtbDataMapper.create(dataSource)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUseKpaProcedureId() {
|
||||
doAnswer(invocationOnMock -> List.of(1))
|
||||
.when(jdbcTemplate)
|
||||
.query(anyString(), any(RowMapper.class), anyString());
|
||||
|
||||
var actual = this.mtbDataMapper.getByCaseId("16000123");
|
||||
assertThat(actual).isInstanceOf(Mtb.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfNoKpaProcedureFound() {
|
||||
var ex = assertThrows(DataAccessException.class, () -> {
|
||||
this.mtbDataMapper.getByCaseId("16000123");
|
||||
});
|
||||
assertThat(ex).hasMessage("No record found for case: 16000123");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowExceptionIfMultipleKpaProceduresFound() {
|
||||
doAnswer(invocationOnMock -> List.of(1, 2))
|
||||
.when(jdbcTemplate)
|
||||
.query(anyString(), any(RowMapper.class), anyString());
|
||||
|
||||
var ex = assertThrows(DataAccessException.class, () -> {
|
||||
this.mtbDataMapper.getByCaseId("16000123");
|
||||
});
|
||||
assertThat(ex).hasMessage("Multiple records found for case: 16000123");
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user