generated from GenomicDataInfrastructure/oss-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02f043b
commit 25ff86c
Showing
16 changed files
with
2,562 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
POST http://localhost:8080/api/v1/datasets/search | ||
Content-Type: application/json | ||
|
||
{ | ||
} | ||
|
||
### | ||
GET http://localhost:8080/api/v1/datasets/dummy |
4 changes: 0 additions & 4 deletions
4
src/main/java/io/github/genomicdatainfrastructure/discovery/.gitignore
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
src/main/java/io/github/genomicdatainfrastructure/discovery/api/DatasetQueryApiImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.genomicdatainfrastructure.discovery.api; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery; | ||
import io.github.genomicdatainfrastructure.discovery.model.DatasetsSearchResponse; | ||
import io.github.genomicdatainfrastructure.discovery.model.RetrievedDataset; | ||
import io.github.genomicdatainfrastructure.discovery.services.DatasetsSearchService; | ||
import io.quarkus.oidc.runtime.OidcJwtCallerPrincipal; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class DatasetQueryApiImpl implements DatasetQueryApi { | ||
|
||
private final SecurityIdentity identity; | ||
private final DatasetsSearchService datasetsSearchService; | ||
|
||
@Override | ||
public DatasetsSearchResponse datasetSearch(DatasetSearchQuery datasetSearchQuery) { | ||
return datasetsSearchService.search(accessToken(), datasetSearchQuery); | ||
} | ||
|
||
@Override | ||
public RetrievedDataset retrieveDataset(String id) { | ||
return RetrievedDataset.builder() | ||
.build(); | ||
} | ||
|
||
private String accessToken() { | ||
if (identity.isAnonymous()) { | ||
return null; | ||
} | ||
var principal = (OidcJwtCallerPrincipal) identity.getPrincipal(); | ||
return principal.getRawToken(); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/io/github/genomicdatainfrastructure/discovery/api/GlobalExceptionMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package io.github.genomicdatainfrastructure.discovery.api; | ||
|
||
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.ErrorResponse; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
import jakarta.ws.rs.ext.ExceptionMapper; | ||
import jakarta.ws.rs.ext.Provider; | ||
import lombok.extern.java.Log; | ||
import java.util.logging.Level; | ||
|
||
@Log | ||
@Provider | ||
public class GlobalExceptionMapper implements ExceptionMapper<Exception> { | ||
|
||
@Override | ||
public Response toResponse(Exception exception) { | ||
log.log(Level.SEVERE, exception, exception::getMessage); | ||
|
||
var errorResponse = ErrorResponse.builder() | ||
.title("Not expected exception") | ||
.status(INTERNAL_SERVER_ERROR.getStatusCode()) | ||
.detail(exception.getMessage()) | ||
.build(); | ||
|
||
return Response | ||
.status(INTERNAL_SERVER_ERROR) | ||
.entity(errorResponse) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...in/java/io/github/genomicdatainfrastructure/discovery/services/DatasetsSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.github.genomicdatainfrastructure.discovery.services; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery; | ||
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQueryFacet; | ||
import io.github.genomicdatainfrastructure.discovery.model.DatasetsSearchResponse; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import java.util.List; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
@ApplicationScoped | ||
public class DatasetsSearchService { | ||
|
||
private static final String FACETS_QUERY = "[\"organization\",\"theme\",\"conforms_to\",\"has_version\",\"access_rights\",\"language\",\"publisher_name\",\"res_format\",\"provenance\"]"; | ||
|
||
private final CkanQueryApi ckanQueryApi; | ||
|
||
@Inject | ||
public DatasetsSearchService( | ||
@RestClient CkanQueryApi ckanQueryApi | ||
) { | ||
this.ckanQueryApi = ckanQueryApi; | ||
} | ||
|
||
public DatasetsSearchResponse search(String accessToken, DatasetSearchQuery query) { | ||
var response = ckanQueryApi.packageSearch( | ||
query.getQuery(), | ||
getFacetsQuery(query.getFacets()), | ||
query.getSort(), | ||
query.getRows(), | ||
query.getStart(), | ||
FACETS_QUERY, | ||
accessToken | ||
); | ||
return PackagesSearchResponseMapper.from(response); | ||
} | ||
|
||
private String getFacetsQuery(List<DatasetSearchQueryFacet> facets) { | ||
var nonNullFacets = ofNullable(facets) | ||
.orElseGet(List::of); | ||
|
||
return nonNullFacets.stream() | ||
.map(facet -> String.format("facet.field=%s", facet)) | ||
.reduce((a, b) -> String.format("%s&%s", a, b)) | ||
.orElse(""); | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
.../io/github/genomicdatainfrastructure/discovery/services/PackagesSearchResponseMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package io.github.genomicdatainfrastructure.discovery.services; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.DatasetsSearchResponse; | ||
import io.github.genomicdatainfrastructure.discovery.model.Facet; | ||
import io.github.genomicdatainfrastructure.discovery.model.FacetGroup; | ||
import io.github.genomicdatainfrastructure.discovery.model.SearchedDataset; | ||
import io.github.genomicdatainfrastructure.discovery.model.ValueLabel; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanFacet; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanOrganization; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanPackage; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.PackagesSearchResponse; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.PackagesSearchResult; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.LocalDateTime; | ||
|
||
import static java.util.Optional.ofNullable; | ||
|
||
public class PackagesSearchResponseMapper { | ||
|
||
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern( | ||
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS" | ||
); | ||
|
||
private PackagesSearchResponseMapper() { | ||
// Utility class | ||
} | ||
|
||
public static DatasetsSearchResponse from(PackagesSearchResponse response) { | ||
return DatasetsSearchResponse.builder() | ||
.count(count(response.getResult())) | ||
.facetGroups(facetGroups(response.getResult())) | ||
.results(results(response.getResult())) | ||
.build(); | ||
} | ||
|
||
private static Integer count(PackagesSearchResult result) { | ||
return ofNullable(result) | ||
.map(PackagesSearchResult::getCount) | ||
.orElse(null); | ||
} | ||
|
||
private static List<FacetGroup> facetGroups(PackagesSearchResult result) { | ||
return ofNullable(result) | ||
.map(PackagesSearchResult::getSearchFacets) | ||
.map(PackagesSearchResponseMapper::facetGroup) | ||
.map(List::of) | ||
.orElseGet(List::of); | ||
} | ||
|
||
private static FacetGroup facetGroup(Map<String, CkanFacet> facets) { | ||
return FacetGroup.builder() | ||
.key("dcat") | ||
.label("Metadata") | ||
.facets(facets.entrySet().stream() | ||
.map(PackagesSearchResponseMapper::facet) | ||
.toList()) | ||
.build(); | ||
} | ||
|
||
private static Facet facet(Map.Entry<String, CkanFacet> entry) { | ||
var key = entry.getKey(); | ||
var facet = entry.getValue(); | ||
var values = ofNullable(facet.getItems()) | ||
.orElseGet(List::of) | ||
.stream().map(value -> ValueLabel.builder() | ||
.value(value.getName()) | ||
.label(value.getDisplayName()) | ||
.build()) | ||
.toList(); | ||
|
||
return Facet.builder() | ||
.key(key) | ||
.label(facet.getTitle()) | ||
.values(values) | ||
.build(); | ||
} | ||
|
||
private static List<SearchedDataset> results(PackagesSearchResult result) { | ||
var nonNullDatasets = ofNullable(result) | ||
.map(PackagesSearchResult::getResults) | ||
.orElseGet(List::of); | ||
|
||
return nonNullDatasets.stream() | ||
.map(PackagesSearchResponseMapper::result) | ||
.toList(); | ||
} | ||
|
||
private static SearchedDataset result(CkanPackage dataset) { | ||
var catalogue = ofNullable(dataset.getOrganization()) | ||
.map(CkanOrganization::getTitle) | ||
.orElse(null); | ||
|
||
return SearchedDataset.builder() | ||
.id(dataset.getId()) | ||
.identifier(dataset.getIdentifier()) | ||
.title(dataset.getName()) | ||
.description(dataset.getNotes()) | ||
.themes(values(dataset.getTheme())) | ||
.catalogue(catalogue) | ||
.modifiedAt(parse(dataset.getMetadataModified())) | ||
.build(); | ||
} | ||
|
||
private static LocalDateTime parse(String date) { | ||
return ofNullable(date) | ||
.map(it -> LocalDateTime.parse(it, DATE_FORMATTER)) | ||
.orElse(null); | ||
} | ||
|
||
private static List<ValueLabel> values(List<String> values) { | ||
return ofNullable(values) | ||
.orElseGet(List::of) | ||
.stream() | ||
.map(it -> ValueLabel.builder() | ||
.value(it) | ||
.label(it) | ||
.build()) | ||
.toList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletions
4
src/test/java/io/github/genomicdatainfrastructure/discovery/.gitignore
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.