diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java index 854d04523556661186d2f32afa8ec90bdf889092..42f3c970807147ed3f21a40e6bc29a4f54ff2388 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java @@ -3,6 +3,7 @@ package com.smartharvester.controller; import com.smartharvester.model.mapping.Path; import com.smartharvester.model.mapping.request.Concepts; +import com.smartharvester.model.mapping.request.Iri; import com.smartharvester.model.mapping.request.MappingRequest; import com.smartharvester.model.mapping.response.MappingResponse; import com.smartharvester.service.MappingFromIsoService; @@ -70,7 +71,7 @@ public class SmartHarvesterMappingController { List<Path> distributionPaths = paths.stream().filter(path -> path.getDcatClass().equals("dcat:distribution")) .collect(Collectors.toList()); - Map<String, List<String>> urls = new HashMap<>(); + Map<String, List<Iri>> urls = new HashMap<>(); concepts.parallelStream().forEach( concept -> urls.put( this.openApiService.getUrls(this.openApiService.getOpenApiByUUDI(catalogId), concept.getId()).get(0), diff --git a/src/main/java/com/smartharvester/model/mapping/request/Concepts.java b/src/main/java/com/smartharvester/model/mapping/request/Concepts.java index 7e90861056a3095ce2a40f1b1c9d58dace581cdb..1f930050de6079a954bcc9edfae36d1be79a4ebc 100644 --- a/src/main/java/com/smartharvester/model/mapping/request/Concepts.java +++ b/src/main/java/com/smartharvester/model/mapping/request/Concepts.java @@ -6,7 +6,7 @@ public class Concepts { private String id; private String url; - private List<String> iris; + private List<Iri> iris; public String getId() { return id; @@ -16,11 +16,11 @@ public class Concepts { this.id = id; } - public List<String> getIris() { + public List<Iri> getIris() { return iris; } - public void setIris(List<String> iris) { + public void setIris(List<Iri> iris) { this.iris = iris; } diff --git a/src/main/java/com/smartharvester/model/mapping/request/Iri.java b/src/main/java/com/smartharvester/model/mapping/request/Iri.java new file mode 100644 index 0000000000000000000000000000000000000000..5cd16d2cfb8bdfb94fb39dbb4f5aad8e257e5963 --- /dev/null +++ b/src/main/java/com/smartharvester/model/mapping/request/Iri.java @@ -0,0 +1,32 @@ +package com.smartharvester.model.mapping.request; + +public class Iri { + + private String iri; + private String label; + private String originalKeyword; + + public String getIri() { + return iri; + } + + public void setIri(String iri) { + this.iri = iri; + } + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getOriginalKeyword() { + return originalKeyword; + } + + public void setOriginalKeyword(String originalKeyword) { + this.originalKeyword = originalKeyword; + } +} diff --git a/src/main/java/com/smartharvester/service/MappingService.java b/src/main/java/com/smartharvester/service/MappingService.java index 0e294247a7a165b7f582f2adc13b794bae8c5975..9b396e3185711e121c7b66638a05ce22532dc042 100644 --- a/src/main/java/com/smartharvester/service/MappingService.java +++ b/src/main/java/com/smartharvester/service/MappingService.java @@ -9,6 +9,7 @@ import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.PathNotFoundException; import com.smartharvester.model.elasticSearch.ElasticSearchResponse; import com.smartharvester.model.mapping.Path; +import com.smartharvester.model.mapping.request.Iri; import com.smartharvester.model.mapping.request.MappingRequest; import com.smartharvester.model.mapping.request.PublishedRequest; import com.smartharvester.model.mapping.response.KeywordResponse; @@ -99,7 +100,7 @@ public class MappingService { public String buildDataset(URI urlRepo, List<Path> paths, boolean isJsonpath, String catId, String fdpUrl, - List<String> iris) throws Exception { + List<Iri> iris) throws Exception { Map<String, List<Map<String, List<String>>>> dcatPropertiesMap = this.mapToDcat(urlRepo, paths, isJsonpath); Map<String, List<String>> datasetMap = dcatPropertiesMap.get("dataset").get(0); @@ -109,9 +110,31 @@ public class MappingService { datasetMap.putIfAbsent("dct:publisher", List.of("undefined")); + StringBuilder datasetString = this.getDatasetString(catId, fdpUrl); + + if (!iris.isEmpty()) { + iris.forEach(iri -> { + datasetString.append(this.write("dcat:theme", iri.getIri())); + if (null != iri.getLabel()) { + if (datasetMap.containsKey("dcat:keyword")) { + List<String> keywords = datasetMap.get("dcat:keyword"); + if (keywords.stream().noneMatch(e -> e.equals(iri.getLabel()))) { + keywords.add(iri.getLabel()); + } + datasetMap.replace("dcat:keyword", keywords); + } else { + List<String> keywords = new ArrayList<>(); + keywords.add(iri.getLabel()); + datasetMap.put("dcat:keyword", keywords); + } + } + }); + + + } for (Map.Entry<String, List<String>> entry : datasetMap.entrySet()) { - if (entry.getValue().isEmpty()) { + if (entry.getValue().isEmpty() && !entry.getKey().equals("dcat:keyword")) { entry.setValue(List.of("undefined")); } if (isReplicable(entry.getKey(), paths) && (entry.getValue().size() > 1)) { @@ -124,7 +147,7 @@ public class MappingService { } if (!iris.isEmpty()) { - iris.forEach(iri -> datasetString.append(this.write("dcat:theme", iri))); + iris.forEach(iri -> datasetString.append(this.write("dcat:theme", iri.getIri()))); } datasetString.append("."); LOGGER.info(datasetString.toString()); @@ -522,11 +545,11 @@ public class MappingService { public void mapAndPublish(String catalogId, String fdpUrl, boolean isJsonpath, List<Path> paths, String fdpToken, List<String> publishedUrl, List<String> notPublishedUrl, List<Path> distributionPaths, - Map.Entry<String, List<String>> entry) { + Map.Entry<String, List<Iri>> entry) { ResponseEntity<String> responseEntity; String datasetId; String url = entry.getKey(); - List<String> iris = entry.getValue(); + List<Iri> iris = entry.getValue(); String locationDataset; try { @@ -582,11 +605,11 @@ public class MappingService { ResponseEntity<Object> response = this.restTemplate.exchange(url + "/catalog/" + catId, HttpMethod.GET, entity, Object.class); - ObjectMapper mapper = new ObjectMapper(); - JSONArray body = new JSONArray(mapper.writeValueAsString(response.getBody())); + ObjectMapper mapper = new ObjectMapper(); + JSONArray body = new JSONArray(mapper.writeValueAsString(response.getBody())); - List<String> datasets = - getValuesFromJsonLd(body.getJSONObject(1), "http://www.w3.org/ns/dcat#dataset", "@id"); + List<String> datasets = + getValuesFromJsonLd(body.getJSONObject(1), "http://www.w3.org/ns/dcat#dataset", "@id"); List<KeywordResponse> keywordResponses = new ArrayList<>(); datasets.stream().forEach(datasetUrl -> { @@ -708,16 +731,46 @@ public class MappingService { concept.getIris().forEach(iri -> { if (null != body.optJSONObject(finalIndex)) { try { - if (concept.getIris().indexOf(iri) == 0) { + if (concept.getIris().indexOf(iri.getIri()) == 0) { body.getJSONObject(finalIndex).remove("http://www.w3.org/ns/dcat#theme"); body.getJSONObject(finalIndex) .put("http://www.w3.org/ns/dcat#theme", new JSONArray()); } - if (null != body.getJSONObject(finalIndex).optJSONArray("http://www.w3.org/ns/dcat#theme")) { - body.getJSONObject(finalIndex) - .getJSONArray("http://www.w3.org/ns/dcat#theme") - .put(new JSONObject().put("@id", iri)); - } + if (null != body.getJSONObject(finalIndex) + .optJSONArray("http://www.w3.org/ns/dcat#theme")) { + body.getJSONObject(finalIndex) + .getJSONArray("http://www.w3.org/ns/dcat#theme") + .put(new JSONObject().put("@id", iri.getIri())); + } + if (null != iri.getLabel()) { + + + if (null != body.getJSONObject(finalIndex) + .optJSONArray("http://www.w3.org/ns/dcat#keyword")) { + JSONArray keywordsArray = body.getJSONObject(finalIndex) + .getJSONArray("http://www.w3.org/ns/dcat#keyword"); + boolean isPresent = false; + for (int i = 0; i < keywordsArray.length(); i++) { + if (keywordsArray.optJSONObject(i) != null + && keywordsArray.optJSONObject(i).getString("@value") + .equals(iri.getLabel())) { + isPresent = true; + } + } + if (!isPresent) { + body.getJSONObject(finalIndex) + .getJSONArray("http://www.w3.org/ns/dcat#keyword") + .put(new JSONObject().put("@value", iri.getLabel())); + } + } else { + body.getJSONObject(finalIndex) + .put("http://www.w3.org/ns/dcat#keyword", new JSONArray()); + body.getJSONObject(finalIndex) + .getJSONArray("http://www.w3.org/ns/dcat#keyword") + .put(new JSONObject().put("@value", iri.getLabel())); + + } + } } catch (JSONException e) { notPublishedUrl.add(concept.getUrl() + " => " + e.getMessage()); LOGGER.error(e.getMessage()); diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 1df8462b08764df8c126bc93284106cc85ca0f66..8dfb3498ce51976f831bf9fb2b2cc368ef26f442 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -44,7 +44,7 @@ spring.security.oauth2.client.registration.oidc.client-secret=cLONCJ8MccdHwobCEM spring.security.oauth2.client.registration.oidc.scope=address,phone,openid,email,profile spring.security.oauth2.client.registration.oidc.redirect-uri=http://localhost:4200/callback -elasticsearch.baseURL=https://localhost:4242/eosc/api +elasticsearch.baseURL=https://90.147.189.237:4242/eosc/api elasticsearch.token=uHiOaPyo6IjKa7lV6qDHWYz3lgcf8bG2 elasticsearch.minChar=3 elasticsearch.maxChar=30