Commit 1f874955 authored by j.hoerdt's avatar j.hoerdt
Browse files

correctly escape description to be valid json

parent 3eee0686
...@@ -7,11 +7,12 @@ import urllib.request ...@@ -7,11 +7,12 @@ import urllib.request
import yaml import yaml
#pid_to_test = "21.11138/2f92c6f6-891b-46ec-9b16-ca0be3342589" #pid_to_test = "21.11138/2f92c6f6-891b-46ec-9b16-ca0be3342589"
#pid_to_test = "21.11138/a6442237-381e-4e97-92f7-74b7749350da" pid_to_test = "21.11138/a6442237-381e-4e97-92f7-74b7749350da"
pid_to_test = "21.11138/c4c0f351-96e2-4e93-bf1f-0fd73e828300" #pid_to_test = "21.11138/c4c0f351-96e2-4e93-bf1f-0fd73e828300"
base_url = "https://hdl.handle.net/" base_url = "https://hdl.handle.net/"
exit_code = 0
pid_content = json.loads(urllib.request.urlopen(f"{base_url}api/handles/{pid_to_test}").read()) pid_content = json.loads(urllib.request.urlopen(f"{base_url}api/handles/{pid_to_test}").read())
for record in pid_content["values"]: for record in pid_content["values"]:
if record["index"] not in [100,9]: if record["index"] not in [100,9]:
...@@ -23,4 +24,6 @@ for record in pid_content["values"]: ...@@ -23,4 +24,6 @@ for record in pid_content["values"]:
jsonschema.validate(instance=data, schema=json.loads(type_info["validationSchema"])) jsonschema.validate(instance=data, schema=json.loads(type_info["validationSchema"]))
except Exception as e: except Exception as e:
print(f"could not validate due to exception: {e}") print(f"could not validate due to exception: {e}")
exit_code = 1
sys.exit(exit_code);
...@@ -58,14 +58,22 @@ class Sensor { ...@@ -58,14 +58,22 @@ class Sensor {
body.add("values", new JsonArray()); body.add("values", new JsonArray());
{ {
var admin_data = new JsonObject(); var admin_value = new JsonObject();
var admin_data_value = new JsonObject(); admin_value.addProperty("index", "100");
admin_data_value.addProperty("index", "300"); admin_value.addProperty("type", "HS_ADMIN");
admin_data_value.addProperty("handle", "21.11138/USER01"); {
admin_data_value.addProperty("permissions", "110011111111"); var data = new JsonObject();
admin_data.addProperty("format", "admin"); data.addProperty("format", "admin");
admin_data.add("value", admin_data_value); {
add_attribute(body, "100", "HS_ADMIN", admin_data); var value = new JsonObject();
value.addProperty("index", "300");
value.addProperty("handle", "21.11138/USER01");
value.addProperty("permissions", "110011111111");
data.add("value", value);
}
admin_value.add("data", data);
}
body.getAsJsonArray("values").add(admin_value);
} }
...@@ -73,69 +81,67 @@ class Sensor { ...@@ -73,69 +81,67 @@ class Sensor {
add_attribute(body, add_attribute(body,
"1", "1",
"21.T11148/8eb858ee0b12e8e463a5", "21.T11148/8eb858ee0b12e8e463a5",
new JsonPrimitive("{\"identifier-general-with-type\":{\"identifierValue\":\""+ rec.get("s.pid").asString() +"\", \"identifierType\":\"Handle\"}}") "{\"identifier-general-with-type\":{\"identifierValue\":\""+ rec.get("s.pid").asString() +"\", \"identifierType\":\"Handle\"}}"
); );
} }
add_attribute(body, add_attribute(body,
"2", "2",
"21.T11148/22c62082a4d2d9ae2602", "21.T11148/22c62082a4d2d9ae2602",
new JsonPrimitive("{\"Dates\":[{\"date\":{\"dateValue\":\"" + rec.get("earliest_msg").asLocalDate().toString() + "\",\"dateType\":\"Commissioned\"}},{\"date\":{\"dateValue\":\"" + rec.get("latest_msg").asLocalDate().toString() + "\"}}]}") "{\"Dates\":[{\"date\":{\"dateValue\":\"" + rec.get("earliest_msg").asLocalDate().toString() + "\",\"dateType\":\"Commissioned\"}},{\"date\":{\"dateValue\":\"" + rec.get("latest_msg").asLocalDate().toString() + "\"}}]}"
); );
add_attribute(body, add_attribute(body,
"3", "3",
"21.T11148/709a23220f2c3d64d1e1", "21.T11148/709a23220f2c3d64d1e1",
new JsonPrimitive('\"' + rec.get("s.sensor_id").asString() + '\"') '\"' + rec.get("s.sensor_id").asString() + '\"'
); );
add_attribute(body, add_attribute(body,
"4", "4",
"21.T11148/c1a0ec5ad347427f25d6", "21.T11148/c1a0ec5ad347427f25d6",
new JsonPrimitive("{\"Model\":{\"modelName\":\"" + rec.get("type.sensor_type").asString() + "\", \"modelIdentifier\":{\"modelIdentifierValue\":\"" + rec.get("type.sensor_type").asString() + "\"}}}") "{\"Model\":{\"modelName\":\"" + rec.get("type.sensor_type").asString() + "\", \"modelIdentifier\":{\"modelIdentifierValue\":\"" + rec.get("type.sensor_type").asString() + "\"}}}"
); );
add_attribute(body, add_attribute(body,
"5", "5",
"21.T11148/9a15a4735d4bda329d80", "21.T11148/9a15a4735d4bda329d80",
new JsonPrimitive("\"https://sensor.community\"") "\"https://sensor.community\""
); );
add_attribute(body, add_attribute(body,
"6", "6",
"21.T11148/4eaec4bc0f1df68ab2a7", "21.T11148/4eaec4bc0f1df68ab2a7",
new JsonPrimitive("{\"Owners\":[{\"owner\":{\"ownerName\":\"SensorCommunity\",\"ownerContact\":\"contact@open-forecast.eu\"}}]}") "{\"Owners\":[{\"owner\":{\"ownerName\":\"SensorCommunity\",\"ownerContact\":\"contact@open-forecast.eu\"}}]}"
); );
add_attribute(body, add_attribute(body,
"7", "7",
"21.T11148/1f3e82ddf0697a497432", "21.T11148/1f3e82ddf0697a497432",
new JsonPrimitive("{\"Manufacturers\":[{\"manufacturer\":{\"manufacturerName\":\""+ rec.get("type.manufacturer").asString() +"\"}}]}") "{\"Manufacturers\":[{\"manufacturer\":{\"manufacturerName\":\""+ rec.get("type.manufacturer").asString() +"\"}}]}"
); );
if (!rec.get("type.description").isNull()) { if (!rec.get("type.description").isNull()) {
add_attribute(body, add_attribute(body,
"8", "8",
"21.T11148/55f8ebc805e65b5b71dd", "21.T11148/55f8ebc805e65b5b71dd",
new JsonPrimitive("\""+ rec.get("type.description").asString() +"\"") new JsonPrimitive(rec.get("type.description").asString()).toString()
); );
} }
add_attribute(body, add_attribute(body,
"9", "9",
"URL", "URL",
new JsonPrimitive("https://sensor.community/") "https://sensor.community/"
); );
add_attribute(body, add_attribute(body,
"10", "10",
"21.T11148/72928b84e060d491ee41", "21.T11148/72928b84e060d491ee41",
new JsonPrimitive( "{\"MeasuredVariables\":[" +
"{\"MeasuredVariables\":[" + rec.get("collect(measured_variable.variable)")
rec.get("collect(measured_variable.variable)") .asList(Values.ofString())
.asList(Values.ofString()) .stream()
.stream() .map(mv -> "{\"measuredVariable\":\"" + mv + "\"}")
.map(mv -> "{\"measuredVariable\":\"" + mv + "\"}") .collect(Collectors.joining(",")) +
.collect(Collectors.joining(",")) + "]}"
"]}"
)
); );
if (!rec.get("type.classification").isNull()) { if (!rec.get("type.classification").isNull()) {
add_attribute(body, add_attribute(body,
"11", "11",
"21.T11148/f76ad9d0324302fc47dd", "21.T11148/f76ad9d0324302fc47dd",
new JsonPrimitive('\"' + rec.get("type.classification").asString() + '\"') '\"' + rec.get("type.classification").asString() + '\"'
); );
} }
...@@ -167,11 +173,11 @@ class Sensor { ...@@ -167,11 +173,11 @@ class Sensor {
} }
} }
private void add_attribute(JsonObject body, String index, String type, JsonElement data) { private void add_attribute(JsonObject body, String index, String type, String data) {
JsonObject obj = new JsonObject(); var obj = new JsonObject();
obj.addProperty("index", index); obj.addProperty("index", index);
obj.addProperty("type", type); obj.addProperty("type", type);
obj.add("data", data); obj.addProperty("data", data);
body.getAsJsonArray("values").add(obj); body.getAsJsonArray("values").add(obj);
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment