Skip to content

Commit

Permalink
Add FilterProcessor to handle filter processing logic
Browse files Browse the repository at this point in the history
Introduced a new FilterProcessor component to centralize filter processing logic. Enhanced getDescription, getParentName, and getParentDisplay methods in MetadataResultSetUtil for better validation using StringUtils.
  • Loading branch information
Gcolon021 committed Nov 15, 2024
1 parent edcd629 commit f7a47dd
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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<? extends HttpMessageConverter<?>> converterType) {
return true;
Expand All @@ -35,29 +41,12 @@ public Object afterBodyRead(
Class<? extends HttpMessageConverter<?>> converterType
) {
if (body instanceof Filter filter) {
return processsFilter(filter);
return filterProcessor.processsFilter(filter);
}
return body;
}

public static Filter processsFilter(Filter filter) {
List<Facet> newFacets = filter.facets();
List<String> 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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Facet> newFacets = filter.facets();
List<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,14 +15,19 @@
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);
JsonNode queryNode = rootNode.get("query");

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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
}
}

0 comments on commit f7a47dd

Please sign in to comment.