From f7a47dd4e3ecf6b561ad03044cf9ae3a079d6af5 Mon Sep 17 00:00:00 2001 From: GeorgeC Date: Fri, 15 Nov 2024 16:12:46 -0500 Subject: [PATCH] Add FilterProcessor to handle filter processing logic Introduced a new FilterProcessor component to centralize filter processing logic. Enhanced getDescription, getParentName, and getParentDisplay methods in MetadataResultSetUtil for better validation using StringUtils. --- .../dictionary/facet/FilterPreProcessor.java | 35 +++++++------------ .../dictionary/filter/FilterProcessor.java | 34 ++++++++++++++++++ .../legacysearch/LegacySearchQueryMapper.java | 9 +++-- .../legacysearch/MetadataResultSetUtil.java | 15 ++++---- 4 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterProcessor.java diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FilterPreProcessor.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FilterPreProcessor.java index 16510f1..3c6eda1 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FilterPreProcessor.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/facet/FilterPreProcessor.java @@ -1,22 +1,28 @@ package edu.harvard.dbmi.avillach.dictionary.facet; import edu.harvard.dbmi.avillach.dictionary.filter.Filter; +import edu.harvard.dbmi.avillach.dictionary.filter.FilterProcessor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.MethodParameter; import org.springframework.http.HttpInputMessage; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice; import java.io.IOException; import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.function.Function; @ControllerAdvice public class FilterPreProcessor implements RequestBodyAdvice { + + private final FilterProcessor filterProcessor; + + @Autowired + public FilterPreProcessor(FilterProcessor filterProcessor) { + this.filterProcessor = filterProcessor; + } + + @Override public boolean supports(MethodParameter methodParameter, Type targetType, Class> converterType) { return true; @@ -35,29 +41,12 @@ public Object afterBodyRead( Class> converterType ) { if (body instanceof Filter filter) { - return processsFilter(filter); + return filterProcessor.processsFilter(filter); } return body; } - public static Filter processsFilter(Filter filter) { - List newFacets = filter.facets(); - List newConsents = filter.consents(); - if (filter.facets() != null) { - newFacets = new ArrayList<>(filter.facets()); - newFacets.sort(Comparator.comparing(Facet::name)); - } - if (filter.consents() != null) { - newConsents = new ArrayList<>(newConsents); - newConsents.sort(Comparator.comparing(Function.identity())); - } - filter = new Filter(newFacets, filter.search(), newConsents); - if (StringUtils.hasLength(filter.search())) { - filter = new Filter(filter.facets(), filter.search().replaceAll("_", "/"), filter.consents()); - } - return filter; - } @Override public Object handleEmptyBody( diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterProcessor.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterProcessor.java new file mode 100644 index 0000000..f2478ae --- /dev/null +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/filter/FilterProcessor.java @@ -0,0 +1,34 @@ +package edu.harvard.dbmi.avillach.dictionary.filter; + +import edu.harvard.dbmi.avillach.dictionary.facet.Facet; +import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.function.Function; + +@Component +public class FilterProcessor { + + public Filter processsFilter(Filter filter) { + List newFacets = filter.facets(); + List newConsents = filter.consents(); + if (filter.facets() != null) { + newFacets = new ArrayList<>(filter.facets()); + newFacets.sort(Comparator.comparing(Facet::name)); + } + if (filter.consents() != null) { + newConsents = new ArrayList<>(newConsents); + newConsents.sort(Comparator.comparing(Function.identity())); + } + filter = new Filter(newFacets, filter.search(), newConsents); + + if (StringUtils.hasLength(filter.search())) { + filter = new Filter(filter.facets(), filter.search().replaceAll("_", "/"), filter.consents()); + } + return filter; + } + +} diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchQueryMapper.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchQueryMapper.java index aed5eb6..001436c 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchQueryMapper.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/LegacySearchQueryMapper.java @@ -2,8 +2,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import edu.harvard.dbmi.avillach.dictionary.facet.FilterPreProcessor; import edu.harvard.dbmi.avillach.dictionary.filter.Filter; +import edu.harvard.dbmi.avillach.dictionary.filter.FilterProcessor; import edu.harvard.dbmi.avillach.dictionary.legacysearch.model.LegacySearchQuery; import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Component; @@ -15,6 +15,11 @@ public class LegacySearchQueryMapper { private static final ObjectMapper objectMapper = new ObjectMapper(); + private final FilterProcessor filterProcessor; + + public LegacySearchQueryMapper(FilterProcessor filterProcessor) { + this.filterProcessor = filterProcessor; + } public LegacySearchQuery mapFromJson(String jsonString) throws IOException { JsonNode rootNode = objectMapper.readTree(jsonString); @@ -22,7 +27,7 @@ public LegacySearchQuery mapFromJson(String jsonString) throws IOException { String searchTerm = queryNode.get("searchTerm").asText(); int limit = queryNode.get("limit").asInt(); - Filter filter = FilterPreProcessor.processsFilter(new Filter(List.of(), searchTerm, List.of())); + Filter filter = filterProcessor.processsFilter(new Filter(List.of(), searchTerm, List.of())); return new LegacySearchQuery(filter, PageRequest.of(0, limit)); } diff --git a/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/MetadataResultSetUtil.java b/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/MetadataResultSetUtil.java index 8348f37..bc8eea2 100644 --- a/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/MetadataResultSetUtil.java +++ b/src/main/java/edu/harvard/dbmi/avillach/dictionary/legacysearch/MetadataResultSetUtil.java @@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; @@ -49,8 +50,6 @@ public Result mapContinuousMetadata(ResultSet rs) throws SQLException { ); } - - public Result mapCategoricalMetadata(ResultSet rs) throws SQLException { String hashedVarId = hashVarId(rs.getString("conceptPath")); String description = getDescription(rs); @@ -96,15 +95,15 @@ private static String bytesToHex(byte[] hash) { return hexString.toString(); } - private static String getParentDisplay(ResultSet rs) throws SQLException { - return rs.getString("parentDisplay") == null || rs.getString("parentDisplay").isBlank() ? "" : rs.getString("parentDisplay"); + private String getParentDisplay(ResultSet rs) throws SQLException { + return StringUtils.hasLength("parentDisplay") ? "" : rs.getString("parentDisplay"); } - private static String getParentName(ResultSet rs) throws SQLException { - return rs.getString("parentName") == null || rs.getString("parentName").isBlank() ? "All Variables" : rs.getString("parentName"); + private String getParentName(ResultSet rs) throws SQLException { + return StringUtils.hasLength(rs.getString("parentName")) ? "All Variables" : rs.getString("parentName"); } - private static String getDescription(ResultSet rs) throws SQLException { - return rs.getString("description") == null || rs.getString("description").isBlank() ? "" : rs.getString("description"); + private String getDescription(ResultSet rs) throws SQLException { + return StringUtils.hasLength(rs.getString("description")) ? "" : rs.getString("description"); } }