Skip to content

Commit 19e37de

Browse files
RESTWS-916: Create an endpoint to expose the NameTemplate resource as a REST document (#586)
* RESTWS-916: Create an endpoint to expose the NameTemplate resource as a REST document * RESTWS-916: Create an endpoint to expose the NameTemplate resource as a REST document * RESTWS-916: Create an endpoint to expose the NameTemplate resource as a REST document
1 parent cbcf07f commit 19e37de

File tree

5 files changed

+264
-1
lines changed

5 files changed

+264
-1
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;
11+
12+
import java.util.HashMap;
13+
import java.util.List;
14+
import java.util.Map;
15+
16+
import org.apache.commons.lang3.StringUtils;
17+
import org.openmrs.api.context.Context;
18+
import org.openmrs.layout.name.NameSupport;
19+
import org.openmrs.layout.name.NameTemplate;
20+
import org.openmrs.module.webservices.rest.SimpleObject;
21+
import org.openmrs.module.webservices.rest.web.RestConstants;
22+
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
23+
import org.openmrs.serialization.SerializationException;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.springframework.context.MessageSource;
27+
import org.springframework.context.NoSuchMessageException;
28+
import org.springframework.stereotype.Controller;
29+
import org.springframework.web.bind.annotation.RequestMapping;
30+
import org.springframework.web.bind.annotation.RequestMethod;
31+
import org.springframework.web.bind.annotation.ResponseBody;
32+
import org.springframework.web.context.request.WebRequest;
33+
34+
@Controller
35+
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/nametemplate")
36+
public class NameTemplateController2_0 extends BaseRestController {
37+
38+
private static final Logger log = LoggerFactory.getLogger(NameTemplateController2_0.class);
39+
40+
@RequestMapping(method = RequestMethod.GET)
41+
@ResponseBody
42+
public Object get(WebRequest request) throws SerializationException {
43+
44+
NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate();
45+
46+
// Check global properties for defaults/overrides in the form of n=v,n1=v1, etc
47+
String customDefaults = Context.getAdministrationService().getGlobalProperty("layout.name.defaults");
48+
if (StringUtils.isNotEmpty(customDefaults)) {
49+
String[] tokens = customDefaults.split(",");
50+
Map<String, String> elementDefaults = nameTemplate.getElementDefaults();
51+
52+
for (String token : tokens) {
53+
String[] pair = token.split("=");
54+
if (pair.length == 2) {
55+
String name = pair[0];
56+
String val = pair[1];
57+
58+
if (elementDefaults == null) {
59+
elementDefaults = new HashMap<String, String>();
60+
}
61+
elementDefaults.put(name, val);
62+
} else {
63+
log.debug("Found invalid token while parsing GlobalProperty name format defaults: {}", token);
64+
}
65+
}
66+
67+
nameTemplate.setElementDefaults(elementDefaults);
68+
}
69+
70+
MessageSource messageSource = Context.getMessageSourceService();
71+
List<List<Map<String, String>>> lines = nameTemplate.getLines();
72+
Map<String, String> nameMappings = nameTemplate.getNameMappings();
73+
for (List<Map<String, String>> line : lines) {
74+
for (Map<String, String> elements : line) {
75+
if (elements.containsKey("displayText")) {
76+
String displayCode = elements.get("displayText");
77+
if (StringUtils.isNotBlank(displayCode)) {
78+
String displayText;
79+
try {
80+
displayText = messageSource.getMessage(displayCode, null, Context.getLocale());
81+
}
82+
catch (NoSuchMessageException e) {
83+
displayText = displayCode;
84+
}
85+
86+
elements.put("displayText", displayText);
87+
String codeName = elements.get("codeName");
88+
if (codeName != null && nameMappings.containsKey(codeName)) {
89+
nameMappings.put(codeName, displayText);
90+
}
91+
}
92+
}
93+
}
94+
}
95+
96+
SimpleObject nameTemplateSO = new SimpleObject();
97+
nameTemplateSO.put("displayName", nameTemplate.getDisplayName());
98+
nameTemplateSO.put("codeName", nameTemplate.getCodeName());
99+
nameTemplateSO.put("country", nameTemplate.getCountry());
100+
nameTemplateSO.put("lines", lines);
101+
nameTemplateSO.put("lineByLineFormat", nameTemplate.getLineByLineFormat());
102+
nameTemplateSO.put("nameMappings", nameMappings);
103+
nameTemplateSO.put("sizeMappings", nameTemplate.getSizeMappings());
104+
nameTemplateSO.put("elementDefaults", nameTemplate.getElementDefaults());
105+
nameTemplateSO.put("elementRegex", nameTemplate.getElementRegex());
106+
nameTemplateSO.put("elementRegexFormats", nameTemplate.getElementRegexFormats());
107+
nameTemplateSO.put("requiredElements", nameTemplate.getRequiredElements());
108+
109+
return nameTemplateSO;
110+
}
111+
112+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;
11+
12+
import java.io.InputStream;
13+
14+
import org.apache.commons.io.IOUtils;
15+
import org.hamcrest.Matchers;
16+
import org.junit.Assert;
17+
import org.junit.Test;
18+
import org.openmrs.module.webservices.rest.SimpleObject;
19+
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
20+
import org.springframework.mock.web.MockHttpServletRequest;
21+
22+
public class NameTemplateController2_0Test extends MainResourceControllerTest {
23+
24+
@Override
25+
public String getURI() {
26+
return "nametemplate";
27+
}
28+
29+
@Test
30+
public void shouldGetNameTemplate() throws Exception {
31+
MockHttpServletRequest req = newGetRequest(getURI());
32+
SimpleObject result = deserialize(handle(req));
33+
34+
String json;
35+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("nameTemplate.json")) {
36+
json = IOUtils.toString(inputStream, "UTF-8");
37+
}
38+
39+
Assert.assertThat(result, Matchers.is(SimpleObject.parseJson(json)));
40+
}
41+
42+
@Override
43+
public String getUuid() {
44+
return null;
45+
}
46+
47+
@Override
48+
public long getAllCount() {
49+
return 0;
50+
}
51+
52+
@Override
53+
public void shouldGetAll() throws Exception {
54+
55+
}
56+
57+
@Override
58+
public void shouldGetRefByUuid() throws Exception {
59+
60+
}
61+
62+
@Override
63+
public void shouldGetDefaultByUuid() throws Exception {
64+
65+
}
66+
67+
@Override
68+
public void shouldGetFullByUuid() throws Exception {
69+
70+
}
71+
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{
2+
"displayName": "Short Name Format",
3+
"codeName": "short",
4+
"country": null,
5+
"lines": [
6+
[
7+
{
8+
"isToken": "IS_NOT_NAME_TOKEN",
9+
"displayText": ""
10+
},
11+
{
12+
"isToken": "IS_NAME_TOKEN",
13+
"displayText": "PersonName.givenName",
14+
"codeName": "givenName",
15+
"displaySize": "30"
16+
}
17+
],
18+
[
19+
{
20+
"isToken": "IS_NOT_NAME_TOKEN",
21+
"displayText": ""
22+
},
23+
{
24+
"isToken": "IS_NAME_TOKEN",
25+
"displayText": "PersonName.middleName",
26+
"codeName": "middleName",
27+
"displaySize": "30"
28+
}
29+
],
30+
[
31+
{
32+
"isToken": "IS_NOT_NAME_TOKEN",
33+
"displayText": ""
34+
},
35+
{
36+
"isToken": "IS_NAME_TOKEN",
37+
"displayText": "PersonName.familyName",
38+
"codeName": "familyName",
39+
"displaySize": "30"
40+
}
41+
]
42+
],
43+
"lineByLineFormat": [
44+
"givenName",
45+
"middleName",
46+
"familyName"
47+
],
48+
"nameMappings": {
49+
"familyName": "PersonName.familyName",
50+
"givenName": "PersonName.givenName",
51+
"middleName": "PersonName.middleName"
52+
},
53+
"sizeMappings": {
54+
"familyName": "30",
55+
"givenName": "30",
56+
"middleName": "30"
57+
},
58+
"elementDefaults": null,
59+
"elementRegex": null,
60+
"elementRegexFormats": null,
61+
"requiredElements": null
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<org.openmrs.layout.name.NameTemplate>
2+
<nameMappings class="properties">
3+
<property name="givenName" value="PersonName.givenName"/>
4+
<property name="middleName" value="PersonName.middleName"/>
5+
<property name="familyName" value="PersonName.familyName"/>
6+
</nameMappings>
7+
<sizeMappings class="properties">
8+
<property name="givenName" value="40"/>
9+
<property name="middleName" value="40"/>
10+
<property name="familyName" value="40"/>
11+
</sizeMappings>
12+
<lineByLineFormat>
13+
<string>givenName</string>
14+
<string>middleName</string>
15+
<string>familyName</string>
16+
</lineByLineFormat>
17+
</org.openmrs.layout.name.NameTemplate>

omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public class RestConstants {
201201
public static boolean SWAGGER_LOGS_ON = true;
202202

203203
public static boolean SWAGGER_LOGS_OFF = false;
204-
204+
205205
/**
206206
* Constants used for the Server Log REST Service privilege checking
207207
*/

0 commit comments

Comments
 (0)