Skip to content

Commit

Permalink
Revert "FM2-642: Improve performance of loading locations by tag (#547)"
Browse files Browse the repository at this point in the history
This reverts commit ca8bf98.
  • Loading branch information
ibacher committed Sep 17, 2024
1 parent 0f14570 commit 6842295
Show file tree
Hide file tree
Showing 62 changed files with 529 additions and 458 deletions.
20 changes: 2 additions & 18 deletions api/src/main/java/org/openmrs/module/fhir2/FhirActivator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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 @@ -37,7 +36,6 @@
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 @@ -55,8 +53,6 @@ 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 Down Expand Up @@ -86,13 +82,6 @@ public void willRefreshContext() {

@Override
public void contextRefreshed() {
if (globalPropertyHolder == null) {
globalPropertyHolder = new FhirGlobalPropertyHolder();
Context.getAdministrationService().addGlobalPropertyListener(globalPropertyHolder);
}

FhirGlobalPropertyHolder.reset();

if (!started) {
return;
}
Expand All @@ -106,18 +95,13 @@ public void contextRefreshed() {
@Override
public void willStop() {
lifecycleListeners.forEach(ModuleLifecycleListener::willStop);

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

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

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

@Override
public void setApplicationContext(@Nonnull ApplicationContext applicationContext) throws BeansException {
public void setApplicationContext(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;

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

String getGlobalProperty(String property, String defaultValue);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.dao;

import java.util.Map;

import org.openmrs.GlobalProperty;
import org.openmrs.annotation.Authorized;
import org.openmrs.api.APIException;
import org.openmrs.util.PrivilegeConstants;

public interface FhirGlobalPropertyDao {

@Authorized(PrivilegeConstants.GET_GLOBAL_PROPERTIES)
String getGlobalProperty(String property) throws APIException;

@Authorized(PrivilegeConstants.GET_GLOBAL_PROPERTIES)
GlobalProperty getGlobalPropertyObject(String property);

@Authorized(PrivilegeConstants.GET_GLOBAL_PROPERTIES)
Map<String, String> getGlobalProperties(String... properties);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.dao.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import lombok.AccessLevel;
import lombok.Setter;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.openmrs.GlobalProperty;
import org.openmrs.api.APIException;
import org.openmrs.module.fhir2.api.dao.FhirGlobalPropertyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@Setter(AccessLevel.PACKAGE)
public class FhirGlobalPropertyDaoImpl implements FhirGlobalPropertyDao {

@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;

@Override
public String getGlobalProperty(String property) throws APIException {
GlobalProperty globalProperty = (GlobalProperty) sessionFactory.getCurrentSession().get(GlobalProperty.class,
property);
return globalProperty == null ? null : globalProperty.getPropertyValue();
}

@Override
public GlobalProperty getGlobalPropertyObject(String property) {
return (GlobalProperty) sessionFactory.getCurrentSession().createCriteria(GlobalProperty.class)
.add(Restrictions.eq("property", property)).uniqueResult();
}

@Override
@SuppressWarnings("unchecked")
public Map<String, String> getGlobalProperties(String... properties) {
Map<String, String> globalPropertiesMap = new HashMap<>();

Collection<GlobalProperty> globalProperties = (sessionFactory.getCurrentSession()
.createCriteria(GlobalProperty.class).add(Restrictions.in("property", properties)).list());

for (GlobalProperty property : globalProperties) {
globalPropertiesMap.put(property.getProperty(), property.getPropertyValue());
}

return globalPropertiesMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,45 @@
import lombok.Setter;
import org.openmrs.api.APIException;
import org.openmrs.module.fhir2.api.FhirGlobalPropertyService;
import org.openmrs.module.fhir2.api.util.FhirGlobalPropertyHolder;
import org.openmrs.module.fhir2.api.dao.FhirGlobalPropertyDao;
import org.springframework.beans.factory.annotation.Autowired;
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 FhirGlobalPropertyHolder.getGlobalProperty(property);
return dao.getGlobalProperty(property);
}

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

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

@Override
@Transactional(readOnly = true)
public Map<String, String> getGlobalProperties(String... properties) {
return FhirGlobalPropertyHolder.getGlobalProperties(properties);
return dao.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.getGlobalPropertyAsInteger(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
pageSize = globalPropertyService.getGlobalProperty(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.getGlobalPropertyAsInteger(FhirConstants.OPENMRS_FHIR_DEFAULT_PAGE_SIZE, 10);
pageSize = globalPropertyService.getGlobalProperty(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 implements AllergyIntoleranceTranslator {
public class AllergyIntoleranceTranslatorImpl extends BaseReferenceHandlingTranslator implements AllergyIntoleranceTranslator {

@Autowired
private PractitionerReferenceTranslator<User> practitionerReferenceTranslator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,49 @@

@Setter(AccessLevel.PACKAGE)
@Slf4j
public final class ReferenceHandlingTranslator {
public abstract class BaseReferenceHandlingTranslator {

public static Reference createEncounterReference(@Nonnull Encounter encounter) {
protected Reference createEncounterReference(@Nonnull Encounter encounter) {
return createEncounterReference((OpenmrsObject) encounter);
}

public static Reference createEncounterReference(@Nonnull Visit visit) {
protected Reference createEncounterReference(@Nonnull Visit visit) {
return createEncounterReference((OpenmrsObject) visit);
}

private static Reference createEncounterReference(@Nonnull OpenmrsObject encounter) {
private Reference createEncounterReference(@Nonnull OpenmrsObject encounter) {
return new Reference().setReference(FhirConstants.ENCOUNTER + "/" + encounter.getUuid())
.setType(FhirConstants.ENCOUNTER);
}

public static Reference createValueSetReference(@Nonnull Concept concept) {
protected Reference createValueSetReference(@Nonnull Concept concept) {
if (concept == null || !concept.getSet()) {
return null;
}
return new Reference().setReference(FhirConstants.VALUESET + "/" + concept.getUuid())
.setType(FhirConstants.VALUESET);
}

public static Reference createMedicationReference(@Nonnull Drug drug) {
protected Reference createMedicationReference(@Nonnull Drug drug) {
return new Reference().setReference(FhirConstants.MEDICATION + "/" + drug.getUuid())
.setType(FhirConstants.MEDICATION).setDisplay(drug.getDisplayName());
}

public static Reference createObservationReference(@Nonnull Obs obs) {
protected Reference createObservationReference(@Nonnull Obs obs) {
return new Reference().setReference(FhirConstants.OBSERVATION + "/" + obs.getUuid())
.setType(FhirConstants.OBSERVATION);
}

public static Reference createLocationReferenceByUuid(@Nonnull String uuid) {
protected Reference createLocationReferenceByUuid(@Nonnull String uuid) {
return new Reference().setReference(FhirConstants.LOCATION + "/" + uuid).setType(FhirConstants.LOCATION);
}

public static Reference createLocationReference(@Nonnull Location location) {
protected Reference createLocationReference(@Nonnull Location location) {
return new Reference().setReference(FhirConstants.LOCATION + "/" + location.getUuid())
.setType(FhirConstants.LOCATION).setDisplay(getMetadataTranslation(location));
}

public static Reference createPatientReference(@Nonnull Patient patient) {
protected Reference createPatientReference(@Nonnull Patient patient) {
Reference reference = new Reference().setReference(FhirConstants.PATIENT + "/" + patient.getUuid())
.setType(FhirConstants.PATIENT);

Expand All @@ -108,7 +108,7 @@ public static Reference createPatientReference(@Nonnull Patient patient) {
return reference;
}

public static Reference createPractitionerReference(@Nonnull User user) {
protected Reference createPractitionerReference(@Nonnull User user) {
Reference reference = new Reference().setReference(FhirConstants.PRACTITIONER + "/" + user.getUuid())
.setType(FhirConstants.PRACTITIONER);

Expand All @@ -121,7 +121,7 @@ public static Reference createPractitionerReference(@Nonnull User user) {
return reference;
}

public static Reference createPractitionerReference(@Nonnull Provider provider) {
protected Reference createPractitionerReference(@Nonnull Provider provider) {
Reference reference = new Reference().setReference(FhirConstants.PRACTITIONER + "/" + provider.getUuid())
.setType(FhirConstants.PRACTITIONER);

Expand All @@ -144,7 +144,7 @@ public static Reference createPractitionerReference(@Nonnull Provider provider)
return reference;
}

public static Reference createOrderReference(@Nonnull Order order) {
protected Reference createOrderReference(@Nonnull Order order) {
if (order == null) {
return null;
}
Expand All @@ -160,19 +160,19 @@ public static Reference createOrderReference(@Nonnull Order order) {
}
}

public static Reference createDrugOrderReference(@Nonnull DrugOrder drugOrder) {
protected Reference createDrugOrderReference(@Nonnull DrugOrder drugOrder) {
if (drugOrder == null) {
return null;
}
return new Reference().setReference(FhirConstants.MEDICATION_REQUEST + "/" + drugOrder.getUuid())
.setType(FhirConstants.MEDICATION_REQUEST);
}

public static Optional<String> getReferenceType(Reference reference) {
protected Optional<String> getReferenceType(Reference reference) {
return FhirUtils.getReferenceType(reference);
}

public static Optional<String> getReferenceId(Reference reference) {
protected Optional<String> getReferenceId(Reference reference) {
return FhirUtils.referenceToId(reference.getReference());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,8 @@ public DiagnosticReport toFhirResource(@Nonnull FhirDiagnosticReport fhirDiagnos
diagnosticReport.setId(fhirDiagnosticReport.getUuid());

if (fhirDiagnosticReport.getStatus() != null) {
try {
diagnosticReport.setStatus(
DiagnosticReport.DiagnosticReportStatus.valueOf(fhirDiagnosticReport.getStatus().toString()));
}
catch (IllegalArgumentException e) {
diagnosticReport.setStatus(DiagnosticReport.DiagnosticReportStatus.UNKNOWN);
}
diagnosticReport
.setStatus(DiagnosticReport.DiagnosticReportStatus.valueOf(fhirDiagnosticReport.getStatus().toString()));
} else {
diagnosticReport.setStatus(DiagnosticReport.DiagnosticReportStatus.UNKNOWN);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
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 @@ -26,7 +24,7 @@

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

@Autowired
private FhirLocationDao locationDao;
Expand Down
Loading

0 comments on commit 6842295

Please sign in to comment.