Implemented "reorder rows" feature.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@840 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
bfc7ef7739
commit
23c02b870a
@ -80,6 +80,7 @@ public class GridworksServlet extends HttpServlet {
|
||||
{"annotate-one-row", "com.metaweb.gridworks.commands.row.AnnotateOneRowCommand"},
|
||||
{"annotate-rows", "com.metaweb.gridworks.commands.row.AnnotateRowsCommand"},
|
||||
{"remove-rows", "com.metaweb.gridworks.commands.row.RemoveRowsCommand"},
|
||||
{"reorder-rows", "com.metaweb.gridworks.commands.row.ReorderRowsCommand"},
|
||||
|
||||
{"save-protograph", "com.metaweb.gridworks.commands.freebase.SaveProtographCommand"},
|
||||
|
||||
|
@ -39,6 +39,13 @@ public class Engine implements Jsonizable {
|
||||
protected List<Facet> _facets = new LinkedList<Facet>();
|
||||
protected Mode _mode = Mode.RowBased;
|
||||
|
||||
static public String modeToString(Mode mode) {
|
||||
return mode == Mode.RowBased ? MODE_ROW_BASED : MODE_RECORD_BASED;
|
||||
}
|
||||
static public Mode stringToMode(String s) {
|
||||
return MODE_ROW_BASED.equals(s) ? Mode.RowBased : Mode.RecordBased;
|
||||
}
|
||||
|
||||
public Engine(Project project) {
|
||||
_project = project;
|
||||
}
|
||||
@ -46,6 +53,9 @@ public class Engine implements Jsonizable {
|
||||
public Mode getMode() {
|
||||
return _mode;
|
||||
}
|
||||
public void setMode(Mode mode) {
|
||||
_mode = mode;
|
||||
}
|
||||
|
||||
public FilteredRows getAllRows() {
|
||||
return new FilteredRows() {
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.metaweb.gridworks.commands.row;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import com.metaweb.gridworks.browsing.Engine;
|
||||
import com.metaweb.gridworks.commands.EngineDependentCommand;
|
||||
import com.metaweb.gridworks.model.AbstractOperation;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.operations.row.RowReorderOperation;
|
||||
import com.metaweb.gridworks.util.ParsingUtilities;
|
||||
|
||||
public class ReorderRowsCommand extends EngineDependentCommand {
|
||||
|
||||
@Override
|
||||
protected AbstractOperation createOperation(Project project,
|
||||
HttpServletRequest request, JSONObject engineConfig) throws Exception {
|
||||
|
||||
String mode = request.getParameter("mode");
|
||||
JSONObject sorting = null;
|
||||
|
||||
try{
|
||||
String json = request.getParameter("sorting");
|
||||
|
||||
sorting = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
|
||||
} catch (JSONException e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
return new RowReorderOperation(Engine.stringToMode(mode), sorting);
|
||||
}
|
||||
}
|
@ -0,0 +1,94 @@
|
||||
package com.metaweb.gridworks.model.changes;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.LineNumberReader;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.metaweb.gridworks.history.Change;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.model.Row;
|
||||
import com.metaweb.gridworks.util.Pool;
|
||||
|
||||
public class RowReorderChange implements Change {
|
||||
final protected List<Integer> _rowIndices;
|
||||
|
||||
public RowReorderChange(List<Integer> rowIndices) {
|
||||
_rowIndices = rowIndices;
|
||||
}
|
||||
|
||||
public void apply(Project project) {
|
||||
synchronized (project) {
|
||||
List<Row> oldRows = project.rows;
|
||||
List<Row> newRows = new ArrayList<Row>(oldRows.size());
|
||||
|
||||
for (Integer oldIndex : _rowIndices) {
|
||||
newRows.add(oldRows.get(oldIndex));
|
||||
}
|
||||
|
||||
project.rows.clear();
|
||||
project.rows.addAll(newRows);
|
||||
project.update();
|
||||
}
|
||||
}
|
||||
|
||||
public void revert(Project project) {
|
||||
synchronized (project) {
|
||||
int count = project.rows.size();
|
||||
|
||||
List<Row> newRows = project.rows;
|
||||
List<Row> oldRows = new ArrayList<Row>(count);
|
||||
|
||||
for (int r = 0; r < count; r++) {
|
||||
oldRows.add(null);
|
||||
}
|
||||
|
||||
for (int newIndex = 0; newIndex < count; newIndex++) {
|
||||
int oldIndex = _rowIndices.get(newIndex);
|
||||
Row row = newRows.get(newIndex);
|
||||
oldRows.set(oldIndex, row);
|
||||
}
|
||||
|
||||
project.rows.clear();
|
||||
project.rows.addAll(oldRows);
|
||||
project.update();
|
||||
}
|
||||
}
|
||||
|
||||
public void save(Writer writer, Properties options) throws IOException {
|
||||
writer.write("rowIndexCount="); writer.write(Integer.toString(_rowIndices.size())); writer.write('\n');
|
||||
for (Integer index : _rowIndices) {
|
||||
writer.write(index.toString());
|
||||
writer.write('\n');
|
||||
}
|
||||
writer.write("/ec/\n"); // end of change marker
|
||||
}
|
||||
|
||||
static public Change load(LineNumberReader reader, Pool pool) throws Exception {
|
||||
List<Integer> rowIndices = null;
|
||||
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null && !"/ec/".equals(line)) {
|
||||
int equal = line.indexOf('=');
|
||||
CharSequence field = line.subSequence(0, equal);
|
||||
|
||||
if ("rowIndexCount".equals(field)) {
|
||||
int count = Integer.parseInt(line.substring(equal + 1));
|
||||
|
||||
rowIndices = new ArrayList<Integer>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
line = reader.readLine();
|
||||
if (line != null) {
|
||||
rowIndices.add(Integer.parseInt(line));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RowReorderChange change = new RowReorderChange(rowIndices);
|
||||
|
||||
return change;
|
||||
}
|
||||
}
|
@ -26,6 +26,7 @@ import com.metaweb.gridworks.operations.recon.ReconOperation;
|
||||
import com.metaweb.gridworks.operations.row.DenormalizeOperation;
|
||||
import com.metaweb.gridworks.operations.row.RowFlagOperation;
|
||||
import com.metaweb.gridworks.operations.row.RowRemovalOperation;
|
||||
import com.metaweb.gridworks.operations.row.RowReorderOperation;
|
||||
import com.metaweb.gridworks.operations.row.RowStarOperation;
|
||||
|
||||
public abstract class OperationRegistry {
|
||||
@ -58,6 +59,7 @@ public abstract class OperationRegistry {
|
||||
register("row-removal", RowRemovalOperation.class);
|
||||
register("row-star", RowStarOperation.class);
|
||||
register("row-flag", RowFlagOperation.class);
|
||||
register("row-reorder", RowReorderOperation.class);
|
||||
|
||||
register("save-protograph", SaveProtographOperation.class);
|
||||
register("text-transform", TextTransformOperation.class);
|
||||
|
@ -0,0 +1,122 @@
|
||||
package com.metaweb.gridworks.operations.row;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.metaweb.gridworks.browsing.Engine;
|
||||
import com.metaweb.gridworks.browsing.RecordVisitor;
|
||||
import com.metaweb.gridworks.browsing.RowVisitor;
|
||||
import com.metaweb.gridworks.browsing.Engine.Mode;
|
||||
import com.metaweb.gridworks.history.HistoryEntry;
|
||||
import com.metaweb.gridworks.model.AbstractOperation;
|
||||
import com.metaweb.gridworks.model.Project;
|
||||
import com.metaweb.gridworks.model.Record;
|
||||
import com.metaweb.gridworks.model.Row;
|
||||
import com.metaweb.gridworks.model.changes.RowReorderChange;
|
||||
import com.metaweb.gridworks.operations.OperationRegistry;
|
||||
import com.metaweb.gridworks.sorting.SortingRecordVisitor;
|
||||
import com.metaweb.gridworks.sorting.SortingRowVisitor;
|
||||
|
||||
public class RowReorderOperation extends AbstractOperation {
|
||||
static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception {
|
||||
String mode = obj.getString("mode");
|
||||
JSONObject sorting = obj.has("sorting") && !obj.isNull("sorting") ?
|
||||
obj.getJSONObject("sorting") : null;
|
||||
|
||||
return new RowReorderOperation(Engine.stringToMode(mode), sorting);
|
||||
}
|
||||
|
||||
final protected Mode _mode;
|
||||
final protected JSONObject _sorting;
|
||||
|
||||
public RowReorderOperation(Mode mode, JSONObject sorting) {
|
||||
_mode = mode;
|
||||
_sorting = sorting;
|
||||
}
|
||||
|
||||
public void write(JSONWriter writer, Properties options)
|
||||
throws JSONException {
|
||||
|
||||
writer.object();
|
||||
writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass()));
|
||||
writer.key("description"); writer.value(getBriefDescription(null));
|
||||
writer.key("mode"); writer.value(Engine.modeToString(_mode));
|
||||
writer.key("sorting"); writer.value(_sorting);
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
protected String getBriefDescription(Project project) {
|
||||
return "Reorder rows";
|
||||
}
|
||||
|
||||
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
|
||||
Engine engine = new Engine(project);
|
||||
engine.setMode(_mode);
|
||||
|
||||
List<Integer> rowIndices = new ArrayList<Integer>();
|
||||
if (_mode == Mode.RowBased) {
|
||||
RowVisitor visitor = new IndexingVisitor(rowIndices);
|
||||
if (_sorting != null) {
|
||||
SortingRowVisitor srv = new SortingRowVisitor(visitor);
|
||||
|
||||
srv.initializeFromJSON(project, _sorting);
|
||||
visitor = srv;
|
||||
}
|
||||
|
||||
engine.getAllRows().accept(project, visitor);
|
||||
} else {
|
||||
RecordVisitor visitor = new IndexingVisitor(rowIndices);
|
||||
if (_sorting != null) {
|
||||
SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
|
||||
|
||||
srv.initializeFromJSON(project, _sorting);
|
||||
visitor = srv;
|
||||
}
|
||||
|
||||
engine.getAllRecords().accept(project, visitor);
|
||||
}
|
||||
|
||||
return new HistoryEntry(
|
||||
historyEntryID,
|
||||
project,
|
||||
"Reorder rows",
|
||||
this,
|
||||
new RowReorderChange(rowIndices)
|
||||
);
|
||||
}
|
||||
|
||||
static protected class IndexingVisitor implements RowVisitor, RecordVisitor {
|
||||
List<Integer> _indices;
|
||||
|
||||
IndexingVisitor(List<Integer> indices) {
|
||||
_indices = indices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Project project) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end(Project project) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Project project, int rowIndex, Row row) {
|
||||
_indices.add(rowIndex);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(Project project, Record record) {
|
||||
for (int r = record.fromRowIndex; r < record.toRowIndex; r++) {
|
||||
_indices.add(r);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@ -531,6 +531,22 @@ DataTableView.prototype._createSortingMenu = function(elmt) {
|
||||
self.update();
|
||||
}
|
||||
},
|
||||
{
|
||||
"label" : "Reorder Rows Permanently",
|
||||
"click" : function() {
|
||||
Gridworks.postProcess(
|
||||
"reorder-rows",
|
||||
null,
|
||||
{ "sorting" : JSON.stringify(self._sorting) },
|
||||
{ rowMetadataChanged: true },
|
||||
{
|
||||
onDone: function() {
|
||||
self._sorting.criteria = [];
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
},
|
||||
{}
|
||||
];
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user