Skip to content

Commit

Permalink
[Parvathy, Rahul] | Add. OpenERP Strategy and Context
Browse files Browse the repository at this point in the history
Co-authored-by: Parvathy Babu <[email protected]>
  • Loading branch information
rahu1ramesh and parvathy00 committed Nov 22, 2023
1 parent b4ec291 commit 00f6757
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.bahmni.openerp.web.client.strategy;

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

import java.util.Vector;

public interface OpenERPClientStrategy {

void delete(String resource, Vector params);

Object execute(OpenERPRequest openERPRequest);

Object execute(String resource, String operation, Vector params);

Object executeRead(String resource, String operation, Vector ids, Vector params);

Object read(String resource, Vector ids, Vector params);

Object search(String resource, Vector params);

Object updateCustomerReceivables(String resource, Vector params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.bahmni.openerp.web.client.strategy;

import org.bahmni.openerp.web.request.OpenERPRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Vector;

@Service
@Lazy
public class OpenERPContext {
private final OpenERPClientStrategy openERPClient;

@Autowired
public OpenERPContext(OpenERPClientStrategy openERPClient) {
this.openERPClient = openERPClient;
}

public void delete(String resource, Vector params) {
openERPClient.delete(resource, params);
}

public Object execute(OpenERPRequest openERPRequest) {
return openERPClient.execute(openERPRequest);
}

public Object execute(String resource, String operation, Vector params) {
return openERPClient.execute(resource, operation, params);
}

public Object executeRead(String resource, String operation, Vector ids, Vector params) {
return openERPClient.executeRead(resource, operation, ids, params);
}

public Object read(String resource, Vector ids, Vector params) {
return openERPClient.read(resource, ids, params);
}

public Object search(String resource, Vector params) {
return openERPClient.search(resource, params);
}

public Object updateCustomerReceivables(String resource, Vector params) {
return openERPClient.updateCustomerReceivables(resource, params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package org.bahmni.openerp.web.client.strategy.implementation;

import org.bahmni.openerp.web.OpenERPException;
import org.bahmni.openerp.web.OpenERPProperties;
import org.bahmni.openerp.web.client.OpenERPResponseErrorValidator;
import org.bahmni.openerp.web.client.strategy.OpenERPClientStrategy;
import org.bahmni.openerp.web.request.OpenERPRequest;
import org.bahmni.openerp.web.http.client.HttpClient;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcSun15HttpTransportFactory;
import org.bahmni.openerp.web.request.builder.RequestBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.net.MalformedURLException;
import java.util.Vector;
import java.net.URL;

@Service
@Lazy
public class OpenERPXMLClient implements OpenERPClientStrategy {
public static final String XML_RPC_OBJECT_ENDPOINT = "/xmlrpc/object";
public static final String XML_RPC_COMMON_ENDPOINT = "/xmlrpc/common";

private final int connectionTimeoutInMilliseconds;
private final int replyTimeoutInMilliseconds;
private final String host;
private final int port;
private final String database;
private final String user;
private final String password;

private Object id;

private XmlRpcClient xmlRpcClient;
private final HttpClient httpClient;

@Autowired
public OpenERPXMLClient(HttpClient httpClient, OpenERPProperties openERPProperties) {
this.httpClient = httpClient;
host = openERPProperties.getHost();
port = openERPProperties.getPort();
database = openERPProperties.getDatabase();
user = openERPProperties.getUser();
password = openERPProperties.getPassword();
connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds();
replyTimeoutInMilliseconds = openERPProperties.getReplyTimeoutInMilliseconds();
}

@Override
public void delete(String resource, Vector params) {
execute(resource, "unlink", params);
}

private void login() {
if (id == null) {
XmlRpcClient loginRpcClient = xmlRpcClient(XML_RPC_COMMON_ENDPOINT);

Vector<String> params = new Vector<String>();
params.addElement(database);
params.addElement(user);
params.addElement(password);

Object loginId = executeRPC(loginRpcClient, params, "login");
if(loginId == null || loginId.getClass() != Integer.class)
throw new OpenERPException(String.format("Failed to login. The login id is : %s", loginId));
id = loginId;
}
}

@Override
public Object execute(OpenERPRequest openERPRequest){
login();
String request = RequestBuilder.buildNewXMLRequest(openERPRequest, id, database, password);
String response = httpClient().post("http://" + host + ":" + port + XML_RPC_OBJECT_ENDPOINT, request);
new OpenERPResponseErrorValidator().checkForError(response);
return response;
}

@Override
public Object execute(String resource, String operation, Vector params) {
login();
Object[] args = {database, (Integer) id, password, resource, operation, params};

try {
return xmlRpcClient(XML_RPC_OBJECT_ENDPOINT).execute("execute", args);
} catch (XmlRpcException e) {
throw new OpenERPException(e);
}
}

@Override
public Object executeRead(String resource, String operation,Vector ids, Vector params) {
login();
Object[] args = {database, (Integer) id, password, resource, operation,ids, params};

try {
return xmlRpcClient(XML_RPC_OBJECT_ENDPOINT).execute("execute", args);
} catch (XmlRpcException e) {
throw new OpenERPException(e);
}
}

@Override
public Object read(String resource,Vector ids, Vector params) {
return executeRead(resource, "read", ids, params);
}

@Override
public Object search(String resource, Vector params) {
return execute(resource, "search", params);
}

@Override
public Object updateCustomerReceivables(String resource, Vector params) {
return execute(resource, "update_customer_receivables", params);
}

private Object executeRPC(XmlRpcClient loginRpcClient, Vector params, String methodName) {
try {
return loginRpcClient.execute(methodName, params);
} catch (XmlRpcException e) {
throw new OpenERPException(e);
}
}

private HttpClient httpClient() {
httpClient.setTimeout(replyTimeoutInMilliseconds);
return httpClient;
}

private XmlRpcClient xmlRpcClient(String endpoint) {
if (xmlRpcClient == null) {
xmlRpcClient = createRPCClient();
}
XmlRpcClientConfigImpl clientConfig = (XmlRpcClientConfigImpl) xmlRpcClient.getClientConfig();
try {
clientConfig.setServerURL(new URL("http", host, port, endpoint));
} catch (MalformedURLException e) {
throw new OpenERPException(e);
}
return xmlRpcClient;
}

private XmlRpcClient createRPCClient() {
XmlRpcClientConfigImpl clientConfiguration = new XmlRpcClientConfigImpl();
clientConfiguration.setEnabledForExtensions(true);
clientConfiguration.setEnabledForExceptions(true);
clientConfiguration.setConnectionTimeout(connectionTimeoutInMilliseconds);
clientConfiguration.setReplyTimeout(replyTimeoutInMilliseconds);

XmlRpcClient rpcClient = new XmlRpcClient();
rpcClient.setTransportFactory(new XmlRpcSun15HttpTransportFactory(rpcClient));
rpcClient.setConfig(clientConfiguration);
return rpcClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.bahmni.openerp.web.OpenERPException;
import org.bahmni.openerp.web.request.OpenERPRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.List;

@Service
public class RequestBuilder {

public static String buildNewRequest(OpenERPRequest openERPRequest, Object id, String database, String password) {
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/new_customer.vm");
Template template = velocityEngine.getTemplate("request/template/xml_template.vm");
VelocityContext context = new VelocityContext();
context.put("parametersList", openERPRequest.getParameters());
context.put("id", id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
</struct></value>
</param>
</params>
</methodCall>
</methodCall>
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce
parameters.add(ref);
parameters.add(villageParam);
OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);
String requestXml = RequestBuilder.buildNewRequest(request, id, database, password);
//String requestXmlForComparison = requestXml.replace("", " ");
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 @@ -86,7 +86,7 @@ public void shouldEscapeSpecialCharacters() throws Exception {

OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRequest(request, id, database, password);
String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password);

String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
"<methodCall>" +
Expand Down Expand Up @@ -135,7 +135,7 @@ public void shouldEscapeHindiCharacters() throws Exception {

OpenERPRequest request = new OpenERPRequest("res.partner", "execute", parameters);

String requestXml = RequestBuilder.buildNewRequest(request, id, database, password);
String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password);

String expected = "<?xml version='1.0' encoding='utf-8'?>\n" +
"<methodCall>" +
Expand Down

0 comments on commit 00f6757

Please sign in to comment.