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.
feat: #3 add retrieve dataset endpoint
- Loading branch information
1 parent
2bf215b
commit ba22d70
Showing
21 changed files
with
696 additions
and
20 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
34 changes: 34 additions & 0 deletions
34
...ava/io/github/genomicdatainfrastructure/discovery/api/DatasetNotFoundExceptionMapper.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,34 @@ | ||
// 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.NOT_FOUND; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.exceptions.DatasetNotFoundException; | ||
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; | ||
|
||
@Provider | ||
public class DatasetNotFoundExceptionMapper implements | ||
ExceptionMapper<DatasetNotFoundException> { | ||
|
||
@Override | ||
public Response toResponse(DatasetNotFoundException exception) { | ||
var errorResponse = new ErrorResponse( | ||
"Dataset Not Found", | ||
NOT_FOUND.getStatusCode(), | ||
exception.getMessage() | ||
); | ||
|
||
return Response | ||
.status(NOT_FOUND) | ||
.entity(errorResponse) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
} |
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
14 changes: 14 additions & 0 deletions
14
...va/io/github/genomicdatainfrastructure/discovery/exceptions/DatasetNotFoundException.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,14 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package io.github.genomicdatainfrastructure.discovery.exceptions; | ||
|
||
public class DatasetNotFoundException extends RuntimeException { | ||
|
||
private static final String MESSAGE = "Dataset %s not found"; | ||
|
||
public DatasetNotFoundException(String datasetId) { | ||
super(MESSAGE.formatted(datasetId)); | ||
} | ||
} |
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
105 changes: 105 additions & 0 deletions
105
src/main/java/io/github/genomicdatainfrastructure/discovery/services/PackageShowMapper.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,105 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package io.github.genomicdatainfrastructure.discovery.services; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.model.RetrievedDataset; | ||
import io.github.genomicdatainfrastructure.discovery.model.RetrievedDistribution; | ||
import io.github.genomicdatainfrastructure.discovery.model.ValueLabel; | ||
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.CkanResource; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static java.util.function.Predicate.not; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class PackageShowMapper { | ||
|
||
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern( | ||
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS" | ||
); | ||
|
||
private PackageShowMapper() { | ||
// Utility class | ||
} | ||
|
||
public static RetrievedDataset from(CkanPackage ckanPackage) { | ||
var catalogue = ofNullable(ckanPackage.getOrganization()) | ||
.map(CkanOrganization::getTitle) | ||
.orElse(null); | ||
|
||
return RetrievedDataset.builder() | ||
.id(ckanPackage.getId()) | ||
.identifier(ckanPackage.getIdentifier()) | ||
.title(ckanPackage.getName()) | ||
.description(ckanPackage.getNotes()) | ||
.themes(values(ckanPackage.getTheme())) | ||
.publisherName(ckanPackage.getPublisherName()) | ||
.catalogue(catalogue) | ||
.createdAt(parse(ckanPackage.getMetadataCreated())) | ||
.modifiedAt(parse(ckanPackage.getMetadataModified())) | ||
.url(ckanPackage.getUrl()) | ||
.languages(values(ckanPackage.getLanguage())) | ||
.contact(value(ckanPackage.getContactUri())) | ||
.hasVersions(values(ckanPackage.getHasVersion())) | ||
.accessRights(value(ckanPackage.getAccessRights())) | ||
.conformsTo(values(ckanPackage.getConformsTo())) | ||
.provenance(ckanPackage.getProvenance()) | ||
.spatial(value(ckanPackage.getSpatialUri())) | ||
.distributions(distributions(ckanPackage)) | ||
.build(); | ||
} | ||
|
||
private static List<ValueLabel> values(List<String> values) { | ||
return ofNullable(values) | ||
.orElseGet(List::of) | ||
.stream() | ||
.map(PackageShowMapper::value) | ||
.filter(Objects::nonNull) | ||
.toList(); | ||
} | ||
|
||
private static ValueLabel value(String value) { | ||
return ofNullable(value) | ||
.filter(Objects::nonNull) | ||
.filter(not(String::isBlank)) | ||
.map(it -> ValueLabel.builder() | ||
.value(it) | ||
.label(it) | ||
.build()) | ||
.orElse(null); | ||
} | ||
|
||
private static LocalDateTime parse(String date) { | ||
return ofNullable(date) | ||
.map(it -> LocalDateTime.parse(it, DATE_FORMATTER)) | ||
.orElse(null); | ||
} | ||
|
||
private static List<RetrievedDistribution> distributions(CkanPackage ckanPackage) { | ||
return ofNullable(ckanPackage.getResources()) | ||
.orElseGet(List::of) | ||
.stream() | ||
.map(PackageShowMapper::distribution) | ||
.toList(); | ||
} | ||
|
||
private static RetrievedDistribution distribution(CkanResource ckanResource) { | ||
return RetrievedDistribution.builder() | ||
.id(ckanResource.getId()) | ||
.title(ckanResource.getName()) | ||
.description(ckanResource.getDescription()) | ||
.format(value(ckanResource.getFormat())) | ||
.uri(ckanResource.getUri()) | ||
.createdAt(parse(ckanResource.getCreated())) | ||
.modifiedAt(parse(ckanResource.getLastModified())) | ||
.build(); | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
...n/java/io/github/genomicdatainfrastructure/discovery/services/RetrieveDatasetService.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,44 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package io.github.genomicdatainfrastructure.discovery.services; | ||
|
||
import org.eclipse.microprofile.rest.client.inject.RestClient; | ||
|
||
import io.github.genomicdatainfrastructure.discovery.exceptions.DatasetNotFoundException; | ||
import io.github.genomicdatainfrastructure.discovery.model.RetrievedDataset; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi; | ||
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.CkanPackageShowResponse; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.WebApplicationException; | ||
|
||
@ApplicationScoped | ||
public class RetrieveDatasetService { | ||
|
||
private final CkanQueryApi ckanQueryApi; | ||
|
||
@Inject | ||
public RetrieveDatasetService( | ||
@RestClient CkanQueryApi ckanQueryApi | ||
) { | ||
this.ckanQueryApi = ckanQueryApi; | ||
} | ||
|
||
public RetrievedDataset retrieve(String id, String accessToken) { | ||
var response = retrieveCkanPackage(id, accessToken); | ||
return PackageShowMapper.from(response.getResult()); | ||
} | ||
|
||
public CkanPackageShowResponse retrieveCkanPackage(String id, String accessToken) { | ||
try { | ||
return ckanQueryApi.packageShow(id, accessToken); | ||
} catch (WebApplicationException e) { | ||
if (e.getResponse().getStatus() == 404) { | ||
throw new DatasetNotFoundException(id); | ||
} | ||
throw e; | ||
} | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
src/test/java/io/github/genomicdatainfrastructure/discovery/api/RetrieveDatasetIT.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,11 @@ | ||
// SPDX-FileCopyrightText: 2024 PNED G.I.E. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package io.github.genomicdatainfrastructure.discovery.api; | ||
|
||
import io.quarkus.test.junit.QuarkusIntegrationTest; | ||
|
||
@QuarkusIntegrationTest | ||
public class RetrieveDatasetIT extends RetrieveDatasetTest { | ||
} |
Oops, something went wrong.