Skip to content

Commit

Permalink
[Rahul] | BAH-3382 | Add. FreeMarker As Default Template Engine
Browse files Browse the repository at this point in the history
  • Loading branch information
rahu1ramesh committed Dec 4, 2023
1 parent db774b0 commit eeb9146
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 70 deletions.
13 changes: 10 additions & 3 deletions openerp-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.32</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down Expand Up @@ -171,7 +178,7 @@
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.bahmni.openerp.web.config;

import freemarker.template.Configuration;
import freemarker.template.TemplateModelException;

import java.io.IOException;

public class FreeMarkerConfig {

private static Configuration config;

public static Configuration getConfiguration() throws IOException, TemplateModelException {
if (config == null) {
config = new Configuration(Configuration.VERSION_2_3_23);
config.setClassForTemplateLoading(FreeMarkerConfig.class, "/request/template/");
config.setDefaultEncoding("UTF-8");
}
return config;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.bahmni.openerp.web.request.builder;


import org.apache.commons.lang3.StringEscapeUtils;

public class Parameter {

private String name;
private String value;
private String type;
private final String name;
private final String value;
private final String type;

public Parameter(String name, String value, String type) {
this.name = name;
this.value = StringEscapeUtils.escapeXml(value);
this.value = value;
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
package org.bahmni.openerp.web.request.builder;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import freemarker.template.Template;
import org.bahmni.openerp.web.OpenERPException;
import org.bahmni.openerp.web.config.FreeMarkerConfig;
import org.bahmni.openerp.web.request.OpenERPRequest;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.HashMap;

@Service
public class RequestBuilder {

public static String buildNewXMLRequest(OpenERPRequest openERPRequest, Object id, String database, String password) {
try {
VelocityEngine velocityEngine = new VelocityEngine();
velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
velocityEngine.init();
Template template = velocityEngine.getTemplate("request/template/xml_template.vm");
VelocityContext context = new VelocityContext();
context.put("parametersList", openERPRequest.getParameters());
context.put("id", id);
context.put("database", database);
context.put("password", password);
context.put("resource", openERPRequest.getResource());
context.put("operation", openERPRequest.getOperation());
HashMap<String, Object> context = getXMLContext(openERPRequest, id, database, password);
return buildRequest("xml_template.ftl", context);
} catch (Exception e) {
throw new OpenERPException(e);
}
}

private static HashMap<String, Object> getXMLContext(OpenERPRequest openERPRequest, Object id, String database, String password) {
HashMap<String, Object> context = new HashMap<>();
context.put("parametersList", openERPRequest.getParameters());
context.put("id", id);
context.put("database", database);
context.put("password", password);
context.put("resource", openERPRequest.getResource());
context.put("operation", openERPRequest.getOperation());
return context;
}

public static String buildNewRestRequest(OpenERPRequest openERPRequest, String id) {
try {
HashMap<String, Object> context = getRestContext(openERPRequest, id);
return buildRequest("rest_template.ftl", context);
} catch (Exception e) {
throw new OpenERPException(e);
}
}

private static HashMap<String, Object> getRestContext(OpenERPRequest openERPRequest, String id) {
HashMap<String, Object> context = new HashMap<>();
context.put("parametersList", openERPRequest.getParameters());
context.put("id", id);
return context;
}

private static String buildRequest(String templateName, HashMap<String, Object> context){
try {
Template template= FreeMarkerConfig.getConfiguration().getTemplate(templateName);
StringWriter writer = new StringWriter();
template.merge(context, writer);
template.process(context, writer);
return writer.toString();
} catch (Exception e) {
throw new OpenERPException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": "${id}",
"params": {
"data": {
<#list parametersList as param>
"${param.name}": "${param.value}"<#if param_has_next>,</#if>
</#list>
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version='1.0' encoding='utf-8'?>
<methodCall>
<methodName>execute</methodName>
<params>
<param>
<value><string>${database}</string></value>
</param>
<param>
<value><int>${id}</int></value>
</param>
<param>
<value><string>${password}</string></value>
</param>
<param>
<value><string>${resource}</string></value>
</param>
<param>
<value><string>${operation}</string></value>
</param>
<param>
<value><struct>
<#list parametersList as param>
<member>
<name>${param.name}</name>
<value><${param.type}>${(param.value?xml)!}</${param.type}></value>
</member>
</#list>
</struct></value>
</param>
</params>
</methodCall>
31 changes: 0 additions & 31 deletions openerp-client/src/main/resources/request/template/xml_template.vm

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import org.bahmni.openerp.web.request.OpenERPRequest;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class RequestBuilderTest {

@Test
public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exception {
public void shouldCreateNewXMLCustomerRequestWithPatientDataPopulated() throws Exception {

int id = 1;
String patientName = "Ramu";
Expand All @@ -29,7 +27,6 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce
parameters.add(villageParam);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password);
String requestXmlForComparison = requestXml.replace("", " ");

String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
"<methodCall>" +
Expand Down Expand Up @@ -73,7 +70,29 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce
}

@Test
public void shouldEscapeSpecialCharacters() throws Exception {
public void shouldCreateNewRESTCustomerRequestWithPatientDataPopulated() throws Exception {

String id = "3a0ee6b3-4e3a-425d-a2ef-806ccb0d1744";
String patientName = "Ramu";
String patientId = "13466";
String village = "Ganiyari";
String expected = "{\"id\": \"3a0ee6b3-4e3a-425d-a2ef-806ccb0d1744\",\"params\": {\"data\": {\"name\": \"Ramu\",\"ref\": \"13466\",\"village\": \"Ganiyari\"}}\n}";

Parameter name = new Parameter("name", patientName, "string");
Parameter ref = new Parameter("ref", patientId, "string");
Parameter villageParam = new Parameter("village", village, "string");
List<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(name);
parameters.add(ref);
parameters.add(villageParam);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewRestRequest(request, id);

comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldEscapeSpecialCharactersWhenXMLRequestIsCreated() throws Exception {
String income = "<=10000";

int id = 1;
Expand Down Expand Up @@ -122,7 +141,22 @@ public void shouldEscapeSpecialCharacters() throws Exception {
}

@Test
public void shouldEscapeHindiCharacters() throws Exception {
public void shouldEscapeSpecialCharactersWhenRESTRequestIsCreated() throws Exception {
String income = "<=10000";
String id = "f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f";
String expected = "{\"id\": \"f1cfccea-6f5e-4d6b-9759-f55e92cd4f8f\",\"params\": {\"data\": {\"income\": \"<=10000\"}}\n}";

Parameter parameter = new Parameter("income", income, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewRestRequest(request, id);

comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldEscapeHindiCharactersWhenXMLRequestIsCreated() throws Exception {
String name = "कृपा";

int id = 1;
Expand Down Expand Up @@ -170,6 +204,23 @@ public void shouldEscapeHindiCharacters() throws Exception {
comparingStringWithoutSpaces(requestXml, expected);
}

@Test
public void shouldEscapeHindiCharactersWhenRESTRequestIsCreated() throws Exception {

String name = "कृपा";
String id = "6397d716-5ec8-47c6-b5c8-dd49e3e09235";
String expected = "{\"id\": \"6397d716-5ec8-47c6-b5c8-dd49e3e09235\",\"params\": {\"data\": {\"name\": \"कृपा\"}}\n}";

Parameter parameter = new Parameter("name", name, "string");
ArrayList<Parameter> parameters = new ArrayList<Parameter>();
parameters.add(parameter);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRestRequest(request, id);

comparingStringWithoutSpaces(requestXml, expected);
}

private void comparingStringWithoutSpaces(String requestXml, String expected) {
assertEquals(expected.replaceAll("\\s{2,}", ""), requestXml.replaceAll("\\s{2,}", "").trim());
}
Expand Down
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@
<version>1.0.0.M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
Expand Down

0 comments on commit eeb9146

Please sign in to comment.