Skip to content

Commit

Permalink
Back porting wyclif's changes for including tests to improve the logic
Browse files Browse the repository at this point in the history
for selecting the preferred name - TRUNK-3232
  • Loading branch information
dkayiwa committed May 28, 2013
1 parent 7e5083e commit 2785f88
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
10 changes: 8 additions & 2 deletions web/src/main/java/org/openmrs/web/dwr/ConceptListItem.java
Expand Up @@ -15,6 +15,7 @@

import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openmrs.Concept;
Expand Down Expand Up @@ -132,8 +133,13 @@ private void initialize(Concept concept, ConceptName conceptName, Locale locale)
name = WebUtil.escapeHTML(conceptName.getName());

// if the name hit is not the preferred or fully specified one, put the fully specified one here
if (!conceptName.isPreferred() && !conceptName.isFullySpecifiedName()) {
ConceptName preferredNameObj = concept.getName(locale, false);
if (!conceptName.isPreferred()) {
ConceptName preferredNameObj = concept.getPreferredName(locale);
if (preferredNameObj == null) {
if (!StringUtils.isBlank(locale.getCountry()) || !StringUtils.isBlank(locale.getVariant())) {
preferredNameObj = concept.getPreferredName(new Locale(locale.getLanguage()));
}
}
if (preferredNameObj != null)
preferredName = preferredNameObj.getName();
}
Expand Down
109 changes: 109 additions & 0 deletions web/src/test/java/org/openmrs/web/dwr/ConceptListItemTest.java
@@ -0,0 +1,109 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.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://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.web.dwr;

import static org.junit.Assert.assertEquals;

import java.util.Locale;

import org.junit.Test;
import org.openmrs.Concept;
import org.openmrs.ConceptClass;
import org.openmrs.ConceptDatatype;
import org.openmrs.ConceptName;

public class ConceptListItemTest {

/**
* Tests that the constructor sets the preferred name if any even if the matched name is marked
* as fully specified
*
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
*/
@Test
public void shouldSetThePreferredNameIfFoundEvenWhenTheMatchedNameIsMarkedAsFullySpecified() throws Exception {
Concept concept = new Concept();
concept.setDatatype(new ConceptDatatype());
concept.setConceptClass(new ConceptClass());
Locale locale = Locale.ENGLISH;
ConceptName hit = new ConceptName("name1", locale);
final String expectedPrefName = "name2";
ConceptName name2 = new ConceptName(expectedPrefName, locale);
concept.addName(hit);
concept.addName(name2);
concept.setPreferredName(name2);
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
}

/**
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
* @throws Exception
*/
@Test
public void shouldSetFullySpecifiedNameAsPreferredIfThereIsNoPreferredNameInTheLocale() throws Exception {
Concept concept = new Concept();
concept.setDatatype(new ConceptDatatype());
concept.setConceptClass(new ConceptClass());
Locale locale = Locale.ENGLISH;
ConceptName hit = new ConceptName("name1", locale);
final String expectedPrefName = "name2";
ConceptName name2 = new ConceptName(expectedPrefName, locale);
concept.addName(hit);
concept.addName(name2);
concept.setFullySpecifiedName(name2);
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
}

/**
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
* @throws Exception
*/
@Test
public void shouldSetPreferredNameFromBroaderLocaleIfNoneExistsInALocaleWithACountryOrVariation() throws Exception {
Concept concept = new Concept();
concept.setDatatype(new ConceptDatatype());
concept.setConceptClass(new ConceptClass());
Locale locale = Locale.US;
ConceptName hit = new ConceptName("name1", locale);
final String expectedPrefName = "name2";
ConceptName name2 = new ConceptName(expectedPrefName, Locale.ENGLISH);
ConceptName name3 = new ConceptName("name3", locale);
concept.addName(name2);
concept.addName(hit);
concept.addName(name3);
concept.setPreferredName(name2);
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
}

/**
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
* @throws Exception
*/
@Test
public void shouldSetFullySpecifiedNameFromBroaderLocaleIfNoneExistsInALocaleWithACountryOrVariation() throws Exception {
Concept concept = new Concept();
concept.setDatatype(new ConceptDatatype());
concept.setConceptClass(new ConceptClass());
Locale locale = Locale.US;
ConceptName hit = new ConceptName("name1", locale);
final String expectedPrefName = "name2";
ConceptName name2 = new ConceptName(expectedPrefName, Locale.ENGLISH);
ConceptName name3 = new ConceptName("name3", locale);
concept.addName(name2);
concept.addName(hit);
concept.addName(name3);
concept.setFullySpecifiedName(name2);
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
}
}

0 comments on commit 2785f88

Please sign in to comment.