Skip to content

Commit

Permalink
Only create XDS Documents for encounters with appropriate lab orders (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher authored Jul 11, 2024
1 parent 97d1885 commit 1e4c89a
Show file tree
Hide file tree
Showing 4 changed files with 541 additions and 39 deletions.
19 changes: 19 additions & 0 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

import org.apache.commons.lang.exception.ExceptionUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.api.ConceptService;
import org.openmrs.api.context.Context;
import org.openmrs.event.Event;
import org.openmrs.event.EventListener;
Expand All @@ -34,12 +37,17 @@ public class EncounterEventListener implements EventListener {
private static final String WILDCARD_MATCH = "ALL";

private static final String PROP_ENCOUNTER_TYPE_TO_PROCESS = "xdssender.encounterTypesToProcess";

// N.B. The following three constants are added to restrict XDSSender to only sending lab orders
private static final int TESTS_ORDERED_CONCEPT_ID = 1271;
private static volatile int VIRAL_LOAD_CONCEPT_ID = -1;
private static volatile int EARLY_INFANT_DIAGNOSIS_CONCEPT_ID = -1;

@Autowired
private XdsSenderConfig config;
@Autowired
private PatientEcidUpdater ecidUpdater;

// @Autowired
// private PatientEcidUpdater ecidUpdater;

@Override
public void onMessage(Message message) {
Expand All @@ -48,54 +56,63 @@ public void onMessage(Message message) {
String messageAction = mapMessage.getString("action");

Context.openSession();
Context.authenticate(config.getOpenmrsUsername(), config.getOpenmrsPassword());

if (Event.Action.CREATED.toString().equals(messageAction)) {

LOGGER.debug("Encounter event detected");
String uuid = ((MapMessage) message).getString("uuid");
List<String> encounterTypesToProcess = null;
String propEncounterTypesToProcess = Context.getAdministrationService()
.getGlobalProperty(PROP_ENCOUNTER_TYPE_TO_PROCESS);

if (propEncounterTypesToProcess != null) {
LOGGER.debug("Encounter types to filter detected");

encounterTypesToProcess = Arrays.asList(propEncounterTypesToProcess.split(","));
if (encounterTypesToProcess.size() > 0) {
if (encounterTypesToProcess.contains(WILDCARD_MATCH)) {
LOGGER.debug("Sending for all encounter types");
exportEncounter(uuid);

} else {
LOGGER.debug("Found {} encounter types to filter from global config",
encounterTypesToProcess.size());
for (String encounterTypeUuid : encounterTypesToProcess) {
try {
Context.authenticate(config.getOpenmrsUsername(), config.getOpenmrsPassword());

if (Event.Action.CREATED.toString().equals(messageAction)) {
LOGGER.debug("Encounter event detected");
String uuid = ((MapMessage) message).getString("uuid");
List<String> encounterTypesToProcess = null;
String propEncounterTypesToProcess = Context.getAdministrationService()
.getGlobalProperty(PROP_ENCOUNTER_TYPE_TO_PROCESS);

if (propEncounterTypesToProcess != null) {
LOGGER.debug("Encounter types to filter detected");

encounterTypesToProcess = Arrays.asList(propEncounterTypesToProcess.split(","));
if (!encounterTypesToProcess.isEmpty()) {
if (encounterTypesToProcess.contains(WILDCARD_MATCH)) {
LOGGER.debug("Sending for all encounter types");
exportEncounter(uuid);

} else {
LOGGER.debug("Found {} encounter types to filter from global config",
encounterTypesToProcess.size());
LOGGER.debug("Matching encounter types to send XDS repository: {}", uuid);
Encounter e = Context.getEncounterService().getEncounterByUuid(uuid);
if (encounterTypeUuid.equals(e.getEncounterType().getUuid().toString())) {
LOGGER.debug("Exporting encounter type {} to XDS repository", uuid);
exportEncounter(uuid);


// Since we are no longer using the XDSSender to send everything to an XDS Repository,
// we want to check that this encounter has an appropriate "order". Note that "orders"
// are stored as obs
boolean shouldSendEncounter = shouldSendEncounter(e);

if (shouldSendEncounter) {
for (String encounterTypeUuid : encounterTypesToProcess) {
if (encounterTypeUuid.equals(e.getEncounterType().getUuid())) {
LOGGER.debug("Exporting encounter {} to XDS repository", uuid);
exportEncounter(uuid);
}
}
} else {
LOGGER.info("Skipping encounter {} because there are no appropriate lab orders", uuid);
}
}
} else {
LOGGER.debug("No Encounter types filter detected");
exportEncounter(uuid);
}
} else {
LOGGER.debug("No Encounter types filter detected");
exportEncounter(uuid);

}

}

} finally {
Context.closeSession();
}

Context.closeSession();
}
catch (JMSException e) {
System.out.println("Some error occurred" + e.getErrorCode());
System.out.println("Some error occurred: " + e.getErrorCode());
}
}

private void exportEncounter(String encounterUuid) {
Encounter encounter = Context.getEncounterService().getEncounterByUuid(encounterUuid);
if (encounter.getForm() == null) {
Expand Down Expand Up @@ -136,4 +153,62 @@ private String prepareParameters(Encounter encounter, Patient patient) {
throw new RuntimeException("Cannot prepare parameters for OutgoingMessageException", e);
}
}

private boolean shouldSendEncounter(Encounter e) {
boolean shouldSendEncounter = false;
if (getViralLoadConceptId() != -1 && getEarlyInfantDiagnosisConceptId() != -1) {
for (Obs obs : e.getObs()) {
if (obs.getConcept() != null &&
obs.getConcept().getConceptId() == TESTS_ORDERED_CONCEPT_ID &&
obs.getValueCoded() != null && (
obs.getValueCoded().getConceptId() == getViralLoadConceptId() ||
obs.getValueCoded().getConceptId() == getEarlyInfantDiagnosisConceptId()
)) {
shouldSendEncounter = true;
break;
}
}
}
return shouldSendEncounter;
}

private int getViralLoadConceptId() {
if (VIRAL_LOAD_CONCEPT_ID == -1) {
synchronized (EncounterEventListener.class) {
if (VIRAL_LOAD_CONCEPT_ID == -1) {
ConceptService conceptService = Context.getService(ConceptService.class);
Concept concept = conceptService.getConceptByMapping("CIEL", "856");
if (concept == null) {
concept = conceptService.getConceptByMapping("LOINC", "25836-8");
}

if (concept != null) {
VIRAL_LOAD_CONCEPT_ID = concept.getConceptId();
}
}
}
}

return VIRAL_LOAD_CONCEPT_ID;
}

private int getEarlyInfantDiagnosisConceptId() {
if (EARLY_INFANT_DIAGNOSIS_CONCEPT_ID == -1) {
synchronized (EncounterEventListener.class) {
if (EARLY_INFANT_DIAGNOSIS_CONCEPT_ID == -1) {
ConceptService conceptService = Context.getService(ConceptService.class);
Concept concept = conceptService.getConceptByMapping("CIEL", "844");
if (concept == null) {
concept = conceptService.getConceptByMapping("LOINC", "44871-2");
}

if (concept != null) {
EARLY_INFANT_DIAGNOSIS_CONCEPT_ID = concept.getConceptId();
}
}
}
}

return EARLY_INFANT_DIAGNOSIS_CONCEPT_ID;
}
}
Loading

0 comments on commit 1e4c89a

Please sign in to comment.