Skip to content

Commit

Permalink
Consolidate diagnosis service implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed May 6, 2024
1 parent 391c487 commit d280a21
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 258 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.openmrs.module.emrapi.diagnosis;

import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.api.context.Context;

import java.util.Date;
import java.util.List;

/**
* class implementing the DiagnosisService while delegating calls to the core module
* */
public class CoreDiagnosisService {

public List<Diagnosis> getDiagnoses(Patient patient, Date fromDate) {
return DiagnosisUtils.convert(Context.getDiagnosisService().getDiagnoses(patient, fromDate));
}

public List<Diagnosis> getUniqueDiagnoses(Patient patient, Date fromDate){
return DiagnosisUtils.convert(Context.getDiagnosisService().getUniqueDiagnoses(patient, fromDate));
}

public List<Diagnosis> getPrimaryDiagnoses(Encounter encounter){
return DiagnosisUtils.convert(Context.getDiagnosisService().getPrimaryDiagnoses(encounter));
}

public boolean hasDiagnosis(Encounter encounter, Diagnosis diagnosis){
return true;
}

public List<Obs> codeNonCodedDiagnosis(Obs nonCodedObs, List<Diagnosis> diagnoses){
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,200 +1,86 @@
package org.openmrs.module.emrapi.diagnosis;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.Patient;
import org.openmrs.Person;
import org.openmrs.api.EncounterService;
import org.openmrs.api.ObsService;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.impl.BaseOpenmrsService;
import org.openmrs.module.emrapi.EmrApiProperties;
import org.openmrs.module.emrapi.EmrApiConstants;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* @deprecated as of 1.25.0, replaced by {@link DiagnosisServiceImpl} in the openmrs core platform 2.2.0
*/
@Deprecated
* class implementing the DiagnosisService while delegating calls to the core module
* */
public class DiagnosisServiceImpl extends BaseOpenmrsService implements DiagnosisService {

private static final Log log = LogFactory.getLog(DiagnosisService.class);
private CoreDiagnosisService coreDiagnosisService;

private EmrApiProperties emrApiProperties;
private ObsGroupDiagnosisService obsGroupDiagnosisService;

private ObsService obsService;
private AdministrationService adminService;

private EncounterService encounterService;
public void setCoreDiagnosisService(CoreDiagnosisService coreDiagnosisService) {
this.coreDiagnosisService = coreDiagnosisService;
}

public void setObsGroupDiagnosisService(ObsGroupDiagnosisService obsGroupDiagnosisService) {
this.obsGroupDiagnosisService = obsGroupDiagnosisService;
}

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

public void setObsService(ObsService obsService) {
this.obsService = obsService;
protected boolean useDiagnosesAsObs() {
return adminService.getGlobalProperty(EmrApiConstants.GP_USE_LEGACY_DIAGNOSIS_SERVICE, "false").equalsIgnoreCase("true");
}

public void setEncounterService(EncounterService encounterService) {
this.encounterService = encounterService;
}

@Override
public List<Obs> codeNonCodedDiagnosis(Obs nonCodedObs, List<Diagnosis> diagnoses) {

List<Obs> newDiagnoses = null;
if ( (nonCodedObs != null) && (diagnoses != null && diagnoses.size() > 0) ){
newDiagnoses = new ArrayList<Obs>();

Obs obsGroup = nonCodedObs.getObsGroup();
if ( obsGroup != null ){
Set<Obs> groupMembers = obsGroup.getGroupMembers();
if( (groupMembers!=null) && (groupMembers.size() > 0) ){
for (Obs groupMember : groupMembers) {
obsService.voidObs(groupMember, "code a diagnosis");
}
}
obsGroup = obsService.voidObs(obsGroup, "code a diagnosis");
}

Encounter encounter = nonCodedObs.getEncounter();
List<Diagnosis> primaryDiagnoses = getPrimaryDiagnoses(encounter);
boolean havePrimary =false;
if (primaryDiagnoses !=null && primaryDiagnoses.size() > 0 ){
// the encounter already has a primary diagnosis
havePrimary = true;
}
DiagnosisMetadata diagnosisMetadata = emrApiProperties.getDiagnosisMetadata();
for(Diagnosis diagnosis : diagnoses){
if (hasDiagnosis(encounter, diagnosis)){
// if this encounter already has this diagnosis skip it
continue;
}
if (havePrimary) {
diagnosis.setOrder(Diagnosis.Order.SECONDARY);
}
Obs obs = diagnosisMetadata.buildDiagnosisObsGroup(diagnosis);
if (obs != null) {
newDiagnoses.add(obs);
encounter.addObs(obs);
}

}
encounterService.saveEncounter(encounter);
}
return newDiagnoses;
}

@Override
public List<Diagnosis> getPrimaryDiagnoses(Encounter encounter) {
List<Diagnosis> diagnoses = null;
if (encounter != null && !encounter.isVoided()){
DiagnosisMetadata diagnosisMetadata = emrApiProperties.getDiagnosisMetadata();
diagnoses = new ArrayList<Diagnosis>();
for (Obs obs : encounter.getObsAtTopLevel(false) ){
if (diagnosisMetadata.isDiagnosis(obs)) {
try {
Diagnosis diagnosis = diagnosisMetadata.toDiagnosis(obs);
if (Diagnosis.Order.PRIMARY == diagnosis.getOrder()) {
diagnoses.add(diagnosis);
}
} catch (Exception ex){
log.warn("malformed diagnosis obs group with obsId " + obs.getObsId(), ex);
}
}
}
}
return diagnoses;

}

@Override
public boolean hasDiagnosis(Encounter encounter, Diagnosis diagnosis) {
boolean hasDiagnosis = false;
if (encounter != null && !encounter.isVoided()){
DiagnosisMetadata diagnosisMetadata = emrApiProperties.getDiagnosisMetadata();
for (Obs obs : encounter.getObsAtTopLevel(false) ){
if (diagnosisMetadata.isDiagnosis(obs)) {
try {
Diagnosis existing = diagnosisMetadata.toDiagnosis(obs);
CodedOrFreeTextAnswer answer = existing.getDiagnosis();
if (answer != null && answer.equals(diagnosis.getDiagnosis())){
hasDiagnosis = true;
break;
}
} catch (Exception ex){
log.warn("malformed diagnosis obs group with obsId " + obs.getObsId(), ex);
}
}
}
}
return hasDiagnosis;

}

@Override
public List<Diagnosis> getDiagnoses(Patient patient, Date fromDate) {
List<Diagnosis> diagnoses = new ArrayList<Diagnosis>();

DiagnosisMetadata diagnosisMetadata = emrApiProperties.getDiagnosisMetadata();

List<Obs> observations = obsService.getObservations(Arrays.asList((Person) patient), null, Arrays.asList(diagnosisMetadata.getDiagnosisSetConcept()),
null, null, null, Arrays.asList("obsDatetime"),
null, null, fromDate, null, false);

for (Obs obs : observations) {
Diagnosis diagnosis;
try {
diagnosis = diagnosisMetadata.toDiagnosis(obs);
}
catch (Exception ex) {
log.warn("Error trying to interpret " + obs + " as a diagnosis");
if (log.isDebugEnabled()) {
log.debug("Detailed error", ex);
}
continue;
}

Collection<Concept> nonDiagnosisConcepts = emrApiProperties.getSuppressedDiagnosisConcepts();
Collection<Concept> nonDiagnosisConceptSets = emrApiProperties.getNonDiagnosisConceptSets();

Set<Concept> filter = new HashSet<Concept>();
filter.addAll(nonDiagnosisConcepts);
for (Concept conceptSet : nonDiagnosisConceptSets) {
filter.addAll(conceptSet.getSetMembers());
}

if (!filter.contains(diagnosis.getDiagnosis().getCodedAnswer())) {
diagnoses.add(diagnosis);
}
if (useDiagnosesAsObs()) {
return obsGroupDiagnosisService.getDiagnoses(patient, fromDate);
}
else {
return coreDiagnosisService.getDiagnoses(patient, fromDate);
}

return diagnoses;
}

@Override
public List<Diagnosis> getUniqueDiagnoses(Patient patient, Date fromDate) {
List<Diagnosis> diagnoses = getDiagnoses(patient, fromDate);
public List<Diagnosis> getUniqueDiagnoses(Patient patient, Date fromDate) {
if (useDiagnosesAsObs()) {
return obsGroupDiagnosisService.getUniqueDiagnoses(patient, fromDate);
}
else {
return coreDiagnosisService.getUniqueDiagnoses(patient, fromDate);
}

Set<CodedOrFreeTextAnswer> answers = new HashSet<CodedOrFreeTextAnswer>();
}

Iterator<Diagnosis> it = diagnoses.iterator();
while(it.hasNext()) {
Diagnosis diagnosis = it.next();
public List<Diagnosis> getPrimaryDiagnoses(Encounter encounter) {
if (useDiagnosesAsObs()) {
return obsGroupDiagnosisService.getPrimaryDiagnoses(encounter);
}
else {
return coreDiagnosisService.getPrimaryDiagnoses(encounter);
}
}

if (!answers.add(diagnosis.getDiagnosis())) {
it.remove();
}
public boolean hasDiagnosis(Encounter encounter, Diagnosis diagnosis) {
if (useDiagnosesAsObs()) {
return obsGroupDiagnosisService.hasDiagnosis(encounter, diagnosis);
}
else {
return coreDiagnosisService.hasDiagnosis(encounter, diagnosis);
}
}

return diagnoses;
public List<Obs> codeNonCodedDiagnosis(Obs nonCodedObs, List<Diagnosis> diagnoses) {
if (useDiagnosesAsObs()) {
return obsGroupDiagnosisService.codeNonCodedDiagnosis(nonCodedObs, diagnoses);
}
else {
return coreDiagnosisService.codeNonCodedDiagnosis(nonCodedObs, diagnoses);
}
}

}

This file was deleted.

Loading

0 comments on commit d280a21

Please sign in to comment.