Skip to content

Commit

Permalink
EA-205: Support loading dispositionConfig.json from the file system b…
Browse files Browse the repository at this point in the history
…y default
  • Loading branch information
mogoodrich committed Oct 18, 2024
1 parent 36e524e commit afb7ce8
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class AwaitingAdmissionVisitQueryEvaluatorTest extends BaseReportingTest
@Before
public void setup() throws Exception {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
dispositionDescriptor = ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
query = new AwaitingAdmissionVisitQuery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class MostRecentAdmissionRequestVisitDataEvaluatorTest extends BaseReport
@Before
public void setup() throws Exception {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
def = new MostRecentAdmissionRequestVisitDataDefinition();
dispositionDescriptor = ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
diagnosisMetadata = ContextSensitiveMetadataTestUtils.setupDiagnosisMetadata(conceptService, emrApiProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public class DispositionServiceImpl extends BaseOpenmrsService implements Dispos
private PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

// TODO inject this in some better way than using a setter to override?
private String dispositionConfig = "dispositionConfig.json";
private String dispositionConfig;

private final String DEFAULT_DISPOSITION_CONFIG_LOCATION = "file:" + OpenmrsUtil.getApplicationDataDirectory() + "/configuration/dispositions/dispositionConfig.json";

private final String LEGACY_DEFAULT_DISPOSITION_CONFIG_LOCATION = "classpath*:/dispositionConfig.json"; // prior to 2.1.0 release

public DispositionServiceImpl(ConceptService conceptService, EmrConceptService emrConceptService) {
this.conceptService = conceptService;
Expand Down Expand Up @@ -62,9 +66,47 @@ public DispositionDescriptor getDispositionDescriptor() {

@Override
public List<Disposition> getDispositions() {
return getDispositionsFrom(dispositionConfig);

if (dispositionConfig !=null) {
try {
String path;
if (!dispositionConfig.contains("file:")) {
path = "classpath*:/" + dispositionConfig;
} else {
path = "file:" + OpenmrsUtil.getApplicationDataDirectory() + "/" + dispositionConfig.replace("file:", "");
}
return getDispositionsFrom(path);
} catch (IOException ignored) {
// if this fails for some reason, we will try the default locations below
}
}

// if no config file specified, try the default locations
try {
return getDispositionsFrom(DEFAULT_DISPOSITION_CONFIG_LOCATION);
} catch (IOException ignored) {
// exception will be thrown below if needed
}

try {
return getDispositionsFrom(LEGACY_DEFAULT_DISPOSITION_CONFIG_LOCATION);
} catch (IOException ignored) {
// exception will be thrown below if needed
}

throw new RuntimeException("No disposition file found at: " + dispositionConfig + " or " + DEFAULT_DISPOSITION_CONFIG_LOCATION + " or " + LEGACY_DEFAULT_DISPOSITION_CONFIG_LOCATION);
}

private List<Disposition> getDispositionsFrom(String path) throws IOException {
Resource[] dispositionDefinitions = resourceResolver.getResources(path);
for (Resource dispositionDefinition : dispositionDefinitions) {
return objectMapper.readValue(dispositionDefinition.getInputStream(), new TypeReference<List<Disposition>>() {
});
}
throw new IOException("No disposition file found at " + path);
}


@Override
@Transactional(readOnly = true)
public Disposition getDispositionByUniqueId(String uniqueId) {
Expand Down Expand Up @@ -170,30 +212,6 @@ public void setDispositionConfig(String dispositionConfig) {
this.dispositionConfig = dispositionConfig;
}

private List<Disposition> getDispositionsFrom(String configFile) {

try {

String path;

if (configFile.indexOf("file:") == -1) {
path = "classpath*:/" + configFile;
}
else {
path = "file:" + OpenmrsUtil.getApplicationDataDirectory() + "/" + configFile.replace("file:","");
}

Resource[] dispositionDefinitions = resourceResolver.getResources(path);
for (Resource dispositionDefinition : dispositionDefinitions) {
return objectMapper.readValue(dispositionDefinition.getInputStream(), new TypeReference<List<Disposition>>() {});
}
return null;
}
catch (IOException e) {
throw new RuntimeException ("Unable to read disposition file " + configFile, e);
}

}

// TODO handle this better--this property is only used to allow use to inject a mock disposition descriptor
private DispositionDescriptor dispositionDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.openmrs.module.emrapi.disposition.DispositionService;
import org.openmrs.module.emrapi.test.ContextSensitiveMetadataTestUtils;
import org.openmrs.module.emrapi.visit.VisitDomainWrapper;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
Expand Down Expand Up @@ -108,6 +107,7 @@ public boolean evaluate(Object o) {
@Before
public void setUp() throws Exception {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public class AdtServiceImplTest extends EmrApiContextSensitiveTest {
@Before
public void setup() throws Exception {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
dispositionDescriptor = ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
visitLocation = testDataManager.location().name("Hospital").tag("Visit Location").save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void setUp(){

dispositionService = new DispositionServiceImpl(concertService, emrConceptService);
dispositionService.setDispositionDescriptor(dispositionDescriptor);
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class MaternalServiceImplTest extends EmrApiContextSensitiveTest {
@Before
public void setUp() {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class VisitDomainWrapperComponentTest extends EmrApiContextSensitiveTest
@Before
public void setup() throws Exception {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
dispositionDescriptor = ContextSensitiveMetadataTestUtils.setupDispositionDescriptor(conceptService, dispositionService);
ContextSensitiveMetadataTestUtils.setupAdmissionDecisionConcept(conceptService, emrApiProperties);
ContextSensitiveMetadataTestUtils.setupDiagnosisMetadata(conceptService, emrApiProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class EmrApiConfigurationControllerTest extends BaseModuleWebContextSensi
@Before
public void setUp() {
executeDataSet("baseTestDataset.xml");
dispositionService.setDispositionConfig("testDispositionConfig.json"); // use demo disposition config from test resources
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
}
Expand Down

0 comments on commit afb7ce8

Please sign in to comment.