Skip to content

Commit

Permalink
FM2-642: Improve performance of loading locations by tag (#547) (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher authored Oct 8, 2024
1 parent 9707c4d commit 983b724
Show file tree
Hide file tree
Showing 64 changed files with 481 additions and 614 deletions.
22 changes: 18 additions & 4 deletions api/src/main/java/org/openmrs/module/fhir2/FhirActivator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import ca.uhn.fhir.rest.server.IResourceProvider;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.openmrs.api.context.Context;
import org.openmrs.module.BaseModuleActivator;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleException;
Expand All @@ -36,6 +37,7 @@
import org.openmrs.module.fhir2.api.spi.ModuleLifecycleListener;
import org.openmrs.module.fhir2.api.spi.ServiceClassLoader;
import org.openmrs.module.fhir2.api.translators.FhirTranslator;
import org.openmrs.module.fhir2.api.util.FhirGlobalPropertyHolder;
import org.openmrs.module.fhir2.model.GroupMember;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand All @@ -53,6 +55,8 @@ public class FhirActivator extends BaseModuleActivator implements ApplicationCon
@Getter
private static ConfigurableApplicationContext applicationContext;

private static FhirGlobalPropertyHolder globalPropertyHolder = null;

private final Map<String, Set<Class<?>>> services = new HashMap<>();

private final List<ModuleLifecycleListener> lifecycleListeners = new ArrayList<>();
Expand All @@ -65,8 +69,6 @@ public void started() {
throw new ModuleException("Cannot load FHIR2 module as the main application context is not available");
}

applicationContext.getBean("fhirR4", FhirContext.class).registerCustomType(GroupMember.class);

loadModules();
started = true;
log.info("Started FHIR");
Expand All @@ -86,6 +88,13 @@ public void contextRefreshed() {
return;
}

if (globalPropertyHolder == null) {
globalPropertyHolder = new FhirGlobalPropertyHolder();
Context.getAdministrationService().addGlobalPropertyListener(globalPropertyHolder);
}

FhirGlobalPropertyHolder.reset();

applicationContext.getBean("fhirR4", FhirContext.class).registerCustomType(GroupMember.class);
loadModules();

Expand All @@ -95,13 +104,18 @@ public void contextRefreshed() {
@Override
public void willStop() {
lifecycleListeners.forEach(ModuleLifecycleListener::willStop);
unloadModules();

if (globalPropertyHolder != null) {
Context.getAdministrationService().removeGlobalPropertyListener(globalPropertyHolder);
}
}

@Override
public void stopped() {
lifecycleListeners.forEach(ModuleLifecycleListener::stopped);
unloadModules();

globalPropertyHolder = null;
started = false;
log.info("Shutdown FHIR");
}
Expand All @@ -117,7 +131,7 @@ public void removeModuleLifecycleLister(@Nonnull ModuleLifecycleListener lifecyc
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
if (applicationContext instanceof ConfigurableApplicationContext) {
FhirActivator.applicationContext = (ConfigurableApplicationContext) applicationContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface FhirGlobalPropertyService {

String getGlobalProperty(String property) throws APIException;

Integer getGlobalProperty(String property, Integer defaultValue);
int getGlobalPropertyAsInteger(String property, int defaultValue);

String getGlobalProperty(String property, String defaultValue);

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,30 @@
import lombok.Setter;
import org.openmrs.api.APIException;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.dao.FhirGlobalPropertyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.openmrs.module.fhir2.api.util.FhirGlobalPropertyHolder;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
@Setter(AccessLevel.PACKAGE)
public class FhirGlobalPropertyServiceImpl implements FhirGlobalPropertyService {

@Autowired
private FhirGlobalPropertyDao dao;

@Override
@Transactional(readOnly = true)
public String getGlobalProperty(String property) throws APIException {
return dao.getGlobalProperty(property);
return FhirGlobalPropertyHolder.getGlobalProperty(property);
}

@Override
@Transactional(readOnly = true)
public Integer getGlobalProperty(String property, Integer defaultValue) {
try {
return Integer.valueOf(getGlobalProperty(property, String.valueOf(defaultValue)));
}
catch (NumberFormatException e) {
return defaultValue;
}
public int getGlobalPropertyAsInteger(String property, int defaultValue) {
return FhirGlobalPropertyHolder.getGlobalPropertyAsInteger(property, defaultValue);
}

@Override
@Transactional(readOnly = true)
public String getGlobalProperty(String property, String defaultValue) {
return this.getGlobalProperty(property) == null ? defaultValue : this.getGlobalProperty(property);
return FhirGlobalPropertyHolder.getGlobalProperty(property, defaultValue);
}

@Override
@Transactional(readOnly = true)
public Map<String, String> getGlobalProperties(String... properties) {
return dao.getGlobalProperties(properties);
return FhirGlobalPropertyHolder.getGlobalProperties(properties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public List<IBaseResource> getResources(int fromIndex, int toIndex) {
@Override
public Integer preferredPageSize() {
if (pageSize == null) {
pageSize = globalPropertyService.getGlobalProperty(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
pageSize = globalPropertyService.getGlobalPropertyAsInteger(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
}

return pageSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public List<IBaseResource> getResources(int fromIndex, int toIndex) {
@Override
public Integer preferredPageSize() {
if (pageSize == null) {
pageSize = globalPropertyService.getGlobalProperty(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
pageSize = globalPropertyService.getGlobalPropertyAsInteger(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
}

return pageSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

@Component
@Setter(AccessLevel.PACKAGE)
public class AllergyIntoleranceTranslatorImpl extends BaseReferenceHandlingTranslator implements AllergyIntoleranceTranslator {
public class AllergyIntoleranceTranslatorImpl implements AllergyIntoleranceTranslator {

@Autowired
private PractitionerReferenceTranslator<User> practitionerReferenceTranslator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@ public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnos
diagnosticReport.setId(fhirDiagnosticReport.getUuid());

if (fhirDiagnosticReport.getStatus() != null) {
diagnosticReport
.setStatus(DiagnosticReport.DiagnosticReportStatus.valueOf(fhirDiagnosticReport.getStatus().toString()));
try {
diagnosticReport.setStatus(
DiagnosticReport.DiagnosticReportStatus.valueOf(fhirDiagnosticReport.getStatus().toString()));
}
catch (IllegalArgumentException e) {
diagnosticReport.setStatus(DiagnosticReport.DiagnosticReportStatus.UNKNOWN);
}
} else {
diagnosticReport.setStatus(DiagnosticReport.DiagnosticReportStatus.UNKNOWN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package org.openmrs.module.fhir2.api.translators.impl;

import static org.apache.commons.lang3.Validate.notNull;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId;

import javax.annotation.Nonnull;

Expand All @@ -24,7 +26,7 @@

@Component
@Setter(AccessLevel.PACKAGE)
public class EncounterLocationTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterLocationTranslator {
public class EncounterLocationTranslatorImpl implements EncounterLocationTranslator {

@Autowired
private FhirLocationDao locationDao;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package org.openmrs.module.fhir2.api.translators.impl;

import static org.apache.commons.lang3.Validate.notNull;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createPractitionerReference;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId;

import javax.annotation.Nonnull;

Expand All @@ -27,7 +29,7 @@

@Component
@Setter(AccessLevel.PACKAGE)
public class EncounterParticipantTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterParticipantTranslator {
public class EncounterParticipantTranslatorImpl implements EncounterParticipantTranslator {

private static final String DEFAULT_ENCOUNTER_ROLE_UUID_PROPERTY = "fhir2.encounterParticipantComponentUuid";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
package org.openmrs.module.fhir2.api.translators.impl;

import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createEncounterReference;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType;

import javax.annotation.Nonnull;

import lombok.AccessLevel;
Expand All @@ -23,7 +27,7 @@

@Component
@Setter(AccessLevel.PACKAGE)
public class EncounterReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements EncounterReferenceTranslator<Encounter> {
public class EncounterReferenceTranslatorImpl implements EncounterReferenceTranslator<Encounter> {

@Autowired
private FhirEncounterDao encounterDao;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
*/
package org.openmrs.module.fhir2.api.translators.impl;

import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.createLocationReference;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceId;
import static org.openmrs.module.fhir2.api.translators.impl.ReferenceHandlingTranslator.getReferenceType;

import javax.annotation.Nonnull;

import lombok.AccessLevel;
Expand All @@ -23,7 +27,7 @@

@Component
@Setter(AccessLevel.PACKAGE)
public class LocationReferenceTranslatorImpl extends BaseReferenceHandlingTranslator implements LocationReferenceTranslator {
public class LocationReferenceTranslatorImpl implements LocationReferenceTranslator {

@Autowired
private FhirLocationDao locationDao;
Expand Down
Loading

0 comments on commit 983b724

Please sign in to comment.