diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java new file mode 100644 index 00000000..29ea6eff --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java @@ -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); +} \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java new file mode 100644 index 00000000..027d314b --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java @@ -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); + } +} \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java new file mode 100644 index 00000000..a43eb82f --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java @@ -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 params = new Vector(); + 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; + } +} \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java index f4fc447d..01fab54f 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/RequestBuilder.java @@ -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); diff --git a/openerp-client/src/main/resources/request/template/new_customer.vm b/openerp-client/src/main/resources/request/template/xml_template.vm similarity index 98% rename from openerp-client/src/main/resources/request/template/new_customer.vm rename to openerp-client/src/main/resources/request/template/xml_template.vm index 39a3cbd7..755cce27 100644 --- a/openerp-client/src/main/resources/request/template/new_customer.vm +++ b/openerp-client/src/main/resources/request/template/xml_template.vm @@ -28,4 +28,4 @@ - + \ No newline at end of file diff --git a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java index 6b20ed8e..2092b4d0 100644 --- a/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java +++ b/openerp-client/src/test/java/org/bahmni/openerp/web/request/builder/RequestBuilderTest.java @@ -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 = "\n" + "" + @@ -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 = "\n" + "" + @@ -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 = "\n" + "" +