Skip to content

Commit

Permalink
RESTWS-916: Create an endpoint to expose the NameTemplate resource as…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
IamMujuziMoses authored Sep 10, 2023
1 parent cbcf07f commit 19e37de
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.api.context.Context;
import org.openmrs.layout.name.NameSupport;
import org.openmrs.layout.name.NameTemplate;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.openmrs.serialization.SerializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/nametemplate")
public class NameTemplateController2_0 extends BaseRestController {

private static final Logger log = LoggerFactory.getLogger(NameTemplateController2_0.class);

@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public Object get(WebRequest request) throws SerializationException {

NameTemplate nameTemplate = NameSupport.getInstance().getDefaultLayoutTemplate();

// Check global properties for defaults/overrides in the form of n=v,n1=v1, etc
String customDefaults = Context.getAdministrationService().getGlobalProperty("layout.name.defaults");
if (StringUtils.isNotEmpty(customDefaults)) {
String[] tokens = customDefaults.split(",");
Map<String, String> elementDefaults = nameTemplate.getElementDefaults();

for (String token : tokens) {
String[] pair = token.split("=");
if (pair.length == 2) {
String name = pair[0];
String val = pair[1];

if (elementDefaults == null) {
elementDefaults = new HashMap<String, String>();
}
elementDefaults.put(name, val);
} else {
log.debug("Found invalid token while parsing GlobalProperty name format defaults: {}", token);
}
}

nameTemplate.setElementDefaults(elementDefaults);
}

MessageSource messageSource = Context.getMessageSourceService();
List<List<Map<String, String>>> lines = nameTemplate.getLines();
Map<String, String> nameMappings = nameTemplate.getNameMappings();
for (List<Map<String, String>> line : lines) {
for (Map<String, String> elements : line) {
if (elements.containsKey("displayText")) {
String displayCode = elements.get("displayText");
if (StringUtils.isNotBlank(displayCode)) {
String displayText;
try {
displayText = messageSource.getMessage(displayCode, null, Context.getLocale());
}
catch (NoSuchMessageException e) {
displayText = displayCode;
}

elements.put("displayText", displayText);
String codeName = elements.get("codeName");
if (codeName != null && nameMappings.containsKey(codeName)) {
nameMappings.put(codeName, displayText);
}
}
}
}
}

SimpleObject nameTemplateSO = new SimpleObject();
nameTemplateSO.put("displayName", nameTemplate.getDisplayName());
nameTemplateSO.put("codeName", nameTemplate.getCodeName());
nameTemplateSO.put("country", nameTemplate.getCountry());
nameTemplateSO.put("lines", lines);
nameTemplateSO.put("lineByLineFormat", nameTemplate.getLineByLineFormat());
nameTemplateSO.put("nameMappings", nameMappings);
nameTemplateSO.put("sizeMappings", nameTemplate.getSizeMappings());
nameTemplateSO.put("elementDefaults", nameTemplate.getElementDefaults());
nameTemplateSO.put("elementRegex", nameTemplate.getElementRegex());
nameTemplateSO.put("elementRegexFormats", nameTemplate.getElementRegexFormats());
nameTemplateSO.put("requiredElements", nameTemplate.getRequiredElements());

return nameTemplateSO;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.webservices.rest.web.v1_0.controller.openmrs2_0;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.hamcrest.Matchers;
import org.junit.Assert;
import org.junit.Test;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.springframework.mock.web.MockHttpServletRequest;

public class NameTemplateController2_0Test extends MainResourceControllerTest {

@Override
public String getURI() {
return "nametemplate";
}

@Test
public void shouldGetNameTemplate() throws Exception {
MockHttpServletRequest req = newGetRequest(getURI());
SimpleObject result = deserialize(handle(req));

String json;
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("nameTemplate.json")) {
json = IOUtils.toString(inputStream, "UTF-8");
}

Assert.assertThat(result, Matchers.is(SimpleObject.parseJson(json)));
}

@Override
public String getUuid() {
return null;
}

@Override
public long getAllCount() {
return 0;
}

@Override
public void shouldGetAll() throws Exception {

}

@Override
public void shouldGetRefByUuid() throws Exception {

}

@Override
public void shouldGetDefaultByUuid() throws Exception {

}

@Override
public void shouldGetFullByUuid() throws Exception {

}

}
62 changes: 62 additions & 0 deletions omod-2.0/src/test/resources/nameTemplate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"displayName": "Short Name Format",
"codeName": "short",
"country": null,
"lines": [
[
{
"isToken": "IS_NOT_NAME_TOKEN",
"displayText": ""
},
{
"isToken": "IS_NAME_TOKEN",
"displayText": "PersonName.givenName",
"codeName": "givenName",
"displaySize": "30"
}
],
[
{
"isToken": "IS_NOT_NAME_TOKEN",
"displayText": ""
},
{
"isToken": "IS_NAME_TOKEN",
"displayText": "PersonName.middleName",
"codeName": "middleName",
"displaySize": "30"
}
],
[
{
"isToken": "IS_NOT_NAME_TOKEN",
"displayText": ""
},
{
"isToken": "IS_NAME_TOKEN",
"displayText": "PersonName.familyName",
"codeName": "familyName",
"displaySize": "30"
}
]
],
"lineByLineFormat": [
"givenName",
"middleName",
"familyName"
],
"nameMappings": {
"familyName": "PersonName.familyName",
"givenName": "PersonName.givenName",
"middleName": "PersonName.middleName"
},
"sizeMappings": {
"familyName": "30",
"givenName": "30",
"middleName": "30"
},
"elementDefaults": null,
"elementRegex": null,
"elementRegexFormats": null,
"requiredElements": null
}
17 changes: 17 additions & 0 deletions omod-2.0/src/test/resources/nameTemplate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<org.openmrs.layout.name.NameTemplate>
<nameMappings class="properties">
<property name="givenName" value="PersonName.givenName"/>
<property name="middleName" value="PersonName.middleName"/>
<property name="familyName" value="PersonName.familyName"/>
</nameMappings>
<sizeMappings class="properties">
<property name="givenName" value="40"/>
<property name="middleName" value="40"/>
<property name="familyName" value="40"/>
</sizeMappings>
<lineByLineFormat>
<string>givenName</string>
<string>middleName</string>
<string>familyName</string>
</lineByLineFormat>
</org.openmrs.layout.name.NameTemplate>
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public class RestConstants {
public static boolean SWAGGER_LOGS_ON = true;

public static boolean SWAGGER_LOGS_OFF = false;

/**
* Constants used for the Server Log REST Service privilege checking
*/
Expand Down

0 comments on commit 19e37de

Please sign in to comment.