Introduce EngineConfig class to avoid manipulating JSON objects directly
This commit is contained in:
parent
7a3d13ef47
commit
5b4cab947e
93
main/src/com/google/refine/browsing/EngineConfig.java
Normal file
93
main/src/com/google/refine/browsing/EngineConfig.java
Normal file
@ -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<FacetConfig> _facets;
|
||||
protected final Mode _mode;
|
||||
|
||||
public EngineConfig(List<FacetConfig> facets, Mode mode) {
|
||||
_facets = facets;
|
||||
_mode = mode;
|
||||
}
|
||||
|
||||
public static EngineConfig reconstruct(JSONObject o) {
|
||||
if (o == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<FacetConfig> 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();
|
||||
}
|
||||
|
||||
}
|
@ -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.
|
||||
|
@ -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");
|
||||
|
@ -96,6 +96,7 @@ public class RangeFacet implements Facet {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initializeFromJSON(JSONObject o) {
|
||||
_name = o.getString("name");
|
||||
_expression = o.getString("expression");
|
||||
|
@ -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;
|
||||
|
@ -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");
|
||||
|
@ -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");
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user