Commit dcb0e8b5 authored by Franck Desaize's avatar Franck Desaize
Browse files

Code Review and refactoring

Add logger
parent c7c3c537
Pipeline #267981 failed with stages
in 13 minutes and 35 seconds
......@@ -2,7 +2,6 @@ package net.ihe.gazelle.app.patientregistryapi.application;
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;
......@@ -16,7 +15,7 @@ public interface PatientCrossReferenceSearch {
* retrieve the patient alias corresponding to the requested
*
* @param sourceIdentifier identifier of the reference patient
* @param targetDomains list of TargetDomains, the list cardinality must be [0..*]
* @param targetDomains list of TargetDomains, the list cardinality must be [0..*]
* @return a patient aliases that match the patient with sourceIdentifier
* @throws SearchCrossReferenceException if an error happens during the search processing.
*/
......
......@@ -5,7 +5,7 @@ import javax.persistence.*;
@Entity
@Table(name = "pam_hierarchic_designator", schema = "public")
@SequenceGenerator(name = "pam_hierarchic_designator_sequence", sequenceName = "pam_hierarchic_designator_id_seq", allocationSize = 1)
@NamedQuery(name = "HierarchicDesignatorDB.findByUniversalID", query = "SELECT hd FROM HierarchicDesignatorDB hd WHERE hd.universalID = :universalID and hd.usage =: givenUsage")
@NamedQuery(name = "HierarchicDesignatorDB.findByUniversalID", query = "SELECT hd FROM HierarchicDesignatorDB hd WHERE hd.universalID = :universalID AND hd.usage =: givenUsage")
public class HierarchicDesignatorDB {
private static final String DEFAULT_UNIVERSAL_ID_TYPE = "ISO";
......
......@@ -17,7 +17,7 @@ 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 pdb from PatientDB pdb, IN (pdb.patientIdentifiers) pid WHERE pid.identifier = :patient_identifier")
@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)
......
......@@ -14,7 +14,7 @@ import java.util.List;
@Table(name = "pam_patient_identifier", schema = "public", uniqueConstraints = @UniqueConstraint(columnNames = {
"full_patient_id", "identifier_type_code" }))
@SequenceGenerator(name = "pam_patient_identifier_sequence", sequenceName = "pam_patient_identifier_id_seq", allocationSize = 1)
@NamedQuery(name = "PatientIdentifierDB.findByHierarchicDomainAndId", query = "SELECT pi FROM PatientIdentifierDB pi WHERE pi.domain = :domain_id and pi.identifier = :id_number" )
@NamedQuery(name = "PatientIdentifierDB.findByHierarchicDomainAndId", query = "SELECT pi FROM PatientIdentifierDB pi WHERE pi.domain.id = :domain_id AND pi.identifier = :id_number" )
public class PatientIdentifierDB {
@Id
......
......@@ -36,6 +36,9 @@ public class CrossReferenceDB {
public CrossReferenceDB() {
this.lastChanged = new Date();
}
public void setId(Integer id){
this.id = id;
}
public List<PatientDB> getPatients() {
return patients;
......
package net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.references.CrossReferenceDB;
......@@ -9,8 +8,22 @@ import java.util.List;
public interface PatientCrossReferenceDAO {
PatientDB searchForPatientDBWithSourceIdentifier(EntityIdentifier sourceIdentifier) throws SearchCrossReferenceException;
/**
* Method to retrieve matching patient to given systemId and patientId in the system
* @param systemID of hte system
* @param patientIDInSystem Id of the patient in the system.
* @return The matching patientDB
* @throws SearchCrossReferenceException if an error occurred during the search
*/
PatientDB searchForPatientDBWithSourceIdentifier(String systemID, String patientIDInSystem) throws SearchCrossReferenceException;
List<CrossReferenceDB> searchForPatientAliasesWithPatientDB(PatientDB patient) throws SearchCrossReferenceException;
/**
* Search for a list of patientAliases by its patient cross reference id
*
* @param patientDBPixReferenceId retrieve a list of XRef matching with a patient
* @return a list of cross reference
* @throws SearchCrossReferenceException if an error occurred during the search
*/
List<CrossReferenceDB> searchForPatientAliasesWithPatientDB(Integer patientDBPixReferenceId) throws SearchCrossReferenceException;
}
package net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service;
import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceException;
import net.ihe.gazelle.app.patientregistryapi.business.EntityIdentifier;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.EntityManagerProducer;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.DesignatorTypeDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.HierarchicDesignatorDB;
......@@ -39,112 +38,75 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
}
/**
* Search for a patient by its entity identifier.
* The request need to go through HierarchicDesignatorDB to PatientIdentifierDB to PatientDB
*
* @param entityIdentifier sourceIdentifier
* @return PatientDB result
* @throws SearchCrossReferenceException if an error occurred during the search
* {@inheritDoc}
*/
@Override
public PatientDB searchForPatientDBWithSourceIdentifier(EntityIdentifier entityIdentifier) throws SearchCrossReferenceException {
checkSourceIdentifier(entityIdentifier);
String systemIdentifier = entityIdentifier.getSystemIdentifier();
String idNumber = entityIdentifier.getValue();
HierarchicDesignatorDB hierarchicDesignatorDB = searchForHierarchicDesignator(systemIdentifier);
PatientIdentifierDB patientIdentifier = searchForPatientIdentifier(hierarchicDesignatorDB, idNumber);
public PatientDB searchForPatientDBWithSourceIdentifier(String systemID, String patientIDInSystem) throws SearchCrossReferenceException {
if (systemID == null || systemID.isBlank()) {
throw new SearchCrossReferenceException("The system identifier from entityIdentifier cannot be null or empty");
}
if (patientIDInSystem == null || patientIDInSystem.isBlank()) {
throw new SearchCrossReferenceException("The value from entityIdentifier cannot be null or empty");
}
PatientDB patientDB = searchForPatient(patientIdentifier);
return patientDB;
Integer hierarchicDesignatorDID = searchForHierarchicDesignator(systemID);
String patientIdentifier = searchForPatientIdentifier(hierarchicDesignatorDID, patientIDInSystem);
return searchForPatient(patientIdentifier);
}
/**
* Search for a list of patientAliases by its patient cross reference id
*
* @param patientDB retrieve a list of XREf matching with a patient
* @return a list of cross reference
* @throws SearchCrossReferenceException if an error occurred during the search
* {@inheritDoc}
*/
@Override
public List<CrossReferenceDB> searchForPatientAliasesWithPatientDB(PatientDB patientDB) throws SearchCrossReferenceException {
if (patientDB != null) {
String uuid = patientDB.getUuid();
if (uuid == null || uuid.isBlank()) {
throw new IllegalArgumentException("The uuid of patientDB shall not be null");
}
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByCrossRefId", CrossReferenceDB.class);
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");
}
return crossReferenceDBS;
public List<CrossReferenceDB> searchForPatientAliasesWithPatientDB(Integer patientDBPixReferenceId) throws SearchCrossReferenceException {
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByCrossRefId",
CrossReferenceDB.class);
queryForPatientAliases.setParameter("id", patientDBPixReferenceId);
List<CrossReferenceDB> crossReferenceDBS = queryForPatientAliases.getResultList();
if (crossReferenceDBS.isEmpty() || crossReferenceDBS.get(0).getPatients().isEmpty()) {
throw new SearchCrossReferenceException("Search error : No X-ref for given Patient");
}
throw new IllegalArgumentException("Patient Cannot be null");
}
return crossReferenceDBS;
/**
* Check if the source identifier is valid
* <p>
* Throw an exception if the source identifier :
* - is null
* - has mandatory variables missing
* - is unknown
*
* @param entityIdentifier Check the content of the sourceIdentifier
* @throws SearchCrossReferenceException if an error occurred
*/
private void checkSourceIdentifier(EntityIdentifier entityIdentifier) throws SearchCrossReferenceException {
if (entityIdentifier == null) {
throw new SearchCrossReferenceException("The source identifier cannot be null");
}
String systemIdentifier = entityIdentifier.getSystemIdentifier();
String value = entityIdentifier.getValue();
if (systemIdentifier == null || systemIdentifier.isBlank()) {
throw new SearchCrossReferenceException("The system identifier from entityIdentifier cannot be null or empty");
}
if (value == null || value.isBlank()) {
throw new SearchCrossReferenceException("The value from entityIdentifier cannot be null or empty");
}
}
/**
* Search for a HierarchicDesignatorDB given a system identifier
*
* @param systemIdentifier to be check
* @return HierarchicDesignatorDB matching to the systemIdentifier
* @return Id of the matching HierarchicDesignatorDB to the systemIdentifier
* @throws SearchCrossReferenceException if an error occurred during the search
*/
private HierarchicDesignatorDB searchForHierarchicDesignator(String systemIdentifier) throws SearchCrossReferenceException {
private Integer searchForHierarchicDesignator(String systemIdentifier) throws SearchCrossReferenceException {
try {
TypedQuery<HierarchicDesignatorDB> queryForAPatient = entityManager.createNamedQuery("HierarchicDesignatorDB.findByUniversalID", HierarchicDesignatorDB.class);
TypedQuery<HierarchicDesignatorDB> queryForAPatient = entityManager.createNamedQuery("HierarchicDesignatorDB.findByUniversalID",
HierarchicDesignatorDB.class);
queryForAPatient.setParameter("universalID", systemIdentifier);
queryForAPatient.setParameter("givenUsage", DesignatorTypeDB.PATIENT_ID);
HierarchicDesignatorDB hierarchicDesignatorDB = queryForAPatient.getSingleResult();
return hierarchicDesignatorDB;
return queryForAPatient.getSingleResult().getId();
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("System not found");
throw new SearchCrossReferenceException("Error in the sourceIdentifier : System does not exit");
}
}
/**
* search for a PatientIdentifierDB given its HierarchicDesignatorDB and its id_number
*
* @param hierarchicDesignatorDB to check and retrieve
* @param idNumber of the patient in the system
* @param hierarchicDesignatorDBID id of the system domain
* @param idNumber of the patient in the system
* @return PatientIdentifierDB
* @throws SearchCrossReferenceException if an error occurred during the search
*/
private PatientIdentifierDB searchForPatientIdentifier(HierarchicDesignatorDB hierarchicDesignatorDB, String idNumber) throws SearchCrossReferenceException {
private String searchForPatientIdentifier(Integer hierarchicDesignatorDBID, String idNumber) throws SearchCrossReferenceException {
try {
TypedQuery<PatientIdentifierDB> queryForAPatient = entityManager.createNamedQuery("PatientIdentifierDB.findByHierarchicDomainAndId", PatientIdentifierDB.class);
queryForAPatient.setParameter("domain_id", hierarchicDesignatorDB);
TypedQuery<PatientIdentifierDB> queryForAPatient = entityManager.createNamedQuery("PatientIdentifierDB.findByHierarchicDomainAndId",
PatientIdentifierDB.class);
queryForAPatient.setParameter("domain_id", hierarchicDesignatorDBID);
queryForAPatient.setParameter("id_number", idNumber);
return queryForAPatient.getSingleResult();
return queryForAPatient.getSingleResult().getIdentifier();
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("PatientIdentifier not found");
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it does not match any identity");
}
}
......@@ -155,14 +117,13 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
* @return PatientDB matching the patient identifier
* @throws SearchCrossReferenceException if an error occurred during the search
*/
private PatientDB searchForPatient(PatientIdentifierDB patientIdentifier) throws SearchCrossReferenceException {
private PatientDB searchForPatient(String patientIdentifier) throws SearchCrossReferenceException {
try {
String patient_identifier = patientIdentifier.getIdentifier();
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByPatientIdentifier", PatientDB.class);
queryForAPatient.setParameter("patient_identifier", patient_identifier);
queryForAPatient.setParameter("patient_identifier", patientIdentifier);
return queryForAPatient.getSingleResult();
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("Patient not found with corresponding PatientIdentifier");
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it does not match any Patient");
}
}
......
......@@ -3,6 +3,8 @@ package net.ihe.gazelle.app.patientregistryservice.adapter.ws;
import com.gitb.ps.Void;
import com.gitb.ps.*;
import net.ihe.gazelle.app.patientregistryapi.adapter.ws.CrossReferenceProcessingWebServiceConstants;
import net.ihe.gazelle.framework.loggerservice.application.GazelleLogger;
import net.ihe.gazelle.framework.loggerservice.application.GazelleLoggerFactory;
import javax.ejb.Stateless;
import javax.inject.Inject;
......@@ -16,7 +18,7 @@ import javax.jws.WebService;
targetNamespace = "http://www.gitb.com/ps/v1/",
endpointInterface = "com.gitb.ps.ProcessingService")
public class CrossReferenceProcessingService implements ProcessingService {
private static final GazelleLogger LOGGER = GazelleLoggerFactory.getInstance().getLogger(CrossReferenceProcessingService.class);
private static final String UNSUPPORTED_OPERATION = "Unsupported Operation";
@Inject
......@@ -44,10 +46,14 @@ public class CrossReferenceProcessingService implements ProcessingService {
@Override
public ProcessResponse process(ProcessRequest processRequest) {
LOGGER.info("The CrossReference GITB Service received a request");
if (processRequest != null) {
if (CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION.equals(processRequest.getOperation())) {
LOGGER.info("The CrossReference GITB Service received a Search XReference request");
return crossReferenceSearchProcessingService.processPatientCrossReferenceSearch(processRequest);
} else {
LOGGER.error("ProcessRequest.operation shall be %s !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION);
throw new UnsupportedOperationException(String.format("ProcessRequest.operation shall be %s !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION));
}
......
......@@ -10,6 +10,8 @@ import net.ihe.gazelle.app.patientregistryapi.application.PatientCrossReferenceS
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.framework.loggerservice.application.GazelleLogger;
import net.ihe.gazelle.framework.loggerservice.application.GazelleLoggerFactory;
import net.ihe.gazelle.lib.gitbutils.adapter.GITBAnyContentType;
import net.ihe.gazelle.lib.gitbutils.adapter.MapperAnyContentToObject;
import net.ihe.gazelle.lib.gitbutils.adapter.MapperObjectToAnyContent;
......@@ -23,7 +25,7 @@ import java.util.List;
* Processing service for Patient X-Ref Search requests.
*/
public class CrossReferenceSearchProcessingService {
private static final GazelleLogger LOGGER = GazelleLoggerFactory.getInstance().getLogger(CrossReferenceSearchProcessingService.class);
private final MapperAnyContentToObject mapper;
private final PatientCrossReferenceSearch crossReferenceSearch;
......@@ -46,42 +48,55 @@ public class CrossReferenceSearchProcessingService {
* @return ProcessResponse corresponding to the received request
*/
public ProcessResponse processPatientCrossReferenceSearch(ProcessRequest processRequest) {
PatientAliases patientAliases;
LOGGER.info("Proceed with the XReferenceSearch request");
List<AnyContent> searchParameter = processRequest.getInput();
if (searchParameter.size() != 2) {
LOGGER.error("ProcessRequest.input shall contain two parameters to retrieve Xref !");
throw new IllegalArgumentException("ProcessRequest.input shall contain two parameters to retrieve Xref !");
}
try {
PatientAliases patientAliases;
AnyContent sourceIdentifier = searchParameter.get(0);
AnyContent targetDomains = searchParameter.get(1);
if (targetDomains == null) {
if(!CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME.equals(sourceIdentifier.getName())) {
if (!CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME.equals(sourceIdentifier.getName())) {
LOGGER.error(String.format("ProcessRequest.inputs shall have the name '%s' and '%s' for a %s operation !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_TARGET_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION));
throw new IllegalArgumentException(String.format("ProcessRequest.inputs shall have the name '%s' and '%s' for a %s operation !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_TARGET_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION));
}
LOGGER.info("Search with sourceIdentifier");
patientAliases = crossReferenceSearch.search(mapper.getObject(searchParameter.get(0), EntityIdentifier.class),
new ArrayList<String>());
} else {
if(!CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME.equals(sourceIdentifier.getName()) ||
if (!CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME.equals(sourceIdentifier.getName()) ||
!CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_TARGET_NAME.equals(targetDomains.getName())) {
LOGGER.error(String.format("ProcessRequest.inputs shall have the name '%s' and '%s' for a %s operation !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_TARGET_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION));
throw new IllegalArgumentException(String.format("ProcessRequest.inputs shall have the name '%s' and '%s' for a %s operation !",
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_INPUT_TARGET_NAME,
CrossReferenceProcessingWebServiceConstants.CROSS_REFERENCE_PATIENT_SEARCH_OPERATION));
}
LOGGER.info("Search with sourceIdentifier and targetDomains ");
patientAliases = crossReferenceSearch.search(mapper.getObject(searchParameter.get(0), EntityIdentifier.class),
mapper.getObjectCollection(searchParameter.get(1), ArrayList.class, String.class));
}
LOGGER.info("Create GITB Response");
return createPatientSearchProcessResponse(patientAliases);
} catch (MappingException e) {
LOGGER.error(e, "Mapping exception");
throw new IllegalArgumentException("Cannot decode Request inputs as SourceIdentifier !", e);
} catch (SearchCrossReferenceException e) {
LOGGER.error(e, "SearchCrossReferenceException has been raised");
return ProcessResponseWithReportCreator.createFailedProcessResponse(e, searchParameter.get(0));
}
}
......@@ -97,7 +112,6 @@ public class CrossReferenceSearchProcessingService {
*/
private ProcessResponse createPatientSearchProcessResponse(PatientAliases xRefPatient) throws SearchCrossReferenceException {
AnyContent patientAliasesAnyContent;
ProcessResponse processResponse = new ProcessResponse();
if (xRefPatient == null) {
throw new IllegalArgumentException("Successful operation shall return a not null Patient Aliases as an output !");
}
......@@ -113,6 +127,7 @@ public class CrossReferenceSearchProcessingService {
} catch (MappingException e) {
throw new SearchCrossReferenceException("Search request response cannot be mapped to GITB AnyContent !", e);
}
ProcessResponse processResponse = new ProcessResponse();
processResponse.getOutput().add(patientAliasesAnyContent);
processResponse.setReport(ProcessResponseWithReportCreator.createReport(TestResultType.SUCCESS));
return processResponse;
......
......@@ -6,11 +6,12 @@ import net.ihe.gazelle.app.patientregistryapi.application.SearchCrossReferenceEx
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.converter.PatientAliasesConverter;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.converter.PatientConverter;
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 net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.service.PatientCrossReferenceDAO;
import net.ihe.gazelle.framework.loggerservice.application.GazelleLogger;
import net.ihe.gazelle.framework.loggerservice.application.GazelleLoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
......@@ -20,6 +21,7 @@ import java.util.List;
public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSearch {
private PatientCrossReferenceDAO patientCrossReferenceDAO;
private DomainService domainService;
private static final GazelleLogger LOGGER = GazelleLoggerFactory.getInstance().getLogger(PatientCrossReferenceSearchImpl.class);
@Inject
public PatientCrossReferenceSearchImpl(
......@@ -30,7 +32,6 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
/**
* Search for a patient alias by a source identifier
* <p>
* Verify that the source identifier is valid
* Call the DAO to get a list of patient alias
* Merge those patient aliases into one
......@@ -43,86 +44,38 @@ public class PatientCrossReferenceSearchImpl implements PatientCrossReferenceSea
@Override
public PatientAliases search(EntityIdentifier sourceIdentifier, List<String> targetDomains) throws SearchCrossReferenceException {
checkSourceIdentifier(sourceIdentifier);
boolean usableTargetList = checkTargetDomains(targetDomains);
boolean usableTargetList = checkTargetDomains(targetDomains);
PatientDB patientDB = null;
PatientAliases patientAlias = null;
try {
patientDB = patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(sourceIdentifier);
List<CrossReferenceDB> crossReferenceDBS = patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB);
patientAlias = processingOnResult(crossReferenceDBS, patientDB);
} catch (SearchCrossReferenceException searchCrossReferenceException) {
switch (searchCrossReferenceException.getMessage()) {
case "System not found":
throw new SearchCrossReferenceException("Error in the sourceIdentifier : System does not exit");
case "PatientIdentifier not found":
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it does not match any identity");
case "Patient not found with with corresponding PatientIdentifier":
throw new SearchCrossReferenceException("Error in the sourceIdentifier : it does not match any Patient");
case "Search error : No X-ref for given Patient":
return convertPatientDBToPatientAliases(patientDB);
default:
throw searchCrossReferenceException;
try {
String systemIdentifier = sourceIdentifier.getSystemIdentifier();
String id = sourceIdentifier.getValue();
patientDB = patientCrossReferenceDAO.searchForPatientDBWithSourceIdentifier(systemIdentifier,
id);
checkRetrievedPatientDB(patientDB);
LOGGER.info("Patient with uuid : %s has been retrieved for system %s and id %s", patientDB.getUuid(), systemIdentifier, id);
if (patientDB.getPixReference() == null) {
LOGGER.error("Patient does not have any CrossReference");
return convertPatientDBToPatientAliases(patientDB);
} else {
LOGGER.info("Retrieve XReference");
List<CrossReferenceDB> crossReferenceDBS =
patientCrossReferenceDAO.searchForPatientAliasesWithPatientDB(patientDB.getPixReference().getId());
LOGGER.info("Retrieved XReference");
return processingOnResult(crossReferenceDBS, patientDB, usableTargetList, targetDomains);
}
} catch (SearchCrossReferenceException searchCrossReferenceException) {
LOGGER.error(searchCrossReferenceException.getMessage());
throw searchCrossReferenceException;
} catch (Exception exception) {
LOGGER.error(exception, "Internal Error occurred");
throw new SearchCrossReferenceException("An error occurred during CrossReference Search", exception);
}
if (patientAlias == null) {
throw new SearchCrossReferenceException("An error occurred during CrossReference Search and the patient aliases could not have been set");
}
if(usableTargetList){
return removePatientFromPatientAliasWithNonMatchingTargetDomain(patientAlias, targetDomains);
}else {
return patientAlias;
}
}
/**
* 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 to be filtered by target system
* @param targetDomains to filter
* @return filtered PatientAliases matching target domains
*/
private PatientAliases removePatientFromPatientAliasWithNonMatchingTargetDomain(PatientAliases patientAlias, List<String> targetDomains) {
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;
}
/**
* Check if the list of target domain contains the current source identifier
* Return true if the list is not empty, false otherwise
*
* @param targetDomains to be check
* @return usableTargetDomains
* @throws SearchCrossReferenceException if an error occurred
*/
private boolean checkTargetDomains(List<String> targetDomains) throws SearchCrossReferenceException {
if (targetDomains == null || targetDomains.isEmpty()) {
return false;
}
for (String targetDomain : targetDomains) {
if (targetDomain.isBlank() || !domainService.exist(targetDomain)) {
throw new SearchCrossReferenceException("One of the target domain does not exist");
}
}
return true;
}