From 1798255e8e14f89d0d6ab9b622f7e8064793b7ff Mon Sep 17 00:00:00 2001 From: Parvathy Babu <102500787+parvathy00@users.noreply.github.com> Date: Thu, 30 Nov 2023 15:21:32 +0530 Subject: [PATCH] BAH-3331 | Add. Rest Client Implementation for Create Drug * [Rahul] | Add. Rest Based Implementation * [Rahul] | Add. Endpoints For Each Job * [Parvathy, Rahul] | BAH-3331 | Add. Docker Flag Co-authored-by: Parvathy Babu * [Parvathy, Rahul] | BAH-3331 | Add. Un/Escape Character Parser Co-authored-by: Parvathy Babu * [Parvathy, Rahul] | BAH-3331 | Refactor. Rename Variables and Introduced Jobs Enum Co-authored-by: Parvathy Babu * [Parvathy, Rahul] | BAH-3331 | Refactor. Rename Endpoint URL -> Odoo URL Co-authored-by: Parvathy Babu --------- Co-authored-by: Rahul Ramesh --- .../openerp/AtomfeedServiceConstants.java | 15 +++ .../openerp/OpenERPAtomFeedProperties.java | 29 ++++-- .../openerp/client/AtomFeedClientHelper.java | 22 +++-- .../openerp/client/FeedClientFactory.java | 17 ++-- .../openerp/client/WebClientProvider.java | 4 +- .../domain/openelis/OpenElisLabOrder.java | 2 +- .../openerp/job/{Jobs.java => FeedURI.java} | 4 +- .../openerp/job/OpenERPCustomerFeedJob.java | 4 +- .../feed/openerp/job/OpenERPDrugFeedJob.java | 4 +- .../feed/openerp/job/OpenERPLabFeedJob.java | 4 +- .../openerp/job/OpenERPSaleOrderFeedJob.java | 4 +- .../openerp/job/OpenERPSaleableFeedJob.java | 2 +- .../openerp/job/OpenElisSaleOrderFeedJob.java | 4 +- .../openerp/job/ReferenceDataFeedJob.java | 4 +- .../feed/openerp/job/SimpleFeedJob.java | 8 +- .../org/bahmni/feed/openerp/worker/Jobs.java | 15 +++ .../OpenERPCustomerServiceEventWorker.java | 7 +- .../worker/OpenERPDrugServiceEventWorker.java | 55 ++++++----- ...OpenERPLabOrderTypeServiceEventWorker.java | 7 +- .../worker/OpenERPSaleOrderEventWorker.java | 7 +- .../worker/OpenERPSaleableResourceWorker.java | 7 +- .../worker/OpenElisSaleOrderEventWorker.java | 7 +- .../feed/openerp/worker/WorkerFactory.java | 18 ++-- .../main/resources/erp-atomfeed.properties | 10 +- .../src/main/resources/jobs-context.xml | 2 + .../openerp/client/OpenERPCustomerFeedIT.java | 11 ++- ...OpenERPCustomerServiceEventWorkerTest.java | 6 +- .../openerp/job/OpenERPDrugFeedJobTest.java | 2 +- .../openerp/job/OpenERPLabFeedJobTest.java | 2 +- .../job/OpenERPSaleableFeedJobTest.java | 2 +- .../feed/openerp/server/AtomFeedServerIT.java | 1 - .../OpenERPDrugServiceEventWorkerTest.java | 8 +- ...ERPLabOrderTypeServiceEventWorkerTest.java | 15 +-- .../worker/OpenERPSaleOrderEventWorkerIT.java | 6 +- .../OpenERPSaleableResourceWorkerTest.java | 9 +- openerp-client/pom.xml | 31 ++++++ .../strategy/OpenERPClientStrategy.java | 2 +- .../web/client/strategy/OpenERPContext.java | 4 +- .../implementation/OdooRESTClient.java | 34 +++++++ .../implementation/OpenERPXMLClient.java | 17 ++-- .../openerp/web/http/client/RestClient.java | 98 +++++++++++++++++++ .../{HttpClient.java => XMLClient.java} | 6 +- .../web/request/builder/Parameter.java | 5 +- .../web/request/builder/RequestBuilder.java | 20 ++++ .../request/template/rest_template.vm | 10 ++ .../request/template/xml_template.vm | 62 ++++++------ .../request/builder/RequestBuilderTest.java | 13 ++- pom.xml | 9 +- 48 files changed, 464 insertions(+), 171 deletions(-) create mode 100644 openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java rename openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/{Jobs.java => FeedURI.java} (91%) create mode 100644 openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/Jobs.java create mode 100644 openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java create mode 100644 openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java rename openerp-client/src/main/java/org/bahmni/openerp/web/http/client/{HttpClient.java => XMLClient.java} (93%) create mode 100644 openerp-client/src/main/resources/request/template/rest_template.vm diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java new file mode 100644 index 00000000..88fbf6d0 --- /dev/null +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/AtomfeedServiceConstants.java @@ -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; + } +} diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java index 3bfe5995..8f4400ea 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/OpenERPAtomFeedProperties.java @@ -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; @@ -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) { @@ -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; @@ -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; @@ -191,8 +209,6 @@ private void debug() { logger.debug("**************** DEBUG OpenERPAtomFeedProperties ************************ "); } - - private HashMap getInfo() { HashMap values = new HashMap<>(); values.put("chunking.strategy",chunkingStrategy ); @@ -218,5 +234,4 @@ private HashMap getInfo() { return values; } - } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java index 4733f737..c887242f 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java @@ -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; @@ -15,22 +17,27 @@ 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); @@ -38,14 +45,15 @@ public FeedClient getAtomFeedClient(Jobs jobName) throws FeedException { 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) { diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java index f7fbcfaa..be76154b 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java @@ -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; @@ -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()){ @@ -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: diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/WebClientProvider.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/WebClientProvider.java index 273dfa61..ffb94763 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/WebClientProvider.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/WebClientProvider.java @@ -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; @@ -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() ; diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/openelis/OpenElisLabOrder.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/openelis/OpenElisLabOrder.java index c48bacd4..ae0382c9 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/openelis/OpenElisLabOrder.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/domain/openelis/OpenElisLabOrder.java @@ -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) { diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/Jobs.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/FeedURI.java similarity index 91% rename from openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/Jobs.java rename to openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/FeedURI.java index a0497811..5fbea626 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/Jobs.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/FeedURI.java @@ -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"), @@ -12,7 +12,7 @@ public enum Jobs { private final String feedUriRef; - Jobs(String feedUriRef) { + FeedURI(String feedUriRef) { this.feedUriRef = feedUriRef; } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPCustomerFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPCustomerFeedJob.java index 452a11ea..e2a18cf0 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPCustomerFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPCustomerFeedJob.java @@ -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); } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJob.java index ebaeba29..9a6a91bb 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJob.java @@ -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); } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJob.java index f6658e58..bbd4ef0a 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJob.java @@ -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); } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleOrderFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleOrderFeedJob.java index 500361b5..eb0741d6 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleOrderFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleOrderFeedJob.java @@ -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); } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJob.java index 322c9e9c..7863f584 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJob.java @@ -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; diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenElisSaleOrderFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenElisSaleOrderFeedJob.java index f1ae64a3..4e0ffa43 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenElisSaleOrderFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/OpenElisSaleOrderFeedJob.java @@ -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); } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/ReferenceDataFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/ReferenceDataFeedJob.java index 511bd695..2e0c6427 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/ReferenceDataFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/ReferenceDataFeedJob.java @@ -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); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/SimpleFeedJob.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/SimpleFeedJob.java index 63e98f1a..3984e689 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/SimpleFeedJob.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/job/SimpleFeedJob.java @@ -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); @@ -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); @@ -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")) ) { diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/Jobs.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/Jobs.java new file mode 100644 index 00000000..1147183d --- /dev/null +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/Jobs.java @@ -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; + } +} diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java index e5c45f76..e33c67a4 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java @@ -24,13 +24,16 @@ public class OpenERPCustomerServiceEventWorker implements EventWorker { OpenERPContext openERPContext; private String feedUrl; + + private String odooURL; private OpenMRSWebClient webClient; private String urlPrefix; private static Logger logger = LogManager.getLogger(OpenERPCustomerServiceEventWorker.class); - public OpenERPCustomerServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { + public OpenERPCustomerServiceEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; + this.odooURL = odooURL; this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; @@ -39,7 +42,7 @@ public OpenERPCustomerServiceEventWorker(String feedUrl, OpenERPContext openERPC @Override public void process(Event event) { try { - openERPContext.execute(mapRequest(event)); + openERPContext.execute(mapRequest(event), odooURL); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java index 3ea2db41..3e32d4b4 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java @@ -18,17 +18,21 @@ public class OpenERPDrugServiceEventWorker implements EventWorker { - private static Logger logger = LogManager.getLogger(OpenERPDrugServiceEventWorker.class); + private static final Logger logger = LogManager.getLogger(OpenERPDrugServiceEventWorker.class); - private OpenERPContext openERPContext; + private final OpenERPContext openERPContext; private String feedUrl; - private OpenMRSWebClient webClient; - private String urlPrefix; + private final String odooURL; - public OpenERPDrugServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + private final OpenMRSWebClient webClient; + private final String urlPrefix; + + + public OpenERPDrugServiceEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { this.openERPContext = openERPContext; this.feedUrl = feedUrl; + this.odooURL = odooURL; this.webClient = openMRSWebClient; this.urlPrefix = urlPrefix; } @@ -37,7 +41,7 @@ public OpenERPDrugServiceEventWorker(String feedUrl, OpenERPContext openERPConte public void process(Event event) { logger.debug("Processing the event [{}]", event.getContent()); try { - openERPContext.execute(mapRequest(event)); + openERPContext.execute(mapRequest(event), odooURL); } catch (Exception e) { throw new RuntimeException(e); } @@ -46,35 +50,36 @@ public void process(Event event) { private OpenERPRequest mapRequest(Event event) throws IOException { String drugJson = webClient.get(URI.create(urlPrefix + event.getContent())); OpenMRSDrug drug = ObjectMapperRepository.objectMapper.readValue(drugJson, OpenMRSDrug.class); - return new OpenERPRequest("atom.event.worker", "process_event", buildParameters(event,drug)); } - private List buildParameters(Event event,OpenMRSDrug drug) { + private List buildParameters(Event event, OpenMRSDrug drug) { List 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", Jobs.DRUG_JOB.getJobRef()); + 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 parameters, String name, String value) { + if (value != null && !value.isEmpty()) { + parameters.add(new Parameter(name, value)); + } + } + @Override public void cleanUp(Event event) { - } } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java index a53efa60..f2e5f2d0 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java @@ -23,14 +23,17 @@ public class OpenERPLabOrderTypeServiceEventWorker implements EventWorker { private OpenERPContext openERPContext; private String feedUrl; + + private String odooURL; private OpenMRSWebClient webClient; private String urlPrefix; private Map labOrderTypeEventMap = new HashMap<>(); - public OpenERPLabOrderTypeServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + public OpenERPLabOrderTypeServiceEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { this.openERPContext = openERPContext; this.feedUrl = feedUrl; + this.odooURL = odooURL; this.webClient = openMRSWebClient; this.urlPrefix = urlPrefix; labOrderTypeEventMap.put(OpenMRSRadiologyTestEvent.RADIOLOGY_TEST_EVENT_NAME, new OpenMRSRadiologyTestEvent()); @@ -44,7 +47,7 @@ public void process(Event event) { try { OpenMRSLabOrderTypeEvent openMRSLabOrderTypeEvent = labOrderTypeEventMap.get(event.getTitle()); if(openMRSLabOrderTypeEvent == null) return ; - openERPContext.execute(mapRequest(event, openMRSLabOrderTypeEvent)); + openERPContext.execute(mapRequest(event, openMRSLabOrderTypeEvent), odooURL); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java index e00b5baa..bdddc3b0 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java @@ -19,14 +19,17 @@ public class OpenERPSaleOrderEventWorker implements EventWorker { OpenERPContext openERPContext; private String feedUrl; + + private String odooURL; private OpenMRSWebClient webClient; private String urlPrefix; private static Logger logger = LogManager.getLogger(OpenERPSaleOrderEventWorker.class); - public OpenERPSaleOrderEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { + public OpenERPSaleOrderEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; + this.odooURL = odooURL; this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; @@ -38,7 +41,7 @@ public void process(Event event) { OpenERPRequest openERPRequest = mapRequest(event); if (!openERPRequest.shouldERPConsumeEvent()) return; - openERPContext.execute(openERPRequest); + openERPContext.execute(openERPRequest, odooURL); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java index 2f4fae19..ef2ad7d3 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java @@ -26,11 +26,14 @@ public class OpenERPSaleableResourceWorker implements EventWorker { private OpenMRSWebClient openMRSWebClient; private String urlPrefix; + private String odooURL; + private static Logger logger = LogManager.getLogger(OpenERPSaleableResourceWorker.class); - public OpenERPSaleableResourceWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + public OpenERPSaleableResourceWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { this.openERPContext = openERPContext; this.feedUrl = feedUrl; + this.odooURL = odooURL; this.openMRSWebClient = openMRSWebClient; this.urlPrefix = urlPrefix; } @@ -44,7 +47,7 @@ public void process(Event event) { logger.info(String.format("Resource is not a saleable resource. Ignoring. Event [%s]", event.getId())); return; } - openERPContext.execute(mapToOpenERPRequest(event, resource)); + openERPContext.execute(mapToOpenERPRequest(event, resource), odooURL); } catch (Exception e) { logger.error(String.format("Error occurred while trying to process Saleable Event [%s]", event.getId()), e); throw new RuntimeException(String.format("Error occurred while trying to process Saleable Event [%s]", event.getId()), e); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java index c11e924e..a22dcd25 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java @@ -20,10 +20,13 @@ public class OpenElisSaleOrderEventWorker implements EventWorker { private final OpenElisWebClient webClient; private final String urlPrefix; + private String odooURL; + private static final Logger logger = LogManager.getLogger(OpenElisSaleOrderEventWorker.class); - public OpenElisSaleOrderEventWorker(String feedUrl, OpenERPContext openERPContext, OpenElisWebClient webClient, String urlPrefix) { + public OpenElisSaleOrderEventWorker(String feedUrl, String odooURL, OpenERPContext openERPContext, OpenElisWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; + this.odooURL = odooURL; this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; @@ -36,7 +39,7 @@ public void process(Event event) { OpenERPRequest openERPRequest = mapRequest(event); if (!openERPRequest.shouldERPConsumeEvent()) return; - openERPContext.execute(openERPRequest); + openERPContext.execute(openERPRequest, odooURL); } catch (Exception e) { logger.error("Error processing openelis sale order event : {}", event.toString(), e); throw new RuntimeException(e); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java index b373bc45..176e0992 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java @@ -1,6 +1,6 @@ package org.bahmni.feed.openerp.worker; -import org.bahmni.feed.openerp.job.Jobs; +import org.bahmni.feed.openerp.job.FeedURI; import org.bahmni.feed.openerp.client.WebClientProvider; import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.ict4h.atomfeed.client.service.EventWorker; @@ -14,14 +14,14 @@ public WorkerFactory(WebClientProvider webClientProvider) { } - public EventWorker getWorker(Jobs jobName, String feedUrl, OpenERPContext openERPContext, String urlPrefix) { - switch (jobName){ - case CUSTOMER_FEED: return new OpenERPCustomerServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case OPENELIS_SALEORDER_FEED: return new OpenElisSaleOrderEventWorker(feedUrl, openERPContext, webClientProvider.openElisWebClient(), urlPrefix); - case DRUG_FEED: return new OpenERPDrugServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case LAB_FEED: return new OpenERPLabOrderTypeServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case SALEABLE_FEED: return new OpenERPSaleableResourceWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + public EventWorker getWorker(FeedURI jobName, String feedUrl, String odooURL, OpenERPContext openERPContext, String urlPrefix) { + switch (jobName) { + case CUSTOMER_FEED: return new OpenERPCustomerServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case OPENELIS_SALEORDER_FEED: return new OpenElisSaleOrderEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.openElisWebClient(), urlPrefix); + case DRUG_FEED: return new OpenERPDrugServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case LAB_FEED: return new OpenERPLabOrderTypeServiceEventWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case SALEABLE_FEED: return new OpenERPSaleableResourceWorker(feedUrl, odooURL, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); } throw new RuntimeException(String.format("No worker for %s", jobName)); } diff --git a/openerp-atomfeed-service/src/main/resources/erp-atomfeed.properties b/openerp-atomfeed-service/src/main/resources/erp-atomfeed.properties index 3e5969d5..9edcc32f 100644 --- a/openerp-atomfeed-service/src/main/resources/erp-atomfeed.properties +++ b/openerp-atomfeed-service/src/main/resources/erp-atomfeed.properties @@ -50,4 +50,12 @@ openmrs.auth.uri=http://localhost:8050/openmrs/ws/rest/v1/session scheduler.initial.delay=60000 -referencedata.endpoint=http://localhost:8050/reference-data \ No newline at end of file +referencedata.endpoint=http://localhost:8050/reference-data + +customer.endpoint=/api/bahmni-customer +drug.endpoint=/api/bahmni-drug +lab.endpoint=/api/bahmni-lab-test +radiology.endpoint=/api/bahmni-radiology-test +labpanel.endpoint=/api/bahmni-lab-panel +saleable.endpoint=/api/bahmni-service-sale +saleorder.endpoint=/api/bahmni-saleorder diff --git a/openerp-atomfeed-service/src/main/resources/jobs-context.xml b/openerp-atomfeed-service/src/main/resources/jobs-context.xml index f079463f..696ff6ba 100644 --- a/openerp-atomfeed-service/src/main/resources/jobs-context.xml +++ b/openerp-atomfeed-service/src/main/resources/jobs-context.xml @@ -250,6 +250,8 @@ + + diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java index 600f229b..4d2d0ab9 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java @@ -2,12 +2,12 @@ import com.sun.syndication.feed.atom.Content; import com.sun.syndication.feed.atom.Entry; -import com.sun.syndication.feed.atom.Feed; import com.sun.syndication.feed.atom.Link; +import com.sun.syndication.feed.atom.Feed; import com.sun.syndication.io.FeedException; import org.bahmni.feed.openerp.ObjectMapperRepository; 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.job.OpenERPCustomerFeedJob; import org.bahmni.feed.openerp.job.SimpleFeedJob; import org.bahmni.feed.openerp.worker.OpenERPCustomerServiceEventWorker; @@ -164,6 +164,7 @@ private List getEntries(FeedEnumerator feedEnumerator) { @Test public void shouldCreateCustomerInOpenERP() throws URISyntaxException, FeedException { String feedUrl = "http://host/patients/notifications"; + String odooURL = "http://client/patients/notifications"; String feedname = "customer.feed.generator.uri"; when(atomFeedProperties.getFeedUri(feedname)).thenReturn(feedUrl); @@ -189,14 +190,14 @@ public void shouldCreateCustomerInOpenERP() throws URISyntaxException, FeedExcep authenticationResponse.setSessionId("sessionIdValue"); when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); - when(webClientProvider.getWebClient(any(Jobs.class))).thenReturn(webClient); - when(workerFactory.getWorker(Jobs.CUSTOMER_FEED, feedUrl, openERPContext, "http://mrs.auth.uri")).thenReturn(openERPCustomerServiceEventWorker); + when(webClientProvider.getWebClient(any(FeedURI.class))).thenReturn(webClient); + when(workerFactory.getWorker(FeedURI.CUSTOMER_FEED, feedUrl,odooURL, openERPContext, "http://mrs.auth.uri")).thenReturn(openERPCustomerServiceEventWorker); AtomFeedClientHelper clientHelper = mock(AtomFeedClientHelper.class); AtomFeedClient atomFeedClient = new AtomFeedClient(allFeedsMock, allMarkersJdbc, allFailedEvents, FeedClientFactory.atomFeedProperties(atomFeedProperties), transactionSupport, new URI(feedUrl), openERPCustomerServiceEventWorker); - when(clientHelper.getAtomFeedClient(Jobs.CUSTOMER_FEED)). + when(clientHelper.getAtomFeedClient(FeedURI.CUSTOMER_FEED)). thenReturn(atomFeedClient); SimpleFeedJob openMRSFeedJob = new SimpleFeedJob(clientHelper); diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java index fa3fc111..bafedc24 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java @@ -28,6 +28,8 @@ public class OpenERPCustomerServiceEventWorkerTest { private OpenMRSWebClient mockWebClient; private String MRSURLPrefix; + private String odooURL = "https://client/openmrs/ws/atomfeed/customer/recent"; + @Before public void setUp() throws Exception { openERPContext = mock(OpenERPContext.class); @@ -38,7 +40,7 @@ public void setUp() throws Exception { public void shouldCallOpenERPClientWithRightParameters() throws FileNotFoundException { MRSURLPrefix = "urlPrefixTest"; OpenERPCustomerServiceEventWorker customerServiceEventWorker = - new OpenERPCustomerServiceEventWorker("www.openmrs.com", openERPContext, mockWebClient, MRSURLPrefix); + new OpenERPCustomerServiceEventWorker("www.openmrs.com", odooURL, openERPContext, mockWebClient, MRSURLPrefix); InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("patientResource.xml"); String patientResource = new Scanner(resourceAsStream).useDelimiter("\\Z").next(); @@ -48,7 +50,7 @@ public void shouldCallOpenERPClientWithRightParameters() throws FileNotFoundExce customerServiceEventWorker.process(event); - verify(openERPContext).execute(createOpenERPRequest(event)); + verify(openERPContext).execute(createOpenERPRequest(event), odooURL); } diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJobTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJobTest.java index 2cb080e1..b737cf31 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJobTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPDrugFeedJobTest.java @@ -6,7 +6,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.bahmni.feed.openerp.job.Jobs.DRUG_FEED; +import static org.bahmni.feed.openerp.job.FeedURI.DRUG_FEED; import static org.mockito.Mockito.verify; public class OpenERPDrugFeedJobTest { diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJobTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJobTest.java index 58edfe02..2a6265ee 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJobTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPLabFeedJobTest.java @@ -6,7 +6,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.bahmni.feed.openerp.job.Jobs.LAB_FEED; +import static org.bahmni.feed.openerp.job.FeedURI.LAB_FEED; import static org.mockito.Mockito.verify; public class OpenERPLabFeedJobTest { diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJobTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJobTest.java index 453886a1..f45dcd21 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJobTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/job/OpenERPSaleableFeedJobTest.java @@ -6,7 +6,7 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import static org.bahmni.feed.openerp.job.Jobs.SALEABLE_FEED; +import static org.bahmni.feed.openerp.job.FeedURI.SALEABLE_FEED; import static org.mockito.Mockito.verify; public class OpenERPSaleableFeedJobTest { diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/server/AtomFeedServerIT.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/server/AtomFeedServerIT.java index 9acaf248..6e6f5450 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/server/AtomFeedServerIT.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/server/AtomFeedServerIT.java @@ -4,7 +4,6 @@ import org.bahmni.feed.openerp.utils.MVCTestUtils; import org.ict4h.atomfeed.Configuration; import org.ict4h.atomfeed.IntegrationTest; -import org.ict4h.atomfeed.client.service.AtomFeedClient; import org.ict4h.atomfeed.jdbc.JdbcConnectionProvider; import org.ict4h.atomfeed.jdbc.JdbcUtils; import org.ict4h.atomfeed.server.domain.EventRecord; diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java index cd13c245..0638330a 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java @@ -19,6 +19,7 @@ import java.util.List; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -32,6 +33,7 @@ public class OpenERPDrugServiceEventWorkerTest { private OpenMRSWebClient webClient; private String feedUri = "http://feeduri"; + private String odooURL = "http://client/prefix/"; private String drugOrderJson; private OpenERPDrugServiceEventWorker worker; @@ -49,7 +51,7 @@ public void setUp(){ " \"name\": \"Paracetamol 250mg\"\n" + "}"; MockitoAnnotations.initMocks(this); - worker = new OpenERPDrugServiceEventWorker(feedUri,openERPContext,webClient,"http://prefix/"); + worker = new OpenERPDrugServiceEventWorker(feedUri,odooURL,openERPContext,webClient,"http://prefix/"); } @@ -60,7 +62,7 @@ public void testProcess() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(),eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); @@ -87,7 +89,7 @@ public void testFailedEvent() throws IOException{ worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(),eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java index de8bb7cd..0c0c4c6f 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java @@ -21,6 +21,7 @@ import static org.bahmni.feed.openerp.domain.labOrderType.OpenMRSLabPanelEvent.LAB_PANEL_EVENT_NAME; import static org.bahmni.feed.openerp.domain.labOrderType.OpenMRSLabTestEvent.LAB_TEST_EVENT_NAME; import static org.bahmni.feed.openerp.domain.labOrderType.OpenMRSRadiologyTestEvent.RADIOLOGY_TEST_EVENT_NAME; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -34,6 +35,8 @@ public class OpenERPLabOrderTypeServiceEventWorkerTest { private OpenMRSWebClient webClient; private String feedUri = "http://feeduri"; + + private String odooURL = "http://client/prefix/"; private OpenERPLabOrderTypeServiceEventWorker worker; @Before @@ -84,7 +87,7 @@ public void setUp() { "}"; MockitoAnnotations.initMocks(this); - worker = new OpenERPLabOrderTypeServiceEventWorker(feedUri, openERPContext, webClient, "http://prefix/"); + worker = new OpenERPLabOrderTypeServiceEventWorker(feedUri, odooURL, openERPContext, webClient, "http://prefix/"); when(webClient.get(URI.create("http://prefix/radiology"))).thenReturn(radiologyTestJson); when(webClient.get(URI.create("http://prefix/test"))).thenReturn(labTestJson); when(webClient.get(URI.create("http://prefix/panel"))).thenReturn(labPanelJson); @@ -97,7 +100,7 @@ public void shouldProcessRadiologyTestEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(),eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -117,7 +120,7 @@ public void shouldProcessLabTestEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -136,7 +139,7 @@ public void shouldProcessLabPanelEvent() throws Exception { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -155,7 +158,7 @@ public void testRadiologyFailedEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); @@ -169,7 +172,7 @@ public void testTestFailedEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java index 5f66cbcb..d4b1286c 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java @@ -87,7 +87,8 @@ public void create_SaleOrder_In_ERP_For_Lab_Order(){ // when(openERPContext.read(any(String.class),any(Vector.class), any(Vector.class))).thenReturn(new Object[]{fieldMap}); String feedUrl = "http://xxxx/encounter/feed/2"; - OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl,openERPContext,webClient,"http://mrs.auth.uri"); + String odooURL = "http://xxxx/encounter/feed/2"; + OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl,odooURL,openERPContext, webClient,"http://mrs.auth.uri"); Event event = new Event("Test",feedUrl); eventWorker.process(event); } @@ -118,7 +119,8 @@ public void create_SaleOrder_For_Bed_In_ERP_When_Bed_Is_Assigned() { String feedUrl = "http://xxxx/encounter/feed/2"; Event event = new Event("Test", feedUrl); - OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl, openERPContext, webClient,"http://mrs.auth.uri"); + String odooURL = "http://xxxx/encounter/feed/2"; + OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl,odooURL,openERPContext, webClient,"http://mrs.auth.uri"); eventWorker.process(event); } diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java index 2ffc7ebc..376b4357 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java @@ -18,6 +18,7 @@ import java.util.List; import static org.bahmni.feed.openerp.worker.OpenERPSaleableResourceWorker.ERP_EVENT_CATEGORY; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -36,6 +37,8 @@ public class OpenERPSaleableResourceWorkerTest { private String eventUrl = "https://mybahmni/" + eventContent; private String feedUri = "https://mybahmni/openmrs/ws/atomfeed/saleable/recent"; + private String odooURL = "https://client/openmrs/ws/atomfeed/saleable/recent"; + String sampleProcedureJsonWithSaleableAsFalse = "{\n" + @@ -59,7 +62,7 @@ public class OpenERPSaleableResourceWorkerTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - worker = new OpenERPSaleableResourceWorker(feedUri, openERPContext, webClient, "https://mybahmni/"); + worker = new OpenERPSaleableResourceWorker(feedUri,odooURL, openERPContext, webClient, "https://mybahmni/"); when(webClient.get(URI.create(eventUrl))).thenReturn(sampleProcedureJson); } @@ -68,7 +71,7 @@ public void shouldProcessSaleableTypeEvent() throws Exception { Event event = new Event("1", eventContent, "Dressing", feedUri, new Date()); worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -90,7 +93,7 @@ public void shouldUpdateAsInactiveWhenNotSaleableEvenWhenMRSResourceIsActive() t Event event = new Event("1", eventContent, "Dressing", feedUri, new Date()); worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPContext).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture(), eq(odooURL)); List parameters = erpRequestCatcher.getValue().getParameters(); Assert.assertTrue(parameters.contains(new Parameter("name", "Dressing of Wound"))); diff --git a/openerp-client/pom.xml b/openerp-client/pom.xml index 5903255f..a516e2c6 100644 --- a/openerp-client/pom.xml +++ b/openerp-client/pom.xml @@ -59,6 +59,32 @@ + + org.springframework + spring-webflux + ${spring.version} + + + commons-logging + commons-logging + + + + + io.projectreactor.netty + reactor-netty-http + 1.1.13 + + + org.springframework.boot + spring-boot-starter-webflux + 3.0.0 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.15.2 + org.springframework spring-web @@ -73,6 +99,11 @@ org.apache.velocity velocity-engine-core + + org.apache.velocity.tools + velocity-tools-generic + 3.1 + org.apache.httpcomponents httpclient 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 index f0bd17d8..ee690b92 100644 --- 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 @@ -5,6 +5,6 @@ import java.util.Vector; public interface OpenERPClientStrategy { - Object execute(OpenERPRequest openERPRequest); + Object execute(OpenERPRequest openERPRequest, String URI); } \ 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 index 8eaedbc7..aecc8ad4 100644 --- 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 @@ -17,8 +17,8 @@ public OpenERPContext(OpenERPClientStrategy openERPClient) { this.openERPClient = openERPClient; } - public Object execute(OpenERPRequest openERPRequest) { - return openERPClient.execute(openERPRequest); + public Object execute(OpenERPRequest openERPRequest, String URI) { + return openERPClient.execute(openERPRequest, URI); } } \ No newline at end of file diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java new file mode 100644 index 00000000..07af23eb --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/client/strategy/implementation/OdooRESTClient.java @@ -0,0 +1,34 @@ +package org.bahmni.openerp.web.client.strategy.implementation; + +import org.bahmni.openerp.web.OpenERPProperties; +import org.bahmni.openerp.web.client.strategy.OpenERPClientStrategy; +import org.bahmni.openerp.web.http.client.RestClient; +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 RestClient restClient; + + @Autowired + public OdooRESTClient(OpenERPProperties openERPProperties) { + String host = openERPProperties.getHost(); + int port = openERPProperties.getPort(); + String user = openERPProperties.getUser(); + String password = openERPProperties.getPassword(); + int connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds(); + restClient = new RestClient("http://" + host + ":" + port, user, password, connectionTimeoutInMilliseconds); + } + + @Override + public Object execute(OpenERPRequest openERPRequest, String URI) { + String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString()); + return restClient.post(URI, requestBody); + } +} 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 index ad959ff1..81bf3659 100644 --- 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 @@ -5,7 +5,7 @@ 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.bahmni.openerp.web.http.client.XMLClient; import org.apache.xmlrpc.XmlRpcException; import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; @@ -36,11 +36,11 @@ public class OpenERPXMLClient implements OpenERPClientStrategy { 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(); @@ -67,7 +67,7 @@ private void login() { } @Override - public Object execute(OpenERPRequest openERPRequest){ + public Object execute(OpenERPRequest openERPRequest, String URI) { login(); String request = RequestBuilder.buildNewXMLRequest(openERPRequest, id, database, password); String response = httpClient().post("http://" + host + ":" + port + XML_RPC_OBJECT_ENDPOINT, request); @@ -84,9 +84,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) { @@ -108,7 +108,6 @@ private XmlRpcClient createRPCClient() { clientConfiguration.setEnabledForExceptions(true); clientConfiguration.setConnectionTimeout(connectionTimeoutInMilliseconds); clientConfiguration.setReplyTimeout(replyTimeoutInMilliseconds); - XmlRpcClient rpcClient = new XmlRpcClient(); rpcClient.setTransportFactory(new XmlRpcSun15HttpTransportFactory(rpcClient)); rpcClient.setConfig(clientConfiguration); diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java new file mode 100644 index 00000000..a4eafccf --- /dev/null +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/RestClient.java @@ -0,0 +1,98 @@ +package org.bahmni.openerp.web.http.client; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bahmni.openerp.web.OpenERPException; +import org.bahmni.openerp.web.request.OpenERPRequest; +import org.bahmni.openerp.web.request.builder.Parameter; +import org.bahmni.openerp.web.request.builder.RequestBuilder; +import org.springframework.http.HttpHeaders; +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 final int connectionTimeout; + private String accessToken; + + private final String baseURL; + + private final String username; + + private final 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), new Parameter("password", password))); + String requestBody = RequestBuilder.buildNewRestRequest(openERPRequest, UUID.randomUUID().toString()); + WebClient client = getWebClient(baseURL); + HttpHeaders headers = getHttpHeaders(); + Consumer consumer = httpHeaders -> httpHeaders.addAll(headers); + try{ + 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"); + } + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode responseNode = objectMapper.readTree(response); + accessToken = responseNode.get("result").get("data").get("access_token").asText(); + } + catch (Exception e){ + logger.debug("Failed to login for user {}", username); + throw new OpenERPException(String.format("Failed to login. The login user is : %s", username)); + } + } + } + + public String post(String URI, String requestBody) { + try { + login(); + logger.debug("Post Data: {}", requestBody); + WebClient client = getWebClient(baseURL); + HttpHeaders headers = getHttpHeaders(); + headers.set(HttpHeaders.AUTHORIZATION, accessToken); + Consumer consumer = httpHeaders -> httpHeaders.addAll(headers); + 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)); + } + return response; + } catch (Exception e) { + logger.error("Could not post to {}", URI, e); + logger.error("Post data: {}", requestBody); + throw new RuntimeException("Could not post message", e); + } + } + + private WebClient getWebClient(String baseURL) { + if(webClient == null){ + webClient = WebClient.builder() + .baseUrl(baseURL) + .build(); + } + return webClient; + } + + private HttpHeaders getHttpHeaders() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON); + httpHeaders.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); + return httpHeaders; + } + +} diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java similarity index 93% rename from openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java rename to openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java index 7165ba29..090835b8 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/HttpClient.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/http/client/XMLClient.java @@ -12,14 +12,14 @@ import org.springframework.web.client.RestTemplate; @Component -public class HttpClient { - private static final Logger logger = LogManager.getLogger(HttpClient.class); +public class XMLClient { + private static final Logger logger = LogManager.getLogger(XMLClient.class); private RestTemplate restTemplate; private boolean isTimeoutSet; @Autowired - public HttpClient(RestTemplate restTemplate) { + public XMLClient(RestTemplate restTemplate) { this.restTemplate = restTemplate; } diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java index f471ce5a..42193685 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/request/builder/Parameter.java @@ -1,8 +1,5 @@ package org.bahmni.openerp.web.request.builder; - -import org.apache.commons.lang3.StringEscapeUtils; - public class Parameter { private String name; @@ -11,7 +8,7 @@ public class Parameter { public Parameter(String name, String value, String type) { this.name = name; - this.value = StringEscapeUtils.escapeXml(value); + this.value = value; this.type = type; } 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 01fab54f..e6004c44 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 @@ -5,6 +5,7 @@ import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.runtime.RuntimeConstants; import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; +import org.apache.velocity.tools.generic.EscapeTool; import org.bahmni.openerp.web.OpenERPException; import org.bahmni.openerp.web.request.OpenERPRequest; import org.springframework.stereotype.Service; @@ -22,6 +23,7 @@ public static String buildNewXMLRequest(OpenERPRequest openERPRequest, Object id velocityEngine.init(); Template template = velocityEngine.getTemplate("request/template/xml_template.vm"); VelocityContext context = new VelocityContext(); + context.put("esc", new EscapeTool()); //Refer : https://github.com/Bahmni/openerp-atomfeed-service/pull/80#discussion_r1410242808 context.put("parametersList", openERPRequest.getParameters()); context.put("id", id); context.put("database", database); @@ -36,4 +38,22 @@ public static String buildNewXMLRequest(OpenERPRequest openERPRequest, Object id throw new OpenERPException(e); } } + + public static String buildNewRestRequest(OpenERPRequest openERPRequest, String id) { + 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/rest_template.vm"); + VelocityContext context = new VelocityContext(); + context.put("parametersList", openERPRequest.getParameters()); + context.put("id", id); + StringWriter writer = new StringWriter(); + template.merge(context, writer); + return writer.toString(); + } catch (Exception e) { + throw new OpenERPException(e); + } + } } \ No newline at end of file diff --git a/openerp-client/src/main/resources/request/template/rest_template.vm b/openerp-client/src/main/resources/request/template/rest_template.vm new file mode 100644 index 00000000..34a8528c --- /dev/null +++ b/openerp-client/src/main/resources/request/template/rest_template.vm @@ -0,0 +1,10 @@ +{ + "id": "$id", + "params": { + "data": { + #foreach( $param in $parametersList ) + "$param.name": "$param.value"#if($foreach.hasNext),#end + #end + } + } +} \ No newline at end of file diff --git a/openerp-client/src/main/resources/request/template/xml_template.vm b/openerp-client/src/main/resources/request/template/xml_template.vm index 755cce27..bc8353a6 100644 --- a/openerp-client/src/main/resources/request/template/xml_template.vm +++ b/openerp-client/src/main/resources/request/template/xml_template.vm @@ -1,31 +1,31 @@ - - - execute - - - $database - - - $id - - - $password - - - $resource - - - $operation - - - - #foreach( $param in $parametersList ) - - $param.name - <$param.type>$!param.value - - #end - - - - \ No newline at end of file + + + execute + + + $database + + + $id + + + $password + + + $resource + + + $operation + + + + #foreach( $param in $parametersList ) + + $param.name + <$param.type>$esc.xml($!param.value) + + #end + + + + \ 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 2092b4d0..92bf8914 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 @@ -31,7 +31,7 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password); String requestXmlForComparison = requestXml.replace("", " "); - String expected = "\n" + + String expected = "" + "" + " execute" + " " + @@ -66,7 +66,7 @@ public void shouldCreateNewCustomerRequestWithPatientDataPopulated() throws Exce " " + " " + " " + - " \n" + + " " + ""; comparingStringWithoutSpaces(requestXml, expected); @@ -88,7 +88,7 @@ public void shouldEscapeSpecialCharacters() throws Exception { String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password); - String expected = "\n" + + String expected = "" + "" + " execute" + " " + @@ -115,7 +115,7 @@ public void shouldEscapeSpecialCharacters() throws Exception { " " + " " + " " + - " \n" + + " " + ""; comparingStringWithoutSpaces(requestXml, expected); @@ -137,7 +137,7 @@ public void shouldEscapeHindiCharacters() throws Exception { String requestXml = RequestBuilder.buildNewXMLRequest(request, id, database, password); - String expected = "\n" + + String expected = "" + "" + " execute" + " " + @@ -164,9 +164,8 @@ public void shouldEscapeHindiCharacters() throws Exception { " " + " " + " " + - " \n" + + " " + ""; - comparingStringWithoutSpaces(requestXml, expected); } diff --git a/pom.xml b/pom.xml index 7b8badc5..be20c856 100644 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 4.9.0 2.13.5 4.4.5 - 4.5.13 + 4.5.13 3.0.1 2.17.1 0.94.3 @@ -37,6 +37,11 @@ + + org.springframework.boot + spring-boot-starter-webflux + 3.0.0 + commons-lang commons-lang @@ -222,7 +227,7 @@ org.apache.httpcomponents httpclient - ${httpClient.version} + ${XMLClient.version} commons-logging