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 3, 2023
1 parent 67f85c0 commit ddf0d24
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 66 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 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 @@ -250,7 +250,8 @@
<constructor-arg ref="openERPAtomFeedProperties"/>
<constructor-arg ref="atomFeedTrasactionSupport" />
<constructor-arg ref="openERPXMLClient" />
<constructor-arg ref="openERPRESTClient" />
<constructor-arg ref="environment" />
<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 @@ -10,24 +10,17 @@
import java.util.UUID;

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
Expand Down
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 Down Expand Up @@ -84,9 +81,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,36 @@
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.WebClient;

import java.time.Duration;
import java.util.Arrays;
import java.util.UUID;
import java.util.function.Consumer;

public class RestClient {
private static final Logger logger = LogManager.getLogger(RestClient.class);
private WebClient webClient;

private String accessToken;

private final String baseURL;

private final String username;

private final String password;
private final int connectionTimeout;

public RestClient(String baseURL, String username, String password) {
public RestClient(String baseURL, String username, String password, int connectionTimeout) {
this.baseURL = baseURL;
this.username = username;
this.password = password;
this.connectionTimeout = connectionTimeout;
}

private void login() {
if (accessToken == null) {
OpenERPRequest openERPRequest = new OpenERPRequest("res.users", "login", Arrays.asList(new Parameter("username", username, "String"), new Parameter("password", password, "String")));
OpenERPRequest openERPRequest = new OpenERPRequest("res.users", "login", Arrays.asList(new Parameter("username", username), new Parameter("password", password)));
String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString());
WebClient client = getWebClient(baseURL);
HttpHeaders headers = getHttpHeaders();
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
try{
String response = client.post().uri("api/odoo-login").headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).block();
String response = client.post().uri("api/odoo-login").headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
if (response == null) {
throw new OpenERPException("Login failed");
}
Expand All @@ -65,7 +64,7 @@ public String post(String URI, String requestBody) {
HttpHeaders headers = getHttpHeaders();
headers.set(HttpHeaders.AUTHORIZATION, accessToken);
Consumer<HttpHeaders> consumer = httpHeaders -> httpHeaders.addAll(headers);
String response = client.post().uri(URI).headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).block();
String response = client.post().uri(URI).headers(consumer).bodyValue(requestBody).retrieve().bodyToMono(String.class).timeout(Duration.ofMillis(connectionTimeout)).block();
if (response == null) {
throw new OpenERPException(String.format("Could not post to %s", URI));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.springframework.web.client.RestTemplate;

@Component
public class HttpClient {
private static final Logger logger = LogManager.getLogger(HttpClient.class);
private RestTemplate restTemplate;
public class XMLClient {
private static final Logger logger = LogManager.getLogger(XMLClient.class);
private final RestTemplate restTemplate;

private boolean isTimeoutSet;

@Autowired
public HttpClient(RestTemplate restTemplate) {
public XMLClient(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

Expand Down

0 comments on commit ddf0d24

Please sign in to comment.