Commit 83bc2b89 authored by Matthieu Bonnouvrier's avatar Matthieu Bonnouvrier
Browse files

Add DAO TUs

parent 09921b95
Pipeline #262808 failed with stages
in 4 minutes and 42 seconds
......@@ -12,100 +12,100 @@ import java.util.List;
@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.findById", query = "SELECT cr FROM CrossReferenceDB cr WHERE cr.id = :id")
@NamedQuery(name = "CrossReferenceDB.findByPatientId", query = "SELECT cr FROM CrossReferenceDB cr WHERE :uuid = id")
@NamedQuery(name = "CrossReferenceDB.countPatient", query = "SELECT COUNT (cr) FROM CrossReferenceDB cr WHERE :patient in (cr.patients)")
public class CrossReferenceDB {
@Id
@GeneratedValue(generator = "pix_cross_reference_sequence", strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Integer id;
@OneToMany(mappedBy = "pixReference", fetch = FetchType.LAZY)
private List<PatientDB> patients;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_changed")
private Date lastChanged;
@Column(name = "last_modifier")
private String lastModifier;
public CrossReferenceDB(String username) {
this.lastChanged = new Date();
this.lastModifier = username;
}
public CrossReferenceDB() {
this.lastChanged = new Date();
}
public List<PatientDB> getPatients() {
return patients;
}
public void setPatients(List<PatientDB> patients) {
this.patients = patients;
}
public Date getLastChanged() {
if (lastChanged != null){
return (Date) lastChanged.clone();
}
return null;
}
public void setLastChanged(Date lastChanged) {
if (lastChanged != null) {
this.lastChanged = (Date) lastChanged.clone();
} else {
this.lastChanged = null;
}
}
public String getLastModifier() {
return lastModifier;
}
public void setLastModifier(String lastModifier) {
this.lastModifier = lastModifier;
}
public Integer getId() {
return id;
}
public CrossReferenceDB updateReference(EntityManager entityManager, String user) {
this.lastChanged = new Date();
this.lastModifier = user;
return entityManager.merge(this);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CrossReferenceDB)) {
return false;
}
CrossReferenceDB that = (CrossReferenceDB) o;
if (patients != null ? !patients.equals(that.patients) : that.patients != null) {
return false;
}
if (lastChanged != null ? !lastChanged.equals(that.lastChanged) : that.lastChanged != null) {
return false;
}
return lastModifier != null ? lastModifier.equals(that.lastModifier) : that.lastModifier == null;
}
@Override
public int hashCode() {
int result = patients != null ? patients.hashCode() : 0;
result = 31 * result + (lastChanged != null ? lastChanged.hashCode() : 0);
result = 31 * result + (lastModifier != null ? lastModifier.hashCode() : 0);
return result;
}
@Id
@GeneratedValue(generator = "pix_cross_reference_sequence", strategy = GenerationType.SEQUENCE)
@Column(name = "id")
private Integer id;
@OneToMany(mappedBy = "pixReference", fetch = FetchType.LAZY)
private List<PatientDB> patients;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "last_changed")
private Date lastChanged;
@Column(name = "last_modifier")
private String lastModifier;
public CrossReferenceDB(String username) {
this.lastChanged = new Date();
this.lastModifier = username;
}
public CrossReferenceDB() {
this.lastChanged = new Date();
}
public List<PatientDB> getPatients() {
return patients;
}
public void setPatients(List<PatientDB> patients) {
this.patients = patients;
}
public Date getLastChanged() {
if (lastChanged != null) {
return (Date) lastChanged.clone();
}
return null;
}
public void setLastChanged(Date lastChanged) {
if (lastChanged != null) {
this.lastChanged = (Date) lastChanged.clone();
} else {
this.lastChanged = null;
}
}
public String getLastModifier() {
return lastModifier;
}
public void setLastModifier(String lastModifier) {
this.lastModifier = lastModifier;
}
public Integer getId() {
return id;
}
public CrossReferenceDB updateReference(EntityManager entityManager, String user) {
this.lastChanged = new Date();
this.lastModifier = user;
return entityManager.merge(this);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof CrossReferenceDB)) {
return false;
}
CrossReferenceDB that = (CrossReferenceDB) o;
if (patients != null ? !patients.equals(that.patients) : that.patients != null) {
return false;
}
if (lastChanged != null ? !lastChanged.equals(that.lastChanged) : that.lastChanged != null) {
return false;
}
return lastModifier != null ? lastModifier.equals(that.lastModifier) : that.lastModifier == null;
}
@Override
public int hashCode() {
int result = patients != null ? patients.hashCode() : 0;
result = 31 * result + (lastChanged != null ? lastChanged.hashCode() : 0);
result = 31 * result + (lastModifier != null ? lastModifier.hashCode() : 0);
return result;
}
}
......@@ -41,16 +41,15 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
this.entityManager = entityManager;
}
@Override
public PatientDB searchForPatientDBWithSourceIdentifier(EntityIdentifier sourceIdentifier) throws SearchCrossReferenceException {
String systemIdentifier = sourceIdentifier.getSystemIdentifier();
String value = sourceIdentifier.getValue();
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, value);
PatientIdentifierDB patientIdentifier = searchForPatientIdentifier(hierarchicDesignatorDB, idNumber);
PatientDB patientDB = searchforPatient(patientIdentifier);
return patientDB;
......@@ -64,18 +63,42 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
if (uuid == null || uuid.isBlank()) {
throw new IllegalArgumentException("The uuid of patientDB shall not be null");
}
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByPatient", CrossReferenceDB.class);
TypedQuery<CrossReferenceDB> queryForPatientAliases = entityManager.createNamedQuery("CrossReferenceDB.findByPatientId", CrossReferenceDB.class);
queryForPatientAliases.setParameter("patient", patientDB);
queryForPatientAliases.setParameter("uuid", patientDB.getUuid());
List<CrossReferenceDB> crossReferenceDBS = queryForPatientAliases.getResultList();
if (crossReferenceDBS.isEmpty()) {
throw new SearchCrossReferenceException("Search error : No X-ref for given Patient");
}
return crossReferenceDBS;
}
throw new IllegalArgumentException("Patient Cannot be null ");
throw new IllegalArgumentException("Patient Cannot be null");
}
/**
* 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
* @throws SearchCrossReferenceException
*/
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");
}
}
private HierarchicDesignatorDB searchForHierarchicDesignator(String systemIdentifier) throws SearchCrossReferenceException {
try {
......@@ -83,7 +106,7 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
queryForAPatient.setParameter("universalID", systemIdentifier);
HierarchicDesignatorDB hierarchicDesignatorDB = queryForAPatient.getSingleResult();
return hierarchicDesignatorDB;
} catch (NoResultException | NonUniqueResultException exception) {
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("System not found");
}
}
......@@ -94,17 +117,18 @@ public class PatientCrossReferenceDAOImpl implements PatientCrossReferenceDAO {
queryForAPatient.setParameter("domain_id", hierarchicDesignatorDB);
queryForAPatient.setParameter("id_number", idNumber);
return queryForAPatient.getSingleResult();
} catch (NoResultException | NonUniqueResultException exception) {
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("PatientIdentifier not found");
}
}
private PatientDB searchforPatient(PatientIdentifierDB patientIdentifier) throws SearchCrossReferenceException {
try {
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByPatientIdentifier", PatientDB.class);
queryForAPatient.setParameter("patientIdentifier", patientIdentifier);
String uuid = patientIdentifier.getIdentifier();
TypedQuery<PatientDB> queryForAPatient = entityManager.createNamedQuery("PatientDB.findByUuid", PatientDB.class);
queryForAPatient.setParameter("uuid", uuid);
return queryForAPatient.getSingleResult();
} catch (NoResultException | NonUniqueResultException exception) {
} catch (NoResultException exception) {
throw new SearchCrossReferenceException("Patient not found with with corresponding PatientIdentifier");
}
}
......
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