Skip to content

Commit 8b70192

Browse files
wluyimawluyima
wluyima
authored andcommittedMay 9, 2013
Follow up to set the preferred or fully specified name for a concept
list item - TRUK-3232
1 parent de8c005 commit 8b70192

File tree

2 files changed

+117
-2
lines changed

2 files changed

+117
-2
lines changed
 

‎web/src/main/java/org/openmrs/web/dwr/ConceptListItem.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import java.util.Locale;
1717

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

134135
// if the name hit is not the preferred or fully specified one, put the fully specified one here
135-
if (!conceptName.isPreferred() && !conceptName.isFullySpecifiedName()) {
136-
ConceptName preferredNameObj = concept.getName(locale, false);
136+
if (!conceptName.isPreferred()) {
137+
ConceptName preferredNameObj = concept.getPreferredName(locale);
138+
if (preferredNameObj == null) {
139+
if (!StringUtils.isBlank(locale.getCountry()) || !StringUtils.isBlank(locale.getVariant())) {
140+
preferredNameObj = concept.getPreferredName(new Locale(locale.getLanguage()));
141+
}
142+
}
137143
if (preferredNameObj != null)
138144
preferredName = preferredNameObj.getName();
139145
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
14+
package org.openmrs.web.dwr;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
import java.util.Locale;
19+
20+
import org.junit.Test;
21+
import org.openmrs.Concept;
22+
import org.openmrs.ConceptClass;
23+
import org.openmrs.ConceptDatatype;
24+
import org.openmrs.ConceptName;
25+
26+
public class ConceptListItemTest {
27+
28+
/**
29+
* Tests that the constructor sets the preferred name if any even if the matched name is marked
30+
* as fully specified
31+
*
32+
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
33+
*/
34+
@Test
35+
public void shouldSetThePreferredNameIfFoundEvenWhenTheMatchedNameIsMarkedAsFullySpecified() throws Exception {
36+
Concept concept = new Concept();
37+
concept.setDatatype(new ConceptDatatype());
38+
concept.setConceptClass(new ConceptClass());
39+
Locale locale = Locale.ENGLISH;
40+
ConceptName hit = new ConceptName("name1", locale);
41+
final String expectedPrefName = "name2";
42+
ConceptName name2 = new ConceptName(expectedPrefName, locale);
43+
concept.addName(hit);
44+
concept.addName(name2);
45+
concept.setPreferredName(name2);
46+
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
47+
}
48+
49+
/**
50+
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
51+
* @throws Exception
52+
*/
53+
@Test
54+
public void shouldSetFullySpecifiedNameAsPreferredIfThereIsNoPreferredNameInTheLocale() throws Exception {
55+
Concept concept = new Concept();
56+
concept.setDatatype(new ConceptDatatype());
57+
concept.setConceptClass(new ConceptClass());
58+
Locale locale = Locale.ENGLISH;
59+
ConceptName hit = new ConceptName("name1", locale);
60+
final String expectedPrefName = "name2";
61+
ConceptName name2 = new ConceptName(expectedPrefName, locale);
62+
concept.addName(hit);
63+
concept.addName(name2);
64+
concept.setFullySpecifiedName(name2);
65+
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
66+
}
67+
68+
/**
69+
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
70+
* @throws Exception
71+
*/
72+
@Test
73+
public void shouldSetPreferredNameFromBroaderLocaleIfNoneExistsInALocaleWithACountryOrVariation() throws Exception {
74+
Concept concept = new Concept();
75+
concept.setDatatype(new ConceptDatatype());
76+
concept.setConceptClass(new ConceptClass());
77+
Locale locale = Locale.US;
78+
ConceptName hit = new ConceptName("name1", locale);
79+
final String expectedPrefName = "name2";
80+
ConceptName name2 = new ConceptName(expectedPrefName, Locale.ENGLISH);
81+
ConceptName name3 = new ConceptName("name3", locale);
82+
concept.addName(name2);
83+
concept.addName(hit);
84+
concept.addName(name3);
85+
concept.setPreferredName(name2);
86+
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
87+
}
88+
89+
/**
90+
* @see {@link ConceptListItem#ConceptListItem(Concept, ConceptName, Locale)}
91+
* @throws Exception
92+
*/
93+
@Test
94+
public void shouldSetFullySpecifiedNameFromBroaderLocaleIfNoneExistsInALocaleWithACountryOrVariation() throws Exception {
95+
Concept concept = new Concept();
96+
concept.setDatatype(new ConceptDatatype());
97+
concept.setConceptClass(new ConceptClass());
98+
Locale locale = Locale.US;
99+
ConceptName hit = new ConceptName("name1", locale);
100+
final String expectedPrefName = "name2";
101+
ConceptName name2 = new ConceptName(expectedPrefName, Locale.ENGLISH);
102+
ConceptName name3 = new ConceptName("name3", locale);
103+
concept.addName(name2);
104+
concept.addName(hit);
105+
concept.addName(name3);
106+
concept.setFullySpecifiedName(name2);
107+
assertEquals(expectedPrefName, new ConceptListItem(concept, hit, locale).getPreferredName());
108+
}
109+
}

0 commit comments

Comments
 (0)
Please sign in to comment.