Skip to content

Commit

Permalink
Merge pull request #1150 from frbattid/bug/1144_insert_null_in_ckan_c…
Browse files Browse the repository at this point in the history
…olumn_empty_value__1.2.0

bug/1144_insert_null_in_ckan_column_empty_value__1.2.0
  • Loading branch information
fgalan authored Sep 7, 2016
2 parents 6fefbdc + d01f1b9 commit 461c047
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- [cygnus-common][bug] Release connection upon persistence error (#1139)
- [cygnus][hardening] Add architecture documentation (#1127)
- [cygnus-common][bug] Insert NULL in CKAN columns when an empty attribute value ("", null, {} or []) or empty attribute metadata (null or []) is notified (#1144)
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ public String getType() {
* @return The context value for this context attribute in String format.
*/
public String getContextValue(boolean asStringRepresentation) {
if (value.isJsonObject()) {
if (value.isJsonNull()) {
return null;
} else if (value.isJsonObject()) {
return value.getAsJsonObject().toString();
} else if (value.isJsonArray()) {
return value.getAsJsonArray().toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,16 +316,10 @@ public void aggregate(NGSIEvent cygnusEvent) throws Exception {
+ "\"" + NGSIConstants.ENTITY_ID + "\": \"" + entityId + "\","
+ "\"" + NGSIConstants.ENTITY_TYPE + "\": \"" + entityType + "\","
+ "\"" + NGSIConstants.ATTR_NAME + "\": \"" + attrName + "\","
+ "\"" + NGSIConstants.ATTR_TYPE + "\": \"" + attrType + "\","
+ "\"" + NGSIConstants.ATTR_VALUE + "\": " + attrValue;

// metadata is an special case, because CKAN doesn't support empty array, e.g. "[ ]"
// (http://stackoverflow.com/questions/24207065/inserting-empty-arrays-in-json-type-fields-in-datastore)
if (!attrMetadata.equals(CommonConstants.EMPTY_MD)) {
record += ",\"" + NGSIConstants.ATTR_MD + "\": " + attrMetadata + "}";
} else {
record += "}";
} // if else
+ "\"" + NGSIConstants.ATTR_TYPE + "\": \"" + attrType + "\""
+ (isSpecialValue(attrValue) ? "" : ",\"" + NGSIConstants.ATTR_VALUE + "\": " + attrValue)
+ (isSpecialMetadata(attrMetadata) ? "" : ",\"" + NGSIConstants.ATTR_MD + "\": " + attrMetadata)
+ "}";

if (records.isEmpty()) {
records += record;
Expand Down Expand Up @@ -383,13 +377,8 @@ public void aggregate(NGSIEvent cygnusEvent) throws Exception {
+ attrType + ")");

// create part of the column with the current attribute (a.k.a. a column)
record += ",\"" + attrName + "\": " + attrValue;

// metadata is an special case, because CKAN doesn't support empty array, e.g. "[ ]"
// (http://stackoverflow.com/questions/24207065/inserting-empty-arrays-in-json-type-fields-in-datastore)
if (!attrMetadata.equals(CommonConstants.EMPTY_MD)) {
record += ",\"" + attrName + "_md\": " + attrMetadata;
} // if
record += (isSpecialValue(attrValue) ? "" : ",\"" + attrName + "\": " + attrValue)
+ (isSpecialMetadata(attrMetadata) ? "" : ",\"" + attrName + "_md\": " + attrMetadata);
} // for

// now, aggregate the column
Expand Down Expand Up @@ -484,5 +473,13 @@ private String buildResName(String destination) throws Exception {

return resName;
} // buildResName

private boolean isSpecialValue(String value) {
return value == null || value.equals(("\"\"")) || value.equals("{}") || value.equals("[]");
} // isSpecialValue

private boolean isSpecialMetadata(String value) {
return value == null || value.equals("[]");
} // isSpecialMetadata

} // NGSICKANSink

0 comments on commit 461c047

Please sign in to comment.