diff --git a/model/swagger.yml b/model/swagger.yml index 90bf1f7b..c515bc0e 100644 --- a/model/swagger.yml +++ b/model/swagger.yml @@ -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: @@ -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: diff --git a/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaProductsTransmuter.java b/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaProductsTransmuter.java index 15d94bad..1b8c3f45 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaProductsTransmuter.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaProductsTransmuter.java @@ -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; @@ -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 getRequest() { return Optional.empty(); @@ -112,4 +126,58 @@ public ResponseEntity selectByLidvidLatest(String identifier, return this.processs(new Standard(), this.uriParametersBuilder.setIdentifier(identifier) .setFields(fields).setVersion(ProductVersionSelector.LATEST).build()); } + + @Override + public ResponseEntity> 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 displayTypesByDbType = + Map.of( + "keyword", "string", + "text", "string", + "date", "timestamp", + "integer", "integer", + "long", "integer", + "float", "float", + "double", "float"); + + List 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); + } + } } diff --git a/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaTransmuter.java b/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaTransmuter.java index 5a44bc7b..b594478c 100644 --- a/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaTransmuter.java +++ b/service/src/main/java/gov/nasa/pds/api/registry/controller/SwaggerJavaTransmuter.java @@ -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; @@ -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); @@ -430,9 +431,16 @@ public ResponseEntity classMembersMembersVers(String propertyClass, Stri @Override public ResponseEntity classMembersVers(String propertyClass, String identifier, - String versions, @Valid List fields, @Min(0) @Valid Integer limit, - @Valid List sort, @Min(0) @Valid Integer start) { + String versions, @Valid List fields, @Min(0) @Valid Integer limit, + @Valid List sort, @Min(0) @Valid Integer start) { // TODO Auto-generated method stub return super.classMembersVers(propertyClass, identifier, versions, fields, limit, sort, start); } + + @Override + public ResponseEntity> productPropertiesList() { + // TODO Auto-generated method stub + return super.productPropertiesList(); + } + }