Skip to content

Commit

Permalink
EA-198: Add support for configuring Mother Child relationships within…
Browse files Browse the repository at this point in the history
… the EMR API module
  • Loading branch information
mogoodrich committed Aug 5, 2024
1 parent 238b854 commit a1367c7
Show file tree
Hide file tree
Showing 9 changed files with 570 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,6 @@ public class EmrApiConstants {
));

public static final String GP_USE_LEGACY_DIAGNOSIS_SERVICE = "emrapi.useLegacyDiagnosisService";

public static final String METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE = "emrapi.motherChildRelationshipType";
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.openmrs.PatientIdentifierType;
import org.openmrs.PersonAttributeType;
import org.openmrs.Provider;
import org.openmrs.RelationshipType;
import org.openmrs.Role;
import org.openmrs.VisitType;
import org.openmrs.module.emrapi.diagnosis.DiagnosisMetadata;
Expand Down Expand Up @@ -347,4 +348,8 @@ public List<Disposition> getDispositions() {
public DispositionDescriptor getDispositionDescriptor() {
return dispositionService.getDispositionDescriptor();
}

public RelationshipType getMotherChildRelationshipType() {
return getEmrApiMetadataByCode(RelationshipType.class, EmrApiConstants.METADATA_MAPPING_MOTHER_CHILD_RELATIONSHIP_TYPE, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.openmrs.module.emrapi.maternal;

import java.util.List;

import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.api.OpenmrsService;

public interface MaternalService extends OpenmrsService {

/**
* Returns all "newborns" of the specified patient, where "newborn" is defined as a patient who is:
* - linked to the specified patient by a relationship of type emrapi.motherChildRelationshipType
* - has as an active visit (at the visitLocation, if specified)
* - has a birthdate that is on or after the start date of the mother's active visit (at the visitLocation, if specified) (note matches on date, not datetime to account for retrospective data entry or only have a date component of birthdate)
*
* @param mother
* @param visitLocation if not null, restrict matching visits to only those at the specified location
* @return
*/
public List<Newborn> getNewbornsByMother(Patient mother, Location visitLocation);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.openmrs.module.emrapi.maternal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.openmrs.Location;
import org.openmrs.Patient;
import org.openmrs.RelationshipType;
import org.openmrs.Visit;
import org.openmrs.api.APIException;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.db.EmrApiDAO;

public class MaternalServiceImpl extends BaseOpenmrsService implements MaternalService {

private EmrApiProperties emrApiProperties;

private EmrApiDAO emrApiDAO;

public void setEmrApiProperties(EmrApiProperties emrApiProperties) {
this.emrApiProperties = emrApiProperties;
}

public void setEmrApiDAO(EmrApiDAO emrApiDAO) {
this.emrApiDAO = emrApiDAO;
}

public List<Newborn> getNewbornsByMother(Patient mother, Location visitLocation) {

RelationshipType motherChildRelationshipType = emrApiProperties.getMotherChildRelationshipType();

if (motherChildRelationshipType == null) {
throw new APIException("Mother-Child relationship type has not been configured");
}

if (mother == null) {
throw new APIException("Mother cannot be null");
}

Map<String, Object> parameters = new HashMap<>();
parameters.put("mother", mother);
parameters.put("motherChildRelationshipType", motherChildRelationshipType);
parameters.put("visitLocation", visitLocation);

List<?> l = emrApiDAO.executeHqlFromResource("hql/newborns_by_mother.hql", parameters, List.class);

List<Newborn> ret = new ArrayList<>();

for (Object req : l) {
Object[] row = (Object[]) req;
Newborn newborn = new Newborn();
newborn.setNewborn((Patient) row[0]);
newborn.setNewbornVisit((Visit) row[1]);
ret.add(newborn);
}

return ret;
}
}
11 changes: 11 additions & 0 deletions api/src/main/java/org/openmrs/module/emrapi/maternal/Newborn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.openmrs.module.emrapi.maternal;

import lombok.Data;
import org.openmrs.Patient;
import org.openmrs.Visit;

@Data
public class Newborn {
private Patient newborn;
private Visit newbornVisit;
}
24 changes: 24 additions & 0 deletions api/src/main/resources/hql/newborns_by_mother.hql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
select
baby,
babyVisit
from
Person as mother,
Person as baby,
Relationship as motherChildRelationship,
Visit as motherVisit,
Visit as babyVisit
where
mother = :mother
and motherChildRelationship.personA = mother
and motherChildRelationship.personB = baby
and motherChildRelationship.relationshipType = :motherChildRelationshipType
and motherVisit.patient = mother and motherVisit.stopDatetime is null
and babyVisit.patient = baby and babyVisit.stopDatetime is null
and year(baby.birthdate) >= year(motherVisit.startDatetime)
and month(baby.birthdate) >= month(motherVisit.startDatetime)
and day(baby.birthdate) >= day(motherVisit.startDatetime)
and (:visitLocation is null or (motherVisit.location = :visitLocation and babyVisit.location = :visitLocation))
and mother.voided = false and baby.voided = false and motherChildRelationship.voided = false and motherVisit.voided = false and babyVisit.voided = false



27 changes: 27 additions & 0 deletions api/src/main/resources/moduleApplicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@
</property>
</bean>

<bean id="maternalService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
</property>
<property name="target">
<bean class="org.openmrs.module.emrapi.maternal.MaternalServiceImpl">
<property name="emrApiProperties" ref="emrApiProperties"/>
<property name="emrApiDAO" ref="emrApiDAOImpl"/>
</bean>
</property>
<property name="preInterceptors">
<ref bean="serviceInterceptors"/>
</property>
<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource"/>
</property>
</bean>

<bean parent="serviceContext">
<property name="moduleService">
<list merge="true">
<value>org.openmrs.module.emrapi.maternal.MaternalService</value>
<ref bean="maternalService"/>
</list>
</property>
</bean>

<bean id="exitFromCareService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager"/>
Expand Down
Loading

0 comments on commit a1367c7

Please sign in to comment.