diff --git a/main/src/com/google/refine/browsing/EngineConfig.java b/main/src/com/google/refine/browsing/EngineConfig.java new file mode 100644 index 000000000..622ed3729 --- /dev/null +++ b/main/src/com/google/refine/browsing/EngineConfig.java @@ -0,0 +1,93 @@ +package com.google.refine.browsing; + +import java.util.LinkedList; +import java.util.List; +import java.util.Properties; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.google.refine.Jsonizable; +import com.google.refine.browsing.Engine.Mode; +import com.google.refine.browsing.facets.FacetConfig; +import com.google.refine.browsing.facets.ListFacet.ListFacetConfig; +import com.google.refine.browsing.facets.RangeFacet.RangeFacetConfig; +import com.google.refine.browsing.facets.ScatterplotFacet.ScatterplotFacetConfig; +import com.google.refine.browsing.facets.TextSearchFacet.TextSearchFacetConfig; +import com.google.refine.browsing.facets.TimeRangeFacet.TimeRangeFacetConfig; + + +public class EngineConfig implements Jsonizable { + + protected final List _facets; + protected final Mode _mode; + + public EngineConfig(List facets, Mode mode) { + _facets = facets; + _mode = mode; + } + + public static EngineConfig reconstruct(JSONObject o) { + if (o == null) { + return null; + } + + List facets = new LinkedList<>(); + if (o.has("facets") && !o.isNull("facets")) { + JSONArray a = o.getJSONArray("facets"); + int length = a.length(); + + for (int i = 0; i < length; i++) { + JSONObject fo = a.getJSONObject(i); + String type = fo.has("type") ? fo.getString("type") : "list"; + + FacetConfig facet = null; + if ("list".equals(type)) { + facet = new ListFacetConfig(); + } else if ("range".equals(type)) { + facet = new RangeFacetConfig(); + } else if ("timerange".equals(type)) { + facet = new TimeRangeFacetConfig(); + } else if ("scatterplot".equals(type)) { + facet = new ScatterplotFacetConfig(); + } else if ("text".equals(type)) { + facet = new TextSearchFacetConfig(); + } + + if (facet != null) { + facet.initializeFromJSON(fo); + facets.add(facet); + } + } + } + + Mode mode = Mode.RowBased; + // for backward compatibility + if (o.has(Engine.INCLUDE_DEPENDENT) && !o.isNull(Engine.INCLUDE_DEPENDENT)) { + mode = o.getBoolean(Engine.INCLUDE_DEPENDENT) ? Mode.RecordBased : Mode.RowBased; + } + + if (o.has(Engine.MODE) && !o.isNull(Engine.MODE)) { + mode = Engine.MODE_ROW_BASED.equals(o.getString(Engine.MODE)) ? Mode.RowBased : Mode.RecordBased; + } + + return new EngineConfig(facets, mode); + } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("facets"); + writer.array(); + for (FacetConfig facet : _facets) { + facet.write(writer, options); + } + writer.endArray(); + writer.key(Engine.MODE); writer.value(_mode == Mode.RowBased ? Engine.MODE_ROW_BASED : Engine.MODE_RECORD_BASED); + writer.endObject(); + } + +} diff --git a/main/src/com/google/refine/browsing/facets/FacetConfig.java b/main/src/com/google/refine/browsing/facets/FacetConfig.java index d51bc4fc2..c6786749e 100644 --- a/main/src/com/google/refine/browsing/facets/FacetConfig.java +++ b/main/src/com/google/refine/browsing/facets/FacetConfig.java @@ -1,5 +1,7 @@ package com.google.refine.browsing.facets; +import org.json.JSONObject; + import com.google.refine.Jsonizable; import com.google.refine.model.Project; @@ -14,6 +16,11 @@ import com.google.refine.model.Project; * */ public interface FacetConfig extends Jsonizable { + /** + * Reads the facet configuration from a JSON object (will be removed once we migrate to Jackson) + * @param fo + */ + public void initializeFromJSON(JSONObject fo); /** * Instantiates the given facet on a particular project. diff --git a/main/src/com/google/refine/browsing/facets/ListFacet.java b/main/src/com/google/refine/browsing/facets/ListFacet.java index fcf667073..484b283e8 100644 --- a/main/src/com/google/refine/browsing/facets/ListFacet.java +++ b/main/src/com/google/refine/browsing/facets/ListFacet.java @@ -101,6 +101,7 @@ public class ListFacet implements Facet { writer.endObject(); } + @Override public void initializeFromJSON(JSONObject o) { name = o.getString("name"); expression = o.getString("expression"); diff --git a/main/src/com/google/refine/browsing/facets/RangeFacet.java b/main/src/com/google/refine/browsing/facets/RangeFacet.java index ddb0de157..b54f7b022 100644 --- a/main/src/com/google/refine/browsing/facets/RangeFacet.java +++ b/main/src/com/google/refine/browsing/facets/RangeFacet.java @@ -96,6 +96,7 @@ public class RangeFacet implements Facet { } + @Override public void initializeFromJSON(JSONObject o) { _name = o.getString("name"); _expression = o.getString("expression"); diff --git a/main/src/com/google/refine/browsing/facets/ScatterplotFacet.java b/main/src/com/google/refine/browsing/facets/ScatterplotFacet.java index d8398db13..52f7cc208 100644 --- a/main/src/com/google/refine/browsing/facets/ScatterplotFacet.java +++ b/main/src/com/google/refine/browsing/facets/ScatterplotFacet.java @@ -144,6 +144,7 @@ public class ScatterplotFacet implements Facet { return facet; } + @Override public void initializeFromJSON(JSONObject o) { name = o.getString(NAME); l = size = (o.has(SIZE)) ? o.getInt(SIZE) : 100; diff --git a/main/src/com/google/refine/browsing/facets/TextSearchFacet.java b/main/src/com/google/refine/browsing/facets/TextSearchFacet.java index 48fb1f242..f0011aac1 100644 --- a/main/src/com/google/refine/browsing/facets/TextSearchFacet.java +++ b/main/src/com/google/refine/browsing/facets/TextSearchFacet.java @@ -86,6 +86,7 @@ public class TextSearchFacet implements Facet { return facet; } + @Override public void initializeFromJSON(JSONObject o) { _name = o.getString("name"); _columnName = o.getString("columnName"); diff --git a/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java b/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java index 7d8d50683..52566aa05 100644 --- a/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java +++ b/main/src/com/google/refine/browsing/facets/TimeRangeFacet.java @@ -95,6 +95,7 @@ public class TimeRangeFacet implements Facet { writer.endObject(); } + @Override public void initializeFromJSON(JSONObject o) throws JSONException { _name = o.getString("name"); _expression = o.getString("expression"); diff --git a/main/tests/server/src/com/google/refine/tests/browsing/facets/EngineConfigTests.java b/main/tests/server/src/com/google/refine/tests/browsing/facets/EngineConfigTests.java new file mode 100644 index 000000000..07960a721 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/browsing/facets/EngineConfigTests.java @@ -0,0 +1,32 @@ +package com.google.refine.tests.browsing.facets; + +import org.json.JSONObject; +import org.testng.annotations.Test; + +import com.google.refine.browsing.EngineConfig; +import com.google.refine.tests.util.TestUtils; + +public class EngineConfigTests { + + public static String engineConfigJson = + "{\n" + + " \"mode\": \"row-based\",\n" + + " \"facets\": [\n" + + " {\n" + + " \"mode\": \"text\",\n" + + " \"invert\": false,\n" + + " \"caseSensitive\": false,\n" + + " \"query\": \"www\",\n" + + " \"name\": \"reference\",\n" + + " \"type\": \"text\",\n" + + " \"columnName\": \"reference\"\n" + + " }\n" + + " ]\n" + + " }"; + + @Test + public void serializeEngineConfig() { + EngineConfig ec = EngineConfig.reconstruct(new JSONObject(engineConfigJson)); + TestUtils.isSerializedTo(ec, engineConfigJson); + } +}