diff --git a/pom.xml b/pom.xml
index ad91f99e4..d0fcfbe68 100755
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
opensrp-server-core
jar
- 2.12.3-SNAPSHOT
+ 2.12.4-SNAPSHOT
opensrp-server-core
OpenSRP Server Core module
https://github.com/OpenSRP/opensrp-server-core
diff --git a/src/main/java/org/opensrp/repository/postgres/SettingRepositoryImpl.java b/src/main/java/org/opensrp/repository/postgres/SettingRepositoryImpl.java
index 26d3deb6e..c4afa2539 100755
--- a/src/main/java/org/opensrp/repository/postgres/SettingRepositoryImpl.java
+++ b/src/main/java/org/opensrp/repository/postgres/SettingRepositoryImpl.java
@@ -182,13 +182,21 @@ public SettingConfiguration findSetting(SettingSearchBean settingQueryBean,
public List findSettings(SettingSearchBean settingQueryBean, int limit,
Map> treeNodeHashMap) {
+ List settingConfigurations;
if (settingQueryBean.isResolveSettings()) {
- return resolveSettings(findSettingsAndSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
- settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings());
+ settingConfigurations = settingQueryBean.isETL() ?
+ resolveSettingsMetadata(findSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
+ settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings()) :
+ resolveSettings(findSettingsAndSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
+ settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings());
} else {
- return convertToSettingConfigurations(findSettingsAndSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
- settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings());
+ settingConfigurations = settingQueryBean.isETL() ?
+ convertSettingsMetadataToSettingConfigurations(findSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
+ settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings()) :
+ convertToSettingConfigurations(findSettingsAndSettingsMetadata(settingQueryBean, treeNodeHashMap, limit),
+ settingQueryBean.isV1Settings(), settingQueryBean.isResolveSettings());
}
+ return settingConfigurations;
}
public SettingsAndSettingsMetadataJoined findSettingsAndSettingsMetadata(SettingSearchBean settingQueryBean,
@@ -218,6 +226,10 @@ public List findSettingsAndSettingsMetadata(S
criteria.andMetadataVersionGreaterThan(settingQueryBean.getMetadataVersion());
}
+ if(settingQueryBean.getOrderByType() != null && settingQueryBean.getOrderByFieldName() != null) {
+ metadataExample.setOrderByClause(getOrderByClause(settingQueryBean.getOrderByFieldName().name(),settingQueryBean.getOrderByType().name()));
+ }
+
if (settingQueryBean.isResolveSettings()) {
return fetchSettingsPerLocation(settingQueryBean, metadataExample, treeNodeHashMap, limit, criteria);
} else {
@@ -228,6 +240,39 @@ public List findSettingsAndSettingsMetadata(S
}
}
+ public List findSettingsMetadata(SettingSearchBean settingQueryBean, Map> treeNodeHashMap, int limit) {
+ SettingsMetadataExample metadataExample = new SettingsMetadataExample();
+ SettingsMetadataExample.Criteria criteria = metadataExample.createCriteria();
+ String locationId = settingQueryBean.getLocationId();
+ Long primaryKey = settingQueryBean.getPrimaryKey();
+ updateCriteria(criteria, settingQueryBean);
+
+ if (primaryKey != null) {
+ metadataExample.or(metadataExample.createCriteria().andSettingsIdEqualTo(primaryKey));
+ }
+
+ if (StringUtils.isBlank(settingQueryBean.getId())) {
+ criteria.andServerVersionGreaterThanOrEqualTo(settingQueryBean.getServerVersion());
+ }
+
+ if (settingQueryBean.getMetadataVersion() != null) {
+ criteria.andMetadataVersionGreaterThan(settingQueryBean.getMetadataVersion());
+ }
+
+ if (settingQueryBean.getOrderByType() != null && settingQueryBean.getOrderByFieldName() != null) {
+ metadataExample.setOrderByClause(getOrderByClause(settingQueryBean.getOrderByFieldName().name(), settingQueryBean.getOrderByType().name()));
+ }
+
+ if (settingQueryBean.isResolveSettings()) {
+ return fetchSettingsMetadataPerLocation(settingQueryBean, metadataExample, treeNodeHashMap, limit, criteria);
+ } else {
+ if (StringUtils.isNotEmpty(locationId)) {
+ criteria.andLocationIdEqualTo(locationId);
+ }
+ return settingMetadataMapper.selectManySettingsMetadata(metadataExample, 0, limit);
+ }
+ }
+
private SettingsMetadataExample.Criteria updateCriteria(SettingsMetadataExample.Criteria criteria,
SettingSearchBean settingQueryBean) {
String providerId = settingQueryBean.getProviderId();
@@ -289,6 +334,28 @@ private List fetchSettingsPerLocation(Setting
return settingsAndSettingsMetadataJoinedList;
}
+ private List fetchSettingsMetadataPerLocation(SettingSearchBean settingSearchBean,
+ SettingsMetadataExample metadataExample, Map> treeNodeHashMap, int limit,
+ SettingsMetadataExample.Criteria criteria) {
+
+ List settingsMetadataList = new ArrayList<>();
+ if (StringUtils.isNotBlank(settingSearchBean.getLocationId())) {
+ locationUuid = settingSearchBean.getLocationId();
+
+ reformattedLocationHierarchy.clear();
+ if (treeNodeHashMap != null && treeNodeHashMap.size() > 0) {
+ reformattedLocationHierarchy(treeNodeHashMap);
+ }
+
+ if (reformattedLocationHierarchy.size() > 0) {
+ criteria.andLocationIdIn(reformattedLocationHierarchy);
+ settingsMetadataList = settingMetadataMapper.selectManySettingsMetadata(metadataExample, 0, limit);
+ }
+ }
+
+ return settingsMetadataList;
+ }
+
private void reformattedLocationHierarchy(Map> parentLocation) {
Map.Entry> stringMapEntry = parentLocation.entrySet().iterator().next();
String locationKey = stringMapEntry.getKey();
@@ -327,6 +394,30 @@ private List resolveSettings(
return reconcileConfigurations(stringConfigurationMap, stringSettingsMap);
}
+ private List resolveSettingsMetadata(
+ List settingsMetadataList, boolean isV1Settings,
+ boolean resolveSettings) {
+ Map stringConfigurationMap = new HashMap<>();
+ Map stringSettingsMap = new HashMap<>();
+
+ List configurations = convertSettingsMetadataToSettingConfigurations(settingsMetadataList,
+ isV1Settings, resolveSettings);
+
+ for (SettingConfiguration configuration : configurations) {
+ String configKey =
+ configuration.getId() + "_" + configuration.getLocationId() + "_" + configuration.getIdentifier();
+ stringConfigurationMap.put(configKey, configuration);
+
+ List settingsList = configuration.getSettings();
+ for (Setting setting : settingsList) {
+ String settingKey = setting.getKey() + "_" + configuration.getIdentifier();
+ stringSettingsMap.put(settingKey, setting);
+ }
+ }
+
+ return reconcileConfigurations(stringConfigurationMap, stringSettingsMap);
+ }
+
private List reconcileConfigurations(Map stringConfigurationMap,
Map stringSettingsMap) {
List settingConfigurations = new ArrayList<>();
@@ -444,6 +535,25 @@ private List convertToSettingConfigurations(
return settingConfigurations;
}
+ private List convertSettingsMetadataToSettingConfigurations(
+ List settingsMetadataList, boolean isV1Settings,
+ boolean resolveSettings) {
+ List settingConfigurations = new ArrayList<>();
+ if (settingsMetadataList == null || settingsMetadataList.isEmpty()) {
+ return settingConfigurations;
+ }
+
+ SettingConfiguration settingConfiguration = new SettingConfiguration();
+ settingConfiguration.setSettings(new ArrayList<>());
+ for (SettingsMetadata settingsMetadata : settingsMetadataList) {
+ settingConfiguration.getSettings().add(convertToSetting(settingsMetadata, isV1Settings, resolveSettings));
+ settingConfiguration.setDocumentId(settingConfiguration.getSettings().get(0).getDocumentId());
+ settingConfiguration.setIdentifier(settingConfiguration.getSettings().get(0).getSettingIdentifier());
+ }
+ settingConfigurations.add(settingConfiguration);
+ return settingConfigurations;
+ }
+
private Setting convertToSetting(SettingsMetadata settingsMetadata, boolean isV1Settings, boolean resolveSettings) {
Setting setting = new Setting();
getSettingValue(settingsMetadata, setting);
diff --git a/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomSettingMetadataMapper.java b/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomSettingMetadataMapper.java
index 6a1fa0360..8f6958515 100644
--- a/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomSettingMetadataMapper.java
+++ b/src/main/java/org/opensrp/repository/postgres/mapper/custom/CustomSettingMetadataMapper.java
@@ -19,5 +19,9 @@ List selectMany(@Param("example") SettingsMet
int insertMany(@Param("settingsMetadata") List settingsMetadata);
int updateMany(@Param("settingsMetadata") List settingsMetadata);
+
+ List selectManySettingsMetadata(@Param("example") SettingsMetadataExample settingsExample,
+ @Param("offset") int offset,
+ @Param("limit") int limit);
}
diff --git a/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomSettingsMetadataMapper.xml b/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomSettingsMetadataMapper.xml
index 9e370f062..ee50fe408 100644
--- a/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomSettingsMetadataMapper.xml
+++ b/src/main/java/org/opensrp/repository/postgres/mapper/custom/xml/CustomSettingsMetadataMapper.xml
@@ -26,6 +26,32 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
s.json as settings_configuration_json, sm.id as settings_metadata_id, sm.json as settings_metadata_configuration_json, sm.*
@@ -36,6 +62,17 @@
sm.settings_id = s.id
+
+
+ id, settings_id, document_id, identifier, server_version, team, team_id, provider_id,
+ location_id, uuid, json, setting_type, setting_value, setting_key, setting_description,
+ inherited_from, metadata_version
+
+
+
+
diff --git a/src/main/java/org/opensrp/search/SettingSearchBean.java b/src/main/java/org/opensrp/search/SettingSearchBean.java
index 1d8f171ae..f9b41b9e0 100644
--- a/src/main/java/org/opensrp/search/SettingSearchBean.java
+++ b/src/main/java/org/opensrp/search/SettingSearchBean.java
@@ -14,6 +14,14 @@
@NoArgsConstructor
@AllArgsConstructor
public class SettingSearchBean extends BaseDataObject {
+
+ public enum OrderByType {
+ ASC, DESC
+ };
+
+ public enum FieldName {
+ id, metadata_version, server_version
+ };
@JsonProperty
private String identifier;
@@ -47,6 +55,12 @@ public class SettingSearchBean extends BaseDataObject {
private Integer limit;
+ private FieldName orderByFieldName;
+
+ private OrderByType orderByType;
+
+ private boolean isETL;
+
public String getProviderId() {
return providerId;
}
@@ -142,4 +156,28 @@ public Integer getLimit() {
public void setLimit(Integer limit) {
this.limit = limit;
}
+
+ public FieldName getOrderByFieldName() {
+ return orderByFieldName;
+ }
+
+ public void setOrderByFieldName(FieldName orderByFieldName) {
+ this.orderByFieldName = orderByFieldName;
+ }
+
+ public OrderByType getOrderByType() {
+ return orderByType;
+ }
+
+ public void setOrderByType(OrderByType orderByType) {
+ this.orderByType = orderByType;
+ }
+
+ public boolean isETL() {
+ return isETL;
+ }
+
+ public void setETL(boolean ETL) {
+ isETL = ETL;
+ }
}
diff --git a/src/test/java/org/opensrp/repository/postgres/SettingRepositoryTest.java b/src/test/java/org/opensrp/repository/postgres/SettingRepositoryTest.java
index 2f90540eb..9565cf419 100755
--- a/src/test/java/org/opensrp/repository/postgres/SettingRepositoryTest.java
+++ b/src/test/java/org/opensrp/repository/postgres/SettingRepositoryTest.java
@@ -121,6 +121,24 @@ public void testFindByCriteria() {
assertEquals(5, settings.size());
+ settingQueryBean = new SettingSearchBean();
+ settingQueryBean.setServerVersion(0L);
+ settingQueryBean.setMetadataVersion(0L);
+ settingQueryBean.setETL(true);
+ settings = settingRepository.findSettings(settingQueryBean, null);
+
+ assertEquals(1, settings.size());
+
+ settingQueryBean = new SettingSearchBean();
+ settingQueryBean.setServerVersion(0L);
+ settingQueryBean.setLocationId("44de66fb-e6c6-4bae-92bb-386dfe626eba");
+ settingQueryBean.setETL(true);
+ settingQueryBean.setOrderByFieldName(SettingSearchBean.FieldName.metadata_version);
+ settingQueryBean.setOrderByType(SettingSearchBean.OrderByType.ASC);
+ settings = settingRepository.findSettings(settingQueryBean, null);
+
+ assertEquals(1, settings.size());
+
}
@Test