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
  • Loading branch information
IamMujuziMoses committed Aug 30, 2023
1 parent cbcf07f commit 814f769
Show file tree
Hide file tree
Showing 6 changed files with 446 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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.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.module.webservices.rest.web.v1_0.support.openmrs2_0.NameSupport2_0;
import org.openmrs.module.webservices.rest.web.v1_0.template.openmrs2_0.NameTemplate2_0;
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 {

NameTemplate2_0 nameTemplate = NameSupport2_0.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.isEmpty()) {
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,138 @@
/**
* 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.support.openmrs2_0;


import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Vector;

import org.apache.commons.collections.CollectionUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.GlobalPropertyListener;
import org.openmrs.api.context.Context;
import org.openmrs.layout.LayoutSupport;
import org.openmrs.module.webservices.rest.web.v1_0.template.openmrs2_0.NameTemplate2_0;
import org.openmrs.serialization.SerializationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NameSupport2_0 extends LayoutSupport<NameTemplate2_0> implements GlobalPropertyListener {
private static NameSupport2_0 singleton;
private boolean initialized = false;
static Logger log = LoggerFactory.getLogger(NameSupport2_0.class);

private NameSupport2_0() {
if (Objects.isNull(singleton))
singleton = this;
log.debug("Setting singleton: {}", singleton);
}

public static NameSupport2_0 getInstance() {
synchronized (NameSupport2_0.class) {
if (Objects.isNull(singleton))
singleton = new NameSupport2_0();
}

singleton.init();
return singleton;
}

private void init() {
if (!this.initialized) {
Context.getAdministrationService().addGlobalPropertyListener(singleton);
String layoutTemplateXml = getNameFormat();
this.setNameTemplate(layoutTemplateXml);
List<String> specialTokens = new ArrayList<String>();
specialTokens.add("familyName");
specialTokens.add("middleName");
specialTokens.add("givenName");
this.setSpecialTokens(specialTokens);
this.initialized = true;
}
}

@Override
public String getDefaultLayoutFormat() {
return this.defaultLayoutFormat;
}

public void setNameTemplate(List<NameTemplate2_0> nameTemplates) {
this.layoutTemplates = nameTemplates;
this.setDefaultLayoutFormat(this.layoutTemplates.get(0).getCodeName());
}

public List<NameTemplate2_0> getNameTemplate() {
if (CollectionUtils.isEmpty(this.layoutTemplates))
try {
String xml = getNameFormat();
this.setNameTemplate(xml);
} catch (Exception ignored) {
}

return this.layoutTemplates;
}

@Override
public boolean supportsPropertyName(String s) {
return getNameFormat().equals(s);
}

@Override
public void globalPropertyChanged(GlobalProperty globalProperty) {
if (getNameFormat().equals(globalProperty.getProperty())) {
try {
this.setNameTemplate(globalProperty.getPropertyValue());
} catch (Exception var3) {
log.error("Error in new xml global property value", var3);
this.setNameTemplate((List)(new Vector()));
}
}
}

private void setNameTemplate(String xml) {
NameTemplate2_0 nameTemplate;
try {
nameTemplate = Context.getSerializationService().getDefaultSerializer().deserialize(xml, NameTemplate2_0.class);
} catch (SerializationException var4) {
log.error("Error in deserializing name template", var4);
nameTemplate = new NameTemplate2_0("Error while deserializing name layout template.");
}

List<NameTemplate2_0> list = new ArrayList<>();
list.add(nameTemplate);
this.setNameTemplate(list);
}

@Override
public void globalPropertyDeleted(String s) {
if (getNameFormat().equals(s))
this.setNameTemplate((List)(new Vector()));
}

public String getNameFormat() {
return "<org.openmrs.module.webservices.rest.web.v1_0.template.openmrs2_0.NameTemplate2_0>" +
" <nameMappings class=\"properties\">" +
" <property name=\"familyName\" value=\"Person.familyName\"/>" +
" <property name=\"middleName\" value=\"Person.middleName\"/>" +
" <property name=\"givenName\" value=\"Person.givenName\"/>" +
" </nameMappings>" +
" <sizeMappings class=\"properties\">" +
" <property name=\"familyName\" value=\"40\"/>" +
" <property name=\"middleName\" value=\"40\"/>" +
" <property name=\"givenName\" value=\"40\"/>" +
" </sizeMappings>" +
" <lineByLineFormat>" +
" <string>familyName middleName givenName</string>" +
" </lineByLineFormat>" +
"</org.openmrs.module.webservices.rest.web.v1_0.template.openmrs2_0.NameTemplate2_0>";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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.template.openmrs2_0;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.openmrs.layout.LayoutSupport;
import org.openmrs.layout.LayoutTemplate;
import org.openmrs.module.webservices.rest.web.v1_0.support.openmrs2_0.NameSupport2_0;

public class NameTemplate2_0 extends LayoutTemplate implements Serializable {

private static final long serialVersionUID = 1L;

public NameTemplate2_0(String string) {
super(string);
}
@Override
public String getLayoutToken() {
return "IS_NAME_TOKEN";
}

@Override
public String getNonLayoutToken() {
return "IS_NOT_NAME_TOKEN";
}

@Override
public LayoutSupport<?> getLayoutSupportInstance() {
return NameSupport2_0.getInstance();
}

public String getCodeName() {
if (StringUtils.isEmpty(this.codeName)) {
this.codeName = "default";
}
return this.codeName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* 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.api.context.Context;
import org.openmrs.module.webservices.rest.SimpleObject;
import org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceControllerTest;
import org.openmrs.util.OpenmrsConstants;
import org.springframework.mock.web.MockHttpServletRequest;

public class NameTemplateController2_0Test extends MainResourceControllerTest {

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

@Test
public void shouldGetNameTemplate() throws Exception {
String xml;
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("nameTemplate.xml")) {
xml = IOUtils.toString(inputStream, "UTF-8");
}

Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_LAYOUT_NAME_FORMAT, xml);
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 {

}

}
Loading

0 comments on commit 814f769

Please sign in to comment.