Skip to content

Commit 0933bb6

Browse files
authored
RESTWS-959: Support localization of Concepts via standard "ui.i18n.Co… (#633)
1 parent 61a27fa commit 0933bb6

File tree

5 files changed

+89
-29
lines changed

5 files changed

+89
-29
lines changed

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ConceptResource1_8.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,18 @@ public static List<ConceptMap> getMappings(Concept instance) {
462462
* @param instance the delegate instance to get the display name off
463463
*/
464464
@PropertyGetter("display")
465-
public String getDisplayName(Concept instance) {
466-
ConceptName cn = instance.getName();
467-
return cn == null ? null : cn.getName();
465+
public String getDisplayString(Concept instance) {
466+
String localization = getLocalization("Concept", instance.getUuid());
467+
if (StringUtils.isNotBlank(localization)) {
468+
return localization;
469+
} else {
470+
ConceptName cn = instance.getName();
471+
if (cn != null) {
472+
return cn.getName();
473+
} else {
474+
return instance.toString();
475+
}
476+
}
468477
}
469478

470479
/**

omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/RelationShipTypeResource1_8.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public String getDisplayBIsToAe(RelationshipType delegate) {
220220
}
221221
}
222222

223-
private String getLocalization(String uuid, String type) {
223+
protected String getLocalization(String uuid, String type) {
224224
String code = "ui.i18n.RelationshipType" + "." + type + "." + uuid;
225225
String localization = Context.getMessageSourceService().getMessage(code);
226226
if (localization == null || localization.equals(code)) {

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_9/ConceptResource1_9Test.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import static org.junit.Assert.assertEquals;
1313
import static org.junit.Assert.assertFalse;
1414
import static org.junit.Assert.assertTrue;
15+
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.when;
1517

1618
import java.util.ArrayList;
1719
import java.util.List;
@@ -23,6 +25,8 @@
2325
import org.openmrs.ConceptName;
2426
import org.openmrs.api.ConceptNameType;
2527
import org.openmrs.api.context.Context;
28+
import org.openmrs.api.context.ServiceContext;
29+
import org.openmrs.messagesource.MessageSourceService;
2630
import org.openmrs.module.webservices.rest.SimpleObject;
2731
import org.openmrs.module.webservices.rest.web.RestTestConstants1_8;
2832
import org.openmrs.module.webservices.rest.web.representation.NamedRepresentation;
@@ -141,4 +145,50 @@ public void testGetNamedRepresentation() throws Exception {
141145
Assert.assertFalse(e.getCause().getCause().getMessage().contains("Cycles in children are not supported."));
142146
}
143147
}
148+
149+
@Test
150+
public void testGetDisplayName() throws Exception {
151+
Concept concept = new Concept();
152+
ConceptName fullySpecifiedName = new ConceptName("some name", new Locale("en", "US"));
153+
fullySpecifiedName.setConceptNameId(1);
154+
fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
155+
fullySpecifiedName.setLocalePreferred(false);
156+
concept.addName(fullySpecifiedName);
157+
158+
ConceptResource1_9 resource = new ConceptResource1_9();
159+
String result = resource.getDisplayString(concept);
160+
161+
Assert.assertEquals("some name", result);
162+
}
163+
164+
@Test
165+
public void testGetDisplayNameForConceptWithNoName() throws Exception {
166+
Concept concept = new Concept(1);
167+
ConceptResource1_9 resource = new ConceptResource1_9();
168+
String result = resource.getDisplayString(concept);
169+
Assert.assertEquals("1", result); // this will need to updated to "Concept #1" when we start building against more recent versions of OpenMRS Core
170+
}
171+
172+
@Test
173+
public void testGetDisplayNameWithLocalizationOverride() throws Exception {
174+
175+
String UUID = "f0c5b621-3e5e-4f0d-8b9e-1d3e2b2c1b2d";
176+
177+
Concept concept = new Concept();
178+
concept.setUuid(UUID);
179+
MessageSourceService messageSourceService = mock(MessageSourceService.class);
180+
when(messageSourceService.getMessage("ui.i18n.Concept.name." + UUID)).thenReturn("Overridden by message source");
181+
ServiceContext.getInstance().setMessageSourceService(messageSourceService);
182+
183+
ConceptName fullySpecifiedName = new ConceptName("some name", new Locale("en", "US"));
184+
fullySpecifiedName.setConceptNameId(1);
185+
fullySpecifiedName.setConceptNameType(ConceptNameType.FULLY_SPECIFIED);
186+
fullySpecifiedName.setLocalePreferred(false);
187+
concept.addName(fullySpecifiedName);
188+
189+
ConceptResource1_9 resource = new ConceptResource1_9();
190+
String result = resource.getDisplayString(concept);
191+
192+
Assert.assertEquals("Overridden by message source", result);
193+
}
144194
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/DelegatingCrudResource.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import org.apache.commons.logging.Log;
1616
import org.apache.commons.logging.LogFactory;
17+
import org.openmrs.api.context.Context;
1718
import org.openmrs.module.webservices.rest.SimpleObject;
1819
import org.openmrs.module.webservices.rest.web.ConversionUtil;
1920
import org.openmrs.module.webservices.rest.web.RequestContext;
@@ -292,4 +293,29 @@ public boolean isRetirable() {
292293
public boolean isVoidable() {
293294
return false;
294295
}
296+
297+
/**
298+
* This code is largely copied from the UI Framework:
299+
* org.openmrs.ui.framework.FormatterImpl#format(org.openmrs.OpenmrsMetadata, java.util.Locale)
300+
*
301+
* @param shortClassName
302+
* @param uuid
303+
* @return localization for the given metadata, from message source, in the authenticated locale
304+
*/
305+
protected String getLocalization(String shortClassName, String uuid) {
306+
// in case this is a hibernate proxy, strip off anything after an underscore
307+
// ie: EncounterType_$$_javassist_26 needs to be converted to EncounterType
308+
int underscoreIndex = shortClassName.indexOf("_$");
309+
if (underscoreIndex > 0) {
310+
shortClassName = shortClassName.substring(0, underscoreIndex);
311+
}
312+
313+
String code = "ui.i18n." + shortClassName + ".name." + uuid;
314+
String localization = Context.getMessageSourceService().getMessage(code);
315+
if (localization == null || localization.equals(code)) {
316+
return null;
317+
} else {
318+
return localization;
319+
}
320+
}
295321
}

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/resource/impl/MetadataDelegatingCrudResource.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,31 +175,6 @@ public String getDisplayString(T delegate) {
175175
}
176176
}
177177

178-
/**
179-
* This code is largely copied from the UI Framework:
180-
* org.openmrs.ui.framework.FormatterImpl#format(org.openmrs.OpenmrsMetadata, java.util.Locale)
181-
*
182-
* @param shortClassName
183-
* @param uuid
184-
* @return localization for the given metadata, from message source, in the authenticated locale
185-
*/
186-
private String getLocalization(String shortClassName, String uuid) {
187-
// in case this is a hibernate proxy, strip off anything after an underscore
188-
// ie: EncounterType_$$_javassist_26 needs to be converted to EncounterType
189-
int underscoreIndex = shortClassName.indexOf("_$");
190-
if (underscoreIndex > 0) {
191-
shortClassName = shortClassName.substring(0, underscoreIndex);
192-
}
193-
194-
String code = "ui.i18n." + shortClassName + ".name." + uuid;
195-
String localization = Context.getMessageSourceService().getMessage(code);
196-
if (localization == null || localization.equals(code)) {
197-
return null;
198-
} else {
199-
return localization;
200-
}
201-
}
202-
203178
/**
204179
* @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource#getCreatableProperties()
205180
*/

0 commit comments

Comments
 (0)