Skip to content

Commit

Permalink
Save null values for geometry when answer cleared
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Dec 2, 2019
1 parent a973c13 commit 0a8fbef
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,22 @@ private void updateInstanceDatabase(boolean incomplete, boolean canEditAfterComp
}

/**
* Extracts geometry information from the instance, if present.
* Extracts geometry information from the given xpath path in the given instance.
*
* Returns a ContentValues object with values set for {@link InstanceColumns.GEOMETRY} and
* {@link InstanceColumns.GEOMETRY_TYPE}. Those value are null if anything goes wrong with
* parsing the geometry and converting it to GeoJSON.
*
* Returns null if the given XPath path is null.
*/
private ContentValues extractGeometryContentValues(FormInstance instance, String xpath) {
ContentValues values = new ContentValues();

if (xpath == null) {
Timber.w("Geometry XPath is missing for instance %s!", instance);
return null;
}

try {
XPathExpression expr = XPathParseTool.parseXPath(xpath);
EvaluationContext context = new EvaluationContext(instance);
Expand All @@ -262,13 +271,13 @@ private ContentValues extractGeometryContentValues(FormInstance instance, String
// For now, only use the first node found.
TreeElement element = instance.resolveReference(nodes.getRefAt(0));
IAnswerData value = element.getValue();

if (value instanceof GeoPointData) {
try {
JSONObject json = toGeoJson((GeoPointData) value);
Timber.i("Geometry for \"%s\" instance found at %s: %s",
instance.getName(), xpath, json);

ContentValues values = new ContentValues();
values.put(InstanceColumns.GEOMETRY, json.toString());
values.put(InstanceColumns.GEOMETRY_TYPE, json.getString("type"));
return values;
Expand All @@ -281,7 +290,9 @@ private ContentValues extractGeometryContentValues(FormInstance instance, String
Timber.w(e, "Could not evaluate geometry XPath %s in instance", xpath);
}

return null;
values.put(InstanceColumns.GEOMETRY, (String) null);
values.put(InstanceColumns.GEOMETRY_TYPE, (String) null);
return values;
}

@NonNull
Expand Down

0 comments on commit 0a8fbef

Please sign in to comment.