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.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;
@ -48,16 +50,38 @@ public class TemplatingExporter implements Exporter {
} 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"));
template = Parser.parse(templateString);
} 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.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();

View File

@ -17,10 +17,15 @@
<div class="input-container"><textarea bind="previewTextarea" class="code" wrap="off" style="height: 40em;"></textarea></div>
</td>
</tr>
</table></div>'
</table></div>
</div>
<div class="dialog-footer" bind="dialogFooter">
<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>
</div>
</td>
</tr></table></div></div>
</div>

View File

@ -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) {