|
15 | 15 | package org.eclipse.edc.heleade.federated.catalog.extension.content.based; |
16 | 16 |
|
17 | 17 | import jakarta.json.Json; |
| 18 | +import jakarta.json.JsonArray; |
18 | 19 | import jakarta.json.JsonObject; |
19 | 20 | import org.eclipse.edc.catalog.transform.JsonObjectToDatasetTransformer; |
| 21 | +import org.eclipse.edc.connector.controlplane.catalog.spi.DataService; |
20 | 22 | import org.eclipse.edc.connector.controlplane.catalog.spi.Dataset; |
21 | | -import org.eclipse.edc.heleade.commons.content.based.catalog.CbmConstants; |
| 23 | +import org.eclipse.edc.connector.controlplane.catalog.spi.Distribution; |
22 | 24 | import org.eclipse.edc.transform.spi.TransformerContext; |
23 | 25 | import org.jetbrains.annotations.NotNull; |
24 | 26 | import org.jetbrains.annotations.Nullable; |
25 | 27 |
|
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.eclipse.edc.heleade.commons.content.based.catalog.CbmConstants.CBM_HAS_DATA_DICTIONARY; |
| 32 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_ACCESS_SERVICE_ATTRIBUTE; |
| 33 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_DISTRIBUTION_ATTRIBUTE; |
| 34 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_ENDPOINT_DESCRIPTION_ATTRIBUTE; |
| 35 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCAT_ENDPOINT_URL_ATTRIBUTE; |
| 36 | +import static org.eclipse.edc.jsonld.spi.PropertyAndTypeNames.DCT_FORMAT_ATTRIBUTE; |
| 37 | + |
26 | 38 | /** |
27 | 39 | * Transformer class for converting a JSON object into a Dataset with additional content-based |
28 | 40 | * adjustments specific to federated catalog implementations. |
29 | 41 | * |
30 | 42 | * Features: |
31 | 43 | * - Extends {@code JsonObjectToDatasetTransformer} to inherit base transformation functionality. |
32 | 44 | * - Relocates data dictionary information from distribution arrays to the root dataset object when present. |
| 45 | + * - Fixes issues with default transformer including the data access URL in the result |
33 | 46 | */ |
34 | 47 | public class JsonObjectToDatasetContentBasedTransformer extends JsonObjectToDatasetTransformer { |
35 | 48 |
|
36 | 49 | @Override |
37 | 50 | public @Nullable Dataset transform(@NotNull JsonObject object, @NotNull TransformerContext context) { |
38 | 51 | var modifiedObject = moveDataDictionaryToDataset(object); |
39 | | - var dataset = super.transform(modifiedObject, context); |
40 | | - return dataset; |
| 52 | + Dataset dataset = super.transform(modifiedObject, context); |
| 53 | + Dataset fixedDataset = fixDataset(dataset, modifiedObject); |
| 54 | + return fixedDataset; |
41 | 55 | } |
42 | 56 |
|
43 | 57 | private JsonObject moveDataDictionaryToDataset(JsonObject object) { |
44 | 58 | // Get a distribution array if it exists |
45 | | - var distributions = object.getJsonArray(CbmConstants.DISTRIBUTION_TAG); |
| 59 | + var distributions = object.getJsonArray(DCAT_DISTRIBUTION_ATTRIBUTE); |
46 | 60 | if (distributions == null || distributions.isEmpty()) { |
47 | 61 | return object; |
48 | 62 | } |
49 | 63 |
|
50 | 64 | // Find first distribution with data dictionary |
51 | 65 | for (var i = 0; i < distributions.size(); i++) { |
52 | 66 | var distribution = distributions.getJsonObject(i); |
53 | | - if (distribution.containsKey(CbmConstants.CBM_HAS_DATA_DICTIONARY)) { |
| 67 | + if (distribution.containsKey(CBM_HAS_DATA_DICTIONARY)) { |
54 | 68 | // Create a new object with data dictionary at root |
55 | 69 | return Json.createObjectBuilder(object) |
56 | | - .add(CbmConstants.CBM_HAS_DATA_DICTIONARY, |
57 | | - distribution.get(CbmConstants.CBM_HAS_DATA_DICTIONARY)) |
| 70 | + .add(CBM_HAS_DATA_DICTIONARY, |
| 71 | + distribution.get(CBM_HAS_DATA_DICTIONARY)) |
58 | 72 | .build(); |
59 | 73 | } |
60 | 74 | } |
61 | 75 |
|
62 | 76 | return object; |
63 | 77 | } |
| 78 | + |
| 79 | + private List<Distribution> getDistributions(JsonObject object) { |
| 80 | + |
| 81 | + // Get a distribution array if it exists |
| 82 | + var distributions = object.getJsonArray(DCAT_DISTRIBUTION_ATTRIBUTE); |
| 83 | + if (distributions == null || distributions.isEmpty()) { |
| 84 | + return List.of(); |
| 85 | + } |
| 86 | + |
| 87 | + // Fix distributions |
| 88 | + ArrayList<Distribution> distributionsList = new ArrayList<>(); |
| 89 | + |
| 90 | + for (var i = 0; i < distributions.size(); i++) { |
| 91 | + var distributionBuilder = Distribution.Builder.newInstance(); |
| 92 | + var distribution = distributions.getJsonObject(i); |
| 93 | + distributionBuilder.format(distribution.getJsonArray(DCT_FORMAT_ATTRIBUTE).getJsonObject(0).getString("@id")); |
| 94 | + if (distribution.containsKey(DCAT_ACCESS_SERVICE_ATTRIBUTE)) { |
| 95 | + var accessServiceValue = distribution.getJsonArray(DCAT_ACCESS_SERVICE_ATTRIBUTE).get(0); |
| 96 | + if (accessServiceValue instanceof JsonObject) { |
| 97 | + var dataServiceBuilder = DataService.Builder.newInstance(); |
| 98 | + JsonObject accessService = (JsonObject) accessServiceValue; |
| 99 | + dataServiceBuilder.id(accessService.getString("@id")); |
| 100 | + var endpointDescription = accessService.get(DCAT_ENDPOINT_DESCRIPTION_ATTRIBUTE); |
| 101 | + var endpointUrl = accessService.get(DCAT_ENDPOINT_URL_ATTRIBUTE); |
| 102 | + if (endpointDescription instanceof JsonArray) { |
| 103 | + String endpointDescriptionString = ((JsonArray) endpointDescription).getJsonObject(0).getString("@value"); |
| 104 | + dataServiceBuilder.endpointDescription(endpointDescriptionString); |
| 105 | + } |
| 106 | + if (endpointUrl instanceof JsonArray) { |
| 107 | + String endpointUrlString = ((JsonArray) endpointUrl).getJsonObject(0).getString("@value"); |
| 108 | + dataServiceBuilder.endpointUrl(endpointUrlString); |
| 109 | + } |
| 110 | + distributionBuilder.dataService(dataServiceBuilder.build()); |
| 111 | + } |
| 112 | + } |
| 113 | + distributionsList.add(distributionBuilder.build()); |
| 114 | + } |
| 115 | + |
| 116 | + return distributionsList; |
| 117 | + } |
| 118 | + |
| 119 | + private Dataset fixDataset(Dataset dataset, JsonObject jsonDataset) { |
| 120 | + var fixedDistributions = getDistributions(jsonDataset); |
| 121 | + var fixedDatasetBuilder = Dataset.Builder.newInstance(); |
| 122 | + fixedDatasetBuilder.id(dataset.getId()); |
| 123 | + fixedDatasetBuilder.properties(dataset.getProperties()); |
| 124 | + fixedDatasetBuilder.offers(dataset.getOffers()); |
| 125 | + fixedDatasetBuilder.distributions(fixedDistributions); |
| 126 | + return fixedDatasetBuilder.build(); |
| 127 | + } |
64 | 128 | } |
0 commit comments