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 cf69e13
Showing
17 changed files
with
2,609 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,26 @@ | ||
# 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 | ||
|
||
{ | ||
"query": "COVID", | ||
"facets": [{ | ||
"facetGroup": "ckan", | ||
"facet": "theme", | ||
"value": "http://purl.bioontology.org/ontology/ICD10CM/U07.1" | ||
}, { | ||
"facetGroup": "ckan", | ||
"facet": "theme", | ||
"value": "http://purl.org/zonmw/covid19/10003" | ||
}, { | ||
"facetGroup": "ckan", | ||
"facet": "tags", | ||
"value": "COVID-19" | ||
}] | ||
} | ||
|
||
### | ||
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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...n/java/io/github/genomicdatainfrastructure/discovery/services/CkanFacetsQueryBuilder.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,48 @@ | ||
package io.github.genomicdatainfrastructure.discovery.services; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQueryFacet; | ||
|
||
import static io.quarkus.runtime.util.StringUtil.isNullOrEmpty; | ||
import static java.util.Optional.ofNullable; | ||
import static java.util.stream.Collectors.groupingBy; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class CkanFacetsQueryBuilder { | ||
|
||
private static final String CKAN_FACET_GROUP = "ckan"; | ||
private static final String QUOTED_VALUE = "\"%s\""; | ||
private static final String FACET_PATTERN = "%s:(%s)"; | ||
private static final String AND = " AND "; | ||
|
||
private CkanFacetsQueryBuilder() { | ||
// utility class | ||
} | ||
|
||
public static String buildFacetQuery(List<DatasetSearchQueryFacet> facets) { | ||
var nonNullFacets = ofNullable(facets) | ||
.orElseGet(List::of) | ||
.stream() | ||
.filter(CkanFacetsQueryBuilder::isCkanGroupAndValueIsNotBlank) | ||
.collect(groupingBy(DatasetSearchQueryFacet::getFacet)); | ||
|
||
return nonNullFacets.entrySet().stream() | ||
.map(entry -> getFacetQuery(entry.getKey(), entry.getValue())) | ||
.collect(joining(AND)); | ||
} | ||
|
||
private static Boolean isCkanGroupAndValueIsNotBlank(DatasetSearchQueryFacet facet) { | ||
return Objects.equals(CKAN_FACET_GROUP, facet.getFacetGroup()) && | ||
!isNullOrEmpty(facet.getValue()); | ||
} | ||
|
||
private static String getFacetQuery(String key, List<DatasetSearchQueryFacet> facets) { | ||
var values = facets.stream() | ||
.map(facet -> QUOTED_VALUE.formatted(facet.getValue())) | ||
.collect(joining(AND)); | ||
|
||
return FACET_PATTERN.formatted(key, values); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...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,37 @@ | ||
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.DatasetsSearchResponse; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
@ApplicationScoped | ||
public class DatasetsSearchService { | ||
|
||
private static final String FACETS_QUERY = "[\"access_rights\",\"theme\",\"tags\",\"spatial_uri\",\"organization\",\"publisher_name\",\"res_format\"]"; | ||
|
||
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(), | ||
CkanFacetsQueryBuilder.buildFacetQuery(query.getFacets()), | ||
query.getSort(), | ||
query.getRows(), | ||
query.getStart(), | ||
FACETS_QUERY, | ||
accessToken | ||
); | ||
return PackagesSearchResponseMapper.from(response); | ||
} | ||
} |
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("ckan") | ||
.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
Oops, something went wrong.