From f0eae04c0c35bc4242fa907717e235394a4ed24c Mon Sep 17 00:00:00 2001 From: David Huynh Date: Fri, 6 Aug 2010 23:50:22 +0000 Subject: [PATCH] Forgot to add 2 files in the last commit git-svn-id: http://google-refine.googlecode.com/svn/trunk@1141 7d457c2a-affb-35e4-300a-418c747d4874 --- .../operations/cell/BlankDownOperation.java | 101 ++++++++++++++++++ .../operations/cell/FillDownOperation.java | 97 +++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 main/src/com/google/gridworks/operations/cell/BlankDownOperation.java create mode 100644 main/src/com/google/gridworks/operations/cell/FillDownOperation.java diff --git a/main/src/com/google/gridworks/operations/cell/BlankDownOperation.java b/main/src/com/google/gridworks/operations/cell/BlankDownOperation.java new file mode 100644 index 000000000..ccae880ce --- /dev/null +++ b/main/src/com/google/gridworks/operations/cell/BlankDownOperation.java @@ -0,0 +1,101 @@ +package com.google.gridworks.operations.cell; + +import java.util.List; +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.google.gridworks.browsing.RowVisitor; +import com.google.gridworks.expr.ExpressionUtils; +import com.google.gridworks.model.AbstractOperation; +import com.google.gridworks.model.Cell; +import com.google.gridworks.model.Column; +import com.google.gridworks.model.Project; +import com.google.gridworks.model.Row; +import com.google.gridworks.model.changes.CellChange; +import com.google.gridworks.operations.EngineDependentMassCellOperation; +import com.google.gridworks.operations.OperationRegistry; + +public class BlankDownOperation extends EngineDependentMassCellOperation { + + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { + JSONObject engineConfig = obj.getJSONObject("engineConfig"); + + return new BlankDownOperation( + engineConfig, + obj.getString("columnName") + ); + } + + public BlankDownOperation( + JSONObject engineConfig, + String columnName + ) { + super(engineConfig, columnName, true); + } + + 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("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.endObject(); + } + + protected String getBriefDescription(Project project) { + return "Fill down cells in column " + _columnName; + } + + protected String createDescription(Column column, + List cellChanges) { + + return "Fill down " + cellChanges.size() + + " cells in column " + column.getName(); + } + + protected RowVisitor createRowVisitor(Project project, List cellChanges, long historyEntryID) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + Cell previousCell; + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + @Override + public void start(Project project) { + // nothing to do + } + + @Override + public void end(Project project) { + // nothing to do + } + + public boolean visit(Project project, int rowIndex, Row row) { + Object value = row.getCellValue(cellIndex); + if (ExpressionUtils.isNonBlankData(value)) { + Cell cell = row.getCell(cellIndex); + if (previousCell != null && cell.value.equals(previousCell.value)) { + CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, null); + cellChanges.add(cellChange); + } + previousCell = cell; + } else { + previousCell = null; + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } +} diff --git a/main/src/com/google/gridworks/operations/cell/FillDownOperation.java b/main/src/com/google/gridworks/operations/cell/FillDownOperation.java new file mode 100644 index 000000000..af3eea2e7 --- /dev/null +++ b/main/src/com/google/gridworks/operations/cell/FillDownOperation.java @@ -0,0 +1,97 @@ +package com.google.gridworks.operations.cell; + +import java.util.List; +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.google.gridworks.browsing.RowVisitor; +import com.google.gridworks.expr.ExpressionUtils; +import com.google.gridworks.model.AbstractOperation; +import com.google.gridworks.model.Cell; +import com.google.gridworks.model.Column; +import com.google.gridworks.model.Project; +import com.google.gridworks.model.Row; +import com.google.gridworks.model.changes.CellChange; +import com.google.gridworks.operations.EngineDependentMassCellOperation; +import com.google.gridworks.operations.OperationRegistry; + +public class FillDownOperation extends EngineDependentMassCellOperation { + + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { + JSONObject engineConfig = obj.getJSONObject("engineConfig"); + + return new FillDownOperation( + engineConfig, + obj.getString("columnName") + ); + } + + public FillDownOperation( + JSONObject engineConfig, + String columnName + ) { + super(engineConfig, columnName, true); + } + + 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("engineConfig"); writer.value(getEngineConfig()); + writer.key("columnName"); writer.value(_columnName); + writer.endObject(); + } + + protected String getBriefDescription(Project project) { + return "Fill down cells in column " + _columnName; + } + + protected String createDescription(Column column, + List cellChanges) { + + return "Fill down " + cellChanges.size() + + " cells in column " + column.getName(); + } + + protected RowVisitor createRowVisitor(Project project, List cellChanges, long historyEntryID) throws Exception { + Column column = project.columnModel.getColumnByName(_columnName); + + return new RowVisitor() { + int cellIndex; + List cellChanges; + Cell previousCell; + + public RowVisitor init(int cellIndex, List cellChanges) { + this.cellIndex = cellIndex; + this.cellChanges = cellChanges; + return this; + } + + @Override + public void start(Project project) { + // nothing to do + } + + @Override + public void end(Project project) { + // nothing to do + } + + public boolean visit(Project project, int rowIndex, Row row) { + Object value = row.getCellValue(cellIndex); + if (ExpressionUtils.isNonBlankData(value)) { + previousCell = row.getCell(cellIndex); + } else if (previousCell != null) { + CellChange cellChange = new CellChange(rowIndex, cellIndex, row.getCell(cellIndex), previousCell); + cellChanges.add(cellChange); + } + return false; + } + }.init(column.getCellIndex(), cellChanges); + } +}