Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement /getAll on SettingsResource v2 #486

Open
wants to merge 5 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<artifactId>opensrp-server-core</artifactId>
<packaging>jar</packaging>
<version>2.12.3-SNAPSHOT</version>
<version>2.12.4-SNAPSHOT</version>
<name>opensrp-server-core</name>
<description>OpenSRP Server Core module</description>
<url>https://github.com/OpenSRP/opensrp-server-core</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,21 @@ public SettingConfiguration findSetting(SettingSearchBean settingQueryBean,

public List<SettingConfiguration> findSettings(SettingSearchBean settingQueryBean, int limit,
Map<String, TreeNode<String, Location>> treeNodeHashMap) {
List<SettingConfiguration> 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,
Expand Down Expand Up @@ -218,6 +226,10 @@ public List<SettingsAndSettingsMetadataJoined> 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 {
Expand All @@ -228,6 +240,39 @@ public List<SettingsAndSettingsMetadataJoined> findSettingsAndSettingsMetadata(S
}
}

public List<SettingsMetadata> findSettingsMetadata(SettingSearchBean settingQueryBean, Map<String, TreeNode<String, Location>> 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();
Expand Down Expand Up @@ -289,6 +334,28 @@ private List<SettingsAndSettingsMetadataJoined> fetchSettingsPerLocation(Setting
return settingsAndSettingsMetadataJoinedList;
}

private List<SettingsMetadata> fetchSettingsMetadataPerLocation(SettingSearchBean settingSearchBean,
SettingsMetadataExample metadataExample, Map<String, TreeNode<String, Location>> treeNodeHashMap, int limit,
SettingsMetadataExample.Criteria criteria) {

List<SettingsMetadata> 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<String, TreeNode<String, Location>> parentLocation) {
Map.Entry<String, TreeNode<String, Location>> stringMapEntry = parentLocation.entrySet().iterator().next();
String locationKey = stringMapEntry.getKey();
Expand Down Expand Up @@ -327,6 +394,30 @@ private List<SettingConfiguration> resolveSettings(
return reconcileConfigurations(stringConfigurationMap, stringSettingsMap);
}

private List<SettingConfiguration> resolveSettingsMetadata(
List<SettingsMetadata> settingsMetadataList, boolean isV1Settings,
boolean resolveSettings) {
Map<String, SettingConfiguration> stringConfigurationMap = new HashMap<>();
Map<String, Setting> stringSettingsMap = new HashMap<>();

List<SettingConfiguration> configurations = convertSettingsMetadataToSettingConfigurations(settingsMetadataList,
isV1Settings, resolveSettings);

for (SettingConfiguration configuration : configurations) {
String configKey =
configuration.getId() + "_" + configuration.getLocationId() + "_" + configuration.getIdentifier();
stringConfigurationMap.put(configKey, configuration);

List<Setting> settingsList = configuration.getSettings();
for (Setting setting : settingsList) {
String settingKey = setting.getKey() + "_" + configuration.getIdentifier();
stringSettingsMap.put(settingKey, setting);
}
}

return reconcileConfigurations(stringConfigurationMap, stringSettingsMap);
}

private List<SettingConfiguration> reconcileConfigurations(Map<String, SettingConfiguration> stringConfigurationMap,
Map<String, Setting> stringSettingsMap) {
List<SettingConfiguration> settingConfigurations = new ArrayList<>();
Expand Down Expand Up @@ -444,6 +535,25 @@ private List<SettingConfiguration> convertToSettingConfigurations(
return settingConfigurations;
}

private List<SettingConfiguration> convertSettingsMetadataToSettingConfigurations(
List<SettingsMetadata> settingsMetadataList, boolean isV1Settings,
boolean resolveSettings) {
List<SettingConfiguration> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ List<SettingsAndSettingsMetadataJoined> selectMany(@Param("example") SettingsMet
int insertMany(@Param("settingsMetadata") List<SettingsMetadata> settingsMetadata);

int updateMany(@Param("settingsMetadata") List<SettingsMetadata> settingsMetadata);

List<SettingsMetadata> selectManySettingsMetadata(@Param("example") SettingsMetadataExample settingsExample,
@Param("offset") int offset,
@Param("limit") int limit);
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
<result column="metadata_version" jdbcType="BIGINT" property="settingsMetadata.metadataVersion" />
</resultMap>

<resultMap id="BaseResultMapForSettingsMetadata" type="org.opensrp.domain.postgres.SettingsMetadata">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Apr 24 10:25:50 EAT 2020.
-->
<id column="id" jdbcType="BIGINT" property="id" />
<result column="settings_id" jdbcType="BIGINT" property="settingsId" />
<result column="document_id" jdbcType="VARCHAR" property="documentId" />
<result column="identifier" jdbcType="VARCHAR" property="identifier" />
<result column="server_version" jdbcType="BIGINT" property="serverVersion" />
<result column="team" jdbcType="VARCHAR" property="team" />
<result column="team_id" jdbcType="VARCHAR" property="teamId" />
<result column="provider_id" jdbcType="VARCHAR" property="providerId" />
<result column="location_id" jdbcType="VARCHAR" property="locationId" />
<result column="uuid" jdbcType="VARCHAR" property="uuid" />
<result column="json" jdbcType="OTHER" property="json" />
<result column="setting_type" jdbcType="VARCHAR" property="settingType" />
<result column="setting_value" jdbcType="VARCHAR" property="settingValue" />
<result column="setting_key" jdbcType="VARCHAR" property="settingKey" />
<result column="setting_description" jdbcType="VARCHAR" property="settingDescription" />
<result column="setting_label" jdbcType="VARCHAR" property="settingLabel" />
<result column="inherited_from" jdbcType="VARCHAR" property="inheritedFrom" />
<result column="metadata_version" jdbcType="BIGINT" property="metadataVersion" />
</resultMap>

<sql id="Base_Column_List">
s.json as settings_configuration_json, sm.id as settings_metadata_id, sm.json as settings_metadata_configuration_json, sm.*
</sql>
Expand All @@ -36,6 +62,17 @@
sm.settings_id = s.id
</sql>

<sql id="Base_Column_List_Settings_Metadata">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Fri Apr 24 10:25:50 EAT 2020.
-->
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
</sql>

<select id="selectByDocumentId" resultMap="SettingsAndSettingsMetadataMap">
select
<include refid="Base_Column_List" />
Expand Down Expand Up @@ -165,4 +202,21 @@
LIMIT #{limit} OFFSET #{offset}
</select>

<select id="selectManySettingsMetadata" resultMap="BaseResultMapForSettingsMetadata">
SELECT
<if test="example.distinct">
distinct
</if>
<include refid="Base_Column_List_Settings_Metadata" />
from core.settings_metadata sm
<if test="_parameter != null">
<include refid="Example_Where_Clause" />
</if>
<if test="example.orderByClause != null">
order by ${example.orderByClause}
</if>

LIMIT #{limit} OFFSET #{offset}
</select>

</mapper>
38 changes: 38 additions & 0 deletions src/main/java/org/opensrp/search/SettingSearchBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down