Skip to content

Commit

Permalink
Lint things
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Oct 11, 2024
1 parent e749611 commit de8397f
Show file tree
Hide file tree
Showing 40 changed files with 664 additions and 851 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootApplication
public class DictionaryApplication {

public static void main(String[] args) {
SpringApplication.run(DictionaryApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(DictionaryApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,19 @@ public class ConceptController {
private Integer MAX_DEPTH;


public ConceptController(
@Autowired ConceptService conceptService
) {
public ConceptController(@Autowired ConceptService conceptService) {
this.conceptService = conceptService;
}


@PostMapping(path = "/concepts")
public ResponseEntity<Page<Concept>> listConcepts(
@RequestBody Filter filter,
@RequestParam(name = "page_number", defaultValue = "0", required = false) int page,
@RequestBody Filter filter, @RequestParam(name = "page_number", defaultValue = "0", required = false) int page,
@RequestParam(name = "page_size", defaultValue = "10", required = false) int size
) {
PageRequest pagination = PageRequest.of(page, size);
PageImpl<Concept> pageResp = new PageImpl<>(
conceptService.listConcepts(filter, pagination),
pagination,
conceptService.countConcepts(filter)
);
PageImpl<Concept> pageResp =
new PageImpl<>(conceptService.listConcepts(filter, pagination), pagination, conceptService.countConcepts(filter));

return ResponseEntity.ok(pageResp);
}
Expand All @@ -52,35 +46,26 @@ public ResponseEntity<Page<Concept>> dumpConcepts(
) {
PageRequest pagination = PageRequest.of(page, size);
PageImpl<Concept> pageResp = new PageImpl<>(
conceptService.listDetailedConcepts(new Filter(List.of(), "", List.of()), pagination),
pagination,
conceptService.listDetailedConcepts(new Filter(List.of(), "", List.of()), pagination), pagination,
conceptService.countConcepts(new Filter(List.of(), "", List.of()))
);

return ResponseEntity.ok(pageResp);
}

@PostMapping(path = "/concepts/detail/{dataset}")
public ResponseEntity<Concept> conceptDetail(
@PathVariable(name = "dataset") String dataset,
@RequestBody() String conceptPath
) {
return conceptService.conceptDetail(dataset, conceptPath)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
public ResponseEntity<Concept> conceptDetail(@PathVariable(name = "dataset") String dataset, @RequestBody() String conceptPath) {
return conceptService.conceptDetail(dataset, conceptPath).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}

@PostMapping(path = "/concepts/tree/{dataset}")
public ResponseEntity<Concept> conceptTree(
@PathVariable(name = "dataset") String dataset,
@RequestBody() String conceptPath,
@PathVariable(name = "dataset") String dataset, @RequestBody() String conceptPath,
@RequestParam(name = "depth", required = false, defaultValue = "2") Integer depth
) {
if (depth < 0 || depth > MAX_DEPTH) {
return ResponseEntity.badRequest().build();
}
return conceptService.conceptTree(dataset, conceptPath, depth)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
return conceptService.conceptTree(dataset, conceptPath, depth).map(ResponseEntity::ok).orElse(ResponseEntity.notFound().build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,35 @@ AND concept_node_meta.KEY IN (:disallowed_meta_keys)
""";

private static final String CONSENT_QUERY = """
dataset.dataset_id IN (
SELECT
consent.dataset_id
FROM consent
LEFT JOIN dataset ON dataset.dataset_id = consent.dataset_id
WHERE
concat(dataset.ref, '.', consent.consent_code) IN (:consents) OR
(dataset.ref IN (:consents) AND consent.consent_code = '')
UNION
SELECT
dataset_harmonization.harmonized_dataset_id
FROM consent
JOIN dataset_harmonization ON dataset_harmonization.source_dataset_id = consent.dataset_id
LEFT JOIN dataset ON dataset.dataset_id = dataset_harmonization.source_dataset_id
WHERE
concat(dataset.ref, '.', consent.consent_code) IN (:consents) OR
(dataset.ref IN (:consents) AND consent.consent_code = '')
) AND
""";
dataset.dataset_id IN (
SELECT
consent.dataset_id
FROM consent
LEFT JOIN dataset ON dataset.dataset_id = consent.dataset_id
WHERE
concat(dataset.ref, '.', consent.consent_code) IN (:consents) OR
(dataset.ref IN (:consents) AND consent.consent_code = '')
UNION
SELECT
dataset_harmonization.harmonized_dataset_id
FROM consent
JOIN dataset_harmonization ON dataset_harmonization.source_dataset_id = consent.dataset_id
LEFT JOIN dataset ON dataset.dataset_id = dataset_harmonization.source_dataset_id
WHERE
concat(dataset.ref, '.', consent.consent_code) IN (:consents) OR
(dataset.ref IN (:consents) AND consent.consent_code = '')
) AND
""";

@Autowired
public ConceptFilterQueryGenerator(
@Value("${filtering.unfilterable_concepts}") List<String> disallowedMetaFields
) {
public ConceptFilterQueryGenerator(@Value("${filtering.unfilterable_concepts}") List<String> disallowedMetaFields) {
this.disallowedMetaFields = disallowedMetaFields;
}

/**
* This generates a query that will return a list of concept_node IDs for the given filter.
* <p>
* A filter object has a list of facets, each belonging to a category. Within a category,
* facets are ORed together. Between categories, facets are ANDed together.
* In SQL, this is represented as N clauses for N facets, all INTERSECTed together. Search
* also acts as its own special facet here.
* This generates a query that will return a list of concept_node IDs for the given filter. <p> A filter object has a list of facets,
* each belonging to a category. Within a category, facets are ORed together. Between categories, facets are ANDed together. In SQL,
* this is represented as N clauses for N facets, all INTERSECTed together. Search also acts as its own special facet here.
*
* @param filter universal filter object for the page
* @param pageable pagination, if applicable
Expand Down Expand Up @@ -113,8 +108,7 @@ ORDER BY max((1 + rank) * coalesce(rank_adjustment, 1)) DESC, q.concept_node_id
LIMIT :limit
OFFSET :offset
""";
params.addValue("limit", pageable.getPageSize())
.addValue("offset", pageable.getOffset());
params.addValue("limit", pageable.getPageSize()).addValue("offset", pageable.getOffset());
}

superQuery = " concepts_filtered_sorted AS (\n" + superQuery + "\n)";
Expand Down Expand Up @@ -150,26 +144,26 @@ private String createValuelessNodeFilter(String search, List<String> consents) {
continuous_max.value <> '' OR
categorical_values.value <> ''
)
""".formatted(rankQuery, rankWhere, consentWhere);
"""
.formatted(rankQuery, rankWhere, consentWhere);
}

private List<String> createFacetFilter(Filter filter, MapSqlParameterSource params) {
String consentWhere = CollectionUtils.isEmpty(filter.consents()) ? "" : CONSENT_QUERY;
return filter.facets().stream()
.collect(Collectors.groupingBy(Facet::category))
.entrySet().stream()
.map(facetsForCategory -> {
params
// The templating here is to namespace the params for each facet category in the query
.addValue("facets_for_category_%s".formatted(facetsForCategory.getKey()), facetsForCategory.getValue().stream().map(Facet::name).toList())
.addValue("category_%s".formatted(facetsForCategory.getKey()), facetsForCategory.getKey());
String rankQuery = "0";
String rankWhere = "";
if (StringUtils.hasLength(filter.search())) {
rankQuery = "ts_rank(searchable_fields, (phraseto_tsquery(:search)::text || ':*')::tsquery)";
rankWhere = "concept_node.searchable_fields @@ (phraseto_tsquery(:search)::text || ':*')::tsquery AND";
}
return """
return filter.facets().stream().collect(Collectors.groupingBy(Facet::category)).entrySet().stream().map(facetsForCategory -> {
params
// The templating here is to namespace the params for each facet category in the query
.addValue(
"facets_for_category_%s".formatted(facetsForCategory.getKey()),
facetsForCategory.getValue().stream().map(Facet::name).toList()
).addValue("category_%s".formatted(facetsForCategory.getKey()), facetsForCategory.getKey());
String rankQuery = "0";
String rankWhere = "";
if (StringUtils.hasLength(filter.search())) {
rankQuery = "ts_rank(searchable_fields, (phraseto_tsquery(:search)::text || ':*')::tsquery)";
rankWhere = "concept_node.searchable_fields @@ (phraseto_tsquery(:search)::text || ':*')::tsquery AND";
}
return """
(
SELECT
facet__concept_node.concept_node_id AS concept_node_id,
Expand All @@ -187,8 +181,7 @@ facet.name IN (:facets_for_category_%s ) AND facet_category.name = :category_%s
facet__concept_node.concept_node_id
)
""".formatted(rankQuery, rankWhere, consentWhere, facetsForCategory.getKey(), facetsForCategory.getKey());
})
.toList();
}).toList();
}

}
Loading

0 comments on commit de8397f

Please sign in to comment.