Skip to content

Commit

Permalink
fixed output for geoproperties
Browse files Browse the repository at this point in the history
  • Loading branch information
benni committed Oct 29, 2019
1 parent 8a4757c commit e5dcc57
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public interface NGSIConstants {
public final static String NGSI_LD_HAS_OBJECT = "https://uri.etsi.org/ngsi-ld/hasObject";
public final static String NGSI_LD_COORDINATES = "https://uri.etsi.org/ngsi-ld/coordinates";
public final static String NGSI_LD_GEOPROPERTY = "https://uri.etsi.org/ngsi-ld/GeoProperty";
public final static String NGSI_LD_GEOPROPERTY_SHORT = "GeoProperty";
public final static String NGSI_LD_LOCATION = "https://uri.etsi.org/ngsi-ld/location";
public final static String NGSI_LD_CREATED_AT = "https://uri.etsi.org/ngsi-ld/createdAt";
public final static String NGSI_LD_MODIFIED_AT = "https://uri.etsi.org/ngsi-ld/modifiedAt";
Expand Down Expand Up @@ -86,7 +87,8 @@ public interface NGSIConstants {

public final static String GEO_JSON_COORDINATES = "coordinates";
public final static String GEO_JSON_TYPE = "type";
public final static String VALUE = "@value";

public final static String VALUE = "value";

// Entity validation attribute types
public final static String VALID_NGSI_ATTRIBUTE_TYPES = "Relationship,Property,DateTime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,10 @@ public class ContextResolverBasic {
@PostConstruct
private void setup() {
try {
// defaultOptions.setCompactArrays(false);
CORE_CONTEXT_URL = new URI(CORE_CONTEXT_URL_STR);

String json = httpUtils.doGet(CORE_CONTEXT_URL);
CORE_CONTEXT = (Map<String, Object>) ((Map) JsonUtils.fromString(json)).get("@context");
// DEFAULT_CONTEXT_URL = new URI(
// "https://forge.etsi.org/gitlab/NGSI-LD/NGSI-LD/raw/master/defaultContext/defaultContextVocab.jsonld");
// json = httpUtils.doGet(DEFAULT_CONTEXT_URL);
// DEFAULT_CONTEXT = (Map<String, Object>) ((Map)
// JsonUtils.fromString(json)).get("@context");
BASE_CONTEXT.putAll(CORE_CONTEXT);
// BASE_CONTEXT.putAll(DEFAULT_CONTEXT);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (IOException e) {
Expand Down Expand Up @@ -228,25 +220,25 @@ private void protectGeoProps(Map<String, Object> objMap, ArrayList<Object> usedC

}

private void unprotectGeoProps(Object json, List<Object> fullContext) throws JsonParseException, IOException {
private void unprotectGeoProps(Object json) throws JsonParseException, IOException {
if (json instanceof Map) {
unprotectGeoProps((Map<String, Object>) json, fullContext);
unprotectGeoProps((Map<String, Object>) json);
} else if (json instanceof List) {
unprotectGeoProps((List) json, fullContext);
unprotectGeoProps((List) json);
}

}

private void unprotectGeoProps(Map<String, Object> objMap, List<Object> usedContext)
private void unprotectGeoProps(Map<String, Object> objMap)
throws JsonParseException, IOException {
boolean typeFound = false;
Object value = null;
for (Entry<String, Object> mapEntry : objMap.entrySet()) {
String key = mapEntry.getKey();
Object mapValue = mapEntry.getValue();
if (NGSIConstants.JSON_LD_TYPE.equals(key) && (mapValue instanceof List)) {
Object tempObj = ((List) mapValue).get(0);
if (NGSIConstants.NGSI_LD_GEOPROPERTY.equals(tempObj)) {
if (NGSIConstants.QUERY_PARAMETER_TYPE.equals(key) && (mapValue instanceof String)) {

if (NGSIConstants.NGSI_LD_GEOPROPERTY_SHORT.equals(mapValue)) {
typeFound = true;
}
// if(tempObj instanceof Map) {
Expand All @@ -255,38 +247,33 @@ private void unprotectGeoProps(Map<String, Object> objMap, List<Object> usedCont
// }
// }

} else if (NGSIConstants.NGSI_LD_HAS_VALUE.equals(key)) {
} else if (NGSIConstants.VALUE.equals(key)) {
value = mapValue;
} else {
if (mapValue instanceof Map) {
unprotectGeoProps((Map<String, Object>) mapValue, usedContext);
unprotectGeoProps((Map<String, Object>) mapValue);
} else if (mapValue instanceof List) {
unprotectGeoProps((List) mapValue, usedContext);
unprotectGeoProps((List) mapValue);
}
}
}

if (typeFound && value != null
&& ((Map) (((List) value).get(0))).get(NGSIConstants.JSON_LD_VALUE) instanceof String) {
Map<String, Object> temp = (Map<String, Object>) JsonUtils
.fromString((String) (((Map) (((List) value).get(0))).get(NGSIConstants.JSON_LD_VALUE)));
temp.put(NGSIConstants.JSON_LD_CONTEXT, usedContext);
List<Object> expanded = JsonLdProcessor.expand(temp);
// temp.replace("\\\"", "\"");
objMap.put(NGSIConstants.NGSI_LD_HAS_VALUE, expanded);
System.out.println();
if (typeFound && value != null) {

objMap.put(NGSIConstants.VALUE, JsonUtils.fromString((String) value));

}

}

private void unprotectGeoProps(List<Object> objList, List<Object> usedContext)
private void unprotectGeoProps(List<Object> objList)
throws JsonParseException, IOException {
for (Object entry : objList) {
if (entry instanceof Map) {

unprotectGeoProps((Map<String, Object>) entry, usedContext);
unprotectGeoProps((Map<String, Object>) entry);
} else if (entry instanceof List) {
unprotectGeoProps((List) entry, usedContext);
unprotectGeoProps((List) entry);
} else {
// don't care for now i think
}
Expand Down Expand Up @@ -339,7 +326,8 @@ private CompactedJson compact(Object json, Map<String, Object> context, List<Obj
if (context != null && !context.isEmpty()) {
fullContext.add(context);
}
fullContext.add(BASE_CONTEXT);
fullContext.add(CORE_CONTEXT_URL_STR);
// fullContext.add(BASE_CONTEXT);
CompactedJson result = new CompactedJson();
int hash = json.hashCode();
if (context.containsKey(IS_FULL_VALID)) {
Expand All @@ -349,9 +337,9 @@ private CompactedJson compact(Object json, Map<String, Object> context, List<Obj
}
context.remove(IS_FULL_VALID);
try {
unprotectGeoProps(json, fullContext);

Map<String, Object> tempResult = JsonLdProcessor.compact(json, fullContext, defaultOptions);

unprotectGeoProps(tempResult);
if (tempResult.containsKey("@graph")) {
// we are in a multiresult set
Object atContext = tempResult.get("@context");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Subscription deserialize(JsonElement json, Type typeOfT, JsonDeserializat
EntityInfo entity = new EntityInfo();
if (obj.has(NGSIConstants.NGSI_LD_ID_PATTERN)) {
entity.setIdPattern(obj.get(NGSIConstants.NGSI_LD_ID_PATTERN).getAsJsonArray().get(0)
.getAsJsonObject().get(NGSIConstants.VALUE).getAsString());
.getAsJsonObject().get(NGSIConstants.JSON_LD_VALUE).getAsString());
}
if (obj.has(NGSIConstants.JSON_LD_ID)) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static GeoProperty parseGeoProperty(JsonObject jsonProp, String key) {
prop.setGeoValue( DataSerializer.getGeojsonGeometry(primitive.getAsString()) );
}
} else {
JsonElement atValue = propValue.getAsJsonObject().get(NGSIConstants.VALUE);
JsonElement atValue = propValue.getAsJsonObject().get(NGSIConstants.JSON_LD_VALUE);
if (atValue != null) {
if (atValue.isJsonPrimitive()) {
JsonPrimitive primitive = atValue.getAsJsonPrimitive();
Expand Down Expand Up @@ -210,8 +210,8 @@ public void accept(JsonElement t) {
return result;
} else if (element.isJsonObject()) {
JsonObject jsonObj = element.getAsJsonObject();
if (jsonObj.has(NGSIConstants.VALUE)) {
JsonPrimitive atValue = jsonObj.get(NGSIConstants.VALUE).getAsJsonPrimitive();
if (jsonObj.has(NGSIConstants.JSON_LD_VALUE)) {
JsonPrimitive atValue = jsonObj.get(NGSIConstants.JSON_LD_VALUE).getAsJsonPrimitive();
if (atValue.isBoolean()) {
return atValue.getAsBoolean();
}
Expand All @@ -222,7 +222,7 @@ public void accept(JsonElement t) {
return atValue.getAsString();
}

return jsonObj.get(NGSIConstants.VALUE).getAsString();
return jsonObj.get(NGSIConstants.JSON_LD_VALUE).getAsString();
} else {
HashMap<String, List<Object>> result = new HashMap<String, List<Object>>();
for (Entry<String, JsonElement> entry : jsonObj.entrySet()) {
Expand Down Expand Up @@ -262,7 +262,7 @@ public static JsonArray getValueArray(Boolean value) {
private static JsonArray getValueArray(JsonElement value) {
JsonArray result = new JsonArray();
JsonObject temp = new JsonObject();
temp.add(NGSIConstants.VALUE, value);
temp.add(NGSIConstants.JSON_LD_VALUE, value);
result.add(temp);
return result;
}
Expand Down Expand Up @@ -429,7 +429,7 @@ private static JsonElement getJson(List<Object> values, JsonSerializationContext
// }
else {
obj = new JsonObject();
obj.add(NGSIConstants.VALUE, context.serialize(value));
obj.add(NGSIConstants.JSON_LD_VALUE, context.serialize(value));

}
top.add(obj);
Expand Down Expand Up @@ -483,7 +483,7 @@ public static JsonElement getJson(GeoProperty property, JsonSerializationContext
top.add(NGSIConstants.JSON_LD_TYPE, type);
JsonArray value = new JsonArray();
JsonObject objValue = new JsonObject();
objValue.add(NGSIConstants.VALUE, context.serialize(property.getValue()));
objValue.add(NGSIConstants.JSON_LD_VALUE, context.serialize(property.getValue()));
value.add(objValue);
top.add(NGSIConstants.NGSI_LD_HAS_VALUE, value);
if (property.getObservedAt() > 0) {
Expand Down

0 comments on commit e5dcc57

Please sign in to comment.