Skip to content

Commit

Permalink
TRUNK-6165 - ConceptService getConceptsByMapping should only return d… (
Browse files Browse the repository at this point in the history
openmrs#4265)

* TRUNK-6165 - ConceptService getConceptsByMapping should only return distinct concepts

* Add back in distinct transformer per code review
  • Loading branch information
mseaton committed Mar 7, 2023
1 parent bc67161 commit 126f82c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -1027,7 +1028,8 @@ public void remove() {
public List<Concept> getConceptsByMapping(String code, String sourceName, boolean includeRetired) {
Criteria criteria = createSearchConceptMapCriteria(code, sourceName, includeRetired);
criteria.setProjection(Projections.property("concept"));
return (List<Concept>) criteria.list();
List<Concept> concepts = criteria.list();
return concepts.stream().distinct().collect(Collectors.toList());
}

/**
Expand All @@ -1038,7 +1040,8 @@ public List<Concept> getConceptsByMapping(String code, String sourceName, boolea
public List<Integer> getConceptIdsByMapping(String code, String sourceName, boolean includeRetired) {
Criteria criteria = createSearchConceptMapCriteria(code, sourceName, includeRetired);
criteria.setProjection(Projections.property("concept.conceptId"));
return (List<Integer>) criteria.list();
List<Integer> conceptIds = criteria.list();
return conceptIds.stream().distinct().collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;
import java.util.Locale;
Expand All @@ -22,7 +23,11 @@
import org.openmrs.ConceptAttributeType;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptDatatype;
import org.openmrs.ConceptMap;
import org.openmrs.ConceptMapType;
import org.openmrs.ConceptName;
import org.openmrs.ConceptReferenceTerm;
import org.openmrs.ConceptSource;
import org.openmrs.Drug;
import org.openmrs.api.ConceptNameType;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -151,7 +156,6 @@ public void getDrugs_shouldReturnNonRetired() {
public void getDrugs_shouldReturnDrugEvenIf_DrugNameHasSpecialCharacters() {
List<Drug> drugList1 = dao.getDrugs("DRUG_NAME_WITH_SPECIAL_CHARACTERS (", null, true);
assertEquals(1, drugList1.size());

}

/**
Expand Down Expand Up @@ -189,4 +193,24 @@ public void isConceptNameDuplicate_shouldNotFailIfConceptDoesNotHaveADefaultName
assertThat(duplicate, is(false));
}

@Test
public void getConceptIdsByMapping_shouldReturnDistinctConceptIds() {
ConceptSource source = dao.getConceptSourceByName("Some Standardized Terminology");
ConceptMapType sameAs = dao.getConceptMapTypeByName("same-as");
Concept weightConcept = dao.getConcept(5089);
assertNotNull(source);
List<Integer> conceptIds = dao.getConceptIdsByMapping("WGT234", source.getName(), true);
assertEquals(1, conceptIds.size());
assertEquals(weightConcept.getConceptId(), conceptIds.get(0));

// Add another mapping that matches
ConceptReferenceTerm term = new ConceptReferenceTerm(source, "wgt234", null);
weightConcept.addConceptMapping(new ConceptMap(term, sameAs));
dao.saveConcept(weightConcept);

// Querying by this mapping should only return the weight concept id once, even if 2 of its terms match
conceptIds = dao.getConceptIdsByMapping("WGT234", source.getName(), true);
assertEquals(1, conceptIds.size());
assertEquals(weightConcept.getConceptId(), conceptIds.get(0));
}
}

0 comments on commit 126f82c

Please sign in to comment.