Skip to content

Commit

Permalink
[Rahul] | BAH-3331 | Add. Docker Flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rahu1ramesh committed Dec 6, 2023
1 parent e1f8ddc commit 64fdf17
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bahmni.feed.openerp;

public enum AtomfeedServiceConstants {
IS_REST_ENABLED("IS_ODOO_16");

private final String atomfeedServiceConstants;

AtomfeedServiceConstants(String atomfeedServiceConstants) {
this.atomfeedServiceConstants = atomfeedServiceConstants;
}

public String getAtomfeedServiceConstants() {
return atomfeedServiceConstants;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public String getFeedUriForJob(Jobs feedJob) {
throw new RuntimeException("Can not identify feed URI for requested Job.");
}

public String getOdooURIForJob(Jobs feedJob, boolean isOdoo16) {
if (isOdoo16) {
public String getOdooURIForJob(Jobs feedJob, boolean isRestEnabled) {
if (isRestEnabled) {
switch (feedJob) {
case CUSTOMER_FEED:
case SALEORDER_FEED:
Expand All @@ -79,7 +79,7 @@ public String getOdooURIForJob(Jobs feedJob, boolean isOdoo16) {
}
}
else {
return "xmlrpc/object";
return "/xmlrpc/object";
}
}
@Value("${openerp.host}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.bahmni.feed.openerp.client;

import org.bahmni.feed.openerp.AtomfeedServiceConstants;
import org.bahmni.feed.openerp.FeedException;
import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.bahmni.feed.openerp.job.Jobs;
Expand All @@ -16,6 +17,7 @@
import org.ict4h.atomfeed.client.repository.jdbc.AllMarkersJdbcImpl;
import org.ict4h.atomfeed.client.service.FeedClient;
import org.ict4h.atomfeed.server.transaction.AtomFeedSpringTransactionSupport;
import org.springframework.core.env.Environment;

public class AtomFeedClientHelper {
private final OpenERPAtomFeedProperties atomFeedProperties;
Expand All @@ -24,15 +26,17 @@ public class AtomFeedClientHelper {
private final OdooRESTClient odooRESTClient;
private FeedClientFactory feedClientFactory;
private final WebClientProvider webClientProvider;
private final Environment environment;

public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPXMLClient openERPXMLClient, OdooRESTClient odooRESTClient) {
public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, Environment environment, AtomFeedSpringTransactionSupport transactionManager, OpenERPXMLClient openERPXMLClient, OdooRESTClient odooRESTClient) {
this.atomFeedProperties = atomFeedProperties;
this.transactionManager = transactionManager;
this.openERPXMLClient = openERPXMLClient;
this.odooRESTClient = odooRESTClient;
this.webClientProvider = new WebClientProvider(atomFeedProperties);
this.odooRESTClient = odooRESTClient;
this.environment = environment;
}

public FeedClient getAtomFeedClient(Jobs jobName) throws FeedException {
if(this.feedClientFactory == null){
WorkerFactory workerFactory = new WorkerFactory(webClientProvider);
Expand All @@ -46,9 +50,10 @@ FeedClient getAtomFeedClient(Jobs jobName, FeedClientFactory feedClientFactory)
AllFeeds allFeeds = getAllFeeds(atomFeedProperties, cookies);
AllMarkers allMarkers = new AllMarkersJdbcImpl(transactionManager);
AllFailedEvents allFailedEvents = new AllFailedEventsJdbcImpl(transactionManager);
//TODO: Create a toggle between rest and xml client
OpenERPContext openERPContext = new OpenERPContext(openERPXMLClient);
return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPContext, allFeeds, allMarkers, allFailedEvents, jobName);
String isRestEnabled = environment.getProperty(AtomfeedServiceConstants.IS_REST_ENABLED.getAtomfeedServiceConstants());
boolean isRestEnabledValue = Boolean.parseBoolean(isRestEnabled);
OpenERPContext openERPContext = isRestEnabledValue ? new OpenERPContext(odooRESTClient) : new OpenERPContext(openERPXMLClient);
return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPContext, allFeeds, allMarkers, allFailedEvents, jobName, isRestEnabledValue);
}

static AllFeeds getAllFeeds(OpenERPAtomFeedProperties atomFeedProperties, ClientCookies cookies) {
Expand All @@ -59,4 +64,4 @@ static AllFeeds getAllFeeds(OpenERPAtomFeedProperties atomFeedProperties, Client
return new AllFeeds(feedProperties, cookies);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public FeedClientFactory(WorkerFactory workerFactory) {
this.workerFactory = workerFactory;
}

public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPContext openERPContext, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName) {
public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPContext openERPContext, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName, Boolean isRestEnabled) {
String feedUri = openERPAtomFeedProperties.getFeedUriForJob(jobName);
String odooURL = openERPAtomFeedProperties.getOdooURIForJob(jobName,false);
String odooURL = openERPAtomFeedProperties.getOdooURIForJob(jobName, isRestEnabled);
if (StringUtils.isBlank(feedUri)) {
String message = String.format("No feed-uri defined for Job [%s][%s]", jobName, jobName.getFeedUriRef());
logger.warn(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,31 +51,33 @@ private OpenERPRequest mapRequest(Event event) throws IOException {
return new OpenERPRequest("atom.event.worker", "process_event", buildParameters(event,drug));
}

private List<Parameter> buildParameters(Event event,OpenMRSDrug drug) {
private List<Parameter> buildParameters(Event event, OpenMRSDrug drug) {
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter("name",drug.getName()));
parameters.add(new Parameter("shortName",drug.getShortName()));
parameters.add(new Parameter("uuid",drug.getUuid()));
parameters.add(new Parameter("combination",drug.getCombination()));
parameters.add(new Parameter("strength",drug.getStrength()));
parameters.add(new Parameter("dosageForm",drug.getDosageForm()));
parameters.add(new Parameter("genericName",drug.getGenericName()));
parameters.add(new Parameter("maximumDose",drug.getMaximumDose()));
parameters.add(new Parameter("minimumDose",drug.getMinimumDose()));

parameters.add(new Parameter("last_read_entry_id",event.getId()));
parameters.add(new Parameter("category", "create.drug"));
parameters.add(new Parameter("feed_uri_for_last_read_entry",event.getFeedUri()));

addToParametersIfNotEmpty(parameters, "name", drug.getName());
addToParametersIfNotEmpty(parameters, "shortName", drug.getShortName());
addToParametersIfNotEmpty(parameters, "uuid", drug.getUuid());
addToParametersIfNotEmpty(parameters, "combination", drug.getCombination());
addToParametersIfNotEmpty(parameters, "strength", drug.getStrength());
addToParametersIfNotEmpty(parameters, "dosageForm", drug.getDosageForm());
addToParametersIfNotEmpty(parameters, "genericName", drug.getGenericName());
addToParametersIfNotEmpty(parameters, "maximumDose", drug.getMaximumDose());
addToParametersIfNotEmpty(parameters, "minimumDose", drug.getMinimumDose());
addToParametersIfNotEmpty(parameters, "last_read_entry_id", event.getId());
addToParametersIfNotEmpty(parameters, "category", "create.drug");
addToParametersIfNotEmpty(parameters, "feed_uri_for_last_read_entry", event.getFeedUri());
if (event.getFeedUri() == null) {
parameters.add(new Parameter("is_failed_event","1","boolean"));
parameters.add(new Parameter("is_failed_event", "1", "boolean"));
}

return parameters;
}

private void addToParametersIfNotEmpty(List<Parameter> parameters, String name, String value) {
if (value != null && !value.isEmpty()) {
parameters.add(new Parameter(name, value));
}
}

@Override
public void cleanUp(Event event) {

}
}
}
3 changes: 2 additions & 1 deletion openerp-atomfeed-service/src/main/resources/jobs-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,10 @@

<bean id="atomFeedClientHelper" class="org.bahmni.feed.openerp.client.AtomFeedClientHelper" scope="prototype">
<constructor-arg ref="openERPAtomFeedProperties"/>
<constructor-arg ref="environment" />
<constructor-arg ref="atomFeedTrasactionSupport" />
<constructor-arg ref="openERPXMLClient" />
<constructor-arg ref="openERPRESTClient" />
<constructor-arg ref="odooRESTClient" />
</bean>

<bean id="openerpEventsOptimizerJob" class="org.bahmni.feed.openerp.job.OpenERPEventsOptimizerJob" scope="prototype">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
import java.util.Vector;

public interface OpenERPClientStrategy {
Object execute(OpenERPRequest openERPRequest, String URI);

Object execute(OpenERPRequest openERPRequest, String URL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public OpenERPContext(OpenERPClientStrategy openERPClient) {
this.openERPClient = openERPClient;
}

public Object execute(OpenERPRequest openERPRequest, String URI) {
return openERPClient.execute(openERPRequest, URI);
public Object execute(OpenERPRequest openERPRequest, String URL) {
return openERPClient.execute(openERPRequest, URL);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,28 @@
import org.bahmni.openerp.web.request.OpenERPRequest;
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.util.UUID;

@Service
@Lazy
public class OdooRESTClient implements OpenERPClientStrategy {
private final int connectionTimeoutInMilliseconds;
private final int replyTimeoutInMilliseconds;
private String host;
private final int port;
private final String user;
private final String password;
private final RestClient restClient;


@Autowired
public OdooRESTClient(OpenERPProperties openERPProperties) {
host = openERPProperties.getHost();
port = openERPProperties.getPort();
user = openERPProperties.getUser();
password = openERPProperties.getPassword();
restClient = new RestClient("http://" + host + ":" + port, user, password);
connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds();
replyTimeoutInMilliseconds = openERPProperties.getReplyTimeoutInMilliseconds();
final String host = openERPProperties.getHost();
final int port = openERPProperties.getPort();
final String user = openERPProperties.getUser();
final String password = openERPProperties.getPassword();
final int connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds();
restClient = new RestClient("http://" + host + ":" + port, user, password, connectionTimeoutInMilliseconds);
}

@Override
public Object execute(OpenERPRequest openERPRequest, String URI) {
public Object execute(OpenERPRequest openERPRequest, String URL) {
String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString());
return restClient.post(URI, requestBody);
return restClient.post(URL, requestBody);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
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.http.client.XMLClient;
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;
Expand All @@ -24,23 +24,20 @@
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;
private final XMLClient XMLClient;

@Autowired
public OpenERPXMLClient(HttpClient httpClient, OpenERPProperties openERPProperties) {
this.httpClient = httpClient;
public OpenERPXMLClient(XMLClient XMLClient, OpenERPProperties openERPProperties) {
this.XMLClient = XMLClient;
host = openERPProperties.getHost();
port = openERPProperties.getPort();
database = openERPProperties.getDatabase();
Expand All @@ -53,12 +50,10 @@ public OpenERPXMLClient(HttpClient httpClient, OpenERPProperties openERPProperti
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));
Expand All @@ -67,12 +62,16 @@ private void login() {
}

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


Expand All @@ -84,9 +83,9 @@ private Object executeRPC(XmlRpcClient loginRpcClient, Vector params, String met
}
}

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

private XmlRpcClient xmlRpcClient(String endpoint) {
Expand All @@ -95,7 +94,7 @@ private XmlRpcClient xmlRpcClient(String endpoint) {
}
XmlRpcClientConfigImpl clientConfig = (XmlRpcClientConfigImpl) xmlRpcClient.getClientConfig();
try {
clientConfig.setServerURL(new URL("http", host, port, endpoint));
clientConfig.setServerURL(new URL("http", host, port, endpoint));
} catch (MalformedURLException e) {
throw new OpenERPException(e);
}
Expand Down
Loading

0 comments on commit 64fdf17

Please sign in to comment.