From f7e830e7090e3b365b2a0118e25cb467a85557b0 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Sun, 11 Apr 2010 21:54:56 +0000 Subject: [PATCH] Fixed bug in which editing a single cell and then starring the same row seemed to revert the cell back to its original content. Added an option for not guessing cell value type during import. git-svn-id: http://google-refine.googlecode.com/svn/trunk@446 7d457c2a-affb-35e4-300a-418c747d4874 --- .../commands/edit/CreateProjectCommand.java | 26 +++---------------- .../gridworks/importers/ExcelImporter.java | 9 +++---- .../metaweb/gridworks/importers/Importer.java | 4 +-- .../importers/ImporterUtilities.java | 25 ++++++++++++++++++ .../gridworks/importers/TsvCsvImporter.java | 14 +++++----- .../gridworks/importers/XmlImporter.java | 6 ++--- .../importers/parsers/CSVRowParser.java | 4 +-- .../importers/parsers/RowParser.java | 2 +- .../importers/parsers/SeparatorRowParser.java | 4 +-- src/main/webapp/index.html | 2 +- src/main/webapp/scripts/index.js | 3 ++- .../scripts/views/data-table-cell-ui.js | 2 ++ .../webapp/scripts/views/data-table-view.js | 14 ++++++++++ src/main/webapp/styles/index.css | 2 +- 14 files changed, 68 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java index 9d8386c89..86fdaff32 100644 --- a/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/edit/CreateProjectCommand.java @@ -403,10 +403,6 @@ public class CreateProjectCommand extends Command { InputStream rawInputStream, String encoding ) throws Exception { - - int limit = getIntegerOption("limit",options,-1); - int skip = getIntegerOption("skip",options,0); - if (importer.takesReader()) { BufferedInputStream inputStream = new BufferedInputStream(rawInputStream); @@ -449,9 +445,9 @@ public class CreateProjectCommand extends Command { new InputStreamReader(inputStream); } - importer.read(reader, project, options, skip, limit); + importer.read(reader, project, options); } else { - importer.read(rawInputStream, project, options, skip, limit); + importer.read(rawInputStream, project, options); } } @@ -461,11 +457,7 @@ public class CreateProjectCommand extends Command { Properties options, Reader reader ) throws Exception { - - int limit = getIntegerOption("limit",options,-1); - int skip = getIntegerOption("skip",options,0); - - importer.read(reader, project, options, skip, limit); + importer.read(reader, project, options); } protected Importer guessImporter( @@ -509,16 +501,4 @@ public class CreateProjectCommand extends Command { return new TsvCsvImporter(); } - - private int getIntegerOption(String name, Properties options, int def) { - int value = def; - if (options.containsKey(name)) { - String s = options.getProperty(name); - try { - value = Integer.parseInt(s); - } catch (Exception e) { - } - } - return value; - } } diff --git a/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java b/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java index d7e67f062..b2845c8c3 100644 --- a/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java +++ b/src/main/java/com/metaweb/gridworks/importers/ExcelImporter.java @@ -37,14 +37,13 @@ public class ExcelImporter implements Importer { return false; } - public void read(Reader reader, Project project, Properties options, int skip, int limit) - throws Exception { - + public void read(Reader reader, Project project, Properties options) throws Exception { throw new NotImplementedException(); } - public void read(InputStream inputStream, Project project, - Properties options, int skip, int limit) throws Exception { + public void read(InputStream inputStream, Project project, Properties options) throws Exception { + int limit = ImporterUtilities.getIntegerOption("limit",options,-1); + int skip = ImporterUtilities.getIntegerOption("skip",options,0); Workbook wb = null; try { diff --git a/src/main/java/com/metaweb/gridworks/importers/Importer.java b/src/main/java/com/metaweb/gridworks/importers/Importer.java index 32b9032db..d769f171b 100644 --- a/src/main/java/com/metaweb/gridworks/importers/Importer.java +++ b/src/main/java/com/metaweb/gridworks/importers/Importer.java @@ -9,6 +9,6 @@ import com.metaweb.gridworks.model.Project; public interface Importer { public boolean takesReader(); - public void read(Reader reader, Project project, Properties options, int skip, int limit) throws Exception; - public void read(InputStream inputStream, Project project, Properties options, int skip, int limit) throws Exception; + public void read(Reader reader, Project project, Properties options) throws Exception; + public void read(InputStream inputStream, Project project, Properties options) throws Exception; } diff --git a/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java b/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java index 867d14aac..7b453ca36 100644 --- a/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java +++ b/src/main/java/com/metaweb/gridworks/importers/ImporterUtilities.java @@ -1,6 +1,7 @@ package com.metaweb.gridworks.importers; import java.io.Serializable; +import java.util.Properties; public class ImporterUtilities { @@ -25,5 +26,29 @@ public class ImporterUtilities { } return text; } + + static public int getIntegerOption(String name, Properties options, int def) { + int value = def; + if (options.containsKey(name)) { + String s = options.getProperty(name); + try { + value = Integer.parseInt(s); + } catch (Exception e) { + } + } + return value; + } + + static public boolean getBooleanOption(String name, Properties options, boolean def) { + boolean value = def; + if (options.containsKey(name)) { + String s = options.getProperty(name); + try { + value = Boolean.parseBoolean(s); + } catch (Exception e) { + } + } + return value; + } } diff --git a/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java b/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java index b5a67e9d7..e3de06471 100644 --- a/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java +++ b/src/main/java/com/metaweb/gridworks/importers/TsvCsvImporter.java @@ -20,9 +20,11 @@ import com.metaweb.gridworks.model.Row; public class TsvCsvImporter implements Importer { - public void read(Reader reader, Project project, Properties options, int skip, int limit) - throws Exception { - + public void read(Reader reader, Project project, Properties options) throws Exception { + int limit = ImporterUtilities.getIntegerOption("limit",options,-1); + int skip = ImporterUtilities.getIntegerOption("skip",options,0); + boolean guessValueType = ImporterUtilities.getBooleanOption("guess-value-type", options, true); + LineNumberReader lnReader = new LineNumberReader(reader); String sep = options.getProperty("separator"); // auto-detect if not present String line = null; @@ -76,7 +78,7 @@ public class TsvCsvImporter implements Importer { } else { Row row = new Row(cellCount); - if (parser.parseRow(row, line)) { + if (parser.parseRow(row, line, guessValueType)) { rowsWithData++; if (skip <= 0 || rowsWithData > skip) { @@ -92,9 +94,7 @@ public class TsvCsvImporter implements Importer { } } - public void read(InputStream inputStream, Project project, - Properties options, int skip, int limit) throws Exception { - + public void read(InputStream inputStream, Project project, Properties options) throws Exception { throw new NotImplementedException(); } diff --git a/src/main/java/com/metaweb/gridworks/importers/XmlImporter.java b/src/main/java/com/metaweb/gridworks/importers/XmlImporter.java index 368582b38..658ee460d 100644 --- a/src/main/java/com/metaweb/gridworks/importers/XmlImporter.java +++ b/src/main/java/com/metaweb/gridworks/importers/XmlImporter.java @@ -19,7 +19,7 @@ public class XmlImporter implements Importer { return false; } - public void read(Reader reader, Project project, Properties options, int skip, int limit) + public void read(Reader reader, Project project, Properties options) throws Exception { throw new NotImplementedException(); @@ -28,9 +28,7 @@ public class XmlImporter implements Importer { public void read( InputStream inputStream, Project project, - Properties options, - int skip, - int limit + Properties options ) throws Exception { PushbackInputStream pis = new PushbackInputStream(inputStream,BUFFER_SIZE); diff --git a/src/main/java/com/metaweb/gridworks/importers/parsers/CSVRowParser.java b/src/main/java/com/metaweb/gridworks/importers/parsers/CSVRowParser.java index 6f8daf977..af8ed74bf 100644 --- a/src/main/java/com/metaweb/gridworks/importers/parsers/CSVRowParser.java +++ b/src/main/java/com/metaweb/gridworks/importers/parsers/CSVRowParser.java @@ -60,12 +60,12 @@ public class CSVRowParser extends RowParser { return results; } - public boolean parseRow(Row row, String line) { + public boolean parseRow(Row row, String line, boolean guessValueType) { boolean hasData = false; List strings = split(line); for (String s : strings) { - Serializable value = ImporterUtilities.parseCellValue(s); + Serializable value = guessValueType ? ImporterUtilities.parseCellValue(s) : s; if (ExpressionUtils.isNonBlankData(value)) { row.cells.add(new Cell(value, null)); diff --git a/src/main/java/com/metaweb/gridworks/importers/parsers/RowParser.java b/src/main/java/com/metaweb/gridworks/importers/parsers/RowParser.java index f09605989..03e41f2ef 100644 --- a/src/main/java/com/metaweb/gridworks/importers/parsers/RowParser.java +++ b/src/main/java/com/metaweb/gridworks/importers/parsers/RowParser.java @@ -7,5 +7,5 @@ import com.metaweb.gridworks.model.Row; public abstract class RowParser { public abstract List split(String line); - public abstract boolean parseRow(Row row, String line); + public abstract boolean parseRow(Row row, String line, boolean guessValueType); } diff --git a/src/main/java/com/metaweb/gridworks/importers/parsers/SeparatorRowParser.java b/src/main/java/com/metaweb/gridworks/importers/parsers/SeparatorRowParser.java index bdc473f40..d225d2baa 100644 --- a/src/main/java/com/metaweb/gridworks/importers/parsers/SeparatorRowParser.java +++ b/src/main/java/com/metaweb/gridworks/importers/parsers/SeparatorRowParser.java @@ -30,14 +30,14 @@ public class SeparatorRowParser extends RowParser { return results; } - public boolean parseRow(Row row, String line) { + public boolean parseRow(Row row, String line, boolean guessValueType) { boolean hasData = false; String[] cells = StringUtils.splitPreserveAllTokens(line, sep); for (int c = 0; c < cells.length; c++) { String text = cells[c]; - Serializable value = ImporterUtilities.parseCellValue(text); + Serializable value = guessValueType ? ImporterUtilities.parseCellValue(text) : text; if (ExpressionUtils.isNonBlankData(value)) { row.cells.add(new Cell(value, null)); hasData = true; diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index a0437aa8a..d5db0d808 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -1 +1 @@ - Freebase Gridworks
Gridworks
Gridworks

Upload Data File

Data File:
Project Name:
Load up to: data rows (optional)
Skip: initial data rows (optional)
Separator: column separator (optional)

Import Existing Project

Project .tar or .tar.gz File:
Re-name Project: (optional)
\ No newline at end of file + Freebase Gridworks
Gridworks
Gridworks

Upload Data File

Data File:
Project Name:
Load up to: data rows (optional)
Skip: initial data rows (optional)
Column separator: (optional, default to comma or tab)
Guess Value Type: (try to parse cells' content into numbers, dates, etc.)

Import Existing Project

Project .tar or .tar.gz File:
Re-name Project: (optional)
\ No newline at end of file diff --git a/src/main/webapp/scripts/index.js b/src/main/webapp/scripts/index.js index cb0b303cf..571a26efa 100644 --- a/src/main/webapp/scripts/index.js +++ b/src/main/webapp/scripts/index.js @@ -10,7 +10,8 @@ function onClickUploadFileButton(evt) { "/command/create-project-from-upload?" + [ "skip=" + $("#skip-input")[0].value, "limit=" + $("#limit-input")[0].value, - "separator=" + $("#separator-input")[0].value + "separator=" + $("#separator-input")[0].value, + "guess-value-type=" + $("#guess-value-type-input")[0].checked ].join("&")); } } diff --git a/src/main/webapp/scripts/views/data-table-cell-ui.js b/src/main/webapp/scripts/views/data-table-cell-ui.js index b593c9cbf..33dd05c70 100644 --- a/src/main/webapp/scripts/views/data-table-cell-ui.js +++ b/src/main/webapp/scripts/views/data-table-cell-ui.js @@ -275,6 +275,7 @@ DataTableCellUI.prototype._postProcessOneCell = function(command, params, column { onDone: function(o) { self._cell = o.cell; + self._dataTableView._updateCell(self._rowIndex, self._cellIndex, self._cell); self._render(); } } @@ -412,6 +413,7 @@ DataTableCellUI.prototype._startEdit = function(elmt) { { onDone: function(o) { self._cell = o.cell; + self._dataTableView._updateCell(self._rowIndex, self._cellIndex, self._cell); self._render(); } } diff --git a/src/main/webapp/scripts/views/data-table-view.js b/src/main/webapp/scripts/views/data-table-view.js index 1d4ea1da7..037b63fc1 100644 --- a/src/main/webapp/scripts/views/data-table-view.js +++ b/src/main/webapp/scripts/views/data-table-view.js @@ -388,6 +388,20 @@ DataTableView.prototype._createMenuForAllColumns = function(elmt) { ], elmt, { width: "80px", horizontal: false }); }; +DataTableView.prototype._updateCell = function(rowIndex, cellIndex, cell) { + var rows = theProject.rowModel.rows; + for (var r = 0; r < rows.length; r++) { + var row = rows[r]; + if (row.i === rowIndex) { + while (cellIndex >= row.cells.length) { + row.cells.push(null); + } + row.cells[cellIndex] = cell; + break; + } + } +}; + DataTableView.sampleVisibleRows = function(column) { var rowIndices = []; var values = []; diff --git a/src/main/webapp/styles/index.css b/src/main/webapp/styles/index.css index 6946ac40f..a2cb5d933 100644 --- a/src/main/webapp/styles/index.css +++ b/src/main/webapp/styles/index.css @@ -43,7 +43,7 @@ margin: 2em 0 1em 0; } -#import-project-panel-layout { +table.import-project-panel-layout { white-space: pre; }