Skip to content

Commit

Permalink
remove redundant code in controllerAdvice. Add pagination support.
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas loubrieu committed May 1, 2024
1 parent 42be227 commit 05a6881
Showing 1 changed file with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import gov.nasa.pds.api.registry.ConnectionContext;
import gov.nasa.pds.api.registry.model.ErrorMessageFactory;
import gov.nasa.pds.api.registry.model.exceptions.AcceptFormatNotSupportedException;
import gov.nasa.pds.api.registry.model.exceptions.MissSortWithSearchAfterException;
import gov.nasa.pds.api.registry.model.exceptions.NotFoundException;
import gov.nasa.pds.api.registry.model.exceptions.UnhandledException;
import gov.nasa.pds.api.registry.model.api_responses.PdsProductBusinessObject;
Expand All @@ -50,6 +51,7 @@ public class ProductsController implements ProductsApi {
private final RegistrySearchRequestBuilder registrySearchRequestBuilder;
private final ErrorMessageFactory errorMessageFactory;
private final ObjectMapper objectMapper;
private OpenSearchClient openSearchClient;
private SearchRequest presetSearchRequest;

// TODO move that at a better place, it is not specific to this controller
Expand All @@ -60,6 +62,8 @@ static Map<String, Class<? extends ProductBusinessLogic>> getFormatters() {
return formatters;
}

static Integer DEFAULT_LIMIT = 100;

static {
// TODO move that at a better place, it is not specific to this controller
formatters.put("*", PdsProductBusinessObject.class);
Expand Down Expand Up @@ -90,6 +94,7 @@ public ProductsController(ConnectionContext connectionContext,
this.registrySearchRequestBuilder = new RegistrySearchRequestBuilder(connectionContext);


this.openSearchClient = this.connectionContext.getOpenSearchClient();

}

Expand Down Expand Up @@ -135,7 +140,7 @@ private ResponseEntity<Object> formatMultipleProducts(RawMultipleProductResponse

if (!ProductsController.formatters.containsKey(acceptHeaderValue)) {
throw new AcceptFormatNotSupportedException(
"format " + acceptHeaderValue + "is not supported.");
"format " + acceptHeaderValue + " is not supported.");
}

Class<? extends ProductBusinessLogic> formatterClass =
Expand Down Expand Up @@ -213,8 +218,8 @@ public ResponseEntity<Object> selectByLidvidLatest(String identifier, List<Strin

@Override
public ResponseEntity<Object> selectByLidvidAll(String identifier, List<String> fields,
Integer limit, List<String> sort, List<String> searchAfter)
throws UnhandledException, NotFoundException, AcceptFormatNotSupportedException {
Integer limit, List<String> sort, List<String> searchAfter) throws UnhandledException,
NotFoundException, AcceptFormatNotSupportedException, MissSortWithSearchAfterException {

RawMultipleProductResponse response;

Expand All @@ -225,6 +230,7 @@ public ResponseEntity<Object> selectByLidvidAll(String identifier, List<String>
response = new RawMultipleProductResponse(this.getLidVid(pdsIdentifier, fields));

} else {
limit = (limit == null) ? DEFAULT_LIMIT : limit;
response = this.getAllLidVid(pdsIdentifier, fields, limit, sort, searchAfter);
}

Expand All @@ -250,13 +256,11 @@ private HashMap<String, Object> getLidVid(PdsProductIdentifier identifier, List<

SearchRequest searchRequest = registrySearchRequestBuilder.addLidvidMatch(identifier).build();


OpenSearchClient client = this.connectionContext.getOpenSearchClient();

// useless to detail here that the HashMap is parameterized <String, Object>
// because of compilation features, see
// https://stackoverflow.com/questions/2390662/java-how-do-i-get-a-class-literal-from-a-generic-type
SearchResponse<HashMap> searchResponse = client.search(searchRequest, HashMap.class);
SearchResponse<HashMap> searchResponse =
this.openSearchClient.search(searchRequest, HashMap.class);
if (searchResponse.hits().total().value() == 0) {
throw new NotFoundException("No product found with identifier " + identifier.toString());
}
Expand All @@ -267,6 +271,7 @@ private HashMap<String, Object> getLidVid(PdsProductIdentifier identifier, List<
}


@SuppressWarnings("unchecked")
private HashMap<String, Object> getLatestLidVid(PdsProductIdentifier identifier,
List<String> fields) throws OpenSearchException, IOException, NotFoundException {

Expand All @@ -276,13 +281,11 @@ private HashMap<String, Object> getLatestLidVid(PdsProductIdentifier identifier,
SearchRequest searchRequest =
registrySearchRequestBuilder.addLidMatch(identifier).onlyLatest().build();


OpenSearchClient client = this.connectionContext.getOpenSearchClient();

// useless to detail here that the HashMap is parameterized <String, Object>
// because of compilation features, see
// https://stackoverflow.com/questions/2390662/java-how-do-i-get-a-class-literal-from-a-generic-type
SearchResponse<HashMap> searchResponse = client.search(searchRequest, HashMap.class);
SearchResponse<HashMap> searchResponse =
this.openSearchClient.search(searchRequest, HashMap.class);

if (searchResponse.hits().total().value() == 0) {
throw new NotFoundException("No product found with identifier " + identifier.toString());
Expand All @@ -296,20 +299,35 @@ private HashMap<String, Object> getLatestLidVid(PdsProductIdentifier identifier,

private RawMultipleProductResponse getAllLidVid(PdsProductIdentifier identifier,
List<String> fields, Integer limit, List<String> sort, List<String> searchAfter)
throws OpenSearchException, IOException, NotFoundException {
throws OpenSearchException, IOException, NotFoundException, MissSortWithSearchAfterException {

RegistrySearchRequestBuilder registrySearchRequestBuilder =
new RegistrySearchRequestBuilder(this.registrySearchRequestBuilder);

SearchRequest searchRequest = registrySearchRequestBuilder.addLidMatch(identifier).build();
registrySearchRequestBuilder = registrySearchRequestBuilder.addLidMatch(identifier);

if ((sort != null) && (!sort.isEmpty())) {
registrySearchRequestBuilder.sort(sort);
}

registrySearchRequestBuilder.size(limit);

if ((searchAfter != null) && (!searchAfter.isEmpty())) {
if ((sort == null) || (sort.isEmpty())) {
throw new MissSortWithSearchAfterException();
}
registrySearchRequestBuilder.searchAfter(searchAfter);
}



OpenSearchClient client = this.connectionContext.getOpenSearchClient();
SearchRequest searchRequest = registrySearchRequestBuilder.build();

// useless to detail here that the HashMap is parameterized <String, Object>
// because of compilation features, see
// https://stackoverflow.com/questions/2390662/java-how-do-i-get-a-class-literal-from-a-generic-type
SearchResponse<HashMap> searchResponse = client.search(searchRequest, HashMap.class);
SearchResponse<HashMap> searchResponse =
this.openSearchClient.search(searchRequest, HashMap.class);

if (searchResponse.hits().total().value() == 0) {
throw new NotFoundException("No product found with identifier " + identifier.toString());
Expand Down

0 comments on commit 05a6881

Please sign in to comment.