From c42ebcdd212b2c88ddf18523bedbb029753475dc Mon Sep 17 00:00:00 2001 From: Ruhanga <41738040+Ruhanga@users.noreply.github.com> Date: Mon, 14 Oct 2024 12:02:33 +0300 Subject: [PATCH] HSC-319: Able to run without EMR API module and add a erp.properties file location (#12) --- api/pom.xml | 11 +++--- .../org/openmrs/module/erp/ErpContext.java | 37 ------------------- .../erp/api/utils/ErpPropertiesFile.java | 7 +++- .../module/erp/api/utils/TestHelper.java | 16 ++++++-- .../web/controller/ErpInvoiceController.java | 13 ++----- .../web/controller/ErpOrderController.java | 12 +++--- .../web/controller/ErpPartnerController.java | 12 +++--- omod/src/main/resources/config.xml | 3 -- .../controller/ErpInvoiceControllerTest.java | 7 +--- .../controller/ErpOrderControllerTest.java | 6 +-- .../controller/ErpPartnerControllerTest.java | 6 +-- 11 files changed, 42 insertions(+), 88 deletions(-) delete mode 100644 api/src/main/java/org/openmrs/module/erp/ErpContext.java diff --git a/api/pom.xml b/api/pom.xml index 14a992d..744c374 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -31,11 +31,6 @@ openmrs-api ${openmrsPlatformVersion} - - org.openmrs.module - emrapi-api - ${emrapiVersion} - commons-logging commons-logging @@ -73,6 +68,12 @@ ${providermanagementVersion} test + + org.openmrs.module + emrapi-api + ${emrapiVersion} + test + org.openmrs.module emrapi-api-1.12 diff --git a/api/src/main/java/org/openmrs/module/erp/ErpContext.java b/api/src/main/java/org/openmrs/module/erp/ErpContext.java deleted file mode 100644 index af6ed78..0000000 --- a/api/src/main/java/org/openmrs/module/erp/ErpContext.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.openmrs.module.erp; - -import org.openmrs.module.emrapi.utils.ModuleProperties; -import org.openmrs.module.erp.api.ErpInvoiceService; -import org.openmrs.module.erp.api.ErpOrderService; -import org.openmrs.module.erp.api.ErpPartnerService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.stereotype.Component; - -@Component(ErpConstants.COMPONENT_ERP_CONTEXT) -public class ErpContext extends ModuleProperties { - - @Autowired - @Qualifier(ErpConstants.COMPONENT_ODOO_ORDER_SERVICE) - protected ErpOrderService erpOrderService; - - @Autowired - @Qualifier(ErpConstants.COMPONENT_ODOO_INVOICE_SERVICE) - protected ErpInvoiceService erpInvoiceService; - - @Autowired - @Qualifier(ErpConstants.COMPONENT_ODOO_PARTNER_SERVICE) - protected ErpPartnerService erpPartnerService; - - public ErpOrderService getErpOrderService() { - return erpOrderService; - } - - public ErpInvoiceService getErpInvoiceService() { - return erpInvoiceService; - } - - public ErpPartnerService getErpPartnerService() { - return erpPartnerService; - } -} diff --git a/api/src/main/java/org/openmrs/module/erp/api/utils/ErpPropertiesFile.java b/api/src/main/java/org/openmrs/module/erp/api/utils/ErpPropertiesFile.java index ee167ee..e76038d 100644 --- a/api/src/main/java/org/openmrs/module/erp/api/utils/ErpPropertiesFile.java +++ b/api/src/main/java/org/openmrs/module/erp/api/utils/ErpPropertiesFile.java @@ -21,7 +21,12 @@ private ErpPropertiesFile() { public static File getFile() { String fileName = ErpConstants.ERP_PROPERTY_FILE_NAME; - return new File(OpenmrsUtil.getApplicationDataDirectory(), fileName); + File file = new File(OpenmrsUtil.getApplicationDataDirectory(), fileName); + if (file.exists()) { + return file; + } + LogFactory.getLog(ErpPropertiesFile.class).warn("Could not find erp.properties file in directory: " + file.getParent() + ". Failing over to location /etc/properties/."); + return new File("/etc/properties/", fileName); } public static InputStream getInputStream() throws FileNotFoundException { diff --git a/api/src/test/java/org/openmrs/module/erp/api/utils/TestHelper.java b/api/src/test/java/org/openmrs/module/erp/api/utils/TestHelper.java index a4de8ac..c5ed130 100644 --- a/api/src/test/java/org/openmrs/module/erp/api/utils/TestHelper.java +++ b/api/src/test/java/org/openmrs/module/erp/api/utils/TestHelper.java @@ -3,7 +3,9 @@ //import com.odoojava.api.*; import org.openmrs.module.erp.ErpActivator; import org.openmrs.module.erp.ErpConstants; -import org.openmrs.module.erp.ErpContext; +import org.openmrs.module.erp.api.ErpInvoiceService; +import org.openmrs.module.erp.api.ErpOrderService; +import org.openmrs.module.erp.api.ErpPartnerService; import org.openmrs.util.OpenmrsUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -29,8 +31,16 @@ public class TestHelper { private ErpActivator erpActivator; @Autowired - @Qualifier(ErpConstants.COMPONENT_ERP_CONTEXT) - protected ErpContext context; + @Qualifier(ErpConstants.COMPONENT_ODOO_ORDER_SERVICE) + protected ErpOrderService erpOrderService; + + @Autowired + @Qualifier(ErpConstants.COMPONENT_ODOO_INVOICE_SERVICE) + protected ErpInvoiceService erpInvoiceService; + + @Autowired + @Qualifier(ErpConstants.COMPONENT_ODOO_PARTNER_SERVICE) + protected ErpPartnerService erpPartnerService; public static void createErpPropertiesFile() { File erpPropsFile = new File(OpenmrsUtil.getApplicationDataDirectory(), fileName); diff --git a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpInvoiceController.java b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpInvoiceController.java index a9b7929..59049e9 100644 --- a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpInvoiceController.java +++ b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpInvoiceController.java @@ -3,12 +3,12 @@ import org.json.JSONArray; import org.json.JSONObject; import org.openmrs.module.erp.ErpConstants; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.Filter; import org.openmrs.module.erp.api.ErpInvoiceService; import org.openmrs.module.erp.web.RecordRepresentation; import org.openmrs.module.webservices.rest.web.RestConstants; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -21,16 +21,13 @@ public class ErpInvoiceController { @Autowired - protected ErpContext erpContext; - - @Autowired - private ErpInvoiceService erpInvoiceService; - + @Qualifier(ErpConstants.COMPONENT_ODOO_INVOICE_SERVICE) + protected ErpInvoiceService erpInvoiceService; + @RequestMapping(method = RequestMethod.POST) @ResponseBody public Object getInvoicesByFilters(@RequestBody String jsonString, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpInvoiceService = erpContext.getErpInvoiceService(); RecordRepresentation recordRepresentation = new RecordRepresentation(erpInvoiceService.defaultModelAttributes()); @@ -65,8 +62,6 @@ public Object getInvoicesByFilters(@RequestBody String jsonString, public Object getInvoiceById(@PathVariable("id") String invoiceId, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpInvoiceService = erpContext.getErpInvoiceService(); - return new RecordRepresentation(erpInvoiceService.defaultModelAttributes()) .getRepresentedRecord(erpInvoiceService.getInvoiceById(invoiceId), rep); } diff --git a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpOrderController.java b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpOrderController.java index 2b4359d..eb3ff80 100644 --- a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpOrderController.java +++ b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpOrderController.java @@ -3,13 +3,13 @@ import org.json.JSONArray; import org.json.JSONObject; import org.openmrs.module.erp.ErpConstants; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.Filter; import org.openmrs.module.erp.api.ErpOrderService; import org.openmrs.module.erp.web.RecordRepresentation; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -22,16 +22,14 @@ public class ErpOrderController extends BaseRestController { @Autowired - protected ErpContext erpContext; - - @Autowired - private ErpOrderService erpOrderService; + @Qualifier(ErpConstants.COMPONENT_ODOO_ORDER_SERVICE) + protected ErpOrderService erpOrderService; @RequestMapping(method = RequestMethod.POST) @ResponseBody public Object getErpOrdersByFilters(@RequestBody String jsonString, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpOrderService = erpContext.getErpOrderService(); + RecordRepresentation recordRepresentation = new RecordRepresentation(erpOrderService.defaultModelAttributes()); ArrayList filtersArray = new ArrayList<>(); @@ -64,7 +62,7 @@ public Object getErpOrdersByFilters(@RequestBody String jsonString, @ResponseBody public Object getErpOrderById(@PathVariable("id") String id, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpOrderService = erpContext.getErpOrderService(); + return new RecordRepresentation(erpOrderService.defaultModelAttributes()) .getRepresentedRecord(erpOrderService.getErpOrderById(id), rep); } diff --git a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpPartnerController.java b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpPartnerController.java index 16d7120..82e4240 100644 --- a/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpPartnerController.java +++ b/omod/src/main/java/org/openmrs/module/erp/web/controller/ErpPartnerController.java @@ -3,13 +3,13 @@ import org.json.JSONArray; import org.json.JSONObject; import org.openmrs.module.erp.ErpConstants; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.Filter; import org.openmrs.module.erp.api.ErpPartnerService; import org.openmrs.module.erp.web.RecordRepresentation; import org.openmrs.module.webservices.rest.web.RestConstants; import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; @@ -22,16 +22,14 @@ public class ErpPartnerController extends BaseRestController { @Autowired - protected ErpContext erpContext; - - @Autowired - private ErpPartnerService erpPartnerService; + @Qualifier(ErpConstants.COMPONENT_ODOO_PARTNER_SERVICE) + protected ErpPartnerService erpPartnerService; @RequestMapping(method = RequestMethod.POST) @ResponseBody public Object getErpPartnersByFilters(@RequestBody String jsonString, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpPartnerService = erpContext.getErpPartnerService(); + RecordRepresentation recordRepresentation = new RecordRepresentation(erpPartnerService.defaultModelAttributes()); ArrayList filtersArray = new ArrayList<>(); @@ -64,7 +62,7 @@ public Object getErpPartnersByFilters(@RequestBody String jsonString, @ResponseBody public Object getErpPartnerById(@PathVariable("id") String id, @RequestParam(value = "rep", defaultValue = "default") String rep) { - erpPartnerService = erpContext.getErpPartnerService(); + return new RecordRepresentation(erpPartnerService.defaultModelAttributes()) .getRepresentedRecord(erpPartnerService.getErpPartnerById(id), rep); } diff --git a/omod/src/main/resources/config.xml b/omod/src/main/resources/config.xml index 8dd97e5..34d2bd8 100644 --- a/omod/src/main/resources/config.xml +++ b/omod/src/main/resources/config.xml @@ -23,9 +23,6 @@ org.openmrs.module.webservices.rest - - org.openmrs.module.emrapi - diff --git a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpInvoiceControllerTest.java b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpInvoiceControllerTest.java index bd461a9..1f74da3 100644 --- a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpInvoiceControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpInvoiceControllerTest.java @@ -7,12 +7,10 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.api.ErpInvoiceService; import org.openmrs.module.erp.api.utils.TestHelper; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; import org.springframework.beans.factory.annotation.Autowired; - import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -20,12 +18,11 @@ import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.mock; public class ErpInvoiceControllerTest extends BaseModuleWebContextSensitiveTest { @Mock - protected ErpContext erpContext; + protected ErpInvoiceService erpInvoiceService; @InjectMocks @Autowired @@ -40,9 +37,7 @@ public ErpInvoiceControllerTest() { public void setup() { MockitoAnnotations.initMocks(this); - ErpInvoiceService erpInvoiceService = mock(ErpInvoiceService.class); - Mockito.doReturn(erpInvoiceService).when(erpContext).getErpInvoiceService(); Mockito.doReturn(getInvoice()).when(erpInvoiceService).getInvoiceById(anyString()); Mockito.doReturn(Collections.singletonList(getInvoice())).when(erpInvoiceService).getInvoicesByFilters(any()); diff --git a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpOrderControllerTest.java b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpOrderControllerTest.java index ba51264..8591448 100644 --- a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpOrderControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpOrderControllerTest.java @@ -8,7 +8,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.api.ErpOrderService; import org.openmrs.module.erp.api.utils.TestHelper; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; @@ -17,12 +16,11 @@ import java.util.*; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; public class ErpOrderControllerTest extends BaseModuleWebContextSensitiveTest { @Mock - protected ErpContext erpContext; + protected ErpOrderService erpOrderService; @InjectMocks @Autowired @@ -37,9 +35,7 @@ public ErpOrderControllerTest() { public void setup() { MockitoAnnotations.initMocks(this); - ErpOrderService erpOrderService = mock(ErpOrderService.class); - Mockito.doReturn(erpOrderService).when(erpContext).getErpOrderService(); Mockito.doReturn(Collections.singletonList(getOrder())).when(erpOrderService).getErpOrdersByFilters(any()); Mockito.doReturn(getOrder()).when(erpOrderService).getErpOrderById(any()); diff --git a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpPartnerControllerTest.java b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpPartnerControllerTest.java index 22764e6..ad47f23 100644 --- a/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpPartnerControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/erp/web/controller/ErpPartnerControllerTest.java @@ -8,7 +8,6 @@ import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import org.openmrs.module.erp.ErpContext; import org.openmrs.module.erp.api.ErpPartnerService; import org.openmrs.module.erp.api.utils.TestHelper; import org.openmrs.web.test.BaseModuleWebContextSensitiveTest; @@ -20,12 +19,11 @@ import java.util.Map; import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; public class ErpPartnerControllerTest extends BaseModuleWebContextSensitiveTest { @Mock - protected ErpContext erpContext; + protected ErpPartnerService erpPartnerService; @InjectMocks @Autowired @@ -40,9 +38,7 @@ public ErpPartnerControllerTest() { public void setup() { MockitoAnnotations.initMocks(this); - ErpPartnerService erpPartnerService = mock(ErpPartnerService.class); - Mockito.doReturn(erpPartnerService).when(erpContext).getErpPartnerService(); Mockito.doReturn(Collections.singletonList(getPartner())).when(erpPartnerService).getErpPartnersByFilters(any()); Mockito.doReturn(getPartner()).when(erpPartnerService).getErpPartnerById(any());