From 8d7cb45f41ad180d0f92fd785736dca2a3438714 Mon Sep 17 00:00:00 2001
From: Baptiste Toulemonde <toulemonde@cines.fr>
Date: Wed, 14 Sep 2022 11:56:14 +0200
Subject: [PATCH] feature retieve ontol by keyword done (RAF autocomplete)

---
 .../controller/ElacticSearchController.java   |  6 +-
 .../SmartHarvesterMappingController.java      | 30 ++++++----
 .../model/mapping/request/Concepts.java       | 25 ++++++++
 .../model/mapping/request/KeywordRequest.java | 41 +++++++++++++
 .../model/mapping/request/MappingRequest.java | 60 +++++++++----------
 .../service/ElasticSearchService.java         | 10 ++--
 .../service/MappingService.java               |  5 +-
 7 files changed, 126 insertions(+), 51 deletions(-)
 create mode 100644 src/main/java/com/smartharvester/model/mapping/request/Concepts.java
 create mode 100644 src/main/java/com/smartharvester/model/mapping/request/KeywordRequest.java

diff --git a/src/main/java/com/smartharvester/controller/ElacticSearchController.java b/src/main/java/com/smartharvester/controller/ElacticSearchController.java
index d4c8934..6f1b393 100644
--- a/src/main/java/com/smartharvester/controller/ElacticSearchController.java
+++ b/src/main/java/com/smartharvester/controller/ElacticSearchController.java
@@ -1,7 +1,7 @@
 package com.smartharvester.controller;
 
 import com.smartharvester.model.elasticSearch.ElasticSearchResponse;
-import com.smartharvester.model.mapping.request.MappingRequest;
+import com.smartharvester.model.mapping.request.KeywordRequest;
 import com.smartharvester.model.mapping.response.KeywordResponse;
 import com.smartharvester.service.ElasticSearchService;
 import io.swagger.v3.oas.annotations.tags.Tag;
@@ -19,7 +19,7 @@ import java.util.List;
 
 @CrossOrigin(origins = "*")
 @RequestMapping("/harvester/api/es")
-@Tag(name = "Harvest", description = "request to eleasticsearch index")
+@Tag(name = "Harvest", description = "request to elasticsearch index")
 @RestController
 public class ElacticSearchController {
 
@@ -44,7 +44,7 @@ public class ElacticSearchController {
 
 
     @PostMapping("/keywords")
-    public ResponseEntity<List<KeywordResponse>> getKeywords (@RequestBody MappingRequest req, @RequestParam("catalogId") String catalogId, @RequestParam("isJsonPath") boolean isJsonPath) {
+    public ResponseEntity<List<KeywordResponse>> getKeywords (@RequestBody KeywordRequest req, @RequestParam("catalogId") String catalogId, @RequestParam("isJsonPath") boolean isJsonPath) {
         List<KeywordResponse> keywords = this.service.getConceptsByKeywords(req, catalogId, isJsonPath);
 
         return ResponseEntity.ok(keywords);
diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java
index 54fc752..a4df2ad 100644
--- a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java
+++ b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java
@@ -2,6 +2,7 @@ package com.smartharvester.controller;
 
 
 import com.smartharvester.model.mapping.Path;
+import com.smartharvester.model.mapping.request.Concepts;
 import com.smartharvester.model.mapping.request.MappingRequest;
 import com.smartharvester.model.mapping.response.MappingResponse;
 import com.smartharvester.service.MappingFromIsoService;
@@ -24,7 +25,9 @@ import org.springframework.web.bind.annotation.RestController;
 
 import java.net.URI;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ExecutionException;
 import java.util.stream.Collectors;
@@ -59,8 +62,8 @@ public class SmartHarvesterMappingController {
 	@PostMapping("/publish")
 	public ResponseEntity<MappingResponse> map(@RequestBody MappingRequest mappingRequest,
 			@RequestParam("catalogId") String catalogId, @RequestParam String fdpUrl, @RequestParam boolean isJsonpath)  {
-		ResponseEntity<?> responseEntity;
-		List<String> ids = mappingRequest.getIds();
+		ResponseEntity<String> responseEntity;
+		List<Concepts> concepts = mappingRequest.getDataConcepts();
 		List<Path> paths = mappingRequest.getPaths();
 		String fdpToken = mappingRequest.getFdpToken();
 		List<String> publishedUrl = new ArrayList<>();
@@ -68,16 +71,23 @@ public class SmartHarvesterMappingController {
 		List<Path> distributionPaths = paths.stream().filter(path -> path.getDcatClass().equals("dcat:distribution"))
 				.collect(Collectors.toList());
 
-		List<String> urls = new ArrayList<>();
-		ids.parallelStream().forEach(id -> urls.addAll(this.openApiService.getUrls(this.openApiService.getOpenApiByUUDI(catalogId), id)));
+		Map<String, List<String>> urls = new HashMap<>();
+		concepts.parallelStream().forEach(
+			concept -> urls.put(
+				this.openApiService.getUrls(this.openApiService.getOpenApiByUUDI(catalogId), concept.getId()).get(0),
+				concept.getIris()
+			)
+		);
 
-		for (String url : urls) {
+		for (Map.Entry<String, List<String>> entry : urls.entrySet()) {
 			String datasetId;
+			String url = entry.getKey();
+			List<String> iris = entry.getValue();
 
 			String locationDataset;
 			try {
-                String dataset = this.mappingService.buildDataset(new URI(url), paths, isJsonpath, catalogId, fdpUrl);
-				responseEntity = this.mappingService.asyncPostToFdp("/dataset", fdpUrl, dataset, fdpToken).get();
+                String dataset = this.mappingService.buildDataset(new URI(url), paths, isJsonpath, catalogId, fdpUrl, iris);
+				responseEntity = this.mappingService.asyncPostToFdp("/dataset", fdpUrl.replace(":8080", ""), dataset, fdpToken).get();
 				locationDataset = Objects.requireNonNull(responseEntity.getHeaders().getLocation()).toString();
 				datasetId = locationDataset.substring(locationDataset.indexOf("dataset/") + 8);
 
@@ -87,10 +97,10 @@ public class SmartHarvesterMappingController {
 
 					for (String distribution: distributions) {
 						try {
-							ResponseEntity<String> distributionResponse = this.mappingService.asyncPostToFdp("/distribution", fdpUrl, distribution, fdpToken).get();
+							ResponseEntity<String> distributionResponse = this.mappingService.asyncPostToFdp("/distribution", fdpUrl.replace(":8080", ""), distribution, fdpToken).get();
 							String locationDistribution = Objects.requireNonNull(Objects.requireNonNull(distributionResponse.getHeaders().getLocation()).toString());
 							if (distributionResponse.getStatusCode().value() == 201) {
-								this.mappingService.draftToPublished(fdpToken, locationDistribution);
+								this.mappingService.draftToPublished(fdpToken, locationDistribution.replace(":8080", ""));
 							}
 						} catch (ExecutionException | InterruptedException e) {
 							LOGGER.warn(e.getMessage());
@@ -99,7 +109,7 @@ public class SmartHarvesterMappingController {
 				}
 				if (statusCode.value() == 201) {
 					publishedUrl.add(url);
-					this.mappingService.draftToPublished(fdpToken, locationDataset);
+					this.mappingService.draftToPublished(fdpToken, locationDataset.replace(":8080", ""));
 				} else {
 					notPublishedUrl.add(url);
 				}
diff --git a/src/main/java/com/smartharvester/model/mapping/request/Concepts.java b/src/main/java/com/smartharvester/model/mapping/request/Concepts.java
new file mode 100644
index 0000000..66ee8b9
--- /dev/null
+++ b/src/main/java/com/smartharvester/model/mapping/request/Concepts.java
@@ -0,0 +1,25 @@
+package com.smartharvester.model.mapping.request;
+
+import java.util.List;
+
+public class Concepts {
+
+    private String id;
+    private List<String> iris;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public List<String> getIris() {
+        return iris;
+    }
+
+    public void setIris(List<String> iris) {
+        this.iris = iris;
+    }
+}
diff --git a/src/main/java/com/smartharvester/model/mapping/request/KeywordRequest.java b/src/main/java/com/smartharvester/model/mapping/request/KeywordRequest.java
new file mode 100644
index 0000000..c7ff4c4
--- /dev/null
+++ b/src/main/java/com/smartharvester/model/mapping/request/KeywordRequest.java
@@ -0,0 +1,41 @@
+package com.smartharvester.model.mapping.request;
+
+import com.smartharvester.model.mapping.Path;
+
+import java.util.List;
+
+public class KeywordRequest {
+	
+	private List<String> ids;
+	private List<Path> paths;
+	private String fdpToken;
+	
+	
+	public List<String> getIds() {
+		return ids;
+	}
+	
+	public void setIds(List<String> ids) {
+		this.ids = ids;
+	}
+	
+	public List<Path> getPaths() {
+		return paths;
+	}
+	
+	public void setPaths(List<Path> paths) {
+		this.paths = paths;
+	}
+
+	public String getFdpToken() {
+		return fdpToken;
+	}
+
+	public void setFdpToken(String fdpToken) {
+		this.fdpToken = fdpToken;
+	}
+
+	
+	
+
+}
diff --git a/src/main/java/com/smartharvester/model/mapping/request/MappingRequest.java b/src/main/java/com/smartharvester/model/mapping/request/MappingRequest.java
index bb9287f..38817e2 100644
--- a/src/main/java/com/smartharvester/model/mapping/request/MappingRequest.java
+++ b/src/main/java/com/smartharvester/model/mapping/request/MappingRequest.java
@@ -5,37 +5,33 @@ import com.smartharvester.model.mapping.Path;
 import java.util.List;
 
 public class MappingRequest {
-	
-	private List<String> ids;
-	private List<Path> paths;
-	private String fdpToken;
-	
-	
-	public List<String> getIds() {
-		return ids;
-	}
-	
-	public void setIds(List<String> ids) {
-		this.ids = ids;
-	}
-	
-	public List<Path> getPaths() {
-		return paths;
-	}
-	
-	public void setPaths(List<Path> paths) {
-		this.paths = paths;
-	}
-
-	public String getFdpToken() {
-		return fdpToken;
-	}
-
-	public void setFdpToken(String fdpToken) {
-		this.fdpToken = fdpToken;
-	}
-
-	
-	
 
+    private List<Concepts> dataConcepts;
+    private List<Path> paths;
+    private String fdpToken;
+
+
+
+    public List<Concepts> getDataConcepts() {
+        return dataConcepts;
+    }
+
+    public void setDataConcepts(List<Concepts> dataConcepts) {
+        this.dataConcepts = dataConcepts;
+    }
+    public List<Path> getPaths() {
+        return paths;
+    }
+
+    public void setPaths(List<Path> paths) {
+        this.paths = paths;
+    }
+
+    public String getFdpToken() {
+        return fdpToken;
+    }
+
+    public void setFdpToken(String fdpToken) {
+        this.fdpToken = fdpToken;
+    }
 }
diff --git a/src/main/java/com/smartharvester/service/ElasticSearchService.java b/src/main/java/com/smartharvester/service/ElasticSearchService.java
index a865770..78bd19c 100644
--- a/src/main/java/com/smartharvester/service/ElasticSearchService.java
+++ b/src/main/java/com/smartharvester/service/ElasticSearchService.java
@@ -2,7 +2,7 @@ package com.smartharvester.service;
 
 import com.smartharvester.model.elasticSearch.ElasticSearchResponse;
 import com.smartharvester.model.mapping.Path;
-import com.smartharvester.model.mapping.request.MappingRequest;
+import com.smartharvester.model.mapping.request.KeywordRequest;
 import com.smartharvester.model.mapping.response.KeywordResponse;
 import com.smartharvester.model.openapi.OpenApi;
 import org.json.JSONObject;
@@ -96,15 +96,15 @@ public class ElasticSearchService {
         return response;
     }
 
-    public List<KeywordResponse> getKeywords(MappingRequest req, String catalogId, boolean isJsonPath) {
+    public List<KeywordResponse> getKeywords(KeywordRequest req, String catalogId, boolean isJsonPath) {
         OpenApi openApi = this.openApiService.getOpenApiByUUDI(catalogId);
         List<String> urls = new ArrayList<>();
-        req.getIds().parallelStream().forEach(id -> urls.addAll(this.openApiService.getUrls(this.openApiService.getOpenApiByUUDI(catalogId), id)));
+        req.getIds().forEach(id -> urls.addAll(this.openApiService.getUrls(this.openApiService.getOpenApiByUUDI(catalogId), id)));
         List<Path> paths = req.getPaths().stream()
             .filter(path -> path.getProperty().equals("dct:identifier") || path.getProperty().equals("dcat:keyword"))
             .collect(Collectors.toList());
         List<KeywordResponse> result = new ArrayList<>();
-        urls.parallelStream().forEach(url -> {
+        urls.forEach(url -> {
             JSONObject json = new JSONObject();
             List<Map<String, List<String>>> retrievedFromJson = new ArrayList<>();
             try {
@@ -134,7 +134,7 @@ public class ElasticSearchService {
         return result;
     }
 
-    public List<KeywordResponse> getConceptsByKeywords(MappingRequest req, String catalogId, boolean isJsonPath) {
+    public List<KeywordResponse> getConceptsByKeywords(KeywordRequest req, String catalogId, boolean isJsonPath) {
         List<KeywordResponse> keywordResponses = this.getKeywords(req, catalogId, isJsonPath);
         keywordResponses.forEach(k -> {
             List<String> keywords = k.getKeywords();
diff --git a/src/main/java/com/smartharvester/service/MappingService.java b/src/main/java/com/smartharvester/service/MappingService.java
index a72f43c..621a683 100644
--- a/src/main/java/com/smartharvester/service/MappingService.java
+++ b/src/main/java/com/smartharvester/service/MappingService.java
@@ -62,7 +62,7 @@ public class MappingService {
     }
 
 
-    public String buildDataset(URI urlRepo, List<Path> paths, boolean isJsonpath, String catId, String fdpUrl) throws Exception {
+    public String buildDataset(URI urlRepo, List<Path> paths, boolean isJsonpath, String catId, String fdpUrl, List<String> 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);
 
@@ -86,6 +86,9 @@ public class MappingService {
             }
 
         }
+        if (!iris.isEmpty()) {
+            iris.forEach(iri -> datasetString.append(this.write("dcat:theme", iri) ));
+        }
         datasetString.append(".");
         LOGGER.info(datasetString.toString());
         return datasetString.toString();
-- 
GitLab