-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Parvathy, Rahul] | Add. OpenERP Strategy and Context
Co-authored-by: Parvathy Babu <[email protected]>
- Loading branch information
1 parent
b4ec291
commit 00f6757
Showing
6 changed files
with
236 additions
and
9 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
...rp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPClientStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
47 changes: 47 additions & 0 deletions
47
openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/OpenERPContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
...src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OpenERPXMLClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,4 @@ | |
</struct></value> | ||
</param> | ||
</params> | ||
</methodCall> | ||
</methodCall> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters