From 07cf85b2a5a3a4786c72f930285f36247896dfe0 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Wed, 17 Mar 2010 22:30:16 +0000 Subject: [PATCH] Added make_cli option for generating a zip containing all files necessary to do 'gridworks run' at the command line. This excludes Java source files but contains pretty much everything else. Added make_all option that makes dmg, exe, and cli. Added html and xls exporters. Made exported files named after project names rather than project IDs. git-svn-id: http://google-refine.googlecode.com/svn/trunk@314 7d457c2a-affb-35e4-300a-418c747d4874 --- build.xml | 52 +++++++- gridworks | 25 +++- .../commands/info/ExportRowsCommand.java | 15 ++- .../metaweb/gridworks/exporters/Exporter.java | 5 + .../exporters/HtmlTableExporter.java | 97 +++++++++++++++ .../exporters/TripleloaderExporter.java | 12 ++ .../gridworks/exporters/TsvExporter.java | 12 ++ .../gridworks/exporters/XlsExporter.java | 115 ++++++++++++++++++ src/main/webapp/scripts/project/menu-bar.js | 14 ++- 9 files changed, 337 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/metaweb/gridworks/exporters/HtmlTableExporter.java create mode 100644 src/main/java/com/metaweb/gridworks/exporters/XlsExporter.java diff --git a/build.xml b/build.xml index d62e2e118..c5dd0763f 100644 --- a/build.xml +++ b/build.xml @@ -30,6 +30,7 @@ + @@ -97,13 +98,13 @@ - + + /> - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gridworks b/gridworks index 8967f9d95..818833d5d 100755 --- a/gridworks +++ b/gridworks @@ -49,6 +49,8 @@ and is one of make_dmg ........ Make MacOSX DMG distribution make_exe ........ Make Windows EXE distribution + make_cli ........ Make zip distribution to be run at command line + make_all ........ Make all distributions clean ..................... Clean compiled classes distclean ................. Remove all generated files @@ -146,7 +148,13 @@ get_version() { NUM_VERSION=`echo $VERSION | sed 's/[a-zA-Z]//g'` } - + +make_all() { + make_dmg $1 + make_exe $1 + make_cli $1 +} + make_exe() { check_macosx dist_prepare @@ -155,6 +163,15 @@ make_exe() { ant exe } + +make_cli() { + check_macosx + dist_prepare + launch4j_prepare + get_version $1 + + ant cli +} make_dmg() { check_macosx @@ -387,6 +404,12 @@ case "$ACTION" in make_exe) make_exe $1;; + + make_cli) + make_cli $1;; + + make_all) + make_all $1;; *) usage; ;; diff --git a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java index 32e9f738e..4e62e0d13 100644 --- a/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java +++ b/src/main/java/com/metaweb/gridworks/commands/info/ExportRowsCommand.java @@ -11,8 +11,10 @@ import javax.servlet.http.HttpServletResponse; import com.metaweb.gridworks.browsing.Engine; import com.metaweb.gridworks.commands.Command; import com.metaweb.gridworks.exporters.Exporter; +import com.metaweb.gridworks.exporters.HtmlTableExporter; import com.metaweb.gridworks.exporters.TripleloaderExporter; import com.metaweb.gridworks.exporters.TsvExporter; +import com.metaweb.gridworks.exporters.XlsExporter; import com.metaweb.gridworks.model.Project; public class ExportRowsCommand extends Command { @@ -25,11 +27,14 @@ public class ExportRowsCommand extends Command { Engine engine = getEngine(request, project); String format = request.getParameter("format"); - PrintWriter writer = response.getWriter(); Exporter exporter = null; if ("tripleloader".equalsIgnoreCase(format)) { exporter = new TripleloaderExporter(); + } else if ("html".equalsIgnoreCase(format)) { + exporter = new HtmlTableExporter(); + } else if ("xls".equalsIgnoreCase(format)) { + exporter = new XlsExporter(); } else { exporter = new TsvExporter(); } @@ -37,8 +42,12 @@ public class ExportRowsCommand extends Command { response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Type", exporter.getContentType()); - exporter.export(project, new Properties(), engine, writer); - + if (exporter.takeWriter()) { + PrintWriter writer = response.getWriter(); + exporter.export(project, new Properties(), engine, writer); + } else { + exporter.export(project, new Properties(), engine, response.getOutputStream()); + } } catch (Exception e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridworks/exporters/Exporter.java b/src/main/java/com/metaweb/gridworks/exporters/Exporter.java index 2274d55d4..35f745520 100644 --- a/src/main/java/com/metaweb/gridworks/exporters/Exporter.java +++ b/src/main/java/com/metaweb/gridworks/exporters/Exporter.java @@ -1,6 +1,7 @@ package com.metaweb.gridworks.exporters; import java.io.IOException; +import java.io.OutputStream; import java.io.Writer; import java.util.Properties; @@ -10,5 +11,9 @@ import com.metaweb.gridworks.model.Project; public interface Exporter { public String getContentType(); + public boolean takeWriter(); + + public void export(Project project, Properties options, Engine engine, OutputStream outputStream) throws IOException; + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException; } diff --git a/src/main/java/com/metaweb/gridworks/exporters/HtmlTableExporter.java b/src/main/java/com/metaweb/gridworks/exporters/HtmlTableExporter.java new file mode 100644 index 000000000..141dc8515 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/exporters/HtmlTableExporter.java @@ -0,0 +1,97 @@ +package com.metaweb.gridworks.exporters; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.util.Properties; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import com.metaweb.gridworks.ProjectManager; +import com.metaweb.gridworks.browsing.Engine; +import com.metaweb.gridworks.browsing.FilteredRows; +import com.metaweb.gridworks.browsing.RowVisitor; +import com.metaweb.gridworks.model.Cell; +import com.metaweb.gridworks.model.Column; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.Row; + +public class HtmlTableExporter implements Exporter { + public String getContentType() { + return "text/html"; + } + + public boolean takeWriter() { + return true; + } + + public void export(Project project, Properties options, Engine engine, + OutputStream outputStream) throws IOException { + throw new NotImplementedException(); + } + + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { + writer.write("\n"); + writer.write(""); + writer.write(ProjectManager.singleton.getProjectMetadata(project.id).getName()); + writer.write("\n"); + + writer.write("\n"); + writer.write("\n"); + + writer.write(""); + { + for (Column column : project.columnModel.columns) { + writer.write(""); + } + } + writer.write("\n"); + + { + RowVisitor visitor = new RowVisitor() { + Writer writer; + + public RowVisitor init(Writer writer) { + this.writer = writer; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual, boolean includeDependent) { + try { + writer.write(""); + + for (Column column : project.columnModel.columns) { + writer.write(""); + } + + writer.write("\n"); + } catch (IOException e) { + // ignore + } + return false; + } + }.init(writer); + + FilteredRows filteredRows = engine.getAllFilteredRows(true); + filteredRows.accept(project, visitor); + } + + writer.write("
"); + writer.write(column.getName()); + writer.write("
"); + + int cellIndex = column.getCellIndex(); + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + if (cell != null && cell.value != null) { + Object v = cell.value; + writer.write(v instanceof String ? ((String) v) : v.toString()); + } + } + + writer.write("
\n"); + writer.write("\n"); + writer.write("\n"); + } + +} diff --git a/src/main/java/com/metaweb/gridworks/exporters/TripleloaderExporter.java b/src/main/java/com/metaweb/gridworks/exporters/TripleloaderExporter.java index 6caa586ee..63443f47c 100644 --- a/src/main/java/com/metaweb/gridworks/exporters/TripleloaderExporter.java +++ b/src/main/java/com/metaweb/gridworks/exporters/TripleloaderExporter.java @@ -1,9 +1,12 @@ package com.metaweb.gridworks.exporters; import java.io.IOException; +import java.io.OutputStream; import java.io.Writer; import java.util.Properties; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + import com.metaweb.gridworks.browsing.Engine; import com.metaweb.gridworks.model.Project; import com.metaweb.gridworks.protograph.Protograph; @@ -15,6 +18,15 @@ public class TripleloaderExporter implements Exporter { return "application/x-unknown"; } + public boolean takeWriter() { + return true; + } + + public void export(Project project, Properties options, Engine engine, + OutputStream outputStream) throws IOException { + throw new NotImplementedException(); + } + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { diff --git a/src/main/java/com/metaweb/gridworks/exporters/TsvExporter.java b/src/main/java/com/metaweb/gridworks/exporters/TsvExporter.java index 7857ab419..e8eee0afc 100644 --- a/src/main/java/com/metaweb/gridworks/exporters/TsvExporter.java +++ b/src/main/java/com/metaweb/gridworks/exporters/TsvExporter.java @@ -1,9 +1,12 @@ package com.metaweb.gridworks.exporters; import java.io.IOException; +import java.io.OutputStream; import java.io.Writer; import java.util.Properties; +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + import com.metaweb.gridworks.browsing.Engine; import com.metaweb.gridworks.browsing.FilteredRows; import com.metaweb.gridworks.browsing.RowVisitor; @@ -17,6 +20,15 @@ public class TsvExporter implements Exporter { return "text/plain"; } + public boolean takeWriter() { + return true; + } + + public void export(Project project, Properties options, Engine engine, + OutputStream outputStream) throws IOException { + throw new NotImplementedException(); + } + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { boolean first = true; for (Column column : project.columnModel.columns) { diff --git a/src/main/java/com/metaweb/gridworks/exporters/XlsExporter.java b/src/main/java/com/metaweb/gridworks/exporters/XlsExporter.java new file mode 100644 index 000000000..37bb444fb --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/exporters/XlsExporter.java @@ -0,0 +1,115 @@ +package com.metaweb.gridworks.exporters; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.util.Calendar; +import java.util.Date; +import java.util.Properties; + +import org.apache.poi.hssf.usermodel.HSSFHyperlink; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +import com.metaweb.gridworks.ProjectManager; +import com.metaweb.gridworks.browsing.Engine; +import com.metaweb.gridworks.browsing.FilteredRows; +import com.metaweb.gridworks.browsing.RowVisitor; +import com.metaweb.gridworks.model.Cell; +import com.metaweb.gridworks.model.Column; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.Row; + +public class XlsExporter implements Exporter { + public String getContentType() { + return "application/xls"; + } + + public boolean takeWriter() { + return false; + } + + public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { + throw new NotImplementedException(); + } + + public void export(Project project, Properties options, Engine engine, + OutputStream outputStream) throws IOException { + + Workbook wb = new HSSFWorkbook(); + Sheet s = wb.createSheet(); + wb.setSheetName(0, ProjectManager.singleton.getProjectMetadata(project.id).getName()); + + int rowCount = 0; + + { + org.apache.poi.ss.usermodel.Row r = s.createRow(rowCount++); + + int cellCount = 0; + for (Column column : project.columnModel.columns) { + org.apache.poi.ss.usermodel.Cell c = r.createCell(cellCount++); + c.setCellValue(column.getName()); + } + } + + { + RowVisitor visitor = new RowVisitor() { + Sheet sheet; + int rowCount; + + public RowVisitor init(Sheet sheet, int rowCount) { + this.sheet = sheet; + this.rowCount = rowCount; + return this; + } + + public boolean visit(Project project, int rowIndex, Row row, boolean contextual, boolean includeDependent) { + org.apache.poi.ss.usermodel.Row r = sheet.createRow(rowCount++); + + int cellCount = 0; + for (Column column : project.columnModel.columns) { + org.apache.poi.ss.usermodel.Cell c = r.createCell(cellCount++); + + int cellIndex = column.getCellIndex(); + if (cellIndex < row.cells.size()) { + Cell cell = row.cells.get(cellIndex); + if (cell != null && cell.value != null) { + Object v = cell.value; + if (v instanceof Number) { + c.setCellValue(((Number) v).doubleValue()); + } else if (v instanceof Boolean) { + c.setCellValue(((Boolean) v).booleanValue()); + } else if (v instanceof Date) { + c.setCellValue((Date) v); + } else if (v instanceof Calendar) { + c.setCellValue((Calendar) v); + } else if (v instanceof String) { + c.setCellValue((String) v); + } + + if (cell.recon != null && cell.recon.match != null) { + HSSFHyperlink hl = new HSSFHyperlink(HSSFHyperlink.LINK_URL); + hl.setLabel(cell.recon.match.topicName); + hl.setAddress("http://www.freebase.com/view" + cell.recon.match.topicID); + + c.setHyperlink(hl); + } + } + } + } + return false; + } + }.init(s, rowCount); + + FilteredRows filteredRows = engine.getAllFilteredRows(true); + filteredRows.accept(project, visitor); + } + + wb.write(outputStream); + outputStream.flush(); + } + +} diff --git a/src/main/webapp/scripts/project/menu-bar.js b/src/main/webapp/scripts/project/menu-bar.js index 1d000b57f..8d2b51984 100644 --- a/src/main/webapp/scripts/project/menu-bar.js +++ b/src/main/webapp/scripts/project/menu-bar.js @@ -20,6 +20,15 @@ MenuBar.prototype._initializeUI = function() { "label": "Tab-Separated Value", "click": function() { self._doExportRows("tsv", "tsv"); } }, + { + "label": "HTML Table", + "click": function() { self._doExportRows("html", "html"); } + }, + { + "label": "Excel", + "click": function() { self._doExportRows("xls", "xls"); } + }, + {}, { "label": "Tripleloader", "click": function() { self._doExportRows("tripleloader", "txt"); } @@ -137,11 +146,12 @@ MenuBar.prototype._deactivateMenu = function() { }; MenuBar.prototype._doExportRows = function(format, ext) { + var name = $.trim(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-'); var form = document.createElement("form"); $(form) .css("display", "none") .attr("method", "post") - .attr("action", "/command/export-rows/gridworks_" + theProject.id + "." + ext) + .attr("action", "/command/export-rows/" + name + "." + ext) .attr("target", "gridworks-export"); $('') @@ -166,7 +176,7 @@ MenuBar.prototype._doExportRows = function(format, ext) { }; MenuBar.prototype._exportProject = function() { - var name = theProject.metadata.name.replace(/\W/g, ' ').replace(/\s+/g, '-'); + var name = $(theProject.metadata.name.replace(/\W/g, ' ')).replace(/\s+/g, '-'); var form = document.createElement("form"); $(form) .css("display", "none")