Skip to content

Commit

Permalink
BAH-3331 | Add. Rest Client Implementation for Create Drug
Browse files Browse the repository at this point in the history
* [Rahul] | Add. Rest Based Implementation

* [Rahul] | Add. Endpoints For Each Job

* [Parvathy, Rahul] | BAH-3331 | Add. Docker Flag

Co-authored-by: Parvathy Babu <[email protected]>

* [Parvathy, Rahul] | BAH-3331 | Add. Un/Escape Character Parser

Co-authored-by: Parvathy Babu <[email protected]>

* [Parvathy, Rahul] | BAH-3331 | Refactor. Rename Variables and Introduced Jobs Enum

Co-authored-by: Parvathy Babu <[email protected]>

* [Parvathy, Rahul] | BAH-3331 | Refactor. Rename Endpoint URL -> Odoo URL

Co-authored-by: Parvathy Babu <[email protected]>

---------

Co-authored-by: Rahul Ramesh <[email protected]>
  • Loading branch information
parvathy00 and rahu1ramesh authored Nov 30, 2023
1 parent 20014d5 commit 1798255
Show file tree
Hide file tree
Showing 48 changed files with 464 additions and 171 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 @@ -2,7 +2,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bahmni.feed.openerp.job.Jobs;
import org.bahmni.feed.openerp.job.FeedURI;
import org.bahmni.openerp.web.OpenERPProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -27,7 +27,7 @@ public String getSchedulerInitialDelay() {


/**
* @deprecated replaced by {@link #getFeedUriForJob(Jobs)} ()}
* @deprecated replaced by {@link #getFeedUriForJob(FeedURI)} ()}
*/
@Deprecated
public String getFeedUri(String feedname) {
Expand Down Expand Up @@ -55,8 +55,9 @@ public String getFeedUri(String feedname) {
@Value("${saleable.feed.generator.uri}")
private String saleableFeedUri;

public String getFeedUriForJob(Jobs feedJob) {
switch (feedJob){

public String getFeedUriForJob(FeedURI feedURIJob) {
switch (feedURIJob){
case CUSTOMER_FEED: return customFeedUri;
case SALEORDER_FEED: return saleOrderFeedUri;
case OPENELIS_SALEORDER_FEED: return elisSaleOrderFeedUri;
Expand All @@ -67,6 +68,23 @@ public String getFeedUriForJob(Jobs feedJob) {
throw new RuntimeException("Can not identify feed URI for requested Job.");
}

public String getOdooURLForJob(FeedURI feedURIJob, boolean isRestEnabled) {
if (isRestEnabled) {
switch (feedURIJob) {
case CUSTOMER_FEED:
case SALEORDER_FEED:
case LAB_FEED:
case SALEABLE_FEED: return "";
case DRUG_FEED: return "/api/bahmni-drug";
default:
throw new RuntimeException("Can not identify Odoo URL for requested Job.");
}
}
else {
return "xmlrpc/object";
}
}

@Value("${openerp.host}")
private String openErpHost;

Expand Down Expand Up @@ -191,8 +209,6 @@ private void debug() {
logger.debug("**************** DEBUG OpenERPAtomFeedProperties ************************ ");
}



private HashMap<String, String> getInfo() {
HashMap<String, String> values = new HashMap<>();
values.put("chunking.strategy",chunkingStrategy );
Expand All @@ -218,5 +234,4 @@ private HashMap<String, String> getInfo() {
return values;
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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;
import org.bahmni.feed.openerp.job.FeedURI;
import org.bahmni.feed.openerp.worker.WorkerFactory;
import org.bahmni.openerp.web.client.strategy.OpenERPContext;
import org.bahmni.openerp.web.client.strategy.implementation.OdooRESTClient;
import org.bahmni.openerp.web.client.strategy.implementation.OpenERPXMLClient;
import org.bahmni.webclients.ClientCookies;
import org.ict4h.atomfeed.client.AtomFeedProperties;
Expand All @@ -15,37 +17,43 @@
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;
private final AtomFeedSpringTransactionSupport transactionManager;
private final OpenERPXMLClient openERPXMLClient;
private final OdooRESTClient odooRESTClient;
private FeedClientFactory feedClientFactory;
private final WebClientProvider webClientProvider;
private final Environment environment;

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

public FeedClient getAtomFeedClient(Jobs jobName) throws FeedException {
public FeedClient getAtomFeedClient(FeedURI jobName) throws FeedException {
if(this.feedClientFactory == null){
WorkerFactory workerFactory = new WorkerFactory(webClientProvider);
feedClientFactory = new FeedClientFactory(workerFactory);
}
return getAtomFeedClient(jobName, feedClientFactory);
}

FeedClient getAtomFeedClient(Jobs jobName, FeedClientFactory feedClientFactory) throws FeedException {
FeedClient getAtomFeedClient(FeedURI jobName, FeedClientFactory feedClientFactory) throws FeedException {
ClientCookies cookies = webClientProvider.getWebClient(jobName).getCookies();
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.apache.logging.log4j.Logger;
import org.bahmni.feed.openerp.FeedException;
import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.bahmni.feed.openerp.job.Jobs;
import org.bahmni.feed.openerp.job.FeedURI;
import org.bahmni.feed.openerp.worker.WorkerFactory;
import org.bahmni.openerp.web.client.strategy.OpenERPContext;
import org.ict4h.atomfeed.client.AtomFeedProperties;
Expand All @@ -31,24 +31,29 @@ 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, FeedURI jobName, boolean isRestEnabled) {
String feedUri = openERPAtomFeedProperties.getFeedUriForJob(jobName);
String odooURL = openERPAtomFeedProperties.getOdooURLForJob(jobName,isRestEnabled);
if (StringUtils.isBlank(feedUri)) {
String message = String.format("No feed-uri defined for Job [%s][%s]", jobName, jobName.getFeedUriRef());
logger.warn(message);
throw new FeedException(message);
}

if (StringUtils.isBlank(odooURL)) {
String message = String.format("No Odoo URL defined for Job [%s][%s]", jobName, jobName.getFeedUriRef());
logger.warn(message);
throw new FeedException(message);
}
try {
String urlPrefix = getURLPrefix(jobName,openERPAtomFeedProperties);
EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, openERPContext, urlPrefix);
EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, odooURL, openERPContext, urlPrefix);
return new AtomFeedClient(allFeeds, allMarkers, allFailedEvents, atomFeedProperties(openERPAtomFeedProperties), transactionManager, new URI(feedUri), eventWorker) ;
} catch (URISyntaxException e) {
throw new RuntimeException("error for uri:" + feedUri, e);
}
}

static String getURLPrefix(Jobs jobName, OpenERPAtomFeedProperties atomFeedProperties) {
static String getURLPrefix(FeedURI jobName, OpenERPAtomFeedProperties atomFeedProperties) {
String endpointURI = getURIForJob(jobName,atomFeedProperties);
try {
if(endpointURI != null && !endpointURI.isEmpty()){
Expand All @@ -60,7 +65,7 @@ static String getURLPrefix(Jobs jobName, OpenERPAtomFeedProperties atomFeedPrope
}
}

private static String getURIForJob(Jobs jobName,OpenERPAtomFeedProperties atomFeedProperties){
private static String getURIForJob(FeedURI jobName, OpenERPAtomFeedProperties atomFeedProperties){
switch (jobName){
case CUSTOMER_FEED:
case SALEORDER_FEED:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.bahmni.feed.openerp.client;

import org.bahmni.feed.openerp.OpenERPAtomFeedProperties;
import org.bahmni.feed.openerp.job.Jobs;
import org.bahmni.feed.openerp.job.FeedURI;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -38,7 +38,7 @@ public ReferenceDataWebClient referenceDataWebClient(){
return referenceDataWebClient;
}

public AbstractWebClient getWebClient(Jobs jobName){
public AbstractWebClient getWebClient(FeedURI jobName){
switch (jobName){
case CUSTOMER_FEED: return getOpenMRSWebClient() ;
case DRUG_FEED: return getOpenMRSWebClient() ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private boolean orderAlreadyPresent(OpenERPOrders orders, OpenElisTestDetail tes
}

private boolean hasOrders() {
return testDetails.size() > 0;
return !testDetails.isEmpty();
}

private void addNewOrder(OpenERPOrders orders, OpenElisTestDetail testDetail) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.bahmni.feed.openerp.job;


public enum Jobs {
public enum FeedURI {
CUSTOMER_FEED("customer.feed.generator.uri"),
SALEORDER_FEED("saleorder.feed.generator.uri"),
REFERENCE_DATA_FEED("referencedata.feed.generator.uri"),
Expand All @@ -12,7 +12,7 @@ public enum Jobs {

private final String feedUriRef;

Jobs(String feedUriRef) {
FeedURI(String feedUriRef) {
this.feedUriRef = feedUriRef;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public OpenERPCustomerFeedJob(SimpleFeedJob openMRSFeedJob) throws FeedException
}

public void processFeed() {
openMRSFeedJob.processFeed(Jobs.CUSTOMER_FEED);
openMRSFeedJob.processFeed(FeedURI.CUSTOMER_FEED);
}

public void processFailedEvents() {
openMRSFeedJob.processFailedEvents(Jobs.CUSTOMER_FEED);
openMRSFeedJob.processFailedEvents(FeedURI.CUSTOMER_FEED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public OpenERPDrugFeedJob(SimpleFeedJob openMRSFeedJob) throws FeedException {
}

public void processFeed() {
openMRSFeedJob.processFeed(Jobs.DRUG_FEED);
openMRSFeedJob.processFeed(FeedURI.DRUG_FEED);
}

public void processFailedEvents() {
openMRSFeedJob.processFailedEvents(Jobs.DRUG_FEED);
openMRSFeedJob.processFailedEvents(FeedURI.DRUG_FEED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public OpenERPLabFeedJob(SimpleFeedJob openMRSFeedJob) throws FeedException {
}

public void processFeed() {
openMRSFeedJob.processFeed(Jobs.LAB_FEED);
openMRSFeedJob.processFeed(FeedURI.LAB_FEED);
}

public void processFailedEvents() {
openMRSFeedJob.processFailedEvents(Jobs.LAB_FEED);
openMRSFeedJob.processFailedEvents(FeedURI.LAB_FEED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public OpenERPSaleOrderFeedJob(SimpleFeedJob openMRSFeedJob) throws FeedExceptio
}

public void processFeed() {
openMRSFeedJob.processFeed(Jobs.SALEORDER_FEED);
openMRSFeedJob.processFeed(FeedURI.SALEORDER_FEED);
}

public void processFailedEvents() {
openMRSFeedJob.processFailedEvents(Jobs.SALEORDER_FEED);
openMRSFeedJob.processFailedEvents(FeedURI.SALEORDER_FEED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.sun.syndication.io.FeedException;

import static org.bahmni.feed.openerp.job.Jobs.SALEABLE_FEED;
import static org.bahmni.feed.openerp.job.FeedURI.SALEABLE_FEED;

public class OpenERPSaleableFeedJob {
private SimpleFeedJob feedJob;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public OpenElisSaleOrderFeedJob(SimpleFeedJob openElisFeedJob) throws FeedExcept
}

public void processFeed() {
openElisFeedJob.processFeed(Jobs.OPENELIS_SALEORDER_FEED);
openElisFeedJob.processFeed(FeedURI.OPENELIS_SALEORDER_FEED);
}

public void processFailedEvents() {
openElisFeedJob.processFailedEvents(Jobs.OPENELIS_SALEORDER_FEED);
openElisFeedJob.processFailedEvents(FeedURI.OPENELIS_SALEORDER_FEED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public ReferenceDataFeedJob(SimpleFeedJob simpleFeedJob) throws FeedException {
}

public void processFeed() {
simpleFeedJob.processFeed(Jobs.REFERENCE_DATA_FEED);
simpleFeedJob.processFeed(FeedURI.REFERENCE_DATA_FEED);
}

public void processFailedEvents() {
simpleFeedJob.processFailedEvents(Jobs.REFERENCE_DATA_FEED);
simpleFeedJob.processFailedEvents(FeedURI.REFERENCE_DATA_FEED);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SimpleFeedJob(
this.atomFeedClientHelper = atomFeedClientHelper;
}

public void processFeed(Jobs jobName) {
public void processFeed(FeedURI jobName) {
try {
logger.info(String.format("Processing Feed [%s] Job [%s]", jobName.getFeedUriRef(), jobName));
initAtomFeedClient(jobName);
Expand All @@ -30,13 +30,13 @@ public void processFeed(Jobs jobName) {
}
}

private void initAtomFeedClient(Jobs jobName) {
private void initAtomFeedClient(FeedURI jobName) {
if(atomFeedClient == null){
atomFeedClient = (AtomFeedClient) atomFeedClientHelper.getAtomFeedClient(jobName);
}
}

public void processFailedEvents(Jobs jobName) {
public void processFailedEvents(FeedURI jobName) {
try {
logger.info(String.format("Processing failed events for Feed [%s]", jobName.getFeedUriRef()));
initAtomFeedClient(jobName);
Expand All @@ -49,7 +49,7 @@ public void processFailedEvents(Jobs jobName) {
}
}

protected void handleAuthorizationException(Throwable e, Jobs jobName) throws FeedException {
protected void handleAuthorizationException(Throwable e, FeedURI jobName) throws FeedException {
if (e != null &&
(ExceptionUtils.getStackTrace(e).contains("HTTP response code: 401") || ExceptionUtils.getStackTrace(e).contains("Bad response code of 403"))
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.bahmni.feed.openerp.worker;

public enum Jobs {
DRUG_JOB("create.drug");

private final String jobRef;

Jobs(String jobRef) {
this.jobRef = jobRef;
}

public String getJobRef() {
return jobRef;
}
}
Loading

0 comments on commit 1798255

Please sign in to comment.