Skip to content

Commit

Permalink
chore: AssetMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
SaadEGI committed Sep 14, 2023
1 parent bd1667e commit d8fa49d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion extensions/wrapper/wrapper-common-mappers/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dependencies {
api("${edcGroup}:transform-core:${edcVersion}")

api(project(":extensions:wrapper:wrapper-common-api"))

api(project(":utils:json-and-jsonld-utils"))
implementation("org.apache.commons:commons-lang3:3.13.0")
implementation("org.apache.commons:commons-collections4:4.4")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import de.sovity.edc.ext.wrapper.api.common.mappers.utils.EdcPropertyMapperUtils;
import de.sovity.edc.ext.wrapper.api.common.model.UiAsset;
import de.sovity.edc.ext.wrapper.api.common.model.UiAssetCreateRequest;
import de.sovity.edc.utils.JsonUtils;
import jakarta.json.JsonObject;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.eclipse.edc.spi.types.domain.asset.Asset;
import org.eclipse.edc.transform.spi.TypeTransformerRegistry;

import java.util.HashMap;
import java.util.List;
Expand All @@ -21,6 +24,27 @@ public class AssetMapper {
*/
private final ObjectMapper jsonLdObjectMapper;
private final EdcPropertyMapperUtils edcPropertyMapperUtils;
private final TypeTransformerRegistry typeTransformerRegistry;

@SneakyThrows
public UiAsset buildUiAssetFromAsset(Asset asset) {
var uiAsset = buildUiAssetFromAssetHelper(buildHelperDto(jsonLdObjectMapper.writeValueAsString(asset)));

uiAsset.setId(asset.getId());
uiAsset.setPrivateProperties(asset.getPrivateProperties());
uiAsset.setDataAddressProperties(edcPropertyMapperUtils.truncateToMapOfString(asset.getDataAddress().getProperties()));
uiAsset.setKeywords(uiAsset.getKeywords() == null ? List.of() : uiAsset.getKeywords());

return uiAsset;
}

public Asset buildAssetFromAssetPropertiesJsonLd(JsonObject json) {
return typeTransformerRegistry.transform(json, Asset.class).getContent();
}

private String buildJsonLd(Asset asset) {
return JsonUtils.toJson(typeTransformerRegistry.transform(asset, JsonObject.class).getContent());
}

@SneakyThrows
public UiAsset buildUiAssetFromAssetJsonLd(String assetJsonLd) {
Expand Down Expand Up @@ -59,18 +83,6 @@ public AssetHelperDto buildHelperDto(String assetJsonLd) {
return mapper.readValue(assetJsonLd, AssetHelperDto.class);
}

@SneakyThrows
public UiAsset buildUiAssetFromAsset(Asset asset) {
var uiAsset = buildUiAssetFromAssetHelper(buildHelperDto(jsonLdObjectMapper.writeValueAsString(asset)));

uiAsset.setId(asset.getId());
uiAsset.setPrivateProperties(asset.getPrivateProperties());
uiAsset.setDataAddressProperties(edcPropertyMapperUtils.truncateToMapOfString(asset.getDataAddress().getProperties()));
uiAsset.setKeywords(uiAsset.getKeywords() == null ? List.of() : uiAsset.getKeywords());

return uiAsset;
}

@SneakyThrows
public Asset buildAssetFromUiAssetCreateRequest(UiAssetCreateRequest uiAssetCreateRequest) {

Expand Down

0 comments on commit d8fa49d

Please sign in to comment.