Save templating exporter's template.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1221 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-08-24 06:36:49 +00:00
parent baa4e0db8c
commit 276fae8938
3 changed files with 110 additions and 46 deletions

View File

@ -2,11 +2,13 @@ package com.google.gridworks.exporters;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter;
import com.google.gridworks.browsing.Engine; import com.google.gridworks.browsing.Engine;
import com.google.gridworks.browsing.FilteredRecords; 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 { public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
String limitString = options.getProperty("limit"); String limitString = options.getProperty("limit");
int limit = limitString != null ? Integer.parseInt(limitString) : -1; int limit = limitString != null ? Integer.parseInt(limitString) : -1;
JSONObject sortingJson = null; JSONObject sortingJson = null;
try{ try{
String json = options.getProperty("sorting"); String json = options.getProperty("sorting");
sortingJson = (json == null) ? null : sortingJson = (json == null) ? null :
ParsingUtilities.evaluateJsonStringToObject(json); ParsingUtilities.evaluateJsonStringToObject(json);
} catch (JSONException e) { } 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; Template template;
try { try {
template = Parser.parse(options.getProperty("template")); template = Parser.parse(templateString);
} catch (ParsingException e) { } catch (ParsingException e) {
throw new IOException("Missing or bad template", e); throw new IOException("Missing or bad template", e);
} }
template.setPrefix(options.getProperty("prefix")); template.setPrefix(prefixString);
template.setSuffix(options.getProperty("suffix")); template.setSuffix(suffixString);
template.setSeparator(options.getProperty("separator")); 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) { if (engine.getMode() == Mode.RowBased) {
FilteredRows filteredRows = engine.getAllFilteredRows(); FilteredRows filteredRows = engine.getAllFilteredRows();
RowVisitor visitor = template.getRowVisitor(writer, limit); RowVisitor visitor = template.getRowVisitor(writer, limit);
if (sortingJson != null) { if (sortingJson != null) {
try { try {
SortingRowVisitor srv = new SortingRowVisitor(visitor); SortingRowVisitor srv = new SortingRowVisitor(visitor);
srv.initializeFromJSON(project, sortingJson); srv.initializeFromJSON(project, sortingJson);
if (srv.hasCriteria()) { if (srv.hasCriteria()) {
visitor = srv; visitor = srv;
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
filteredRows.accept(project, visitor); filteredRows.accept(project, visitor);
@ -82,16 +106,16 @@ public class TemplatingExporter implements Exporter {
RecordVisitor visitor = template.getRecordVisitor(writer, limit); RecordVisitor visitor = template.getRecordVisitor(writer, limit);
if (sortingJson != null) { if (sortingJson != null) {
try { try {
SortingRecordVisitor srv = new SortingRecordVisitor(visitor); SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
srv.initializeFromJSON(project, sortingJson); srv.initializeFromJSON(project, sortingJson);
if (srv.hasCriteria()) { if (srv.hasCriteria()) {
visitor = srv; visitor = srv;
} }
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
filteredRecords.accept(project, visitor); filteredRecords.accept(project, visitor);

View File

@ -17,10 +17,15 @@
<div class="input-container"><textarea bind="previewTextarea" class="code" wrap="off" style="height: 40em;"></textarea></div> <div class="input-container"><textarea bind="previewTextarea" class="code" wrap="off" style="height: 40em;"></textarea></div>
</td> </td>
</tr> </tr>
</table></div>' </table></div>
</div>
<div class="dialog-footer" bind="dialogFooter">
<button bind="exportButton">Export</button>
<button bind="cancelButton">Cancel</button>
</div> </div>
<div class="dialog-footer" bind="dialogFooter"><div class="grid-layout layout-normal layout-full"><table><tr>
<td align="left">
<button bind="resetButton">Reset Template</button>
</td>
<td style="text-align: right;">
<button bind="exportButton">Export</button>
<button bind="cancelButton">Cancel</button>
</td>
</tr></table></div></div>
</div> </div>

View File

@ -10,20 +10,54 @@ TemplatingExporterDialog.prototype._createDialog = function() {
this._elmts = DOM.bind(dialog); this._elmts = DOM.bind(dialog);
this._elmts.controls.find("textarea").keyup(function() { self._scheduleUpdate(); }); 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.exportButton.click(function() { self._export(); self._dismiss(); });
this._elmts.cancelButton.click(function() { 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); 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() { TemplatingExporterDialog.prototype._scheduleUpdate = function() {
var self = this; var self = this;
@ -56,6 +90,7 @@ TemplatingExporterDialog.prototype._updatePreview = function() {
"suffix" : this._elmts.suffixTextarea[0].value, "suffix" : this._elmts.suffixTextarea[0].value,
"separator" : this._elmts.separatorTextarea[0].value, "separator" : this._elmts.separatorTextarea[0].value,
"template" : this._elmts.templateTextarea[0].value, "template" : this._elmts.templateTextarea[0].value,
"preview" : true,
"limit" : "20" "limit" : "20"
}, },
function (data) { function (data) {