Skip to content

Commit

Permalink
EIP-50 - Make the list of watched table names configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
wluyima committed Sep 15, 2021
1 parent 1bf1a89 commit 6eaec94
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
2 changes: 2 additions & 0 deletions commons/src/main/java/org/openmrs/eip/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public class Constants {
public static final String MGT_DATASOURCE_NAME = "mngtDataSource";

public static final String HTTP_HEADER_AUTH = "Authorization";

public static final String PROP_WATCHED_TABLES = "eip.watchedTables";

}
10 changes: 4 additions & 6 deletions commons/src/main/java/org/openmrs/eip/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.core.env.Environment;

public class Utils {

private static final String[] WATCHED_TABLES = new String[] { "PERSON", "PATIENT", "VISIT", "ENCOUNTER", "OBS",
"PERSON_ATTRIBUTE", "PATIENT_PROGRAM", "PATIENT_STATE", "VISIT_ATTRIBUTE", "ENCOUNTER_DIAGNOSIS", "CONDITION",
"PERSON_NAME", "ALLERGY", "PERSON_ADDRESS", "PATIENT_IDENTIFIER", "ORDERS", "DRUG_ORDER", "TEST_ORDER",
"RELATIONSHIP", "ENCOUNTER_PROVIDER", "ORDER_GROUP", "PATIENT_PROGRAM_ATTRIBUTE" };

/**
* Gets a list of all watched table names
*
* @return
*/
public static List<String> getWatchedTables() {
return Arrays.asList(WATCHED_TABLES);
String watchedTables = WatcherContext.getBean(Environment.class).getProperty(Constants.PROP_WATCHED_TABLES);
return Arrays.asList(watchedTables.split(","));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions commons/src/main/java/org/openmrs/eip/WatcherContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openmrs.eip;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class WatcherContext implements ApplicationContextAware {

private static ApplicationContext appContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
appContext = applicationContext;
}

/**
* Gets the bean matching the specified type from the application context
*
* @return an instance of the bean matching the specified type
*/
public static <T> T getBean(Class<T> clazz) {
return appContext.getBean(clazz);
}

}
21 changes: 21 additions & 0 deletions commons/src/test/java/org/openmrs/eip/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
import java.util.stream.Collectors;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.core.env.Environment;

@RunWith(PowerMockRunner.class)
@PrepareForTest(WatcherContext.class)
public class UtilsTest {

@Test
Expand Down Expand Up @@ -89,4 +97,17 @@ public void getTablesInHierarchy_shouldReturnCommaSeparatedListOfSubclassAndSupe
assertTrue(tables.contains("'orders'"));
}

@Test
public void getWatchedTables_shouldReturnTheWatchedTableNames() {
PowerMockito.mockStatic(WatcherContext.class);
Environment mockEnv = Mockito.mock(Environment.class);
Mockito.when(WatcherContext.getBean(Environment.class)).thenReturn(mockEnv);
Mockito.when(mockEnv.getProperty(Constants.PROP_WATCHED_TABLES)).thenReturn("person,patient,visit");
List<String> watchedTables = Utils.getWatchedTables();
assertEquals(3, watchedTables.size());
assertTrue(watchedTables.contains("person"));
assertTrue(watchedTables.contains("patient"));
assertTrue(watchedTables.contains("visit"));
}

}
3 changes: 3 additions & 0 deletions docs/custom/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#
eip.home=${user.home}${file.separator}.openmrs-eip

# A comma separated list of database tables names to watch for changes
eip.watchedTables=

# Camel endpoints that need to be notified of DB events
db-event.destinations=

Expand Down

0 comments on commit 6eaec94

Please sign in to comment.