Refactored to use JSONWriter to be more efficient at generating output.

Added created and modified time fields to project metadata.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@18 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-01 19:16:09 +00:00
parent 17cbe6b62d
commit fe8810aa07
30 changed files with 309 additions and 246 deletions

View File

@ -0,0 +1,10 @@
package com.metaweb.gridlock;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
public interface Jsonizable {
public void write(JSONWriter writer, Properties options) throws JSONException;
}

View File

@ -1,21 +1,31 @@
package com.metaweb.gridlock; package com.metaweb.gridlock;
import java.io.Serializable; import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
public class ProjectMetadata implements Serializable { public class ProjectMetadata implements Serializable, Jsonizable {
private static final long serialVersionUID = 7959027046468240844L; private static final long serialVersionUID = 7959027046468240844L;
public String name; public String name;
public String password; public String password;
public Date created = new Date();
public Date modified = new Date();
public JSONObject getJSON() throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
o.put("name", name); SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
return o; writer.object();
writer.key("name"); writer.value(name);
writer.key("created"); writer.value(sdf.format(created));
writer.key("modified"); writer.value(sdf.format(modified));
writer.endObject();
} }
} }

View File

@ -3,9 +3,11 @@ package com.metaweb.gridlock.browsing;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
public class DecoratedValue { import com.metaweb.gridlock.Jsonizable;
public class DecoratedValue implements Jsonizable {
final public Object value; final public Object value;
final public String label; final public String label;
@ -14,12 +16,12 @@ public class DecoratedValue {
this.label = label; this.label = label;
} }
public JSONObject getJSON(Properties options) throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
o.put("v", value); writer.object();
o.put("l", label); writer.key("v"); writer.value(value);
writer.key("l"); writer.value(label);
return o; writer.endObject();
} }
} }

View File

@ -1,6 +1,5 @@
package com.metaweb.gridlock.browsing; package com.metaweb.gridlock.browsing;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -8,13 +7,15 @@ import java.util.Properties;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.browsing.facets.Facet; import com.metaweb.gridlock.browsing.facets.Facet;
import com.metaweb.gridlock.browsing.facets.ListFacet; import com.metaweb.gridlock.browsing.facets.ListFacet;
import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.browsing.filters.RowFilter;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
public class Engine { public class Engine implements Jsonizable {
protected Project _project; protected Project _project;
protected List<Facet> _facets = new LinkedList<Facet>(); protected List<Facet> _facets = new LinkedList<Facet>();
@ -39,18 +40,6 @@ public class Engine {
return cfr; return cfr;
} }
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
List<JSONObject> a = new ArrayList<JSONObject>(_facets.size());
for (Facet facet : _facets) {
a.add(facet.getJSON(options));
}
o.put("facets", a);
return o;
}
public void initializeFromJSON(JSONObject o) throws Exception { public void initializeFromJSON(JSONObject o) throws Exception {
JSONArray a = o.getJSONArray("facets"); JSONArray a = o.getJSONArray("facets");
int length = a.length(); int length = a.length();
@ -78,4 +67,17 @@ public class Engine {
facet.computeChoices(_project, filteredRows); facet.computeChoices(_project, filteredRows);
} }
} }
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("facets"); writer.array();
for (Facet facet : _facets) {
facet.write(writer, options);
}
writer.endArray();
writer.endObject();
}
} }

View File

@ -1,20 +1,16 @@
package com.metaweb.gridlock.browsing.facets; package com.metaweb.gridlock.browsing.facets;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.browsing.FilteredRows; import com.metaweb.gridlock.browsing.FilteredRows;
import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.browsing.filters.RowFilter;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
public interface Facet { public interface Facet extends Jsonizable {
public RowFilter getRowFilter(); public RowFilter getRowFilter();
public void computeChoices(Project project, FilteredRows filteredRows); public void computeChoices(Project project, FilteredRows filteredRows);
public JSONObject getJSON(Properties options) throws JSONException;
public void initializeFromJSON(JSONObject o) throws Exception; public void initializeFromJSON(JSONObject o) throws Exception;
} }

View File

@ -1,6 +1,5 @@
package com.metaweb.gridlock.browsing.facets; package com.metaweb.gridlock.browsing.facets;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -8,6 +7,7 @@ import java.util.Properties;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONWriter;
import com.metaweb.gridlock.browsing.DecoratedValue; import com.metaweb.gridlock.browsing.DecoratedValue;
import com.metaweb.gridlock.browsing.FilteredRows; import com.metaweb.gridlock.browsing.FilteredRows;
@ -30,20 +30,20 @@ public class ListFacet implements Facet {
} }
@Override @Override
public JSONObject getJSON(Properties options) throws JSONException { public void write(JSONWriter writer, Properties options)
JSONObject o = new JSONObject(); throws JSONException {
o.put("name", _name); writer.object();
o.put("expression", _expression); writer.key("name"); writer.value(_name);
o.put("cellIndex", _cellIndex); writer.key("expression"); writer.value(_expression);
writer.key("cellIndex"); writer.value(_cellIndex);
List<JSONObject> a = new ArrayList<JSONObject>(_choices.size()); writer.key("choices"); writer.array();
for (NominalFacetChoice choice : _choices) { for (NominalFacetChoice choice : _choices) {
a.add(choice.getJSON(options)); choice.write(writer, options);
} }
o.put("choices", a); writer.endArray();
writer.endObject();
return o;
} }
@Override @Override

View File

@ -3,11 +3,12 @@ package com.metaweb.gridlock.browsing.facets;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.browsing.DecoratedValue; import com.metaweb.gridlock.browsing.DecoratedValue;
public class NominalFacetChoice { public class NominalFacetChoice implements Jsonizable {
final public DecoratedValue decoratedValue; final public DecoratedValue decoratedValue;
public int count; public int count;
public boolean selected; public boolean selected;
@ -16,13 +17,13 @@ public class NominalFacetChoice {
this.decoratedValue = decoratedValue; this.decoratedValue = decoratedValue;
} }
public JSONObject getJSON(Properties options) throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
o.put("v", decoratedValue.getJSON(options)); writer.object();
o.put("c", count); writer.key("v"); decoratedValue.write(writer, options);
o.put("s", selected); writer.key("c"); writer.value(count);
writer.key("s"); writer.value(selected);
return o; writer.endObject();
} }
} }

View File

@ -18,7 +18,9 @@ import org.apache.commons.lang.NotImplementedException;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener; import org.json.JSONTokener;
import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.ProjectManager; import com.metaweb.gridlock.ProjectManager;
import com.metaweb.gridlock.browsing.Engine; import com.metaweb.gridlock.browsing.Engine;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
@ -70,9 +72,16 @@ public abstract class Command {
} }
} }
protected void respondJSON(HttpServletResponse response, JSONObject o) throws IOException { protected void respondJSON(HttpServletResponse response, Jsonizable o) throws IOException, JSONException {
respondJSON(response, o, new Properties());
}
protected void respondJSON(HttpServletResponse response, Jsonizable o, Properties options) throws IOException, JSONException {
response.setHeader("Content-Type", "application/json"); response.setHeader("Content-Type", "application/json");
respond(response, o.toString());
JSONWriter writer = new JSONWriter(response.getWriter());
o.write(writer, options);
} }
protected void respondException(HttpServletResponse response, Exception e) throws IOException { protected void respondException(HttpServletResponse response, Exception e) throws IOException {
@ -89,7 +98,8 @@ public abstract class Command {
o.put("stack", sw.toString()); o.put("stack", sw.toString());
respondJSON(response, o); response.setHeader("Content-Type", "application/json");
respond(response, o.toString());
} catch (JSONException e1) { } catch (JSONException e1) {
e.printStackTrace(response.getWriter()); e.printStackTrace(response.getWriter());
} }

View File

@ -1,7 +1,6 @@
package com.metaweb.gridlock.commands; package com.metaweb.gridlock.commands;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -21,8 +20,7 @@ public class ComputeFacetsCommand extends Command {
engine.computeFacets(); engine.computeFacets();
Properties options = new Properties(); respondJSON(response, engine);
respondJSON(response, engine.getJSON(options));
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -1,7 +1,6 @@
package com.metaweb.gridlock.commands; package com.metaweb.gridlock.commands;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -17,10 +16,9 @@ public class GetColumnModelCommand extends Command {
throws ServletException, IOException { throws ServletException, IOException {
Project project = getProject(request); Project project = getProject(request);
Properties options = new Properties();
try { try {
respondJSON(response, project.columnModel.getJSON(options)); respondJSON(response, project.columnModel);
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -1,7 +1,6 @@
package com.metaweb.gridlock.commands; package com.metaweb.gridlock.commands;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -17,10 +16,8 @@ public class GetHistoryCommand extends Command {
throws ServletException, IOException { throws ServletException, IOException {
Project project = getProject(request); Project project = getProject(request);
Properties options = new Properties();
try { try {
respondJSON(response, project.history.getJSON(options)); respondJSON(response, project.history);
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -1,7 +1,6 @@
package com.metaweb.gridlock.commands; package com.metaweb.gridlock.commands;
import java.io.IOException; import java.io.IOException;
import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -19,9 +18,7 @@ public class GetProcessesCommand extends Command {
Project project = getProject(request); Project project = getProject(request);
try { try {
Properties options = new Properties(); respondJSON(response, project.processManager);
respondJSON(response, project.processManager.getJSON(options));
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -19,7 +19,7 @@ public class GetProjectMetadataCommand extends Command {
Project project = getProject(request); Project project = getProject(request);
try { try {
respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id).getJSON()); respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id));
} catch (JSONException e) { } catch (JSONException e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -1,8 +1,6 @@
package com.metaweb.gridlock.commands; package com.metaweb.gridlock.commands;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@ -10,7 +8,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.browsing.Engine; import com.metaweb.gridlock.browsing.Engine;
import com.metaweb.gridlock.browsing.FilteredRows; import com.metaweb.gridlock.browsing.FilteredRows;
@ -31,17 +29,18 @@ public class GetRowsCommand extends Command {
int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20))); int limit = Math.min(project.rows.size() - start, Math.max(0, getIntegerParameter(request, "limit", 20)));
Properties options = new Properties(); Properties options = new Properties();
JSONObject o = new JSONObject(); response.setHeader("Content-Type", "application/json");
List<JSONObject> a = new ArrayList<JSONObject>(limit); JSONWriter writer = new JSONWriter(response.getWriter());
try { writer.object();
FilteredRows filteredRows = engine.getAllFilteredRows();
{
RowAccumulator acc = new RowAccumulator(start, limit) { RowAccumulator acc = new RowAccumulator(start, limit) {
List<JSONObject> list; JSONWriter writer;
Properties options; Properties options;
public RowAccumulator init(List<JSONObject> list, Properties options) { public RowAccumulator init(JSONWriter writer, Properties options) {
this.list = list; this.writer = writer;
this.options = options; this.options = options;
return this; return this;
} }
@ -49,27 +48,28 @@ public class GetRowsCommand extends Command {
@Override @Override
public boolean internalVisit(int rowIndex, Row row) { public boolean internalVisit(int rowIndex, Row row) {
try { try {
JSONObject ro = row.getJSON(options); options.put("rowIndex", rowIndex);
ro.put("i", rowIndex); row.write(writer, options);
list.add(ro);
} catch (JSONException e) { } catch (JSONException e) {
} }
return false; return false;
} }
}.init(a, options); }.init(writer, options);
FilteredRows filteredRows = engine.getAllFilteredRows();
writer.key("rows"); writer.array();
filteredRows.accept(project, acc); filteredRows.accept(project, acc);
writer.endArray();
o.put("filtered", acc.total); writer.key("filtered"); writer.value(acc.total);
} catch (JSONException e) {
respondException(response, e);
} }
o.put("start", start);
o.put("limit", limit);
o.put("rows", a);
o.put("total", project.rows.size());
respondJSON(response, o); writer.key("start"); writer.value(start);
writer.key("limit"); writer.value(limit);
writer.key("total"); writer.value(project.rows.size());
writer.endObject();
} catch (Exception e) { } catch (Exception e) {
respondException(response, e); respondException(response, e);
} }

View File

@ -2,8 +2,6 @@ package com.metaweb.gridlock.expr;
import java.util.Properties; import java.util.Properties;
import org.json.JSONObject;
public class VariableExpr implements Evaluable { public class VariableExpr implements Evaluable {
final protected String _name; final protected String _name;

View File

@ -2,16 +2,18 @@ package com.metaweb.gridlock.history;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.ProjectManager; import com.metaweb.gridlock.ProjectManager;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
public class History implements Serializable { public class History implements Serializable, Jsonizable {
private static final long serialVersionUID = -1529783362243627391L; private static final long serialVersionUID = -1529783362243627391L;
protected long _projectID; protected long _projectID;
@ -31,7 +33,13 @@ public class History implements Serializable {
_futureEntries.clear(); _futureEntries.clear();
_pastEntries.add(entry); _pastEntries.add(entry);
entry.apply(ProjectManager.singleton.getProject(_projectID)); entry.apply(ProjectManager.singleton.getProject(_projectID));
setModified();
}
protected void setModified() {
ProjectManager.singleton.getProjectMetadata(_projectID).modified = new Date();
} }
public List<HistoryEntry> getLastPastEntries(int count) { public List<HistoryEntry> getLastPastEntries(int count) {
@ -84,6 +92,7 @@ public class History implements Serializable {
_futureEntries.add(0, entry); _futureEntries.add(0, entry);
} }
setModified();
} }
protected void redo(int times) { protected void redo(int times) {
@ -97,23 +106,27 @@ public class History implements Serializable {
_pastEntries.add(entry); _pastEntries.add(entry);
} }
setModified();
} }
public JSONObject getJSON(Properties options) throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
List<JSONObject> a = new ArrayList<JSONObject>(_pastEntries.size()); writer.object();
writer.key("past"); writer.array();
for (HistoryEntry entry : _pastEntries) { for (HistoryEntry entry : _pastEntries) {
a.add(entry.getJSON(options)); entry.write(writer, options);
} }
o.put("past", a); writer.endArray();
List<JSONObject> b = new ArrayList<JSONObject>(_futureEntries.size()); writer.key("future"); writer.array();
for (HistoryEntry entry : _futureEntries) { for (HistoryEntry entry : _futureEntries) {
b.add(entry.getJSON(options)); entry.write(writer, options);
} }
o.put("future", b); writer.endArray();
return o; writer.endObject();
} }
} }

View File

@ -12,12 +12,13 @@ import java.util.Date;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.ProjectManager; import com.metaweb.gridlock.ProjectManager;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
public class HistoryEntry implements Serializable { public class HistoryEntry implements Serializable, Jsonizable {
private static final long serialVersionUID = 532766467813930262L; private static final long serialVersionUID = 532766467813930262L;
public long id; public long id;
@ -38,16 +39,17 @@ public class HistoryEntry implements Serializable {
saveChange(); saveChange();
} }
public JSONObject getJSON(Properties options) throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance();
o.put("id", id); writer.object();
o.put("description", description); writer.key("id"); writer.value(id);
o.put("time", sdf.format(time)); writer.key("description"); writer.value(description);
writer.key("time"); writer.value(sdf.format(time));
return o; writer.endObject();
} }
public void apply(Project project) { public void apply(Project project) {

View File

@ -3,7 +3,7 @@ package com.metaweb.gridlock.history;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
import com.metaweb.gridlock.process.Process; import com.metaweb.gridlock.process.Process;
@ -49,14 +49,14 @@ public class HistoryProcess extends Process {
} }
@Override @Override
public JSONObject getJSON(Properties options) throws JSONException { public void write(JSONWriter writer, Properties options)
JSONObject o = new JSONObject(); throws JSONException {
o.put("description", _description); writer.object();
o.put("immediate", true); writer.key("description"); writer.value(_description);
o.put("status", _done ? "done" : "pending"); writer.key("immediate"); writer.value(true);
writer.key("status"); writer.value(_done ? "done" : "pending");
return o; writer.endObject();
} }
@Override @Override

View File

@ -1,6 +1,5 @@
package com.metaweb.gridlock.history; package com.metaweb.gridlock.history;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;

View File

@ -4,27 +4,17 @@ import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.expr.HasFields; import com.metaweb.gridlock.expr.HasFields;
public class Cell implements Serializable, HasFields { public class Cell implements Serializable, HasFields, Jsonizable {
private static final long serialVersionUID = -5891067829205458102L; private static final long serialVersionUID = -5891067829205458102L;
public Object value; public Object value;
public Recon recon; public Recon recon;
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
o.put("v", value);
if (recon != null && options.containsKey("cell-recon")) {
o.put("recon", recon.getJSON(options));
}
return o;
}
@Override @Override
public Object getField(String name, Properties bindings) { public Object getField(String name, Properties bindings) {
if ("value".equals(name)) { if ("value".equals(name)) {
@ -34,4 +24,17 @@ public class Cell implements Serializable, HasFields {
} }
return null; return null;
} }
@Override
public void write(JSONWriter writer, Properties options) throws JSONException {
writer.object();
writer.key("v");
writer.value(value);
if (recon != null && options.containsKey("cell-recon")) {
writer.key("recon");
recon.write(writer, options);
}
writer.endObject();
}
} }

View File

@ -4,22 +4,25 @@ import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
public class Column implements Serializable { import com.metaweb.gridlock.Jsonizable;
public class Column implements Serializable, Jsonizable {
private static final long serialVersionUID = -1063342490951563563L; private static final long serialVersionUID = -1063342490951563563L;
public int cellIndex; public int cellIndex;
public String headerLabel; public String headerLabel;
public Class valueType; public Class valueType;
public JSONObject getJSON(Properties options) throws JSONException { @Override
JSONObject o = new JSONObject(); public void write(JSONWriter writer, Properties options)
throws JSONException {
o.put("cellIndex", cellIndex); writer.object();
o.put("headerLabel", headerLabel); writer.key("cellIndex"); writer.value(cellIndex);
o.put("valueType", valueType == null ? null : valueType.getSimpleName()); writer.key("headerLabel"); writer.value(headerLabel);
writer.key("valueType"); writer.value(valueType == null ? null : valueType.getSimpleName());
return o; writer.endObject();
} }
} }

View File

@ -1,34 +1,23 @@
package com.metaweb.gridlock.model; package com.metaweb.gridlock.model;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
public class ColumnModel implements Serializable { import com.metaweb.gridlock.Jsonizable;
public class ColumnModel implements Serializable, Jsonizable {
private static final long serialVersionUID = 7679639795211544511L; private static final long serialVersionUID = 7679639795211544511L;
public List<Column> columns = new LinkedList<Column>(); public List<Column> columns = new LinkedList<Column>();
transient protected Map<String, Column> _nameToColumn; transient protected Map<String, Column> _nameToColumn;
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
List<JSONObject> a = new ArrayList<JSONObject>(columns.size());
for (Column column : columns) {
a.add(column.getJSON(options));
}
o.put("columns", a);
return o;
}
public Column getColumnByName(String name) { public Column getColumnByName(String name) {
if (_nameToColumn == null) { if (_nameToColumn == null) {
for (Column column : columns) { for (Column column : columns) {
@ -46,4 +35,18 @@ public class ColumnModel implements Serializable {
} }
return null; return null;
} }
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("columns");
writer.array();
for (Column column : columns) {
column.write(writer, options);
}
writer.endArray();
writer.endObject();
}
} }

View File

@ -8,11 +8,12 @@ import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.expr.HasFields; import com.metaweb.gridlock.expr.HasFields;
public class Recon implements Serializable, HasFields { public class Recon implements Serializable, HasFields, Jsonizable {
private static final long serialVersionUID = 8906257833709315762L; private static final long serialVersionUID = 8906257833709315762L;
static public enum Judgment { static public enum Judgment {
@ -26,13 +27,6 @@ public class Recon implements Serializable, HasFields {
public Judgment judgment = Judgment.None; public Judgment judgment = Judgment.None;
public ReconCandidate match = null; public ReconCandidate match = null;
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
o.put("j", judgmentToString());
return o;
}
@Override @Override
public Object getField(String name, Properties bindings) { public Object getField(String name, Properties bindings) {
if ("best".equals(name)) { if ("best".equals(name)) {
@ -65,4 +59,14 @@ public class Recon implements Serializable, HasFields {
return features.get(name); return features.get(name);
} }
} }
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("j");
writer.value(judgmentToString());
writer.endObject();
}
} }

View File

@ -4,11 +4,12 @@ import java.io.Serializable;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.expr.HasFields; import com.metaweb.gridlock.expr.HasFields;
public class ReconCandidate implements Serializable, HasFields { public class ReconCandidate implements Serializable, HasFields, Jsonizable {
private static final long serialVersionUID = -8013997214978715606L; private static final long serialVersionUID = -8013997214978715606L;
public String topicID; public String topicID;
@ -17,18 +18,6 @@ public class ReconCandidate implements Serializable, HasFields {
public String[] typeIDs; public String[] typeIDs;
public double score; public double score;
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
o.put("id", topicID);
o.put("guid", topicGUID);
o.put("name", topicName);
o.put("types", typeIDs);
o.put("score", score);
return o;
}
@Override @Override
public Object getField(String name, Properties bindings) { public Object getField(String name, Properties bindings) {
if ("id".equals(name)) { if ("id".equals(name)) {
@ -44,4 +33,23 @@ public class ReconCandidate implements Serializable, HasFields {
} }
return null; return null;
} }
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("id"); writer.value(topicID);
writer.key("guid"); writer.value(topicGUID);
writer.key("name"); writer.value(topicName);
writer.key("score"); writer.value(score);
writer.key("types"); writer.array();
for (String typeID : typeIDs) {
writer.value(typeID);
}
writer.endArray();
writer.endObject();
}
} }

View File

@ -6,11 +6,12 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.Jsonizable;
import com.metaweb.gridlock.expr.HasFields; import com.metaweb.gridlock.expr.HasFields;
public class Row implements Serializable, HasFields { public class Row implements Serializable, HasFields, Jsonizable {
private static final long serialVersionUID = -689264211730915507L; private static final long serialVersionUID = -689264211730915507L;
public boolean flagged; public boolean flagged;
@ -21,20 +22,6 @@ public class Row implements Serializable, HasFields {
cells = new ArrayList<Cell>(cellCount); cells = new ArrayList<Cell>(cellCount);
} }
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
List<JSONObject> a = new ArrayList<JSONObject>(cells.size());
for (Cell cell : cells) {
a.add(cell.getJSON(options));
}
o.put("cells", a);
o.put("flagged", flagged);
o.put("starred", starred);
return o;
}
@Override @Override
public Object getField(String name, Properties bindings) { public Object getField(String name, Properties bindings) {
if ("flagged".equals(name)) { if ("flagged".equals(name)) {
@ -61,4 +48,25 @@ public class Row implements Serializable, HasFields {
} }
} }
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("flagged"); writer.value(flagged);
writer.key("starred"); writer.value(starred);
writer.key("cells"); writer.array();
for (Cell cell : cells) {
cell.write(writer, options);
}
writer.endArray();
if (options.containsKey("rowIndex")) {
writer.key("i"); writer.value(options.get("rowIndex"));
}
writer.endObject();
}
} }

View File

@ -3,7 +3,7 @@ package com.metaweb.gridlock.process;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
abstract public class LongRunningProcess extends Process { abstract public class LongRunningProcess extends Process {
final protected String _description; final protected String _description;
@ -25,16 +25,15 @@ abstract public class LongRunningProcess extends Process {
} }
@Override @Override
public public void write(JSONWriter writer, Properties options)
JSONObject getJSON(Properties options) throws JSONException { throws JSONException {
JSONObject o = new JSONObject();
o.put("description", _description); writer.object();
o.put("immediate", false); writer.key("description"); writer.value(_description);
o.put("status", _thread == null ? "pending" : (_thread.isAlive() ? "running" : "done")); writer.key("immediate"); writer.value(false);
o.put("progress", _progress); writer.key("status"); writer.value(_thread == null ? "pending" : (_thread.isAlive() ? "running" : "done"));
writer.key("progress"); writer.value(_progress);
return o; writer.endObject();
} }
@Override @Override

View File

@ -1,11 +1,8 @@
package com.metaweb.gridlock.process; package com.metaweb.gridlock.process;
import java.util.Properties; import com.metaweb.gridlock.Jsonizable;
import org.json.JSONException; public abstract class Process implements Jsonizable {
import org.json.JSONObject;
public abstract class Process {
abstract public boolean isImmediate(); abstract public boolean isImmediate();
abstract public boolean isRunning(); abstract public boolean isRunning();
@ -15,6 +12,4 @@ public abstract class Process {
abstract public void startPerforming(ProcessManager manager); abstract public void startPerforming(ProcessManager manager);
abstract public void cancel(); abstract public void cancel();
public abstract JSONObject getJSON(Properties options) throws JSONException;
} }

View File

@ -1,30 +1,34 @@
package com.metaweb.gridlock.process; package com.metaweb.gridlock.process;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
public class ProcessManager { import com.metaweb.gridlock.Jsonizable;
public class ProcessManager implements Jsonizable {
protected List<Process> _processes = new LinkedList<Process>(); protected List<Process> _processes = new LinkedList<Process>();
public ProcessManager() { public ProcessManager() {
} }
public JSONObject getJSON(Properties options) throws JSONException {
JSONObject o = new JSONObject();
List<JSONObject> a = new ArrayList<JSONObject>(_processes.size()); @Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("processes"); writer.array();
for (Process p : _processes) { for (Process p : _processes) {
a.add(p.getJSON(options)); p.write(writer, options);
} }
o.put("processes", a); writer.endArray();
return o; writer.endObject();
} }
public boolean queueProcess(Process process) { public boolean queueProcess(Process process) {

View File

@ -3,7 +3,7 @@ package com.metaweb.gridlock.process;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONWriter;
import com.metaweb.gridlock.history.HistoryEntry; import com.metaweb.gridlock.history.HistoryEntry;
import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.model.Project;
@ -45,17 +45,17 @@ public class QuickHistoryEntryProcess extends Process {
} }
@Override @Override
public public void write(JSONWriter writer, Properties options)
JSONObject getJSON(Properties options) throws JSONException { throws JSONException {
JSONObject o = new JSONObject();
o.put("description", _historyEntry.description); writer.object();
o.put("immediate", true); writer.key("description"); writer.value(_historyEntry.description);
o.put("status", _done ? "done" : "pending"); writer.key("immediate"); writer.value(true);
writer.key("status"); writer.value(_done ? "done" : "pending");
return o; writer.endObject();
} }
@Override @Override
public boolean isDone() { public boolean isDone() {
return _done; return _done;

View File

@ -25,7 +25,7 @@
.history-past { .history-past {
padding-bottom: 3px; padding-bottom: 3px;
border-bottom: 2px solid #aaa; border-bottom: 2px solid #f88;
} }
.history-future { .history-future {
padding-top: 3px; padding-top: 3px;
@ -33,9 +33,12 @@
a.history-entry { a.history-entry {
display: block; display: block;
padding: 3px 5px; padding: 3px 5px;
border-bottom: 1px solid #eee;
text-decoration: none; text-decoration: none;
color: black; color: black;
border-top: 1px solid #eee;
}
a.history-entry:first-child {
border-top: none;
} }
a.history-entry:hover { a.history-entry:hover {
background: #eee; background: #eee;