Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: (ART-10688) refactor ckan mapper #159

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<surefire.version>3.5.2</surefire.version>
<quarkus-wiremock.version>1.4.0</quarkus-wiremock.version>
<jacoco-maven-plugin.version>0.8.12</jacoco-maven-plugin.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
</properties>
<dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -138,6 +139,17 @@
<artifactId>quarkus-jacoco</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down Expand Up @@ -172,6 +184,7 @@
<configuration>
<compilerArgs>
<arg>-parameters</arg>
<arg>-Amapstruct.unmappedTargetPolicy=ERROR</arg>
</compilerArgs>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import static java.util.stream.Collectors.toMap;

import io.github.genomicdatainfrastructure.discovery.datasets.domain.exceptions.InvalidFacetException;
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQuery;
import io.github.genomicdatainfrastructure.discovery.model.DatasetSearchQueryFacet;
import io.github.genomicdatainfrastructure.discovery.model.FilterType;
import io.github.genomicdatainfrastructure.discovery.model.QueryEntry;
import io.github.genomicdatainfrastructure.discovery.model.*;
import io.github.genomicdatainfrastructure.discovery.remote.beacon.model.BeaconIndividualsRequest;
import io.github.genomicdatainfrastructure.discovery.remote.beacon.model.BeaconIndividualsRequestMeta;
import io.github.genomicdatainfrastructure.discovery.remote.beacon.model.BeaconIndividualsRequestQuery;
Expand All @@ -29,7 +26,7 @@ public class BeaconIndividualsRequestMapper {

private static final String BEACON_FACET_GROUP = "beacon";
private static final String SCOPE = "individual";
private static final String INCLUDE_RESULTSET_RESPONSES = "HIT";
private static final String INCLUDE_RESULT_SET_RESPONSES = "HIT";
private static final String REQUESTED_GRANULARITY = "record";

public BeaconIndividualsRequest from(
Expand All @@ -56,7 +53,7 @@ public BeaconIndividualsRequest from(
return BeaconIndividualsRequest.builder()
.meta(new BeaconIndividualsRequestMeta())
.query(BeaconIndividualsRequestQuery.builder()
.includeResultsetResponses(INCLUDE_RESULTSET_RESPONSES)
.includeResultsetResponses(INCLUDE_RESULT_SET_RESPONSES)
.requestedGranularity(REQUESTED_GRANULARITY)
.testMode(false)
.pagination(new BeaconIndividualsRequestQueryPagination())
Expand Down Expand Up @@ -90,7 +87,7 @@ private BeaconIndividualsRequestQueryFilter buildDropdownBeaconFacet(
private BeaconIndividualsRequestQueryFilter buildFreeTextBeaconFacet(
DatasetSearchQueryFacet facet) {
String operator = ofNullable(facet.getOperator())
.map(it -> it.value())
.map(Operator::value)
.orElseThrow(() -> new InvalidFacetException(
"Facet operator must not be null"));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper;

import io.github.genomicdatainfrastructure.discovery.model.RetrievedDataset;
import io.github.genomicdatainfrastructure.discovery.model.RetrievedDistribution;
import io.github.genomicdatainfrastructure.discovery.model.SearchedDataset;
import io.github.genomicdatainfrastructure.discovery.model.ValueLabel;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.*;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.NullValueMappingStrategy;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.mapstruct.CollectionMappingStrategy.ADDER_PREFERRED;
import static org.mapstruct.NullValueCheckStrategy.ALWAYS;

@Mapper(componentModel = "jakarta", nullValueCheckStrategy = ALWAYS, nullValueIterableMappingStrategy = NullValueMappingStrategy.RETURN_DEFAULT, collectionMappingStrategy = ADDER_PREFERRED)
public interface CkanMapper {

DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern(
"yyyy-MM-dd'T'HH:mm:ss.SSSSSS");

@Mapping(target = "description", source = "notes")
@Mapping(target = "themes", source = "theme")
@Mapping(target = "contacts", source = "contact")
@Mapping(target = "distributions", source = "resources")
@Mapping(target = "keywords", source = "tags")
@Mapping(target = "spatial", source = "spatialUri")
@Mapping(target = "createdAt", source = "issued")
@Mapping(target = "modifiedAt", source = "modified")
@Mapping(target = "creators", source = "creator")
@Mapping(target = "hasVersions", source = "hasVersion")
@Mapping(target = "publishers", source = "publisher")
@Mapping(target = "languages", source = "language")
@Mapping(target = "catalogue", ignore = true)
RetrievedDataset map(CkanPackage ckanPackage);

@Mapping(target = "label", source = "displayName")
@Mapping(target = "value", source = "name")
@Mapping(target = "count", ignore = true)
ValueLabel map(CkanValueLabel ckanValueLabel);

@Mapping(target = "label", source = "displayName")
@Mapping(target = "value", source = "name")
@Mapping(target = "count", ignore = true)
ValueLabel map(CkanTag ckanTag);

@Mapping(target = "title", source = "name")
@Mapping(target = "createdAt", source = "issuedDate")
@Mapping(target = "modifiedAt", source = "modifiedDate")
@Mapping(target = "languages", source = "language")
RetrievedDistribution map(CkanResource ckanResource);

default List<SearchedDataset> map(PackagesSearchResult result) {
if (result == null || result.getResults() == null) {
return Collections.emptyList();
}
return result.getResults().stream()
.map(this::mapToSearchedDataset)
.collect(Collectors.toList());
jadzlnds marked this conversation as resolved.
Show resolved Hide resolved
}

@Mapping(target = "description", source = "notes")
@Mapping(target = "themes", source = "theme")
@Mapping(target = "publishers", source = "publisher")
@Mapping(target = "keywords", source = "tags")
@Mapping(target = "modifiedAt", source = "modified")
@Mapping(target = "createdAt", source = "issued")
@Mapping(target = "distributionsCount", expression = "java(ckanPackage.getResources()!= null ? ckanPackage.getResources().size():0)")
@Mapping(target = "catalogue", ignore = true)
@Mapping(target = "recordsCount", ignore = true)
SearchedDataset mapToSearchedDataset(CkanPackage ckanPackage);

default OffsetDateTime map(String date) {
if (StringUtils.isBlank(date)) {
return null;
}

try {
return OffsetDateTime.parse(date);
} catch (DateTimeParseException e) {
var dateToParse = date;
if (dateToParse.matches("^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01])$")) {
dateToParse += "T00:00:00.000000";
}
return LocalDateTime.parse(dateToParse, DATE_FORMATTER)
.truncatedTo(ChronoUnit.SECONDS)
.atOffset(ZoneOffset.UTC);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import io.github.genomicdatainfrastructure.discovery.datasets.application.ports.DatasetsRepository;
import io.github.genomicdatainfrastructure.discovery.datasets.domain.exceptions.DatasetNotFoundException;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.mapper.CkanMapper;
import io.github.genomicdatainfrastructure.discovery.model.*;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.api.CkanQueryApi;
import io.github.genomicdatainfrastructure.discovery.remote.ckan.model.*;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.utils.PackageSearchMapper;
import io.github.genomicdatainfrastructure.discovery.datasets.infrastructure.ckan.utils.PackageShowMapper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.WebApplicationException;
Expand All @@ -26,12 +25,14 @@
public class CkanDatasetsRepository implements DatasetsRepository {

private final CkanQueryApi ckanQueryApi;
private final CkanMapper ckanMapper;

@Inject
public CkanDatasetsRepository(
@RestClient CkanQueryApi ckanQueryApi
@RestClient CkanQueryApi ckanQueryApi, CkanMapper ckanMapper
) {
this.ckanQueryApi = ckanQueryApi;
this.ckanMapper = ckanMapper;
}

@Override
Expand Down Expand Up @@ -72,14 +73,14 @@ public List<SearchedDataset> search(
request
);

return PackageSearchMapper.from(response.getResult());
return ckanMapper.map(response.getResult());
}

@Override
public RetrievedDataset findById(String id, String accessToken) {
try {
var ckanPackage = ckanQueryApi.packageShow(id);
return PackageShowMapper.from(ckanPackage.getResult());
return ckanMapper.map(ckanPackage.getResult());
} catch (WebApplicationException e) {
if (e.getResponse().getStatus() == 404) {
throw new DatasetNotFoundException(id);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading