From 276fae8938a26365d035098a16529edcadf96874 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Tue, 24 Aug 2010 06:36:49 +0000 Subject: [PATCH] Save templating exporter's template. git-svn-id: http://google-refine.googlecode.com/svn/trunk@1221 7d457c2a-affb-35e4-300a-418c747d4874 --- .../exporters/TemplatingExporter.java | 90 ++++++++++++------- .../dialogs/templating-exporter-dialog.html | 15 ++-- .../dialogs/templating-exporter-dialog.js | 51 +++++++++-- 3 files changed, 110 insertions(+), 46 deletions(-) diff --git a/main/src/com/google/gridworks/exporters/TemplatingExporter.java b/main/src/com/google/gridworks/exporters/TemplatingExporter.java index 9373f5f73..06a05f7c1 100644 --- a/main/src/com/google/gridworks/exporters/TemplatingExporter.java +++ b/main/src/com/google/gridworks/exporters/TemplatingExporter.java @@ -2,11 +2,13 @@ package com.google.gridworks.exporters; import java.io.IOException; import java.io.OutputStream; +import java.io.StringWriter; import java.io.Writer; import java.util.Properties; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONWriter; import com.google.gridworks.browsing.Engine; import com.google.gridworks.browsing.FilteredRecords; @@ -37,43 +39,65 @@ public class TemplatingExporter implements Exporter { } public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException { - String limitString = options.getProperty("limit"); - int limit = limitString != null ? Integer.parseInt(limitString) : -1; - + String limitString = options.getProperty("limit"); + int limit = limitString != null ? Integer.parseInt(limitString) : -1; + JSONObject sortingJson = null; try{ String json = options.getProperty("sorting"); sortingJson = (json == null) ? null : - ParsingUtilities.evaluateJsonStringToObject(json); + ParsingUtilities.evaluateJsonStringToObject(json); } catch (JSONException e) { } + String templateString = options.getProperty("template"); + String prefixString = options.getProperty("prefix"); + String suffixString = options.getProperty("suffix"); + String separatorString = options.getProperty("separator"); + Template template; try { - template = Parser.parse(options.getProperty("template")); - } catch (ParsingException e) { - throw new IOException("Missing or bad template", e); - } - - template.setPrefix(options.getProperty("prefix")); - template.setSuffix(options.getProperty("suffix")); - template.setSeparator(options.getProperty("separator")); - + template = Parser.parse(templateString); + } catch (ParsingException e) { + throw new IOException("Missing or bad template", e); + } + + template.setPrefix(prefixString); + template.setSuffix(suffixString); + template.setSeparator(separatorString); + + if (!"true".equals(options.getProperty("preview"))) { + StringWriter stringWriter = new StringWriter(); + JSONWriter jsonWriter = new JSONWriter(stringWriter); + try { + jsonWriter.object(); + jsonWriter.key("template"); jsonWriter.value(templateString); + jsonWriter.key("prefix"); jsonWriter.value(prefixString); + jsonWriter.key("suffix"); jsonWriter.value(suffixString); + jsonWriter.key("separator"); jsonWriter.value(separatorString); + jsonWriter.endObject(); + } catch (JSONException e) { + // ignore + } + + project.getMetadata().getPreferenceStore().put("exporters.templating.template", stringWriter.toString()); + } + if (engine.getMode() == Mode.RowBased) { FilteredRows filteredRows = engine.getAllFilteredRows(); RowVisitor visitor = template.getRowVisitor(writer, limit); if (sortingJson != null) { - try { - SortingRowVisitor srv = new SortingRowVisitor(visitor); - srv.initializeFromJSON(project, sortingJson); - - if (srv.hasCriteria()) { - visitor = srv; - } - } catch (JSONException e) { - e.printStackTrace(); - } + try { + SortingRowVisitor srv = new SortingRowVisitor(visitor); + srv.initializeFromJSON(project, sortingJson); + + if (srv.hasCriteria()) { + visitor = srv; + } + } catch (JSONException e) { + e.printStackTrace(); + } } filteredRows.accept(project, visitor); @@ -82,16 +106,16 @@ public class TemplatingExporter implements Exporter { RecordVisitor visitor = template.getRecordVisitor(writer, limit); if (sortingJson != null) { - try { - SortingRecordVisitor srv = new SortingRecordVisitor(visitor); - srv.initializeFromJSON(project, sortingJson); - - if (srv.hasCriteria()) { - visitor = srv; - } - } catch (JSONException e) { - e.printStackTrace(); - } + try { + SortingRecordVisitor srv = new SortingRecordVisitor(visitor); + srv.initializeFromJSON(project, sortingJson); + + if (srv.hasCriteria()) { + visitor = srv; + } + } catch (JSONException e) { + e.printStackTrace(); + } } filteredRecords.accept(project, visitor); diff --git a/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.html b/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.html index 70ec17bea..ac4baf5a8 100644 --- a/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.html +++ b/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.html @@ -17,10 +17,15 @@
- ' - - + \ No newline at end of file diff --git a/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.js b/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.js index c8f9cf934..9b9fd0822 100644 --- a/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.js +++ b/main/webapp/modules/core/scripts/dialogs/templating-exporter-dialog.js @@ -10,20 +10,54 @@ TemplatingExporterDialog.prototype._createDialog = function() { this._elmts = DOM.bind(dialog); this._elmts.controls.find("textarea").keyup(function() { self._scheduleUpdate(); }); - this._elmts.prefixTextarea[0].value = '{\n "rows" : [\n'; - this._elmts.suffixTextarea[0].value = '\n ]\n}'; - this._elmts.separatorTextarea[0].value = ',\n'; - this._elmts.templateTextarea[0].value = ' {' + - $.map(theProject.columnModel.columns, function(column, i) { - return '\n "' + column.name + '" : {{jsonize(cells["' + column.name + '"].value)}}'; - }).join(',') + '\n }'; - this._elmts.exportButton.click(function() { self._export(); self._dismiss(); }); this._elmts.cancelButton.click(function() { self._dismiss(); }); + this._elmts.resetButton.click(function() { + self._fillInTemplate(self._createDefaultTemplate()); + self._updatePreview(); + }); + + this._getSavedTemplate(function(t) { + self._fillInTemplate(t || self._createDefaultTemplate()); + self._updatePreview(); + }); this._level = DialogSystem.showDialog(dialog); }; +TemplatingExporterDialog.prototype._getSavedTemplate = function(f) { + $.getJSON( + "/command/core/get-preference?" + $.param({ project: theProject.id, name: "exporters.templating.template" }), + null, + function(data) { + if (data.value != null) { + f(JSON.parse(data.value)); + } else { + f(null); + } + } + ); +}; + +TemplatingExporterDialog.prototype._createDefaultTemplate = function() { + return { + prefix: '{\n "rows" : [\n', + suffix: '\n ]\n}', + separator: ',\n', + template: ' {' + + $.map(theProject.columnModel.columns, function(column, i) { + return '\n "' + column.name + '" : {{jsonize(cells["' + column.name + '"].value)}}'; + }).join(',') + '\n }' + }; +}; + +TemplatingExporterDialog.prototype._fillInTemplate = function(t) { + this._elmts.prefixTextarea[0].value = t.prefix; + this._elmts.suffixTextarea[0].value = t.suffix; + this._elmts.separatorTextarea[0].value = t.separator; + this._elmts.templateTextarea[0].value = t.template; +}; + TemplatingExporterDialog.prototype._scheduleUpdate = function() { var self = this; @@ -56,6 +90,7 @@ TemplatingExporterDialog.prototype._updatePreview = function() { "suffix" : this._elmts.suffixTextarea[0].value, "separator" : this._elmts.separatorTextarea[0].value, "template" : this._elmts.templateTextarea[0].value, + "preview" : true, "limit" : "20" }, function (data) {