Skip to content
Snippets Groups Projects
Commit 26f06a01 authored by Baptiste Toulemonde's avatar Baptiste Toulemonde
Browse files

service to create rdf completed

parent 3e0635a0
No related branches found
No related tags found
1 merge request!11Feature/get values
...@@ -9,16 +9,21 @@ import java.net.HttpURLConnection; ...@@ -9,16 +9,21 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamSource;
import org.apache.commons.lang3.ArrayUtils;
import org.elasticsearch.common.io.stream.Writeable;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import com.smartharvester.model.Path; import com.smartharvester.model.Path;
...@@ -26,8 +31,19 @@ import com.smartharvester.model.Path; ...@@ -26,8 +31,19 @@ import com.smartharvester.model.Path;
import io.swagger.util.Json; import io.swagger.util.Json;
public class MappingService { public class MappingService {
public static String getDatasetString(String catId, String properties, String fdpUrl) {
String dataset = "@prefix dcat: <http://www.w3.org/ns/dcat#>. \n"
+ "@prefix dct: <http://purl.org/dc/terms/>.\n" + "@prefix adms: <http://www.w3.org/ns/adms#> .\n"
+ "@prefix dqv: <http://www.w3.org/ns/dqv#> .\n" + "@prefix geodcat: <http://data.europa.eu/930/>. \n"
+ "@prefix prov: <http://www.w3.org/ns/prov#>.\n"
+ "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.\n"
+ "@prefix language: <http://id.loc.gov/vocabulary/iso639-1/>.\n" + "@prefix s: <" + fdpUrl + "/>.\n"
+ "@prefix c: <" + fdpUrl + "/catalog/>.\n" + "s:new \n" + "a dcat:Dataset, dcat:Resource;\n"
+ "dct:isPartOf c:" + catId + ";\n" + properties + ".";
return dataset;
}
public static JSONObject getJson(String urlRepo) { public static JSONObject getJson(String urlRepo) throws JSONException {
URL url = null; URL url = null;
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
String result = null; String result = null;
...@@ -48,65 +64,107 @@ public class MappingService { ...@@ -48,65 +64,107 @@ public class MappingService {
} }
JSONObject json = null; JSONObject json = null;
try {
json = new JSONObject(result); json = new JSONObject(result);
} catch (JSONException e) {
e.printStackTrace();
}
return json; return json;
} }
public static JSONObject getValues(String urlRepo) throws IOException { public static String write(String indentifier, String value) {
String property = "";
switch (indentifier) {
case "dcat:landingPage":
case "dcat:theme":
case "dcat:contactPoint":
case "dcat:accessURL":
case "dcat:downloadURL":
case "dct:language":
property += indentifier + " <" + value + ">;\n";
break;
case "dct:issued":
case "dct:modified":
property += indentifier + " \"" + value + "\"^^xsd:dateTime;\n";
break;
default:
property += indentifier + " \"" + value + "\";\n";
break;
}
JSONObject json = getJson(urlRepo); return property;
return json;
} }
public String createRDF(Path[] paths, String urlRepo) { public static String createRDF(String urlRepo, Path[] paths, String catalogId, String fdpUrl) throws JSONException {
String properties = null; String properties = "";
JSONObject json = null; JSONObject json = null;
try {
json = getValues(urlRepo);
for (Path path : paths) {
String[] array = path.getPath().split(" : ");
if (array.length == 0) {
if (json.optJSONArray(array[0]) != null) {
JSONArray jsonArray = json.getJSONArray(array[0]);
for (int i = 0; i < jsonArray.length(); i++) {
properties += path.getProperty() + " \"" + jsonArray.get(i) + "\" ;\n";
}
} else { json = getJson(urlRepo);
properties += path.getProperty() + " \"" + json.getString(array[0]) + "\" ;\n";
for (Path path : paths) {
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));
for (int j = 0; j < jsonA.length(); j++) {
properties += write(path.getProperty(), jsonA.getString(j));
} }
} else { } else {
JSONObject jsonT = json; properties += write(path.getProperty(), jsonT.getString(list.get(0)));
for (int i = 0; i < array.length -1 ; i++) { }
if (jsonT.get(array[i]) instanceof JSONObject) { } else {
jsonT = jsonT.getJSONObject(array[i]); for (int i = 0; i < list.size(); i++) {
} else if (jsonT.get(array[i]) instanceof JSONArray) { if (i < list.size() - 1) {
jsonT = jsonT.getJSONArray(array[i]).toJSONObject(null); if (jsonT.optJSONObject(list.get(i)) != null) {
} else if (jsonT.get(array[i]) instanceof String) { jsonT = jsonT.getJSONObject(list.get(i));
properties += path.getProperty() + " \"" + json.getString(array[0]) + "\" ;\n"; } else if (jsonT.optJSONArray(list.get(i)) != null) {
JSONArray jsonA = jsonT.getJSONArray(list.get(i));
if (i < list.size() - 2) {
jsonT = jsonA.getJSONObject(Integer.parseInt(list.get(i + 1)));
list.remove(i + 1);
}
} }
} } else {
if (jsonT.optJSONArray(array[array.length - 1]) != null) { if (jsonT.optJSONArray(list.get(i)) != null) {
JSONArray jsonArray = json.getJSONArray(array[array.length - 1]); JSONArray jsonA = jsonT.getJSONArray(list.get(i));
for (int i = 0; i < jsonArray.length(); i++) { for (int j = 0; j < jsonA.length(); j++) {
properties += path.getProperty() + " \"" + jsonArray.get(i) + "\" ;\n"; properties += write(path.getProperty(), jsonA.getString(j));
}
break;
} else if (jsonT.optJSONArray(list.get(list.size() - 2)) != null) {
JSONArray jsonA = jsonT.getJSONArray(list.get(list.size() - 2));
properties += write(path.getProperty(), jsonA.getString(Integer.parseInt(list.get(i))));
break;
} else if (jsonT.optString(list.get(i)) != null && jsonT.has(list.get(i))) {
properties += write(path.getProperty(), jsonT.getString(list.get(i)));
break;
} else if (jsonT instanceof JSONObject) {
Iterator keys = jsonT.keys();
while (keys.hasNext()) {
String key = keys.next().toString();
if (jsonT.optJSONArray(key) != null) {
JSONArray jsonA = jsonT.getJSONArray(key);
for (int j = 0; j < jsonA.length(); j++) {
String value = jsonA.getJSONObject(j).getString(list.get(i));
properties += write(path.getProperty(), value);
}
}
}
} }
} else if (jsonT.get(array[array.length - 1]) instanceof String ){
properties += path.getProperty() + " \"" + jsonT.getString(array[array.length - 1]) + "\" ;\n";
} }
} }
} }
} catch (IOException | JSONException e) {
e.printStackTrace();
} }
return properties; return getDatasetString(catalogId, properties, fdpUrl);
} }
private static String readStream(InputStream input) throws IOException { private static String readStream(InputStream input) throws IOException {
...@@ -120,8 +178,28 @@ public class MappingService { ...@@ -120,8 +178,28 @@ public class MappingService {
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
System.out.println(getValues(
"https://sextant.ifremer.fr/geonetwork/srv/fre/qi?buildSummary=false&_content_type=json&fast=index&_uuid=7691f8b8-1193-4aef-8e7e-0d7a9c88c057")); Path path1 = new Path();
Path path2 = new Path();
Path path3 = new Path();
Path path4 = new Path();
path1.setProperty("dct:title");
path1.setPath("response : docs : 0 : title");
path2.setProperty("dct:description");
path2.setPath("mresponse : docs : 0 : further_info_url : 0");
path3.setProperty("dct:publisher");
path3.setPath("response : docs : 0 : data_node");
path4.setPath("response : docs : 0 : geo");
path4.setProperty("dct:spatial");
Path[] paths = { path1, path2, path3, path4 };
try {
System.out.println(createRDF(
"https://esgf-data.dkrz.de/esg-search//search?format=application%2Fsolr%2Bjson&id=CMIP6.ScenarioMIP.DKRZ.MPI-ESM1-2-HR.ssp585.r1i1p1f1.Amon.tasmin.gn.v20190710%7Cesgf3.dkrz.de",
paths, "121321231231231", "https://f2ds.eosc-pillar.eu"));
} catch (JSONException e) {
e.printStackTrace();
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment