From c4caf1eba73bf721011aa18f93e0c8991d39a6eb Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 22 Oct 2018 09:47:14 +0100 Subject: [PATCH] Jackson deserialization for TimeRangeFacetConfig --- .../browsing/facets/TimeRangeFacet.java | 37 +++++-------------- .../browsing/facets/TimeRangeFacetTests.java | 16 ++++---- 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java b/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java index c1042f116..38afab3c1 100644 --- a/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java +++ b/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java @@ -73,9 +73,9 @@ public class TimeRangeFacet implements Facet { protected String _columnName; // column to base expression on, if any @JsonProperty(FROM) - protected double _from; // the numeric selection + protected double _from = 0; // the numeric selection @JsonProperty(TO) - protected double _to; + protected double _to = 0; @JsonProperty("selectTime") protected boolean _selectTime; // whether the time selection applies, default true @@ -86,31 +86,12 @@ public class TimeRangeFacet implements Facet { @JsonProperty("selectError") protected boolean _selectError; + // false if we're certain that all rows will match + // and there isn't any filtering to do @JsonIgnore - protected boolean _selected; // false if we're certain that all rows will match - // and there isn't any filtering to do - - @Override - public void initializeFromJSON(JSONObject o) throws JSONException { - _name = o.getString("name"); - _expression = o.getString("expression"); - _columnName = o.getString("columnName"); - - if (o.has(FROM) || o.has(TO)) { - _from = o.has(FROM) ? o.getDouble(FROM) : 0; - _to = o.has(TO) ? o.getDouble(TO) : 0; - _selected = true; - } - - _selectTime = JSONUtilities.getBoolean(o, "selectTime", true); - _selectNonTime = JSONUtilities.getBoolean(o, "selectNonTime", true); - _selectBlank = JSONUtilities.getBoolean(o, "selectBlank", true); - _selectError = JSONUtilities.getBoolean(o, "selectError", true); - - if (!_selectTime || !_selectNonTime || !_selectBlank || !_selectError) { - _selected = true; - } - } + protected boolean isSelected() { + return _from != 0 || _to != 0 || !_selectTime || !_selectNonTime || !_selectBlank || !_selectError; + }; @Override public TimeRangeFacet apply(Project project) { @@ -268,7 +249,7 @@ public class TimeRangeFacet implements Facet { @Override public RowFilter getRowFilter(Project project) { - if (_eval != null && _errorMessage == null && _config._selected) { + if (_eval != null && _errorMessage == null && _config.isSelected()) { return new ExpressionTimeComparisonRowFilter( getRowEvaluable(project), _config._selectTime, _config._selectNonTime, _config._selectBlank, _config._selectError) { @@ -338,7 +319,7 @@ public class TimeRangeFacet implements Facet { _baseBlankCount = index.getBlankRowCount(); _baseErrorCount = index.getErrorRowCount(); - if (_config._selected) { + if (_config.isSelected()) { _config._from = Math.max(_config._from, _min); _config._to = Math.min(_config._to, _max); } else { diff --git a/main/tests/server/src/com/google/refine/tests/browsing/facets/TimeRangeFacetTests.java b/main/tests/server/src/com/google/refine/tests/browsing/facets/TimeRangeFacetTests.java index 16794c035..a0741a03c 100644 --- a/main/tests/server/src/com/google/refine/tests/browsing/facets/TimeRangeFacetTests.java +++ b/main/tests/server/src/com/google/refine/tests/browsing/facets/TimeRangeFacetTests.java @@ -1,10 +1,13 @@ package com.google.refine.tests.browsing.facets; +import java.io.IOException; import java.time.OffsetDateTime; -import org.json.JSONObject; import org.testng.annotations.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + import com.google.refine.browsing.Engine; import com.google.refine.browsing.facets.TimeRangeFacet; import com.google.refine.browsing.facets.TimeRangeFacet.TimeRangeFacetConfig; @@ -12,6 +15,7 @@ import com.google.refine.model.Cell; import com.google.refine.model.Project; import com.google.refine.tests.RefineTest; import com.google.refine.tests.util.TestUtils; +import com.google.refine.util.ParsingUtilities; public class TimeRangeFacetTests extends RefineTest { @@ -50,14 +54,13 @@ public class TimeRangeFacetTests extends RefineTest { " }"; @Test - public void serializeTimeRangeFacetConfig() { - TimeRangeFacetConfig config = new TimeRangeFacetConfig(); - config.initializeFromJSON(new JSONObject(configJson)); + public void serializeTimeRangeFacetConfig() throws JsonParseException, JsonMappingException, IOException { + TimeRangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, TimeRangeFacetConfig.class); TestUtils.isSerializedTo(config, configJson); } @Test - public void serializeTimeRangeFacet() { + public void serializeTimeRangeFacet() throws JsonParseException, JsonMappingException, IOException { Project project = createCSVProject("my column\n" + "placeholder\n" + "nontime\n" @@ -68,8 +71,7 @@ public class TimeRangeFacetTests extends RefineTest { project.rows.get(3).cells.set(0, new Cell(OffsetDateTime.parse("2012-04-05T02:00:01Z"), null)); Engine engine = new Engine(project); - TimeRangeFacetConfig config = new TimeRangeFacetConfig(); - config.initializeFromJSON(new JSONObject(configJson)); + TimeRangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, TimeRangeFacetConfig.class); TimeRangeFacet facet = config.apply(project); facet.computeChoices(project, engine.getAllFilteredRows()); TestUtils.isSerializedTo(facet, facetJson);