mirror of
https://github.com/pcvolkmer/mv64e-onkostar-data.git
synced 2025-07-03 02:52:54 +00:00
refactor: add catalogue factory
This commit is contained in:
@ -4,8 +4,6 @@ import dev.pcvolkmer.onco.datamapper.ResultSet;
|
|||||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Common implementations for all data catalogues
|
* Common implementations for all data catalogues
|
||||||
*
|
*
|
||||||
|
@ -0,0 +1,60 @@
|
|||||||
|
package dev.pcvolkmer.onco.datamapper.datacatalogues;
|
||||||
|
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple catalogue factory to get a catalogue instance
|
||||||
|
*
|
||||||
|
* @author Paul-Christian Volkmer
|
||||||
|
* @since 0.1
|
||||||
|
*/
|
||||||
|
public class CatalogueFactory {
|
||||||
|
|
||||||
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final Map<Class<? extends DataCatalogue>, DataCatalogue> catalogues = new HashMap<>();
|
||||||
|
|
||||||
|
private CatalogueFactory(JdbcTemplate jdbcTemplate) {
|
||||||
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static CatalogueFactory obj;
|
||||||
|
|
||||||
|
public static synchronized CatalogueFactory instance(final JdbcTemplate jdbcTemplate) {
|
||||||
|
if (null == obj) {
|
||||||
|
obj = new CatalogueFactory(jdbcTemplate);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public synchronized <T extends DataCatalogue> T catalogue(Class<T> clazz) {
|
||||||
|
return (T) catalogues.computeIfAbsent(clazz, c -> {
|
||||||
|
if (c == EcogCatalogue.class) {
|
||||||
|
return EcogCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == HistologieCatalogue.class) {
|
||||||
|
return HistologieCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == KpaCatalogue.class) {
|
||||||
|
return KpaCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == PatientCatalogue.class) {
|
||||||
|
return PatientCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == ProzedurCatalogue.class) {
|
||||||
|
return ProzedurCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == TherapielinieCatalogue.class) {
|
||||||
|
return TherapielinieCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == TumorausbreitungCatalogue.class) {
|
||||||
|
return TumorausbreitungCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == TumorgradingCatalogue.class) {
|
||||||
|
return TumorgradingCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == VerwandteCatalogue.class) {
|
||||||
|
return VerwandteCatalogue.create(jdbcTemplate);
|
||||||
|
} else if (c == VorbefundeCatalogue.class) {
|
||||||
|
return VorbefundeCatalogue.create(jdbcTemplate);
|
||||||
|
}
|
||||||
|
throw new RuntimeException("Unknown DataCatalogue class: " + clazz);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package dev.pcvolkmer.onco.datamapper.mapper;
|
package dev.pcvolkmer.onco.datamapper.mapper;
|
||||||
|
|
||||||
import dev.pcvolkmer.mv64e.mtb.Mtb;
|
import dev.pcvolkmer.mv64e.mtb.Mtb;
|
||||||
|
import dev.pcvolkmer.onco.datamapper.datacatalogues.CatalogueFactory;
|
||||||
import dev.pcvolkmer.onco.datamapper.datacatalogues.KpaCatalogue;
|
import dev.pcvolkmer.onco.datamapper.datacatalogues.KpaCatalogue;
|
||||||
import dev.pcvolkmer.onco.datamapper.datacatalogues.PatientCatalogue;
|
import dev.pcvolkmer.onco.datamapper.datacatalogues.PatientCatalogue;
|
||||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||||
@ -21,10 +22,10 @@ public class MtbDataMapper implements DataMapper<Mtb> {
|
|||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
private final CatalogueFactory catalogueFactory;
|
||||||
|
|
||||||
MtbDataMapper(final JdbcTemplate jdbcTemplate) {
|
MtbDataMapper(final JdbcTemplate jdbcTemplate) {
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
this.catalogueFactory = CatalogueFactory.instance(jdbcTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -55,8 +56,8 @@ public class MtbDataMapper implements DataMapper<Mtb> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Mtb getById(int kpaId) {
|
public Mtb getById(int kpaId) {
|
||||||
var kpaCatalogue = KpaCatalogue.create(jdbcTemplate);
|
var kpaCatalogue = catalogueFactory.catalogue(KpaCatalogue.class);
|
||||||
var patientDataMapper = new PatientDataMapper(new PatientCatalogue(jdbcTemplate));
|
var patientDataMapper = new PatientDataMapper(catalogueFactory.catalogue(PatientCatalogue.class));
|
||||||
var kpaPatientDataMapper = new KpaPatientDataMapper(kpaCatalogue);
|
var kpaPatientDataMapper = new KpaPatientDataMapper(kpaCatalogue);
|
||||||
var diagnosisDataMapper = new KpaDiagnosisDataMapper(kpaCatalogue);
|
var diagnosisDataMapper = new KpaDiagnosisDataMapper(kpaCatalogue);
|
||||||
|
|
||||||
@ -84,9 +85,8 @@ public class MtbDataMapper implements DataMapper<Mtb> {
|
|||||||
* @return The loaded Mtb file
|
* @return The loaded Mtb file
|
||||||
*/
|
*/
|
||||||
public Mtb getByCaseId(String caseId) {
|
public Mtb getByCaseId(String caseId) {
|
||||||
var kpa = KpaCatalogue.create(this.jdbcTemplate);
|
|
||||||
return this.getById(
|
return this.getById(
|
||||||
kpa.getProcedureIdByCaseId(caseId)
|
this.catalogueFactory.catalogue(KpaCatalogue.class).getProcedureIdByCaseId(caseId)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package dev.pcvolkmer.onco.datamapper.datacatalogues;
|
package dev.pcvolkmer.onco.datamapper.datacatalogues;
|
||||||
|
|
||||||
|
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@ -7,13 +8,14 @@ import org.mockito.ArgumentCaptor;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
import org.springframework.jdbc.core.RowMapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.anyInt;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.*;
|
||||||
import static org.mockito.Mockito.doAnswer;
|
import static org.mockito.Mockito.doAnswer;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
@ -44,4 +46,13 @@ class KpaCatalogueTest {
|
|||||||
.isEqualTo("SELECT * FROM dk_dnpm_kpa JOIN prozedur ON (prozedur.id = dk_dnpm_kpa.id) WHERE geloescht = 0 AND prozedur.id = ?");
|
.isEqualTo("SELECT * FROM dk_dnpm_kpa JOIN prozedur ON (prozedur.id = dk_dnpm_kpa.id) WHERE geloescht = 0 AND prozedur.id = ?");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldThrowExceptionIfNoKpaProcedureFound() {
|
||||||
|
doAnswer(invocationOnMock -> List.of())
|
||||||
|
.when(jdbcTemplate).query(anyString(), any(RowMapper.class), anyString());
|
||||||
|
|
||||||
|
var ex = assertThrows(DataAccessException.class, () -> catalogue.getProcedureIdByCaseId("16000123"));
|
||||||
|
assertThat(ex).hasMessage("No record found for case: 16000123");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package dev.pcvolkmer.onco.datamapper.mapper;
|
package dev.pcvolkmer.onco.datamapper.mapper;
|
||||||
|
|
||||||
import dev.pcvolkmer.onco.datamapper.exceptions.DataAccessException;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
@ -11,7 +10,6 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
||||||
|
|
||||||
@ExtendWith(MockitoExtension.class)
|
@ExtendWith(MockitoExtension.class)
|
||||||
class MtbDataMapperTest {
|
class MtbDataMapperTest {
|
||||||
@ -31,10 +29,4 @@ class MtbDataMapperTest {
|
|||||||
assertThat(MtbDataMapper.create(dataSource)).isNotNull();
|
assertThat(MtbDataMapper.create(dataSource)).isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
void shouldThrowExceptionIfNoKpaProcedureFound() {
|
|
||||||
var ex = assertThrows(DataAccessException.class, () -> this.mtbDataMapper.getByCaseId("16000123"));
|
|
||||||
assertThat(ex).hasMessage("No record found for case: 16000123");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user