Move SortingConfig to its own file; tests for sorting criteria
This commit is contained in:
parent
6daa1b4f5c
commit
1f40393028
@ -57,7 +57,7 @@ import com.google.refine.importing.ImportingManager;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Record;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.sorting.BaseSorter.SortingConfig;
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.sorting.SortingRecordVisitor;
|
||||
import com.google.refine.sorting.SortingRowVisitor;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
@ -127,7 +127,7 @@ public class GetRowsCommand extends Command {
|
||||
JSONObject sortingJson = (json == null) ? null :
|
||||
ParsingUtilities.evaluateJsonStringToObject(json);
|
||||
if (sortingJson != null) {
|
||||
sortingConfig = SortingConfig.reconstruct(project, sortingJson);
|
||||
sortingConfig = SortingConfig.reconstruct(sortingJson);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ import com.google.refine.commands.EngineDependentCommand;
|
||||
import com.google.refine.model.AbstractOperation;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.operations.row.RowReorderOperation;
|
||||
import com.google.refine.sorting.BaseSorter.SortingConfig;
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
|
||||
public class ReorderRowsCommand extends EngineDependentCommand {
|
||||
@ -60,7 +60,7 @@ public class ReorderRowsCommand extends EngineDependentCommand {
|
||||
String json = request.getParameter("sorting");
|
||||
|
||||
JSONObject sortingJson = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
|
||||
sorting = (sortingJson == null) ? null : SortingConfig.reconstruct(project, sortingJson);
|
||||
sorting = (sortingJson == null) ? null : SortingConfig.reconstruct(sortingJson);
|
||||
} catch (JSONException e) {
|
||||
// ignore
|
||||
}
|
||||
|
@ -50,9 +50,9 @@ import com.google.refine.browsing.RecordVisitor;
|
||||
import com.google.refine.browsing.RowVisitor;
|
||||
import com.google.refine.expr.ParsingException;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.sorting.SortingRecordVisitor;
|
||||
import com.google.refine.sorting.SortingRowVisitor;
|
||||
import com.google.refine.sorting.BaseSorter.SortingConfig;
|
||||
import com.google.refine.templating.Parser;
|
||||
import com.google.refine.templating.Template;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
@ -116,7 +116,7 @@ public class TemplatingExporter implements WriterExporter {
|
||||
|
||||
if (sortingJson != null) {
|
||||
try {
|
||||
SortingConfig sorting = SortingConfig.reconstruct(project, sortingJson);
|
||||
SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
|
||||
SortingRowVisitor srv = new SortingRowVisitor(visitor);
|
||||
srv.initializeFromConfig(project, sorting);
|
||||
|
||||
@ -135,7 +135,7 @@ public class TemplatingExporter implements WriterExporter {
|
||||
|
||||
if (sortingJson != null) {
|
||||
try {
|
||||
SortingConfig sorting = SortingConfig.reconstruct(project, sortingJson);
|
||||
SortingConfig sorting = SortingConfig.reconstruct(sortingJson);
|
||||
SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
|
||||
srv.initializeFromConfig(project, sorting);
|
||||
|
||||
|
@ -54,8 +54,7 @@ import com.google.refine.model.Record;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.model.changes.RowReorderChange;
|
||||
import com.google.refine.operations.OperationRegistry;
|
||||
import com.google.refine.sorting.BaseSorter;
|
||||
import com.google.refine.sorting.BaseSorter.SortingConfig;
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.sorting.SortingRecordVisitor;
|
||||
import com.google.refine.sorting.SortingRowVisitor;
|
||||
|
||||
@ -64,14 +63,14 @@ public class RowReorderOperation extends AbstractOperation {
|
||||
String mode = obj.getString("mode");
|
||||
JSONObject sorting = obj.has("sorting") && !obj.isNull("sorting") ?
|
||||
obj.getJSONObject("sorting") : null;
|
||||
BaseSorter.SortingConfig config = BaseSorter.SortingConfig.reconstruct(project, sorting);
|
||||
SortingConfig config = SortingConfig.reconstruct(sorting);
|
||||
return new RowReorderOperation(Engine.stringToMode(mode), config);
|
||||
}
|
||||
|
||||
final protected Mode _mode;
|
||||
final protected BaseSorter.SortingConfig _sorting;
|
||||
final protected SortingConfig _sorting;
|
||||
|
||||
public RowReorderOperation(Mode mode, BaseSorter.SortingConfig sorting) {
|
||||
public RowReorderOperation(Mode mode, SortingConfig sorting) {
|
||||
_mode = mode;
|
||||
_sorting = sorting;
|
||||
}
|
||||
|
@ -34,17 +34,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
package com.google.refine.sorting;
|
||||
|
||||
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.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import com.google.refine.Jsonizable;
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.sorting.Criterion.KeyMaker;
|
||||
@ -110,55 +102,7 @@ abstract public class BaseSorter {
|
||||
}
|
||||
}
|
||||
|
||||
public final static class SortingConfig implements Jsonizable {
|
||||
|
||||
protected Criterion[] _criteria;
|
||||
|
||||
@JsonCreator
|
||||
public SortingConfig(
|
||||
@JsonProperty("criteria")
|
||||
Criterion[] criteria) {
|
||||
_criteria = criteria;
|
||||
}
|
||||
|
||||
@JsonProperty("criteria")
|
||||
public Criterion[] getCriteria() {
|
||||
return _criteria;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
writer.object();
|
||||
writer.key("criteria");
|
||||
for (int i = 0; i != _criteria.length; i++) {
|
||||
_criteria[i].write(writer, options);
|
||||
}
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
public static SortingConfig reconstruct(Project project, JSONObject obj) {
|
||||
Criterion[] criteria;
|
||||
if (obj != null && obj.has("criteria") && !obj.isNull("criteria")) {
|
||||
JSONArray a = obj.getJSONArray("criteria");
|
||||
int count = a.length();
|
||||
|
||||
criteria = new Criterion[count];
|
||||
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
JSONObject obj2 = a.getJSONObject(i);
|
||||
|
||||
criteria[i] = Criterion.reconstruct(obj2);
|
||||
}
|
||||
} else {
|
||||
criteria = new Criterion[0];
|
||||
}
|
||||
return new SortingConfig(criteria);
|
||||
}
|
||||
}
|
||||
|
||||
public void initializeFromConfig(Project project, BaseSorter.SortingConfig config) throws JSONException {
|
||||
public void initializeFromConfig(Project project, SortingConfig config) throws JSONException {
|
||||
_criteria = config.getCriteria();
|
||||
int count = _criteria.length;
|
||||
_keyMakers = new KeyMaker[count];
|
||||
|
@ -33,11 +33,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.sorting;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.google.refine.expr.EvalError;
|
||||
import com.google.refine.expr.ExpressionUtils;
|
||||
|
||||
|
67
main/src/com/google/refine/sorting/SortingConfig.java
Normal file
67
main/src/com/google/refine/sorting/SortingConfig.java
Normal file
@ -0,0 +1,67 @@
|
||||
package com.google.refine.sorting;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import com.google.refine.Jsonizable;
|
||||
|
||||
/**
|
||||
* Stores the configuration of a row/record sorting setup.
|
||||
* @author Antonin Delpeuch
|
||||
*
|
||||
*/
|
||||
public final class SortingConfig implements Jsonizable {
|
||||
|
||||
protected Criterion[] _criteria;
|
||||
|
||||
@JsonCreator
|
||||
public SortingConfig(
|
||||
@JsonProperty("criteria")
|
||||
Criterion[] criteria) {
|
||||
_criteria = criteria;
|
||||
}
|
||||
|
||||
@JsonProperty("criteria")
|
||||
public Criterion[] getCriteria() {
|
||||
return _criteria;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
writer.object();
|
||||
writer.key("criteria");
|
||||
writer.array();
|
||||
for (int i = 0; i != _criteria.length; i++) {
|
||||
_criteria[i].write(writer, options);
|
||||
}
|
||||
writer.endArray();
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
public static SortingConfig reconstruct(JSONObject obj) {
|
||||
Criterion[] criteria;
|
||||
if (obj != null && obj.has("criteria") && !obj.isNull("criteria")) {
|
||||
JSONArray a = obj.getJSONArray("criteria");
|
||||
int count = a.length();
|
||||
|
||||
criteria = new Criterion[count];
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
JSONObject obj2 = a.getJSONObject(i);
|
||||
|
||||
criteria[i] = Criterion.reconstruct(obj2);
|
||||
}
|
||||
} else {
|
||||
criteria = new Criterion[0];
|
||||
}
|
||||
return new SortingConfig(criteria);
|
||||
}
|
||||
}
|
@ -20,7 +20,7 @@ import com.google.refine.model.Project;
|
||||
import com.google.refine.operations.OperationRegistry;
|
||||
import com.google.refine.operations.row.RowReorderOperation;
|
||||
import com.google.refine.process.Process;
|
||||
import com.google.refine.sorting.BaseSorter.SortingConfig;
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
import com.google.refine.tests.util.TestUtils;
|
||||
|
||||
@ -51,7 +51,7 @@ public class RowReorderOperationTests extends RefineTest {
|
||||
@Test
|
||||
public void testSortEmptyString() throws Exception {
|
||||
String sortingJson = "{\"criteria\":[{\"column\":\"key\",\"valueType\":\"number\",\"reverse\":false,\"blankPosition\":2,\"errorPosition\":1}]}";
|
||||
SortingConfig sortingConfig = SortingConfig.reconstruct(project, new JSONObject(sortingJson));
|
||||
SortingConfig sortingConfig = SortingConfig.reconstruct(new JSONObject(sortingJson));
|
||||
project.rows.get(1).cells.set(0, new Cell("", null));
|
||||
AbstractOperation op = new RowReorderOperation(
|
||||
Mode.RowBased, sortingConfig
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.google.refine.tests.sorting;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.sorting.Criterion;
|
||||
import com.google.refine.tests.util.TestUtils;
|
||||
|
||||
public class BooleanCriterionTest {
|
||||
@Test
|
||||
public void serializeBooleanCriterion() {
|
||||
String json =
|
||||
" {\n" +
|
||||
" \"errorPosition\": 1,\n" +
|
||||
" \"valueType\": \"boolean\",\n" +
|
||||
" \"column\": \"start_year\",\n" +
|
||||
" \"blankPosition\": 2,\n" +
|
||||
" \"reverse\": false\n" +
|
||||
" }\n";
|
||||
TestUtils.isSerializedTo(Criterion.reconstruct(new JSONObject(json)), json);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.google.refine.tests.sorting;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.sorting.Criterion;
|
||||
import com.google.refine.tests.util.TestUtils;
|
||||
|
||||
public class DateCriterionTest {
|
||||
@Test
|
||||
public void serializeDateCriterion() {
|
||||
String json =
|
||||
" {\n" +
|
||||
" \"errorPosition\": 2,\n" +
|
||||
" \"valueType\": \"date\",\n" +
|
||||
" \"column\": \"start_year\",\n" +
|
||||
" \"blankPosition\": -1,\n" +
|
||||
" \"reverse\": true\n" +
|
||||
" }\n";
|
||||
TestUtils.isSerializedTo(Criterion.reconstruct(new JSONObject(json)), json);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.google.refine.tests.sorting;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.sorting.Criterion;
|
||||
import com.google.refine.tests.util.TestUtils;
|
||||
|
||||
public class NumberCriterionTest {
|
||||
@Test
|
||||
public void serializeNumberCriterion() {
|
||||
String json =
|
||||
" {\n" +
|
||||
" \"errorPosition\": 2,\n" +
|
||||
" \"valueType\": \"number\",\n" +
|
||||
" \"column\": \"start_year\",\n" +
|
||||
" \"blankPosition\": 1,\n" +
|
||||
" \"reverse\": true\n" +
|
||||
" }\n";
|
||||
TestUtils.isSerializedTo(Criterion.reconstruct(new JSONObject(json)), json);
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.google.refine.tests.sorting;
|
||||
|
||||
import org.json.JSONObject;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.sorting.SortingConfig;
|
||||
import com.google.refine.tests.util.TestUtils;
|
||||
|
||||
public class SortingConfigTests {
|
||||
@Test
|
||||
public void serializeSortingConfig() {
|
||||
String json = "{\n" +
|
||||
" \"criteria\": [\n" +
|
||||
" {\n" +
|
||||
" \"errorPosition\": 1,\n" +
|
||||
" \"valueType\": \"number\",\n" +
|
||||
" \"column\": \"start_year\",\n" +
|
||||
" \"blankPosition\": 2,\n" +
|
||||
" \"reverse\": false\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }";
|
||||
TestUtils.isSerializedTo(SortingConfig.reconstruct(new JSONObject(json)), json);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user