diff --git a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java index 3239be7ac84cbde24a759f05a130ca7b6a44333f..939b55cebca166df1885aa9c895c7ce8ebf92fed 100644 --- a/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java +++ b/src/main/java/com/smartharvester/controller/SmartHarvesterMappingController.java @@ -66,50 +66,41 @@ public class SmartHarvesterMappingController { List<String> notPublishedUrl = new ArrayList<String>(); List<Path> distributionPaths = paths.stream().filter(path -> path.getDcatClass().equals("dcat:distribution")) .collect(Collectors.toList()); - - for (String url : urls) { - String datasetId = null; - - List<String> properties = new ArrayList<String>(); - try { - properties = this.mappingService.getValues(url, paths); - String dataset = this.mappingService.getDatasetString(catalogId, properties.get(0), fdpUrl); + + for (String url : urls) { + String datasetId = null; + + List<String> properties = new ArrayList<String>(); + + properties = this.mappingService.getValues(url, paths); + String dataset = this.mappingService.getDatasetString(catalogId, properties.get(0), fdpUrl); + try { + responseEntity = this.mappingService.asyncPostToFdp("/dataset", fdpUrl, dataset, fdpToken); + String location = responseEntity.getHeaders().getLocation().toString(); + datasetId = location.substring(location.indexOf("dataset/") + 8); + HttpStatus statusCode = responseEntity.getStatusCode(); + if (distributionPaths.size() > 0) { + String distribution = this.mappingService.getDistributionString(datasetId, properties.get(1), + fdpUrl); try { - responseEntity = this.mappingService.asyncPostToFdp("/dataset", fdpUrl, dataset, fdpToken); - String location = responseEntity.getHeaders().getLocation().toString(); - datasetId = location.substring(location.indexOf("dataset/") + 8); - HttpStatus statusCode = responseEntity.getStatusCode(); - if (distributionPaths.size() > 0) { - String distribution = this.mappingService.getDistributionString(datasetId, properties.get(1), - fdpUrl); - try { - this.mappingService.asyncPostToFdp("/distribution", fdpUrl, distribution, fdpToken); - } catch (HttpStatusCodeException e) { - LOGGER.warn(e.getMessage()); - } - } - if (statusCode.value() == 201) { - publishedUrl.add(url); - } else { - notPublishedUrl.add(url); - } + this.mappingService.asyncPostToFdp("/distribution", fdpUrl, distribution, fdpToken); } catch (HttpStatusCodeException e) { - notPublishedUrl.add(url + " => " + e.getMessage()); LOGGER.warn(e.getMessage()); } - } catch (JSONException e1) { - - LOGGER.warn(e1.getMessage()); - } - - - + if (statusCode.value() == 201) { + publishedUrl.add(url); + } else { + notPublishedUrl.add(url); + } + } catch (HttpStatusCodeException e) { + notPublishedUrl.add(url + " => " + e.getMessage()); + LOGGER.warn(e.getMessage()); } - LOGGER.info("mapping closed"); - return ResponseEntity.ok(new MappingResponse(publishedUrl, notPublishedUrl)); - + } + LOGGER.info("mapping closed"); + return ResponseEntity.ok(new MappingResponse(publishedUrl, notPublishedUrl)); } diff --git a/src/main/java/com/smartharvester/service/MappingService.java b/src/main/java/com/smartharvester/service/MappingService.java index 827a1f74c1cca4f034a0de075fe7a9fff3cabe2d..9291a2dcd5a099417b35d84f9ffa21ed9e20f08b 100644 --- a/src/main/java/com/smartharvester/service/MappingService.java +++ b/src/main/java/com/smartharvester/service/MappingService.java @@ -120,14 +120,18 @@ public class MappingService { return card.endsWith("n"); } - public List<String> getValues(String urlRepo, List<Path> paths) throws JSONException { + public List<String> getValues(String urlRepo, List<Path> paths) { List<String> result = new ArrayList<String>(); String datasetProperties = ""; String distributionProperties = ""; JSONObject json = null; - json = getJson(urlRepo); + try { + json = getJson(urlRepo); + } catch (JSONException e1) { + e1.printStackTrace(); + } if (paths.stream().filter(e -> e.getDcatClass().equals("dcat:dataset")) .filter(e -> e.getProperty().equals("dct:hasVersion")).collect(Collectors.toList()).size() == 0) { @@ -135,13 +139,14 @@ public class MappingService { } distributionProperties += "dct:hasVersion \"null\";\n"; for (Path path : paths) { + try { String[] array = path.getPath().split(" : "); List<String> list = new ArrayList<String>(Arrays.asList(array)); JSONObject jsonT = json; if (list.size() == 1) { if (jsonT.optJSONArray(list.get(0)) != null) { - JSONArray jsonA = jsonT.getJSONArray(list.get(0)); + JSONArray jsonA = jsonT.optJSONArray(list.get(0)); if (isReplicable(path.getCard())) { for (int j = 0; j < jsonA.length(); j++) { if (path.getDcatClass().equals("dcat:dataset")) { @@ -169,9 +174,9 @@ public class MappingService { for (int i = 0; i < list.size(); i++) { if (i < list.size() - 1) { if (jsonT.optJSONObject(list.get(i)) != null) { - jsonT = jsonT.getJSONObject(list.get(i)); + jsonT = jsonT.optJSONObject(list.get(i)); } else if (jsonT.optJSONArray(list.get(i)) != null) { - JSONArray jsonA = jsonT.getJSONArray(list.get(i)); + JSONArray jsonA = jsonT.optJSONArray(list.get(i)); if (i < list.size() - 2) { jsonT = jsonA.getJSONObject(Integer.parseInt(list.get(i + 1))); @@ -181,41 +186,50 @@ public class MappingService { } } else { if (jsonT.optJSONArray(list.get(i)) != null) { - JSONArray jsonA = jsonT.getJSONArray(list.get(i)); - if (isReplicable(path.getCard())) { - for (int j = 0; j < jsonA.length(); j++) { + if (!jsonT.has(list.get(i))) { + if (path.getDcatClass().equals("dcat:dataset")) { + datasetProperties += write(path.getProperty(), "undefined"); + } else if (path.getDcatClass().equals("dcat:distribution")) { + distributionProperties += write(path.getProperty(), "undefined"); + } + } else { + JSONArray jsonA = jsonT.optJSONArray(list.get(i)); + if (isReplicable(path.getCard())) { + for (int j = 0; j < jsonA.length(); j++) { + if (path.getDcatClass().equals("dcat:dataset")) { + datasetProperties += write(path.getProperty(), jsonA.optString(j)); + } else if (path.getDcatClass().equals("dcat:distribution")) { + distributionProperties += write(path.getProperty(), jsonA.optString(j)); + } + } + break; + } else { if (path.getDcatClass().equals("dcat:dataset")) { - datasetProperties += write(path.getProperty(), jsonA.getString(j)); + datasetProperties += write(path.getProperty(), jsonA.optString(0)); } else if (path.getDcatClass().equals("dcat:distribution")) { - distributionProperties += write(path.getProperty(), jsonA.getString(j)); + distributionProperties += write(path.getProperty(), jsonA.optString(0)); } } - } else { - if (path.getDcatClass().equals("dcat:dataset")) { - datasetProperties += write(path.getProperty(), jsonA.getString(0)); - } else if (path.getDcatClass().equals("dcat:distribution")) { - distributionProperties += write(path.getProperty(), jsonA.getString(0)); - } - } - - break; + break; + } + } else if (jsonT.optJSONArray(list.get(list.size() - 2)) != null) { - JSONArray jsonA = jsonT.getJSONArray(list.get(list.size() - 2)); + JSONArray jsonA = jsonT.optJSONArray(list.get(list.size() - 2)); if (path.getDcatClass().equals("dcat:dataset")) { datasetProperties += write(path.getProperty(), - jsonA.getString(Integer.parseInt(list.get(i)))); + jsonA.optString(Integer.parseInt(list.get(i)))); } else if (path.getDcatClass().equals("dcat:distribution")) { distributionProperties += write(path.getProperty(), - jsonA.getString(Integer.parseInt(list.get(i)))); + jsonA.optString(Integer.parseInt(list.get(i)))); } break; } else if (jsonT.optString(list.get(i)) != null && jsonT.has(list.get(i))) { if (path.getDcatClass().equals("dcat:dataset")) { - datasetProperties += write(path.getProperty(), jsonT.getString(list.get(i))); + datasetProperties += write(path.getProperty(), jsonT.optString(list.get(i))); } else if (path.getDcatClass().equals("dcat:distribution")) { - distributionProperties += write(path.getProperty(), jsonT.getString(list.get(i))); + distributionProperties += write(path.getProperty(), jsonT.optString(list.get(i))); } break; } else if (jsonT instanceof JSONObject) { @@ -223,7 +237,7 @@ public class MappingService { while (keys.hasNext()) { String key = keys.next().toString(); if (jsonT.optJSONArray(key) != null) { - JSONArray jsonA = jsonT.getJSONArray(key); + JSONArray jsonA = jsonT.optJSONArray(key); if (isReplicable(path.getCard())) { for (int j = 0; j < jsonA.length(); j++) { String value = jsonA.getJSONObject(j).getString(list.get(i)); @@ -249,6 +263,13 @@ public class MappingService { } } + } catch (JSONException e1) { + if (path.getDcatClass().equals("dcat:dataset")) { + datasetProperties += write(path.getProperty(), "undefined"); + } else if (path.getDcatClass().equals("dcat:distribution")) { + distributionProperties += write(path.getProperty(), "undefined"); + } + } }