Commit 01cd1f98 authored by Me's avatar Me
Browse files

Add some verification

parent a9874aeb
Pipeline #275306 failed with stages
in 5 minutes and 18 seconds
......@@ -5,97 +5,107 @@ import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.GenderCodeDB;
import org.apache.commons.text.similarity.LevenshteinDistance;
import net.ihe.gazelle.app.patientregistryapi.business.GenderCode;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.patient.PatientDB;
import net.ihe.gazelle.app.patientregistryservice.adapter.dao.pamsimulator.model.references.CrossReferenceDB;
/**
* Class called upon creating or updating any Patient in the Database to check if given Patient could partake in any already existing CrossReference
*
* @author pvm
*
* @author pvm
*/
public class PatientCrossReferenceApplication {
/**
* Private constructor to override public implicit one
*/
private PatientCrossReferenceApplication() { }
/**
* Static method to compare one Patient to a collection of Patient
*
* The comparison includes these fields with their respective criteria :
* - The birthDate of the patients. Must be the same
* - The Gender of the patients. Must be the same
* - The familyName of the patients. Must have a levenshtein distance less than 3
* - The givenName of the patients. Must have a levenshtein distance less than 3
* @param toCompare : the Patient to create or update in the Database and so to check if any CrossReference for it already exists
* @param comparing : the collection of Patient to compare the similarities
* @return a CrossReferenceDB object containing the created/updated patient.
*/
public static CrossReferenceDB crossReferenciable(PatientDB toCompare, List<PatientDB> comparing) {
LocalDate birthDate = convertDateToLocalDate(toCompare.getDateOfBirth());
GenderCode gender = toCompare.getGenderCode().toGenderCode();
String familyName = toCompare.getLastName();
String givenName = toCompare.getFirstName();
for (PatientDB patou : comparing) {
if (compare(birthDate, gender, familyName, givenName, patou)) {
return patou.getPixReference();
}
}
return new CrossReferenceDB();
}
/**
* Private Method to convert the deprecate Date type used in Databases to LocalDate.
* @param toConvert Date to convert
* @return a LocalDate object corresponding to the Date given in parameter
*/
private static LocalDate convertDateToLocalDate(Date toConvert) {
return toConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* Private method to compare a single Patient to the collection of data from the created/updated patient.
*
* @param birthDate from the created/updated patient
* @param gender from the created/updated patient
* @param familyName from the created/updated patient
* @param givenName from the created/updated patient
* @param toCompare The PAtient in the Database to compare all these fields with
* @return true if the Patient falls under the threshold of comparison, false if not
*/
private static boolean compare(LocalDate birthDate, GenderCode gender, String familyName, String givenName, PatientDB toCompare) {
LocalDate compareBirthDate = convertDateToLocalDate(toCompare.getDateOfBirth());
if (!birthDate.isEqual(compareBirthDate)) {
return false;
}
GenderCode compareGender = toCompare.getGenderCode().toGenderCode();
if (gender != compareGender) {
return false;
}
LevenshteinDistance stringCompare = new LevenshteinDistance();
String compareFamilyName = toCompare.getLastName();
if (stringCompare.apply(familyName, compareFamilyName) >= 3) {
return false;
}
String compareGivenName = toCompare.getFirstName();
if (stringCompare.apply(givenName, compareGivenName) >= 3 ) {
return false;
}
return true;
}
/**
* Private constructor to override public implicit one
*/
private PatientCrossReferenceApplication() {
}
/**
* Static method to compare one Patient to a collection of Patient
* <p>
* The comparison includes these fields with their respective criteria :
* - The birthDate of the patients. Must be the same
* - The Gender of the patients. Must be the same
* - The familyName of the patients. Must have a levenshtein distance less than 3
* - The givenName of the patients. Must have a levenshtein distance less than 3
*
* @param toCompare : the Patient to create or update in the Database and so to check if any CrossReference for it already exists
* @param comparing : the collection of Patient to compare the similarities
* @return a CrossReferenceDB object containing the created/updated patient.
*/
public static CrossReferenceDB crossReferenciable(PatientDB comparedPatient, List<PatientDB> comparing) {
if (comparedPatient != null) {
for (PatientDB patou : comparing) {
if (isEqual(comparedPatient, patou)) {
return patou.getPixReference();
}
}
}
return new CrossReferenceDB();
}
/**
* Private Method to convert the deprecate Date type used in Databases to LocalDate.
*
* @param toConvert Date to convert
* @return a LocalDate object corresponding to the Date given in parameter
*/
private static LocalDate convertDateToLocalDate(Date toConvert) {
return toConvert.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
/**
* Private method to compare a single Patient to the collection of data from the created/updated patient.
*
* @param comparedPatient
* @param toCompare
* @return
*/
private static boolean isEqual(PatientDB comparedPatient, PatientDB toCompare) {
Date comparedPatientBirthDate = comparedPatient.getDateOfBirth();
Date toComparePatientBirthDate = toCompare.getDateOfBirth();
GenderCodeDB gender = comparedPatient.getGenderCode();
GenderCodeDB gender2 = toCompare.getGenderCode();
String familyName1 = comparedPatient.getLastName();
String familyName2 = toCompare.getLastName();
String givenName1 = comparedPatient.getFirstName();
String givenName2 = toCompare.getFirstName();
return isEqualBirthDate(comparedPatientBirthDate, toComparePatientBirthDate) && isEqualGender(gender, gender2)
&& isLevenshteinComparisonAbove3(familyName1, familyName2) && isLevenshteinComparisonAbove3(givenName1, givenName2);
}
private static boolean isEqualBirthDate(Date date1, Date date2) {
if (date1 != null && date2 != null) {
return convertDateToLocalDate(date1).equals(convertDateToLocalDate(date2));
}
return false;
}
private static boolean isEqualGender(GenderCodeDB genderCodeDB1, GenderCodeDB genderCodeDB2) {
if (genderCodeDB1 != null && genderCodeDB2 != null) {
return genderCodeDB1.toGenderCode().equals(genderCodeDB2.toGenderCode());
}
return false;
}
private static boolean isLevenshteinComparisonAbove3(String string1, String string2) {
if (string1 != null && !string1.isBlank() && string2 != null && string2.isBlank()) {
LevenshteinDistance stringCompare = new LevenshteinDistance();
return stringCompare.apply(string1, string2) >= 3;
}
return false;
}
}
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