Skip to content

Commit

Permalink
Changed SearchDataTransformer in accordance with the community discus…
Browse files Browse the repository at this point in the history
…sion comments
  • Loading branch information
oleg-odysseus authored and alex-odysseus committed Dec 3, 2024
1 parent 4ee6b48 commit 57491c3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ConceptSetAnnotation implements Serializable {
private String vocabularyVersion;

@Column(name = "concept_set_version")
private String conceptSetVersion;
private Integer conceptSetVersion;

@Column(name = "copied_from_concept_set_ids")
private String copiedFromConceptSetIds;
Expand Down Expand Up @@ -90,11 +90,11 @@ public void setVocabularyVersion(String vocabularyVersion) {
this.vocabularyVersion = vocabularyVersion;
}

public String getConceptSetVersion() {
public Integer getConceptSetVersion() {
return conceptSetVersion;
}

public void setConceptSetVersion(String conceptSetVersion) {
public void setConceptSetVersion(Integer conceptSetVersion) {
this.conceptSetVersion = conceptSetVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ private ConceptSetVersion saveVersion(int id) {
@Transactional
public boolean saveConceptSetAnnotation(@PathParam("id") final int conceptSetId, SaveConceptSetAnnotationsRequest request) {
removeAnnotations(conceptSetId, request);
if (request.getNewAnnotation() == null || request.getNewAnnotation().isEmpty()) {
if (request.getNewAnnotation() != null && !request.getNewAnnotation().isEmpty()) {
List<ConceptSetAnnotation> annotationList = request.getNewAnnotation()
.stream()
.map(newAnnotationData -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,101 +5,96 @@
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

@Service
public class SearchDataTransformer {

private static final String FILTER_DATA = "filterData";
private static final String TITLE = "title";
private static final String VALUE = "value";
private static final String KEY = "key";
private static final String FILTER_SOURCE = "filterSource";
private static final String FILTER_SOURCE_LABEL = "Filter Source";
private static final String FILTER_SOURCE_LABEL = "Filtered By";
private static final String SEARCH_TEXT = "searchText";
private static final String SEARCH_TEXT_LABEL = "Search Text";
private static final String FILTER_COLUMNS = "filterColumns";
private static final String QUOTE = "\"";

private static final String DEFAULT_FILTER_SOURCE = "Search";
private static final String DELIMITER = ", ";

private static final String ENTRY_FORMAT = "%s: \"%s\"";

public String convertJsonToReadableFormat(String jsonInput) {
JSONObject searchObject = new JSONObject(jsonInput);
StringBuilder result = new StringBuilder();
JSONObject searchObject = new JSONObject(Optional.ofNullable(jsonInput).orElse("{}"));

String filterDataResult = extractFilterData(searchObject);
appendCommaSeparated(result, filterDataResult);

String filterSourceResult = processFilterSource(searchObject);
appendCommaSeparated(result, filterSourceResult);
if (searchObject.isEmpty()) {
return "";
}

return result.toString();
}
StringBuilder result = new StringBuilder();

private String extractFilterData(JSONObject jsonObject) {
JSONObject filterData = jsonObject.optJSONObject(FILTER_DATA);
if (filterData != null) {
String searchText = extractSearchText(filterData);
String filterColumns = extractFilterColumns(filterData);
return Stream.of(searchText, filterColumns)
.filter(StringUtils::isNotEmpty)
.collect(Collectors.joining(DELIMITER));
}
JSONArray filterDataArray = jsonObject.optJSONArray(FILTER_DATA);
if (filterDataArray != null) {
return formatKeyValuePairs(filterDataArray);
String filterSource = processFilterSource(searchObject);
append(result, getDefaultOrActual(filterSource, DEFAULT_FILTER_SOURCE));

JSONObject filterDataObject = searchObject.optJSONObject(FILTER_DATA);
JSONArray filterDataArray = searchObject.optJSONArray(FILTER_DATA);

if (filterDataObject != null) {
Optional.ofNullable(filterDataObject).map(this::processSearchText).ifPresent(searchText -> appendCommaSeparated(result, formatQuoted(searchText)));
Optional.ofNullable(filterDataObject.optJSONArray("filterColumns")).map(this::formatKeyValuePairs).ifPresent(
fdResult -> appendCommaSeparated(result, FILTER_SOURCE_LABEL + ": \"" + fdResult + "\"")
);
} else if (filterDataArray != null) {
String extractedData = formatKeyValuePairs(filterDataArray);
if (!extractedData.isEmpty()) {
appendCommaSeparated(result, FILTER_SOURCE_LABEL + ": \"" + extractedData + "\"");
}
}
return "";
}

private String extractFilterColumns(JSONObject filterData) {
JSONArray filterColumns = filterData.optJSONArray(FILTER_COLUMNS);
if (filterColumns != null) {
return formatKeyValuePairs(filterColumns);
}
return "";
return result.toString().trim();
}

private String processFilterSource(JSONObject jsonObject) {
String filterSource = jsonObject.optString(FILTER_SOURCE, "");
if (!filterSource.isEmpty()) {
return String.format(ENTRY_FORMAT, FILTER_SOURCE_LABEL, filterSource);
}
return "";
return jsonObject.optString(FILTER_SOURCE, "");
}

private String extractSearchText(JSONObject jsonObject) {
String searchText = jsonObject.optString(SEARCH_TEXT, "");
if (!searchText.isEmpty()) {
return String.format(ENTRY_FORMAT, SEARCH_TEXT_LABEL, searchText);
}
return "";
private String processSearchText(JSONObject filterData) {
return filterData.optString(SEARCH_TEXT, "");
}

private String formatKeyValuePairs(JSONArray filterColumns) {
return IntStream.range(0, filterColumns.length())
.mapToObj(index -> {
JSONObject item = filterColumns.getJSONObject(index);
String title = optString(item, TITLE);
String value = StringUtils.unwrap(optString(item, VALUE), QUOTE);
return String.format(ENTRY_FORMAT, title, value);
})
private String formatKeyValuePairs(JSONArray filterDataArray) {
return IntStream.range(0, filterDataArray.length())
.mapToObj(index -> formatEntry(filterDataArray.getJSONObject(index)))
.collect(Collectors.joining(DELIMITER));
}

private void appendCommaSeparated(StringBuilder resultBuilder, String part) {
private String formatEntry(JSONObject item) {
String title = optString(item, TITLE);
String key = StringUtils.unwrap(optString(item, KEY), '"');
return String.format(ENTRY_FORMAT, title, key);
}

private void appendCommaSeparated(StringBuilder builder, String part) {
if (!part.isEmpty()) {
if (resultBuilder.length() > 0) {
resultBuilder.append(DELIMITER);
}
resultBuilder.append(part);
append(builder, part);
}
}

private void append(StringBuilder builder, String part) {
if (builder.length() > 0) {
builder.append(DELIMITER);
}
builder.append(part);
}

private String optString(JSONObject item, String key) {
return item.optString(key, "");
}
}

private String getDefaultOrActual(String actual, String defaultVal) {
return actual.isEmpty() ? defaultVal : actual;
}

private String formatQuoted(String text) {
return String.format("\"%s\"", text);
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/ohdsi/webapi/service/dto/AnnotationDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class AnnotationDTO {
private String createdBy;
private String createdDate;
private String vocabularyVersion;
private String conceptSetVersion;
private Integer conceptSetVersion;
private String searchData;
private String copiedFromConceptSetIds;
private Integer conceptId;
Expand Down Expand Up @@ -38,11 +38,11 @@ public void setVocabularyVersion(String vocabularyVersion) {
this.vocabularyVersion = vocabularyVersion;
}

public String getConceptSetVersion() {
public Integer getConceptSetVersion() {
return conceptSetVersion;
}

public void setConceptSetVersion(String conceptSetVersion) {
public void setConceptSetVersion(Integer conceptSetVersion) {
this.conceptSetVersion = conceptSetVersion;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;

public class SearchDataTransformerTest {

Expand All @@ -30,50 +28,43 @@ public void shouldReturnEmptyStringWhenInputIsEmpty() {
public void shouldHandleSearchText() {
String input = "{\"filterData\":{\"searchText\":\"testSearch\"}}";
String result = sut.convertJsonToReadableFormat(input);
assertThat(result, is("Search Text: \"testSearch\""));
assertThat(result, is("Search, \"testSearch\""));
}

@Test
public void shouldHandleFilterSource() {
String input = "{\"filterSource\":\"Search\"}";
String result = sut.convertJsonToReadableFormat(input);
assertThat(result, is("Filter Source: \"Search\""));
assertThat(result, is("Search"));
}

@Test
public void shouldHandleFilterColumns() {
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":\"Domain\",\"value\":\"Drug\"}]} }";
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":\"Domain\",\"key\":\"Drug\"}]} }";
String result = sut.convertJsonToReadableFormat(input);
assertThat(result, is("Domain: \"Drug\""));
assertThat(result, is("Search, \"\", Filtered By: \"Domain: \"Drug\"\""));
}

@Test
public void shouldCombineFilterDataAndFilterSource() {
String input = "{\"filterData\":{\"searchText\":\"testSearch\",\"filterColumns\":[{\"title\":\"Domain\",\"value\":\"Drug\"}]},\"filterSource\":\"Search\"}";
String input = "{\"filterData\":{\"searchText\":\"testSearch\",\"filterColumns\":[{\"title\":\"Domain\",\"key\":\"Drug\"}]},\"filterSource\":\"Search\"}";
String result = sut.convertJsonToReadableFormat(input);
String expected = "Search Text: \"testSearch\", Domain: \"Drug\", Filter Source: \"Search\"";
String expected = "Search, \"testSearch\", Filtered By: \"Domain: \"Drug\"\"";
assertThat(result, is(expected));
}

@Test
public void shouldHandleMultipleFilterColumns() {
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":\"Domain\",\"value\":\"Drug\"},{\"title\":\"Class\",\"value\":\"Medication\"}]}}";
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":\"Domain\",\"key\":\"Drug\"},{\"title\":\"Class\",\"key\":\"Medication\"}]}}";
String result = sut.convertJsonToReadableFormat(input);
String expected = "Domain: \"Drug\", Class: \"Medication\"";
String expected = "Search, \"\", Filtered By: \"Domain: \"Drug\"" + ", Class: \"Medication\"\"";
assertThat(result, is(expected));
}

@Test
public void shouldIgnoreEmptyFilterColumnsAndSearchText() {
String input = "{\"filterData\":{\"searchText\":\"\",\"filterColumns\":[]}, \"filterSource\":\"\"}";
String result = sut.convertJsonToReadableFormat(input);
assertThat(result, isEmptyString());
}

@Test
public void shouldHandleNullValuesGracefully() {
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":null,\"value\":null}], \"searchText\":null}, \"filterSource\":null}";
String input = "{\"filterData\":{\"filterColumns\":[{\"title\":null,\"key\":null}], \"searchText\":null}, \"filterSource\":null}";
String result = sut.convertJsonToReadableFormat(input);
assertThat(result, not(containsString("null")));
assertThat(result, is("Search, \"\", Filtered By: \": \"\"\""));
}
}

0 comments on commit 57491c3

Please sign in to comment.