diff --git a/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0.java b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0.java new file mode 100644 index 000000000..00af34038 --- /dev/null +++ b/omod-2.0/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0.java @@ -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 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(); + } + 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>> lines = nameTemplate.getLines(); + Map nameMappings = nameTemplate.getNameMappings(); + for (List> line : lines) { + for (Map 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; + } + +} diff --git a/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0Test.java b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0Test.java new file mode 100644 index 000000000..b197c1e6b --- /dev/null +++ b/omod-2.0/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs2_0/NameTemplateController2_0Test.java @@ -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 { + + } + +} diff --git a/omod-2.0/src/test/resources/nameTemplate.json b/omod-2.0/src/test/resources/nameTemplate.json new file mode 100644 index 000000000..5d1f58cac --- /dev/null +++ b/omod-2.0/src/test/resources/nameTemplate.json @@ -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 +} \ No newline at end of file diff --git a/omod-2.0/src/test/resources/nameTemplate.xml b/omod-2.0/src/test/resources/nameTemplate.xml new file mode 100644 index 000000000..1e170cc3c --- /dev/null +++ b/omod-2.0/src/test/resources/nameTemplate.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + givenName + middleName + familyName + + \ No newline at end of file diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java index b1119e6bc..fc6f9ddca 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/RestConstants.java @@ -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 */