Skip to content

Commit

Permalink
implement /product-properties endpoint (#322)
Browse files Browse the repository at this point in the history
* implement /properties endpoint
  • Loading branch information
alexdunnjpl authored May 1, 2023
1 parent f604e07 commit 021c9fd
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
40 changes: 40 additions & 0 deletions model/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,26 @@ paths:
- $ref: "#/components/parameters/Sort"
- $ref: "#/components/parameters/Start"

/properties:
get:
tags:
- 1. all products
summary: |
return a list of all possible searchable metadata fields for products published in the registry
operationId: product-properties-list
responses:
'200':
$ref: "#/components/responses/PropertiesList"
'400':
$ref: "#/components/responses/Error"
'404':
$ref: "#/components/responses/Error"
'500':
$ref: "#/components/responses/Error"
'501':
$ref: "#/components/responses/Error"
parameters: []

/products/{identifier}:
get:
tags:
Expand Down Expand Up @@ -1284,6 +1304,26 @@ components:
text/xml:
schema:
$ref: '#/components/schemas/pdsProducts'
PropertiesList:
description: An array of all searchable properties of products in the registry.
content:
"*":
schema:
type: array
items:
type: object
properties:
property:
type: string
type:
type: string
enum:
- "string"
- "integer"
- "float"
- "boolean"
- "timestamp"
- "unsupported"
Singular:
description: Successful request
content:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package gov.nasa.pds.api.registry.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import gov.nasa.pds.api.base.PropertiesApi;
import gov.nasa.pds.model.ProductPropertiesList200ResponseInner;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.client.indices.GetIndexRequest;
import org.opensearch.client.indices.GetIndexResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.NativeWebRequest;
import gov.nasa.pds.api.base.ClassesApi;
Expand All @@ -12,7 +26,7 @@
import gov.nasa.pds.api.registry.model.ProductVersionSelector;

abstract class SwaggerJavaProductsTransmuter extends SwaggerJavaClassesTransmuter
implements ControlContext, ProductsApi, ClassesApi {
implements ControlContext, ProductsApi, ClassesApi, PropertiesApi {

public Optional<NativeWebRequest> getRequest() {
return Optional.empty();
Expand Down Expand Up @@ -112,4 +126,58 @@ public ResponseEntity<Object> selectByLidvidLatest(String identifier,
return this.processs(new Standard(), this.uriParametersBuilder.setIdentifier(identifier)
.setFields(fields).setVersion(ProductVersionSelector.LATEST).build());
}

@Override
public ResponseEntity<List<ProductPropertiesList200ResponseInner>> productPropertiesList() {

try {
String registryIndexName = this.getConnection().getRegistryIndex();

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);

GetIndexRequest req = new GetIndexRequest(registryIndexName);
RestHighLevelClient client = this.getConnection().getRestHighLevelClient();
GetIndexResponse response = client.indices().get(req, RequestOptions.DEFAULT);

JsonNode content =
mapper
.valueToTree(response.getMappings().get(registryIndexName).getSourceAsMap())
.get("properties");

Map<String, String> displayTypesByDbType =
Map.of(
"keyword", "string",
"text", "string",
"date", "timestamp",
"integer", "integer",
"long", "integer",
"float", "float",
"double", "float");

List<ProductPropertiesList200ResponseInner> results = new ArrayList<>();
content
.fieldNames()
.forEachRemaining(
(String propertyName) -> {
ProductPropertiesList200ResponseInner propertyElement =
new ProductPropertiesList200ResponseInner();

propertyElement.setProperty(propertyName);

String rawType = content.get(propertyName).get("type").asText();
String displayType = displayTypesByDbType.getOrDefault(rawType, "unsupported");
ProductPropertiesList200ResponseInner.TypeEnum enumType =
ProductPropertiesList200ResponseInner.TypeEnum.fromValue(displayType);
propertyElement.setType(enumType);

results.add(propertyElement);
});

return new ResponseEntity<>(results, HttpStatus.OK);
} catch (IOException err) {
log.error("SwaggerJavaProductsTransmuter.productPropertiesList() failed", err);
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.util.List;
import gov.nasa.pds.api.base.PropertiesApi;
import gov.nasa.pds.model.ProductPropertiesList200ResponseInner;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import org.antlr.v4.runtime.NoViableAltException;
Expand All @@ -26,12 +28,11 @@
import gov.nasa.pds.api.registry.exceptions.NothingFoundException;
import gov.nasa.pds.api.registry.exceptions.UnknownGroupNameException;
import gov.nasa.pds.api.registry.model.ErrorMessageFactory;
import gov.nasa.pds.model.ErrorMessage;
import gov.nasa.pds.api.registry.model.identifiers.LidVidUtils;

@Controller
public class SwaggerJavaTransmuter extends SwaggerJavaDeprecatedTransmuter
implements ControlContext, BundlesApi, CollectionsApi, ClassesApi, ProductsApi {
implements ControlContext, BundlesApi, CollectionsApi, ClassesApi, ProductsApi, PropertiesApi {


private static final Logger log = LoggerFactory.getLogger(SwaggerJavaTransmuter.class);
Expand Down Expand Up @@ -430,9 +431,16 @@ public ResponseEntity<Object> classMembersMembersVers(String propertyClass, Stri

@Override
public ResponseEntity<Object> classMembersVers(String propertyClass, String identifier,
String versions, @Valid List<String> fields, @Min(0) @Valid Integer limit,
@Valid List<String> sort, @Min(0) @Valid Integer start) {
String versions, @Valid List<String> fields, @Min(0) @Valid Integer limit,
@Valid List<String> sort, @Min(0) @Valid Integer start) {
// TODO Auto-generated method stub
return super.classMembersVers(propertyClass, identifier, versions, fields, limit, sort, start);
}

@Override
public ResponseEntity<List<ProductPropertiesList200ResponseInner>> productPropertiesList() {
// TODO Auto-generated method stub
return super.productPropertiesList();
}

}

0 comments on commit 021c9fd

Please sign in to comment.