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.
chore: (ART-10688) refactor ckan mapper
- Loading branch information
Showing
11 changed files
with
400 additions
and
559 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
100 changes: 100 additions & 0 deletions
100
...b/genomicdatainfrastructure/discovery/datasets/infrastructure/ckan/mapper/CkanMapper.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,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()); | ||
} | ||
|
||
@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); | ||
} | ||
} | ||
} |
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: 0 additions & 44 deletions
44
...nomicdatainfrastructure/discovery/datasets/infrastructure/ckan/utils/CkanAgentParser.java
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
...icdatainfrastructure/discovery/datasets/infrastructure/ckan/utils/CkanDatetimeParser.java
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
...genomicdatainfrastructure/discovery/datasets/infrastructure/ckan/utils/CkanTagParser.java
This file was deleted.
Oops, something went wrong.
37 changes: 0 additions & 37 deletions
37
...datainfrastructure/discovery/datasets/infrastructure/ckan/utils/CkanValueLabelParser.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.