Commit 7e771f78 authored by Matthieu Bonnouvrier's avatar Matthieu Bonnouvrier
Browse files

Rework on db request and associated TUs

parent 45228c58
Pipeline #265133 failed with stages
in 16 minutes and 57 seconds
......@@ -17,7 +17,8 @@ import java.util.List;
@NamedQuery(name = "PatientDB.findByUuid", query = "SELECT p FROM PatientDB p WHERE p.uuid = :uuid")
@NamedQuery(name = "PatientDB.findForSimulatedActor", query = "SELECT p FROM PatientDB p WHERE p.simulatedActor = :actorKeyword")
@NamedQuery(name = "PatientDB.findUUID", query = "SELECT p.uuid FROM PatientDB p WHERE p.uuid = :uuid")
@NamedQuery(name = "PatientDB.findByPatientIdentifier", query = "SELECT p.patientIdentifiers FROM PatientDB p WHERE p.patientIdentifiers = :patientIdentifier")
@NamedQuery(name = "PatientDB.findByPatientIdentifier", query = "select pdb from PatientDB pdb, IN (pdb.patientIdentifiers) pid WHERE pid.identifier = :patient_identifier")
@Table(name = "pat_patient", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = "id"))
@SequenceGenerator(name = "pat_patient_sequence", sequenceName = "pat_patient_id_seq", allocationSize = 1)
public class PatientDB extends AbstractPatientDB {
......
......@@ -8,12 +8,9 @@ import java.util.Date;
import java.util.List;
@Entity
//@NamedQuery(name = "CrossReferenceDB.findUUID", query = "SELECT c.patients FROM CrossReferenceDB c WHERE c.patients = :")
@Table(name = "pix_cross_reference", schema = "public")
@SequenceGenerator(name = "pix_cross_reference_sequence", sequenceName = "pix_cross_reference_id_seq", allocationSize = 1)
@NamedQuery(name = "CrossReferenceDB.findByPatient", query = "SELECT cr FROM CrossReferenceDB cr WHERE :patient in (cr.patients)")
@NamedQuery(name = "CrossReferenceDB.findByCrossRefId", query = "SELECT cr FROM CrossReferenceDB cr WHERE :pixReference = id")
@NamedQuery(name = "CrossReferenceDB.countPatient", query = "SELECT COUNT (cr) FROM CrossReferenceDB cr WHERE :patient in (cr.patients)")
@NamedQuery(name = "CrossReferenceDB.findByCrossRefId", query = "SELECT cr FROM CrossReferenceDB cr WHERE :id = id")
public class CrossReferenceDB {
@Id
......
......@@ -65,7 +65,7 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
throw new IllegalArgumentException("The uuid of patientDB shall not be null");
}
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByCrossRefId", CrossReferenceDB.class);
queryForPatientAliases.setParameter("pixReference", patientDB.getPixReference().getId());
queryForPatientAliases.setParameter("id", patientDB.getPixReference().getId());
List<CrossReferenceDB> crossReferenceDBS = queryForPatientAliases.getResultList();
if (crossReferenceDBS.isEmpty() || crossReferenceDBS.get(0).getPatients().isEmpty()) {
throw new SearchCrossReferenceException("Search error : No X-ref for given Patient");
......@@ -107,7 +107,7 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
queryForAPatient.setParameter("givenUsage", DesignatorTypeDB.PATIENT_ID);
HierarchicDesignatorDB hierarchicDesignatorDB = queryForAPatient.getSingleResult();
return hierarchicDesignatorDB;
} catch (NoResultException | NonUniqueResultException exception) {
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("System not found");
}
}
......@@ -125,12 +125,12 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
private PatientDB searchforPatient(PatientIdentifierDB patientIdentifier) throws SearchCrossReferenceException {
try {
String uuid = patientIdentifier.getIdentifier();
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByUuid", PatientDB.class);
queryForAPatient.setParameter("uuid", uuid);
String patient_identifier = patientIdentifier.getIdentifier();
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByPatientIdentifier", PatientDB.class);
queryForAPatient.setParameter("patient_identifier", patient_identifier);
return queryForAPatient.getSingleResult();
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("Patient not found with with corresponding PatientIdentifier");
throw new SearchCrossReferenceException("Patient not found with corresponding PatientIdentifier");
}
}
......
......@@ -17,6 +17,7 @@ import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.servi
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.NoResultException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
......@@ -77,17 +78,22 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
}
/**
* Delete every patient (of a patient aliases) where its uuid does not match the system identifier of any currently in the list
* Delete every patient (of a patient aliases) where its uuid does not match the system identifier of any target domain currently in the list
*
* @param patientAlias
* @param targetDomains
* @return
*/
private PatientAliases removePatientFromPatientAliasWithNonMatchingTargetDomain(PatientAliases patientAlias, List<String> targetDomains) {
List<Patient> patients = patientAlias.getMembers().stream()
.filter(patient -> targetDomains.stream().anyMatch(
targetDomain -> targetDomain.equals(patient.getUuid())))
.collect(Collectors.toList());
List<Patient> patients = new ArrayList<>();
for (Patient patient : patientAlias.getMembers()) {
for (EntityIdentifier entityIdentifier : patient.getIdentifiers()) {
if (targetDomains.contains(entityIdentifier.getSystemIdentifier())) {
patients.add(patient);
}
}
}
patientAlias.setMembers(patients);
return patientAlias;
}
......
......@@ -24,6 +24,7 @@ class PatientCrossReferenceDAOImplTest {
private PatientCrossReferenceDAO patientCrossReferenceDAO;
private EntityManager entityManager;
/**
* database init
*/
......@@ -39,7 +40,6 @@ class PatientCrossReferenceDAOImplTest {
// entityManager.getTransaction().begin();
// entityManager.getTransaction().commit();
// }
@BeforeAll
public void initializeDatabase() throws ParseException {
entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME_CONST).createEntityManager();
......@@ -48,33 +48,11 @@ class PatientCrossReferenceDAOImplTest {
addTestEntitiesToDB(entityManager);
}
// @BeforeEach
// public void tata(){
// if(!entityManager.getTransaction().isActive()) {
// entityManager.getTransaction().begin();
// }
// }
// @Before
// public void toto(){
// if(!entityManager.getTransaction().isActive()) {
// entityManager.getTransaction().begin();
// entityManager.isOpen();
// }
// }
private void addTestEntitiesToDB(EntityManager entityManager) throws ParseException {
initializeDb(entityManager);
}
/**
* Close the transaction corresponding to the test case when it is over.
*/
// @After
// public void commitTransaction() {
// entityManager.getTransaction().commit();
// entityManager.clear();
// entityManager.close();
// }
/**
* Close the connection to the database after all tests are executed.
......@@ -94,12 +72,10 @@ class PatientCrossReferenceDAOImplTest {
@Test
void testNullPatientDB() {
PatientDB patientDB = new PatientDB();
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(null));
assertEquals("Patient Cannot be null", exception.getMessage());
}
@Test
......@@ -109,7 +85,7 @@ class PatientCrossReferenceDAOImplTest {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB));
assertEquals("The uuid of patientDB shall not be null", exception.getMessage());
}
@Test
......@@ -119,7 +95,7 @@ class PatientCrossReferenceDAOImplTest {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB));
assertEquals("The uuid of patientDB shall not be null", exception.getMessage());
}
@Test
......@@ -131,7 +107,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB));
assertEquals("Search error : No X-ref for given Patient", exception.getMessage());
}
@Test
......@@ -141,7 +117,7 @@ class PatientCrossReferenceDAOImplTest {
entityIdentifier.setValue("1235");
PatientDB patientDB = patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier);
patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB);
}
@Test
......@@ -149,7 +125,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(null));
assertEquals("The source identifier cannot be null", exception.getMessage());
}
@Test
......@@ -158,7 +134,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("The system identifier from entityIdentifier cannot be null or empty", exception.getMessage());
}
@Test
......@@ -168,7 +144,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("The system identifier from entityIdentifier cannot be null or empty", exception.getMessage());
}
@Test
......@@ -178,7 +154,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("The value from entityIdentifier cannot be null or empty", exception.getMessage());
}
@Test
......@@ -189,7 +165,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("The value from entityIdentifier cannot be null or empty", exception.getMessage());
}
@Test
......@@ -200,7 +176,7 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("System not found", exception.getMessage());
}
@Test
......@@ -211,18 +187,18 @@ class PatientCrossReferenceDAOImplTest {
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("PatientIdentifier not found", exception.getMessage());
}
@Test
void testNoPatientDB() {
EntityIdentifier entityIdentifier = new EntityIdentifier();
entityIdentifier.setSystemIdentifier("2222");
entityIdentifier.setValue("1236");
entityIdentifier.setSystemIdentifier("4444");
entityIdentifier.setValue("8888");
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("Patient not found with with corresponding PatientIdentifier", exception.getMessage());
assertEquals("Patient not found with corresponding PatientIdentifier", exception.getMessage());
}
......@@ -245,6 +221,12 @@ class PatientCrossReferenceDAOImplTest {
hierarchicDesignatorDB5.setUsage(DesignatorTypeDB.PATIENT_ID);
HierarchicDesignatorDB hierarchicDesignatorDB6 = entityManager.merge(hierarchicDesignatorDB5);
HierarchicDesignatorDB hierarchicDesignatorDB7 = new HierarchicDesignatorDB();
hierarchicDesignatorDB7.setUniversalID("4444");
hierarchicDesignatorDB7.setUniversalIDType("ISO");
hierarchicDesignatorDB7.setUsage(DesignatorTypeDB.PATIENT_ID);
HierarchicDesignatorDB hierarchicDesignatorDB8 = entityManager.merge(hierarchicDesignatorDB7);
PatientIdentifierDB patientIdentifierDB = new PatientIdentifierDB();
patientIdentifierDB.setDomain(hierarchicDesignatorDB2);
patientIdentifierDB.setIdentifier("1235");
......@@ -260,6 +242,11 @@ class PatientCrossReferenceDAOImplTest {
patientIdentifierDB3.setIdentifier("7777");
PatientIdentifierDB patientIdentifierDB3merged = entityManager.merge(patientIdentifierDB3);
PatientIdentifierDB patientIdentifierDB4 = new PatientIdentifierDB();
patientIdentifierDB4.setDomain(hierarchicDesignatorDB8);
patientIdentifierDB4.setIdentifier("8888");
PatientIdentifierDB patientIdentifierDB4merged = entityManager.merge(patientIdentifierDB4);
PatientDB patient1 = new PatientDB();
patient1.setUuid("1235");
patient1.setStillActive(true);
......@@ -284,10 +271,11 @@ class PatientCrossReferenceDAOImplTest {
patient3.setUuid("7777");
patient3.setStillActive(false);
patient3.setGenderCode(GenderCodeDB.M);
patient2.setLastName("Noah");
patient3.setLastName("Noah");
List<PatientIdentifierDB> patientIdentifierDBS3 = new ArrayList<>();
patientIdentifierDBS3.add(patientIdentifierDB3merged);
patient3.setPatientIdentifiers(patientIdentifierDBS3);
PatientDB patient3m = entityManager.merge(patient3);
List<PatientDB> patientDBS = new ArrayList<>();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment