Skip to content

Commit

Permalink
Bugbash issue 4 and 8 (#10)
Browse files Browse the repository at this point in the history
* Bug bash issue 4 and 8
  • Loading branch information
manimaarans authored and vijayanandtwks committed Nov 10, 2023
1 parent 68ace49 commit 304c361
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
16 changes: 8 additions & 8 deletions src/main/java/org/snomed/cdsservice/model/CDSCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ public class CDSCard {

private List<CDSReference> referenceMedications;

private CDSReference referenceCondition;
private List<CDSReference> referenceConditions;

private final String alertType;

public CDSCard(String uuid, String summary, String detail, CDSIndicator indicator, CDSSource source, List<CDSReference> referenceMedications, CDSReference referenceCondition, String alertType) {
public CDSCard(String uuid, String summary, String detail, CDSIndicator indicator, CDSSource source, List<CDSReference> referenceMedications, List<CDSReference> referenceConditions, String alertType) {
this.uuid = uuid;
this.summary = summary;
this.detail = detail;
this.indicator = indicator;
this.source = source;
this.referenceMedications = referenceMedications;
this.referenceCondition = referenceCondition;
this.referenceConditions = referenceConditions;
this.alertType = alertType;
}

Expand Down Expand Up @@ -69,16 +69,16 @@ public void setReferenceMedications(List<CDSReference> referenceMedications) {
this.referenceMedications = referenceMedications;
}

public CDSReference getReferenceCondition() {
return referenceCondition;
public List<CDSReference> getReferenceConditions() {
return referenceConditions;
}

public void setReferenceCondition(CDSReference referenceCondition) {
this.referenceCondition = referenceCondition;
public void setReferenceConditions(List<CDSReference> referenceConditions) {
this.referenceConditions = referenceConditions;
}

public CDSCard cloneCard() {
return new CDSCard(uuid, summary, detail, indicator, source, referenceMedications, referenceCondition, alertType);
return new CDSCard(uuid, summary, detail, indicator, source, referenceMedications, referenceConditions, alertType);
}

public String getAlertType() {
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/org/snomed/cdsservice/model/CDSTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

Expand All @@ -30,15 +32,31 @@ public CDSTrigger(String medicationLabel, Collection<Coding> medicationCodings,
public abstract CDSCard createRelevantCard(Set<Coding> activeDiagnosesOrMedicationCodings, Set<Coding> draftMedicationOrderCodings);

public void addReferenceMedicationToCDSCard(Collection<Coding> medicationIntersection, CDSCard cardInstance) {
CDSReference cdsReference = new CDSReference(medicationIntersection.stream().map(coding -> new CDSCoding(coding.getSystem(), coding.getCode())).collect(Collectors.toList()));
List<CDSReference> cdsReferences = medicationIntersection.stream().map(coding -> new CDSCoding(coding.getSystem(), coding.getCode())).map(cdsCoding -> new CDSReference(Collections.singletonList(cdsCoding))).collect(Collectors.toList());
if (cardInstance.getReferenceMedications() == null) {
cardInstance.setReferenceMedications(new ArrayList<>());
}
cardInstance.getReferenceMedications().add(cdsReference);
if (cardInstance.getReferenceMedications().isEmpty()) {
cardInstance.getReferenceMedications().addAll(cdsReferences);
return;
}
List<String> codes = new ArrayList<>();
cardInstance.getReferenceMedications().forEach(cdsReference -> codes.addAll(cdsReference.getCoding().stream().map(CDSCoding::getCode).toList()));
cardInstance.getReferenceMedications().addAll(cdsReferences.stream().filter(cdsReference -> !codes.contains(cdsReference.getCoding().get(0).getCode())).toList());
}

public void addReferenceConditionToCDSCard(Collection<Coding> conditionIntersection, CDSCard cardInstance) {
cardInstance.setReferenceCondition(new CDSReference(conditionIntersection.stream().map(coding -> new CDSCoding(coding.getSystem(), coding.getCode())).collect(Collectors.toList())));
List<CDSReference> cdsReferences = conditionIntersection.stream().map(coding -> new CDSCoding(coding.getSystem(), coding.getCode())).map(cdsCoding -> new CDSReference(Collections.singletonList(cdsCoding))).collect(Collectors.toList());
if(cardInstance.getReferenceConditions() == null) {
cardInstance.setReferenceConditions( new ArrayList<>());
}
if (cardInstance.getReferenceConditions().isEmpty()) {
cardInstance.getReferenceConditions().addAll(cdsReferences);
return;
}
List<String> codes = new ArrayList<>();
cardInstance.getReferenceConditions().forEach(cdsReference -> codes.addAll(cdsReference.getCoding().stream().map(CDSCoding::getCode).toList()));
cardInstance.getReferenceConditions().addAll(cdsReferences.stream().filter(cdsReference -> !codes.contains(cdsReference.getCoding().get(0).getCode())).toList());
}

public Collection<Coding> getIntersection(Collection<Coding> codingsA, Collection<Coding> codingsB) {
Expand All @@ -53,18 +71,19 @@ public Collection<Coding> getIntersection(Collection<Coding> codingsA, Collectio
}

public String toHumanReadable(Collection<Coding> codings) {
if (codings.isEmpty()) {
Collection<String> codingsDisplay = codings.stream().map(Coding::getDisplay).collect(Collectors.toSet());
if (codingsDisplay.isEmpty()) {
return "";
} else if (codings.size() == 1) {
return format("\"%s\"", codings.iterator().next().getDisplay());
} else if (codingsDisplay.size() == 1) {
return format("\"%s\"", codingsDisplay.iterator().next());
} else {
StringBuilder builder = new StringBuilder();
for (Coding coding : codings) {
for (String codingDisplay : codingsDisplay) {
if (!builder.isEmpty()) {
builder.append(" and ");
}
builder.append("\"");
builder.append(coding.getDisplay());
builder.append(codingDisplay);
builder.append("\"");
}
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public List<CDSCard> call(CDSRequest cdsRequest) {
CDSCard card = trigger.createRelevantCard(activeDiagnosesCodings, draftMedicationOrderCodings);
if (card != null) {
addCodesFromOtherCodingSystemsForDraftMedications(card.getReferenceMedications(), medicationRequests);
addCodesFromOtherCodingSystemsForConditions(card.getReferenceCondition(), activeDiagnoses);
addCodesFromOtherCodingSystemsForConditions(card.getReferenceConditions(), activeDiagnoses);
cards.add(card);
}
}
Expand All @@ -93,14 +93,17 @@ public List<CDSCard> call(CDSRequest cdsRequest) {
return cards;
}

private void addCodesFromOtherCodingSystemsForConditions(CDSReference referenceCondition, List<Condition> activeDiagnoses) {
CDSCoding cdsCoding = referenceCondition.getCoding().get(0);
Optional<Condition> optionalCondition = activeDiagnoses.stream().filter(condition -> {
List<Coding> codingList = condition.getCode().getCoding();
Optional<Coding> optionalCoding = codingList.stream().filter(coding -> coding.getCode().equals(cdsCoding.getCode()) && coding.getSystem().equals(cdsCoding.getSystem())).findFirst();
return optionalCoding.isPresent();
}).findFirst();
optionalCondition.ifPresent(getCDSReferenceConditionConsumer(referenceCondition));
private void addCodesFromOtherCodingSystemsForConditions(List<CDSReference> referenceConditions, List<Condition> activeDiagnoses) {
referenceConditions.forEach(referenceCondition-> {
CDSCoding cdsCoding = referenceCondition.getCoding().get(0);
Optional<Condition> optionalCondition = activeDiagnoses.stream().filter(condition -> {
List<Coding> codingList = condition.getCode().getCoding();
Optional<Coding> optionalCoding = codingList.stream().filter(coding -> coding.getCode().equals(cdsCoding.getCode()) && coding.getSystem().equals(cdsCoding.getSystem())).findFirst();
return optionalCoding.isPresent();
}).findFirst();
optionalCondition.ifPresent(getCDSReferenceConditionConsumer(referenceCondition));
}
);
}

private void addCodesFromOtherCodingSystemsForDraftMedications(List<CDSReference> referenceMedications, List<MedicationRequest> draftMedicationOrders) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void setMockOutput() throws ServiceException {
CDSIndicator.warning,
new CDSSource("Wikipedia"),
Stream.of(new CDSReference(Collections.singletonList(new CDSCoding("http://snomed.info/sct", "1145419005")))).collect(Collectors.toList()),
new CDSReference(Collections.singletonList(new CDSCoding("http://snomed.info/sct", "197321007"))), CONTRAINDICATION_ALERT_TYPE));
Stream.of(new CDSReference(Collections.singletonList(new CDSCoding("http://snomed.info/sct", "197321007")))).collect(Collectors.toList()), CONTRAINDICATION_ALERT_TYPE));
service.setMedicationOrderSelectTriggers(List.of(trigger));
when(mockTsClient.lookup(eq(SNOMEDCT_SYSTEM), eq("1145419005"))).thenReturn(getConceptParamsForDrugAtorvastatinTablet());
when(mockTsClient.lookup(eq(SNOMEDCT_SYSTEM), eq("258684004"))).thenReturn(getConceptParamsForDoseUnitMg());
Expand Down Expand Up @@ -117,7 +117,7 @@ public void shouldReturnAlert_WhenDrugAndConditionIsContraindicated() throws IOE
assertEquals(CDSIndicator.warning, cdsCard.getIndicator());
assertEquals("The use of Atorvastatin is contraindicated when the patient has Disease of liver.", cdsCard.getDetail());
assertEquals("1145419005", cdsCard.getReferenceMedications().get(0).getCoding().get(0).getCode());
assertEquals("197321007", cdsCard.getReferenceCondition().getCoding().get(0).getCode());
assertEquals("197321007", cdsCard.getReferenceConditions().get(0).getCoding().get(0).getCode());
assertEquals(CONTRAINDICATION_ALERT_TYPE, cdsCard.getAlertType());
}

Expand Down

0 comments on commit 304c361

Please sign in to comment.