Skip to content

Commit

Permalink
Follow up to fix hibernate criteria for searching drugs by mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
wluyima committed Feb 26, 2014
1 parent 4fac51c commit db6745c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/src/main/java/org/openmrs/api/ConceptService.java
Expand Up @@ -2100,6 +2100,7 @@ public List<Drug> getDrugsByMapping(String code, ConceptSource conceptSource,
* @since 1.10
* @return the {@link Drug}
* @throws APIException
* @should return a drug that matches the code and source
* @should return a drug that matches the code and source and the best map type
* @should fail if multiple drugs are found matching the best map type
* @should return null if no match found
Expand Down
Expand Up @@ -2108,14 +2108,22 @@ public Drug getDrugByMapping(String code, ConceptSource conceptSource,
if (withAnyOfTheseTypesOrOrderOfPreference.size() > 0) {
for (ConceptMapType conceptMapType : withAnyOfTheseTypesOrOrderOfPreference) {
criteria.add(Restrictions.eq("map.conceptMapType", conceptMapType));
if (criteria.list().size() > 1) {
List<Drug> drugs = criteria.list();
if (drugs.size() > 1) {
throw new DAOException("There are multiple matches for the highest-priority ConceptMapType");
} else if (criteria.list().size() == 1) {
return (Drug) criteria.list().get(0);
} else if (drugs.size() == 1) {
return drugs.get(0);
}
//reset for the next execution to avoid unwanted AND clauses on every found map type
criteria = createSearchDrugByMappingCriteria(code, conceptSource, includeRetired);
}
} else {
List<Drug> drugs = criteria.list();
if (drugs.size() > 1) {
throw new DAOException("There are multiple matches for the highest-priority ConceptMapType");
} else if (drugs.size() == 1) {
return drugs.get(0);
}
}
return null;
}
Expand All @@ -2126,8 +2134,10 @@ private Criteria createSearchDrugByMappingCriteria(String code, ConceptSource co

//join to the drugReferenceMap table
searchCriteria.createAlias("drug.drugReferenceMaps", "map");
// join to the conceptReferenceTerm table
searchCriteria.createAlias("map.conceptReferenceTerm", "term");
if (code != null || conceptSource != null) {
// join to the conceptReferenceTerm table
searchCriteria.createAlias("map.conceptReferenceTerm", "term");
}
// match the source code to the passed code
if (code != null) {
searchCriteria.add(Restrictions.eq("term.code", code));
Expand Down
13 changes: 13 additions & 0 deletions api/src/test/java/org/openmrs/api/ConceptServiceTest.java
Expand Up @@ -2878,4 +2878,17 @@ public void getDrugsByMapping_shouldFailIfNoCodeAndConceptSourceAndWithAnyOfThes
public void getDrugByMapping_shouldFailIfNoCodeAndConceptSourceAndWithAnyOfTheseTypesAreProvided() throws Exception {
conceptService.getDrugByMapping(null, null, Collections.EMPTY_LIST, false);
}

/**
* @verifies return a drug that matches the code and source
* @see ConceptService#getDrugByMapping(String, org.openmrs.ConceptSource, java.util.Collection,
* boolean)
*/
@Test
public void getDrugByMapping_shouldReturnADrugThatMatchesTheCodeAndSource() throws Exception {
executeDataSet(GET_DRUG_MAPPINGS);
final Integer expectedDrugId = 2;
Drug drug = conceptService.getDrugByMapping("WGT234", conceptService.getConceptSource(2), null, false);
assertEquals(expectedDrugId, drug.getDrugId());
}
}

0 comments on commit db6745c

Please sign in to comment.