Skip to content

Commit 58dd370

Browse files
authored
Merge pull request #116 from davidblasby/_ogcapi_records_search
Ogcapi records search
2 parents a254360 + 9209137 commit 58dd370

16 files changed

+1682
-98
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ build/
5757

5858
### VS Code ###
5959
.vscode/
60+
61+
## OS X
62+
.DS_Store

gn_checks.xml

+337
Large diffs are not rendered by default.

modules/services/ogc-api-records/src/main/java/org/fao/geonet/ogcapi/records/controller/ItemApiController.java

+34-49
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

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

8-
import com.fasterxml.jackson.core.JsonFactory;
9-
import com.fasterxml.jackson.core.JsonParser;
108
import com.fasterxml.jackson.databind.JsonNode;
11-
import com.fasterxml.jackson.databind.ObjectMapper;
129
import io.swagger.annotations.Api;
1310
import io.swagger.annotations.ApiParam;
1411
import io.swagger.v3.oas.annotations.responses.ApiResponse;
@@ -54,6 +51,7 @@
5451
import org.fao.geonet.ogcapi.records.model.Item;
5552
import org.fao.geonet.ogcapi.records.model.XsltModel;
5653
import org.fao.geonet.ogcapi.records.service.CollectionService;
54+
import org.fao.geonet.ogcapi.records.service.QueryBuilder;
5755
import org.fao.geonet.ogcapi.records.service.RecordService;
5856
import org.fao.geonet.ogcapi.records.util.MediaTypeUtil;
5957
import org.fao.geonet.ogcapi.records.util.RecordsEsQueryBuilder;
@@ -87,6 +85,7 @@
8785
@Slf4j(topic = "org.fao.geonet.ogcapi.records")
8886
public class ItemApiController {
8987

88+
9089
public static final String EXCEPTION_COLLECTION_NOT_FOUND =
9190
"ogcapir.exception.collection.notFound";
9291
public static final String EXCEPTION_COLLECTION_ITEM_NOT_FOUND =
@@ -114,10 +113,11 @@ public class ItemApiController {
114113
DcatConverter dcatConverter;
115114
@Autowired
116115
RecordService recordService;
116+
@Autowired
117+
QueryBuilder queryBuilder;
117118

118119
/**
119120
* Describe a collection item.
120-
*
121121
*/
122122
@io.swagger.v3.oas.annotations.Operation(
123123
summary = "Describe a collection item.",
@@ -143,7 +143,7 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
143143
@ApiParam(value = "Identifier (name) of a specific collection", required = true)
144144
@PathVariable("collectionId") String collectionId,
145145
@ApiParam(value = "Identifier (name) of a specific record", required = true)
146-
@PathVariable("recordId")String recordId,
146+
@PathVariable("recordId") String recordId,
147147
@ApiIgnore HttpServletRequest request,
148148
@ApiIgnore HttpServletResponse response,
149149
@ApiIgnore Model model) {
@@ -165,7 +165,6 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
165165
MediaType mediaType =
166166
mediaTypeUtil.calculatePriorityMediaTypeFromRequest(request, allowedMediaTypes);
167167

168-
169168
if (mediaType.equals(MediaType.APPLICATION_JSON)
170169
|| mediaType.equals(GnMediaType.APPLICATION_GEOJSON)) {
171170
try {
@@ -201,7 +200,6 @@ public ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGet(
201200

202201
/**
203202
* Describe the collection items.
204-
*
205203
*/
206204
@io.swagger.v3.oas.annotations.Operation(
207205
summary = "Describe the collection items.",
@@ -248,13 +246,27 @@ public ResponseEntity<Void> collectionsCollectionIdItemsGet(
248246
@ApiParam(value = "")
249247
@RequestParam(value = "externalids", required = false)
250248
List<String> externalids,
249+
@RequestParam(value = "ids", required = false)
250+
List<String> ids,
251251
@ApiParam(value = "")
252252
@RequestParam(value = "sortby", required = false)
253253
List<String> sortby,
254254
@ApiIgnore HttpServletRequest request,
255255
@ApiIgnore HttpServletResponse response,
256256
@ApiIgnore Model model) throws Exception {
257257

258+
var query = queryBuilder.buildFromRequest(collectionId,
259+
bbox,
260+
datetime,
261+
limit,
262+
startindex,
263+
type,
264+
q,
265+
ids,
266+
externalids,
267+
sortby,
268+
request.getParameterMap()
269+
);
258270
List<MediaType> allowedMediaTypes =
259271
ListUtils.union(MediaTypeUtil.defaultSupportedMediaTypes,
260272
Arrays.asList(
@@ -276,15 +288,14 @@ public ResponseEntity<Void> collectionsCollectionIdItemsGet(
276288

277289
boolean allSourceFields =
278290
mediaType.equals(GnMediaType.APPLICATION_DCAT2_XML)
279-
|| mediaType.equals(GnMediaType.APPLICATION_RDF_XML);
291+
|| mediaType.equals(GnMediaType.APPLICATION_RDF_XML);
280292

281293
return collectionsCollectionIdItemsGetInternal(
282-
collectionId, bbox, datetime, limit, startindex, type, q, externalids, sortby,
294+
query,
283295
request, response, allSourceFields);
284296

285297
} else {
286-
return collectionsCollectionIdItemsGetAsHtml(collectionId, bbox, datetime, limit,
287-
startindex, type, q, externalids, sortby, request, response, model);
298+
return collectionsCollectionIdItemsGetAsHtml(query, request, response, model);
288299
}
289300
}
290301

@@ -473,7 +484,7 @@ private ResponseEntity<Void> collectionsCollectionIdItemsRecordIdGetAsHtml(
473484
private List<String> setDefaultRssSortBy(List<String> sortby, HttpServletRequest request) {
474485
boolean isRss = "rss".equals(request.getParameter("f"))
475486
|| (request.getHeader(HttpHeaders.ACCEPT) != null
476-
&& request.getHeader(HttpHeaders.ACCEPT).contains(MediaType.APPLICATION_RSS_XML_VALUE));
487+
&& request.getHeader(HttpHeaders.ACCEPT).contains(MediaType.APPLICATION_RSS_XML_VALUE));
477488
if (isRss
478489
&& (sortby == null || sortby.isEmpty())) {
479490
sortby = new ArrayList<>();
@@ -484,27 +495,18 @@ private List<String> setDefaultRssSortBy(List<String> sortby, HttpServletRequest
484495

485496

486497
private String search(
487-
String collectionId,
488-
List<BigDecimal> bbox,
489-
String datetime,
490-
Integer limit,
491-
Integer startindex,
492-
String type,
493-
List<String> q,
494-
List<String> externalids,
495-
List<String> sortby,
498+
Query requestQuery,
496499
HttpServletRequest request, boolean allSourceFields) {
497500

498-
Source source = collectionService.retrieveSourceForCollection(collectionId);
501+
Source source = collectionService.retrieveSourceForCollection(requestQuery.getCollectionId());
499502

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

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

520522

521523
private ResponseEntity<Void> collectionsCollectionIdItemsGetInternal(
522-
String collectionId,
523-
List<BigDecimal> bbox,
524-
String datetime,
525-
Integer limit,
526-
Integer startindex,
527-
String type,
528-
List<String> q,
529-
List<String> externalids,
530-
List<String> sortby,
524+
Query query,
531525
HttpServletRequest request,
532526
HttpServletResponse response,
533527
boolean allSourceFields) {
534528

535-
sortby = setDefaultRssSortBy(sortby, request);
529+
query.setSortBy(setDefaultRssSortBy(query.getSortBy(), request));
536530

537-
String queryResponse = search(collectionId, bbox, datetime, limit, startindex, type, q,
538-
externalids, sortby, request, allSourceFields);
531+
String queryResponse = search(query, request, allSourceFields);
539532

540533
try {
541534
streamResult(response, queryResponse, getResponseContentType(request));
@@ -551,30 +544,22 @@ private ResponseEntity<Void> collectionsCollectionIdItemsGetInternal(
551544
* Collection items as HTML.
552545
*/
553546
private ResponseEntity<Void> collectionsCollectionIdItemsGetAsHtml(
554-
String collectionId,
555-
List<BigDecimal> bbox,
556-
String datetime,
557-
Integer limit,
558-
Integer startindex,
559-
String type,
560-
List<String> q,
561-
List<String> externalids,
562-
List<String> sortby,
547+
Query requestQuery,
563548
HttpServletRequest request,
564549
HttpServletResponse response,
565550
Model model) throws Exception {
566551

567552
Locale locale = LocaleContextHolder.getLocale();
568553
String language = locale.getISO3Language();
569-
Source source = collectionService.retrieveSourceForCollection(collectionId);
554+
Source source = collectionService.retrieveSourceForCollection(requestQuery.getCollectionId());
570555

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

575560
String collectionFilter = collectionService.retrieveCollectionFilter(source, false);
576561
String query = recordsEsQueryBuilder
577-
.buildQuery(q, externalids, bbox, startindex, limit, collectionFilter, sortby, null);
562+
.buildQuery(requestQuery, collectionFilter, null);
578563

579564
EsSearchResults results = new EsSearchResults();
580565
try {
@@ -588,10 +573,10 @@ private ResponseEntity<Void> collectionsCollectionIdItemsGetAsHtml(
588573
XsltModel modelSource = new XsltModel();
589574
Map<String, String[]> parameterMap = new HashMap<>(request.getParameterMap());
590575
if (request.getParameter("limit") == null) {
591-
parameterMap.put("limit", new String[]{limit + ""});
576+
parameterMap.put("limit", new String[]{requestQuery.getLimit() + ""});
592577
}
593578
if (request.getParameter("startindex") == null) {
594-
parameterMap.put("startindex", new String[]{startindex + ""});
579+
parameterMap.put("startindex", new String[]{requestQuery.getStartIndex() + ""});
595580
}
596581
modelSource.setRequestParameters(parameterMap);
597582
modelSource.setCollection(source);

0 commit comments

Comments
 (0)