Skip to content

Commit

Permalink
search via queryables
Browse files Browse the repository at this point in the history
  • Loading branch information
david-blasby committed Oct 9, 2024
1 parent 1eb1bc9 commit 1259263
Show file tree
Hide file tree
Showing 16 changed files with 1,677 additions and 98 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ build/

### VS Code ###
.vscode/

## OS X
.DS_Store
332 changes: 332 additions & 0 deletions gn_checks.xml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

package org.fao.geonet.ogcapi.records.controller;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -54,6 +51,7 @@
import org.fao.geonet.ogcapi.records.model.Item;
import org.fao.geonet.ogcapi.records.model.XsltModel;
import org.fao.geonet.ogcapi.records.service.CollectionService;
import org.fao.geonet.ogcapi.records.service.QueryBuilder;
import org.fao.geonet.ogcapi.records.service.RecordService;
import org.fao.geonet.ogcapi.records.util.MediaTypeUtil;
import org.fao.geonet.ogcapi.records.util.RecordsEsQueryBuilder;
Expand Down Expand Up @@ -87,6 +85,7 @@
@Slf4j(topic = "org.fao.geonet.ogcapi.records")
public class ItemApiController {


public static final String EXCEPTION_COLLECTION_NOT_FOUND =
"ogcapir.exception.collection.notFound";
public static final String EXCEPTION_COLLECTION_ITEM_NOT_FOUND =
Expand Down Expand Up @@ -114,10 +113,11 @@ public class ItemApiController {
DcatConverter dcatConverter;
@Autowired
RecordService recordService;
@Autowired
QueryBuilder queryBuilder;

/**
* Describe a collection item.
*
*/
@io.swagger.v3.oas.annotations.Operation(
summary = "Describe a collection item.",
Expand All @@ -143,7 +143,7 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
@ApiParam(value = "Identifier (name) of a specific collection", required = true)
@PathVariable("collectionId") String collectionId,
@ApiParam(value = "Identifier (name) of a specific record", required = true)
@PathVariable("recordId")String recordId,
@PathVariable("recordId") String recordId,
@ApiIgnore HttpServletRequest request,
@ApiIgnore HttpServletResponse response,
@ApiIgnore Model model) {
Expand All @@ -165,7 +165,6 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
MediaType mediaType =
mediaTypeUtil.calculatePriorityMediaTypeFromRequest(request, allowedMediaTypes);


if (mediaType.equals(MediaType.APPLICATION_JSON)
|| mediaType.equals(GnMediaType.APPLICATION_GEOJSON)) {
try {
Expand Down Expand Up @@ -201,7 +200,6 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(

/**
* Describe the collection items.
*
*/
@io.swagger.v3.oas.annotations.Operation(
summary = "Describe the collection items.",
Expand Down Expand Up @@ -248,13 +246,27 @@ public ResponseEntity<Void> collectionsCollectionIdItemsGet(
@ApiParam(value = "")
@RequestParam(value = "externalids", required = false)
List<String> externalids,
@RequestParam(value = "ids", required = false)
List<String> ids,
@ApiParam(value = "")
@RequestParam(value = "sortby", required = false)
List<String> sortby,
@ApiIgnore HttpServletRequest request,
@ApiIgnore HttpServletResponse response,
@ApiIgnore Model model) throws Exception {

var query = queryBuilder.buildFromRequest(collectionId,
bbox,
datetime,
limit,
startindex,
type,
q,
ids,
externalids,
sortby,
request.getParameterMap()
);
List<MediaType> allowedMediaTypes =
ListUtils.union(MediaTypeUtil.defaultSupportedMediaTypes,
Arrays.asList(
Expand All @@ -276,15 +288,14 @@ public ResponseEntity<Void> collectionsCollectionIdItemsGet(

boolean allSourceFields =
mediaType.equals(GnMediaType.APPLICATION_DCAT2_XML)
|| mediaType.equals(GnMediaType.APPLICATION_RDF_XML);
|| mediaType.equals(GnMediaType.APPLICATION_RDF_XML);

return collectionsCollectionIdItemsGetInternal(
collectionId, bbox, datetime, limit, startindex, type, q, externalids, sortby,
query,
request, response, allSourceFields);

} else {
return collectionsCollectionIdItemsGetAsHtml(collectionId, bbox, datetime, limit,
startindex, type, q, externalids, sortby, request, response, model);
return collectionsCollectionIdItemsGetAsHtml(query, request, response, model);
}
}

Expand Down Expand Up @@ -473,7 +484,7 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsHtml(
private List<String> setDefaultRssSortBy(List<String> sortby, HttpServletRequest request) {
boolean isRss = "rss".equals(request.getParameter("f"))
|| (request.getHeader(HttpHeaders.ACCEPT) != null
&& request.getHeader(HttpHeaders.ACCEPT).contains(MediaType.APPLICATION_RSS_XML_VALUE));
&& request.getHeader(HttpHeaders.ACCEPT).contains(MediaType.APPLICATION_RSS_XML_VALUE));
if (isRss
&& (sortby == null || sortby.isEmpty())) {
sortby = new ArrayList<>();
Expand All @@ -484,27 +495,18 @@ private List<String> setDefaultRssSortBy(List<String> sortby, HttpServletRequest


private String search(
String collectionId,
List<BigDecimal> bbox,
String datetime,
Integer limit,
Integer startindex,
String type,
List<String> q,
List<String> externalids,
List<String> sortby,
Query requestQuery,
HttpServletRequest request, boolean allSourceFields) {

Source source = collectionService.retrieveSourceForCollection(collectionId);
Source source = collectionService.retrieveSourceForCollection(requestQuery.getCollectionId());

if (source == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find collection");
}

String collectionFilter = collectionService.retrieveCollectionFilter(source, false);
String query = recordsEsQueryBuilder
.buildQuery(q, externalids, bbox,
startindex, limit, collectionFilter, sortby,
.buildQuery(requestQuery, collectionFilter,
allSourceFields ? Set.of("*") : null);
try {
return proxy.searchAndGetResult(request.getSession(), request, query, null);
Expand All @@ -519,23 +521,14 @@ private String search(


private ResponseEntity<Void> collectionsCollectionIdItemsGetInternal(
String collectionId,
List<BigDecimal> bbox,
String datetime,
Integer limit,
Integer startindex,
String type,
List<String> q,
List<String> externalids,
List<String> sortby,
Query query,
HttpServletRequest request,
HttpServletResponse response,
boolean allSourceFields) {

sortby = setDefaultRssSortBy(sortby, request);
query.setSortBy(setDefaultRssSortBy(query.getSortBy(), request));

String queryResponse = search(collectionId, bbox, datetime, limit, startindex, type, q,
externalids, sortby, request, allSourceFields);
String queryResponse = search(query, request, allSourceFields);

try {
streamResult(response, queryResponse, getResponseContentType(request));
Expand All @@ -551,30 +544,22 @@ private ResponseEntity<Void> collectionsCollectionIdItemsGetInternal(
* Collection items as HTML.
*/
private ResponseEntity<Void> collectionsCollectionIdItemsGetAsHtml(
String collectionId,
List<BigDecimal> bbox,
String datetime,
Integer limit,
Integer startindex,
String type,
List<String> q,
List<String> externalids,
List<String> sortby,
Query requestQuery,
HttpServletRequest request,
HttpServletResponse response,
Model model) throws Exception {

Locale locale = LocaleContextHolder.getLocale();
String language = locale.getISO3Language();
Source source = collectionService.retrieveSourceForCollection(collectionId);
Source source = collectionService.retrieveSourceForCollection(requestQuery.getCollectionId());

if (source == null) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Unable to find collection");
}

String collectionFilter = collectionService.retrieveCollectionFilter(source, false);
String query = recordsEsQueryBuilder
.buildQuery(q, externalids, bbox, startindex, limit, collectionFilter, sortby, null);
.buildQuery(requestQuery, collectionFilter, null);

EsSearchResults results = new EsSearchResults();
try {
Expand All @@ -588,10 +573,10 @@ private ResponseEntity<Void> collectionsCollectionIdItemsGetAsHtml(
XsltModel modelSource = new XsltModel();
Map<String, String[]> parameterMap = new HashMap<>(request.getParameterMap());
if (request.getParameter("limit") == null) {
parameterMap.put("limit", new String[]{limit + ""});
parameterMap.put("limit", new String[]{requestQuery.getLimit() + ""});
}
if (request.getParameter("startindex") == null) {
parameterMap.put("startindex", new String[]{startindex + ""});
parameterMap.put("startindex", new String[]{requestQuery.getStartIndex() + ""});
}
modelSource.setRequestParameters(parameterMap);
modelSource.setCollection(source);
Expand Down
Loading

0 comments on commit 1259263

Please sign in to comment.