Commit 277cb11b authored by Matthieu Bonnouvrier's avatar Matthieu Bonnouvrier
Browse files

Add targetDomains in DAO and TUs for xref search

parent 18e477f4
Pipeline #263432 failed with stages
in 7 minutes and 12 seconds
......@@ -4,6 +4,8 @@ import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.PatientAliases;
import net.ihe.gazelle.lib.searchmodelapi.business.exception.SearchException;
import java.util.List;
/**
* Patient Cross Reference Search Service API
......@@ -18,4 +20,6 @@ public interface PatientCrossReferenceSearch {
* @throws SearchCrossReferenceException if an error happens during the search processing.
*/
PatientAliases search(EntityIdentifier sourceIdentifier) throws SearchCrossReferenceException;
PatientAliases searchInTargetDomain(EntityIdentifier sourceIdentifier, List<EntityIdentifier> entityIdentifierList) throws SearchCrossReferenceException, UnrecognizedDomainException;
}
package net.ihe.gazelle.app.patientregistryapi.application;
/**
* Exception to be thrown if a targetDomain cannot be searched.
*/
public class UnrecognizedDomainException extends Exception {
/**
* Constructs a new exception with {@code null} as its detail message.
* The cause is not initialized, and may subsequently be initialized by a
* call to {@link #initCause}.
*/
public UnrecognizedDomainException() {
super();
}
/**
* Constructs a new exception with the specified detail message. The
* cause is not initialized, and may subsequently be initialized by
* a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for
* later retrieval by the {@link #getMessage()} method.
*/
public UnrecognizedDomainException(String message) {
super(message);
}
/**
* Constructs a new exception with the specified detail message and
* cause. <p>Note that the detail message associated with
* {@code cause} is <i>not</i> automatically incorporated in
* this exception's detail message.
*
* @param message the detail message (which is saved for later retrieval
* by the {@link #getMessage()} method).
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public UnrecognizedDomainException(String message, Throwable cause) {
super(message, cause);
}
/**
* Constructs a new exception with the specified cause and a detail
* message of {@code (cause==null ? null : cause.toString())} (which
* typically contains the class and detail message of {@code cause}).
* This constructor is useful for exceptions that are little more than
* wrappers for other throwables (for example, {@link
* java.security.PrivilegedActionException}).
*
* @param cause the cause (which is saved for later retrieval by the
* {@link #getCause()} method). (A {@code null} value is
* permitted, and indicates that the cause is nonexistent or
* unknown.)
* @since 1.4
*/
public UnrecognizedDomainException(Throwable cause) {
super(cause);
}
/**
* Constructs a new exception with the specified detail message,
* cause, suppression enabled or disabled, and writable stack
* trace enabled or disabled.
*
* @param message the detail message.
* @param cause the cause. (A {@code null} value is permitted,
* and indicates that the cause is nonexistent or unknown.)
* @param enableSuppression whether or not suppression is enabled
* or disabled
* @param writableStackTrace whether or not the stack trace should
* be writable
* @since 1.7
*/
public UnrecognizedDomainException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
......@@ -64,9 +64,6 @@ 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);
System.out.println("searchForPatientAliasesWithPatientDB");
System.out.println(patientDB.getPixReference());
System.out.println(patientDB.getPixReference().getId());
queryForPatientAliases.setParameter("pixReference", patientDB.getPixReference().getId());
List<CrossReferenceDB> crossReferenceDBS = queryForPatientAliases.getResultList();
if (crossReferenceDBS.isEmpty() || crossReferenceDBS.get(0).getPatients().isEmpty()) {
......
......@@ -2,6 +2,7 @@ package net.ihe.gazelle.app.patientregistryservice.application;
import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceSearch;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.application.UnrecognizedDomainException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.Patient;
import net.ihe.gazelle.app.patientregistryapi.business.PatientAliases;
......@@ -13,9 +14,8 @@ import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.servi
import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSearch {
private PatientCrossReferenceDAO patientCrossReferenceDAO;
......@@ -49,22 +49,74 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
patientAlias = processingOnResult(crossReferenceDBS, patientDB);
} catch (SearchCrossReferenceException searchCrossReferenceException) {
if(searchCrossReferenceException.getMessage().equals("System not found")){
if (searchCrossReferenceException.getMessage().equals("System not found")) {
throw new SearchCrossReferenceException("Error in the sourceIdentifier : System doesn't exit");
}else if(searchCrossReferenceException.getMessage().equals("PatientIdentifier not found")){
} else if (searchCrossReferenceException.getMessage().equals("PatientIdentifier not found")) {
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it doesn't match any identity");
}else if(searchCrossReferenceException.getMessage().equals("Patient not found with with corresponding PatientIdentifier")){
} else if (searchCrossReferenceException.getMessage().equals("Patient not found with with corresponding PatientIdentifier")) {
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it doesn't match any Patient");
}
if (searchCrossReferenceException.getMessage().equals("Search error : No X-ref for given Patient")) {
return convertPatientDBTPatientAliases(patientDB);
//TODO create a patient / patientAliases via the DAO ?
return convertPatientDBToPatientAliases(patientDB);
}
}catch (Exception exception){
throw new SearchCrossReferenceException("An Error occurred during CrossReference Search",exception);
} catch (Exception exception) {
throw new SearchCrossReferenceException("An Error occurred during CrossReference Search", exception);
}
return patientAlias;
}
/**
* Search for a patient alias by a source identifier, in a list of target domain.
* Any patient that do not match the list of target domain will be deleted
*
* @param sourceIdentifier
* @param entityIdentifiers
* @return
* @throws SearchCrossReferenceException
* @throws UnrecognizedDomainException
*/
@Override
public PatientAliases searchInTargetDomain(EntityIdentifier sourceIdentifier, List<EntityIdentifier> entityIdentifiers) throws SearchCrossReferenceException, UnrecognizedDomainException {
checkSourceIdentifier(sourceIdentifier);
checkTargetDomains(sourceIdentifier, entityIdentifiers);
PatientAliases patientAlias = search(sourceIdentifier);
return removePatientWithNonMatchingTargetDomain(patientAlias, entityIdentifiers);
}
/**
* Delete every patient (of a patient aliases) where its uuid does not match the system identifier of any currently in the list
*
* @param patientAlias
* @param entityIdentifiers
* @return
*/
private PatientAliases removePatientWithNonMatchingTargetDomain(PatientAliases patientAlias, List<EntityIdentifier> entityIdentifiers) {
List<Patient> patients = patientAlias.getMembers().stream()
.filter(patient -> entityIdentifiers.stream().anyMatch(
targetDomain -> targetDomain.getSystemIdentifier().equals(patient.getUuid())))
.collect(Collectors.toList());
patientAlias.setMembers(patients);
return patientAlias;
}
/**
* Check if the list of target domain contains the current source identifier
*
* @param sourceIdentifier
* @param entityIdentifiers
* @throws UnrecognizedDomainException
*/
private void checkTargetDomains(EntityIdentifier sourceIdentifier, List<EntityIdentifier> entityIdentifiers) throws UnrecognizedDomainException {
EntityIdentifier entityIdentifier = entityIdentifiers.stream().filter(
internalEntityIdentifier -> sourceIdentifier.getSystemIdentifier()
.equals(internalEntityIdentifier.getSystemIdentifier())).findAny().orElse(null);
if (entityIdentifier == null) {
throw new UnrecognizedDomainException("The current target domain could not be found");
}
}
/**
* Check if the source identifier is valid
* <p>
......@@ -90,7 +142,6 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
}
}
/**
* @param crossReferenceDBS
* @param patientDB
......@@ -109,12 +160,9 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
}
return patientAliasOk;
}
}
private PatientAliases convertPatientDBTPatientAliases(PatientDB patientDB) {
private PatientAliases convertPatientDBToPatientAliases(PatientDB patientDB) {
PatientAliases patientAliases = new PatientAliases();
patientAliases.addMember(PatientConverter.toPatient(patientDB));
return patientAliases;
......
......@@ -2,6 +2,7 @@ package net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.serv
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.PatientAliases;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.GenderCodeDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.HierarchicDesignatorDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
......@@ -47,10 +48,9 @@ public class PatientCrossReferenceDAOImplTest {
entityManager = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME_CONST).createEntityManager();
patientCrossReferenceDAO = new PatientCrossReferenceDAOImpl(entityManager);
addTestEntitiesToDB(entityManager);
System.out.println("tempo");
}
private static void addTestEntitiesToDB(EntityManager entityManager) throws ParseException {
private void addTestEntitiesToDB(EntityManager entityManager) throws ParseException {
initializeDb(entityManager);
}
......@@ -116,12 +116,14 @@ public class PatientCrossReferenceDAOImplTest {
}
@Test
void testAllValid3() throws SearchCrossReferenceException {
void testAllValid() throws SearchCrossReferenceException {
EntityIdentifier entityIdentifier = new EntityIdentifier();
entityIdentifier.setSystemIdentifier("1111");
entityIdentifier.setValue("1235");
PatientDB patientDB = patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier);
patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB);
List<CrossReferenceDB> crossReferenceDBS = patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB);
assert(crossReferenceDBS.get(0).getPatients().size() == 2);
assert(crossReferenceDBS.get(0).getLastModifier().equals("fde"));
}
@Test
......@@ -197,7 +199,19 @@ public class PatientCrossReferenceDAOImplTest {
assertEquals("Patient not found with with corresponding PatientIdentifier", exception.getMessage());
}
private static void initializeDb(EntityManager entityManager) {
@Test
void testWithUnkwownTargetDomain() throws SearchCrossReferenceException {
EntityIdentifier entityIdentifier = new EntityIdentifier();
entityIdentifier.setSystemIdentifier("1111");
entityIdentifier.setValue("1235");
PatientDB patientDB = patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier);
List<CrossReferenceDB> crossReferenceDBS = patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB);
SearchCrossReferenceException exception = assertThrows(SearchCrossReferenceException.class,
() -> patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(entityIdentifier));
assertEquals("Patient not found with with corresponding PatientIdentifier", exception.getMessage());
}
private void initializeDb(EntityManager entityManager) {
HierarchicDesignatorDB hierarchicDesignatorDB = new HierarchicDesignatorDB();
hierarchicDesignatorDB.setUniversalID("1111");
hierarchicDesignatorDB.setUniversalIDType("ISO");
......
......@@ -2,12 +2,15 @@ package net.ihe.gazelle.app.patientregistryservice.adapter.ws;
import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceSearch;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.application.UnrecognizedDomainException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.Patient;
import net.ihe.gazelle.app.patientregistryapi.business.PatientAliases;
import net.ihe.gazelle.app.patientregistryapi.business.PersonName;
import net.ihe.gazelle.lib.searchmodelapi.business.exception.SearchException;
import java.util.List;
public class PatientCrossReferenceSearchMock implements PatientCrossReferenceSearch {
@Override
public PatientAliases search(EntityIdentifier sourceIdentifier) throws SearchCrossReferenceException {
......@@ -27,4 +30,9 @@ public class PatientCrossReferenceSearchMock implements PatientCrossReferenceSea
}
}
@Override
public PatientAliases searchInTargetDomain(EntityIdentifier sourceIdentifier, List<EntityIdentifier> entityIdentifierList) throws SearchCrossReferenceException, UnrecognizedDomainException {
return null;
}
}
......@@ -43,6 +43,10 @@ public class PatientCrossReferenceDAOMock implements PatientCrossReferenceDAO {
patientDB3.setFirstName("testFirstName");
patientDB3.setLastName("testLastName");
return patientDB3;
case "searchTargetDomainFound":
PatientDB patientDB4 = new PatientDB();
patientDB4.setUuid("targetDomain");
return patientDB4;
}
......@@ -60,6 +64,7 @@ public class PatientCrossReferenceDAOMock implements PatientCrossReferenceDAO {
crossReferenceDBS.add(crossReferenceDBS(patient,""));
return crossReferenceDBS;
case "original3":
case "targetDomain":
return multipleCrossReferenceDBS(patient);
}
......
......@@ -2,12 +2,19 @@ package net.ihe.gazelle.app.patientregistryservice.application;
import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceSearch;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.application.UnrecognizedDomainException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryapi.business.Patient;
import net.ihe.gazelle.app.patientregistryapi.business.PatientAliases;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.references.CrossReferenceDB;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
......@@ -148,7 +155,74 @@ public class PatientCrossReferenceSearchImplTest {
}
Assert.assertEquals("The X-ref shall contain the patients matching with the sourceIdentifier, no cross ref has been found ", 9,
patientAliases.getMembers().size());
}
@Test
public void searchWithZeroTargetDomainMatching() throws SearchCrossReferenceException, UnrecognizedDomainException {
EntityIdentifier sourceIdentifier1 = new EntityIdentifier();
sourceIdentifier1.setSystemIdentifier("toto");
sourceIdentifier1.setValue("searchTargetDomainFound");
EntityIdentifier entityIdentifier2 = new EntityIdentifier();
entityIdentifier2.setSystemIdentifier("2222");
entityIdentifier2.setValue("2345");
EntityIdentifier entityIdentifier3 = new EntityIdentifier();
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(sourceIdentifier1);
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
PatientAliases patientAliases = patientCrossReferenceSearch.searchInTargetDomain(sourceIdentifier1, entityIdentifiers);
assert (patientAliases.getMembers().size() == 0);
}
@Test
public void searchWithOneTargetDomainMatching() throws SearchCrossReferenceException, UnrecognizedDomainException {
EntityIdentifier sourceIdentifier1 = new EntityIdentifier();
sourceIdentifier1.setSystemIdentifier("Retrieved11");
sourceIdentifier1.setValue("searchTargetDomainFound");
EntityIdentifier entityIdentifier2 = new EntityIdentifier();
entityIdentifier2.setSystemIdentifier("2222");
entityIdentifier2.setValue("2345");
EntityIdentifier entityIdentifier3 = new EntityIdentifier();
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(sourceIdentifier1);
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
PatientAliases patientAliases = patientCrossReferenceSearch.searchInTargetDomain(sourceIdentifier1, entityIdentifiers);
assert (patientAliases.getMembers().size() == 1);
}
@Test
void testWithUnkwownTargetDomain() throws SearchCrossReferenceException {
EntityIdentifier entityIdentifier = new EntityIdentifier();
entityIdentifier.setSystemIdentifier("1111");
entityIdentifier.setValue("1234");
EntityIdentifier entityIdentifier2 = new EntityIdentifier();
entityIdentifier2.setSystemIdentifier("2222");
entityIdentifier2.setValue("2345");
EntityIdentifier entityIdentifier3 = new EntityIdentifier();
entityIdentifier3.setSystemIdentifier("3333");
entityIdentifier3.setValue("3456");
List<EntityIdentifier> entityIdentifiers = new ArrayList<>();
entityIdentifiers.add(entityIdentifier2);
entityIdentifiers.add(entityIdentifier3);
UnrecognizedDomainException exception = assertThrows(UnrecognizedDomainException.class,
() -> patientCrossReferenceSearch.searchInTargetDomain(entityIdentifier, entityIdentifiers));
assertEquals("The current target domain could not be found", exception.getMessage());
}
}
\ No newline at end of file
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