From fe8810aa0798c2daeca35ab63255691969b5d9b0 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Mon, 1 Feb 2010 19:16:09 +0000 Subject: [PATCH] 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 --- .../java/com/metaweb/gridlock/Jsonizable.java | 10 ++++ .../com/metaweb/gridlock/ProjectMetadata.java | 26 ++++++---- .../gridlock/browsing/DecoratedValue.java | 20 ++++---- .../com/metaweb/gridlock/browsing/Engine.java | 30 ++++++------ .../gridlock/browsing/facets/Facet.java | 8 +--- .../gridlock/browsing/facets/ListFacet.java | 22 ++++----- .../browsing/facets/NominalFacetChoice.java | 21 ++++---- .../metaweb/gridlock/commands/Command.java | 16 +++++-- .../commands/ComputeFacetsCommand.java | 4 +- .../commands/GetColumnModelCommand.java | 4 +- .../gridlock/commands/GetHistoryCommand.java | 5 +- .../commands/GetProcessesCommand.java | 5 +- .../commands/GetProjectMetadataCommand.java | 2 +- .../gridlock/commands/GetRowsCommand.java | 48 +++++++++---------- .../metaweb/gridlock/expr/VariableExpr.java | 2 - .../com/metaweb/gridlock/history/History.java | 35 +++++++++----- .../gridlock/history/HistoryEntry.java | 20 ++++---- .../gridlock/history/HistoryProcess.java | 16 +++---- .../gridlock/history/MassCellChange.java | 1 - .../java/com/metaweb/gridlock/model/Cell.java | 29 ++++++----- .../com/metaweb/gridlock/model/Column.java | 21 ++++---- .../metaweb/gridlock/model/ColumnModel.java | 33 +++++++------ .../com/metaweb/gridlock/model/Recon.java | 22 +++++---- .../gridlock/model/ReconCandidate.java | 36 ++++++++------ .../java/com/metaweb/gridlock/model/Row.java | 40 +++++++++------- .../gridlock/process/LongRunningProcess.java | 21 ++++---- .../com/metaweb/gridlock/process/Process.java | 9 +--- .../gridlock/process/ProcessManager.java | 24 ++++++---- .../process/QuickHistoryEntryProcess.java | 18 +++---- src/main/webapp/styles/history.css | 7 ++- 30 files changed, 309 insertions(+), 246 deletions(-) create mode 100644 src/main/java/com/metaweb/gridlock/Jsonizable.java diff --git a/src/main/java/com/metaweb/gridlock/Jsonizable.java b/src/main/java/com/metaweb/gridlock/Jsonizable.java new file mode 100644 index 000000000..8d3cebed2 --- /dev/null +++ b/src/main/java/com/metaweb/gridlock/Jsonizable.java @@ -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; +} diff --git a/src/main/java/com/metaweb/gridlock/ProjectMetadata.java b/src/main/java/com/metaweb/gridlock/ProjectMetadata.java index ff9d41798..3eb4cf303 100644 --- a/src/main/java/com/metaweb/gridlock/ProjectMetadata.java +++ b/src/main/java/com/metaweb/gridlock/ProjectMetadata.java @@ -1,21 +1,31 @@ package com.metaweb.gridlock; import java.io.Serializable; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; 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; - public String name; - public String password; + public String name; + public String password; + public Date created = new Date(); + public Date modified = new Date(); - public JSONObject getJSON() throws JSONException { - JSONObject o = new JSONObject(); + @Override + 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(); } } diff --git a/src/main/java/com/metaweb/gridlock/browsing/DecoratedValue.java b/src/main/java/com/metaweb/gridlock/browsing/DecoratedValue.java index 5dc787842..43458db23 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/DecoratedValue.java +++ b/src/main/java/com/metaweb/gridlock/browsing/DecoratedValue.java @@ -3,9 +3,11 @@ package com.metaweb.gridlock.browsing; import java.util.Properties; 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 String label; @@ -14,12 +16,12 @@ public class DecoratedValue { this.label = label; } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - - o.put("v", value); - o.put("l", label); - - return o; + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("v"); writer.value(value); + writer.key("l"); writer.value(label); + writer.endObject(); } } diff --git a/src/main/java/com/metaweb/gridlock/browsing/Engine.java b/src/main/java/com/metaweb/gridlock/browsing/Engine.java index e9a8b4b6e..332e8cfcc 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/Engine.java +++ b/src/main/java/com/metaweb/gridlock/browsing/Engine.java @@ -1,6 +1,5 @@ package com.metaweb.gridlock.browsing; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Properties; @@ -8,13 +7,15 @@ import java.util.Properties; import org.json.JSONArray; import org.json.JSONException; 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.ListFacet; import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.model.Project; -public class Engine { +public class Engine implements Jsonizable { protected Project _project; protected List _facets = new LinkedList(); @@ -39,18 +40,6 @@ public class Engine { return cfr; } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - - List a = new ArrayList(_facets.size()); - for (Facet facet : _facets) { - a.add(facet.getJSON(options)); - } - o.put("facets", a); - - return o; - } - public void initializeFromJSON(JSONObject o) throws Exception { JSONArray a = o.getJSONArray("facets"); int length = a.length(); @@ -78,4 +67,17 @@ public class Engine { 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(); + } } diff --git a/src/main/java/com/metaweb/gridlock/browsing/facets/Facet.java b/src/main/java/com/metaweb/gridlock/browsing/facets/Facet.java index 23a58ecad..3f32dcc6f 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/facets/Facet.java +++ b/src/main/java/com/metaweb/gridlock/browsing/facets/Facet.java @@ -1,20 +1,16 @@ package com.metaweb.gridlock.browsing.facets; -import java.util.Properties; - -import org.json.JSONException; import org.json.JSONObject; +import com.metaweb.gridlock.Jsonizable; import com.metaweb.gridlock.browsing.FilteredRows; import com.metaweb.gridlock.browsing.filters.RowFilter; import com.metaweb.gridlock.model.Project; -public interface Facet { +public interface Facet extends Jsonizable { public RowFilter getRowFilter(); public void computeChoices(Project project, FilteredRows filteredRows); - public JSONObject getJSON(Properties options) throws JSONException; - public void initializeFromJSON(JSONObject o) throws Exception; } diff --git a/src/main/java/com/metaweb/gridlock/browsing/facets/ListFacet.java b/src/main/java/com/metaweb/gridlock/browsing/facets/ListFacet.java index 02f0ced3a..28898a058 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/facets/ListFacet.java +++ b/src/main/java/com/metaweb/gridlock/browsing/facets/ListFacet.java @@ -1,6 +1,5 @@ package com.metaweb.gridlock.browsing.facets; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Properties; @@ -8,6 +7,7 @@ import java.util.Properties; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONWriter; import com.metaweb.gridlock.browsing.DecoratedValue; import com.metaweb.gridlock.browsing.FilteredRows; @@ -30,20 +30,20 @@ public class ListFacet implements Facet { } @Override - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + public void write(JSONWriter writer, Properties options) + throws JSONException { - o.put("name", _name); - o.put("expression", _expression); - o.put("cellIndex", _cellIndex); + writer.object(); + writer.key("name"); writer.value(_name); + writer.key("expression"); writer.value(_expression); + writer.key("cellIndex"); writer.value(_cellIndex); - List a = new ArrayList(_choices.size()); + writer.key("choices"); writer.array(); for (NominalFacetChoice choice : _choices) { - a.add(choice.getJSON(options)); + choice.write(writer, options); } - o.put("choices", a); - - return o; + writer.endArray(); + writer.endObject(); } @Override diff --git a/src/main/java/com/metaweb/gridlock/browsing/facets/NominalFacetChoice.java b/src/main/java/com/metaweb/gridlock/browsing/facets/NominalFacetChoice.java index f043b4a74..47a0fe092 100644 --- a/src/main/java/com/metaweb/gridlock/browsing/facets/NominalFacetChoice.java +++ b/src/main/java/com/metaweb/gridlock/browsing/facets/NominalFacetChoice.java @@ -3,11 +3,12 @@ package com.metaweb.gridlock.browsing.facets; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; import com.metaweb.gridlock.browsing.DecoratedValue; -public class NominalFacetChoice { +public class NominalFacetChoice implements Jsonizable { final public DecoratedValue decoratedValue; public int count; public boolean selected; @@ -16,13 +17,13 @@ public class NominalFacetChoice { this.decoratedValue = decoratedValue; } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - - o.put("v", decoratedValue.getJSON(options)); - o.put("c", count); - o.put("s", selected); - - return o; + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + writer.object(); + writer.key("v"); decoratedValue.write(writer, options); + writer.key("c"); writer.value(count); + writer.key("s"); writer.value(selected); + writer.endObject(); } } diff --git a/src/main/java/com/metaweb/gridlock/commands/Command.java b/src/main/java/com/metaweb/gridlock/commands/Command.java index 2d340537e..fad9b3fac 100644 --- a/src/main/java/com/metaweb/gridlock/commands/Command.java +++ b/src/main/java/com/metaweb/gridlock/commands/Command.java @@ -18,7 +18,9 @@ import org.apache.commons.lang.NotImplementedException; import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; import com.metaweb.gridlock.ProjectManager; import com.metaweb.gridlock.browsing.Engine; 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"); - respond(response, o.toString()); + + JSONWriter writer = new JSONWriter(response.getWriter()); + + o.write(writer, options); } protected void respondException(HttpServletResponse response, Exception e) throws IOException { @@ -89,7 +98,8 @@ public abstract class Command { o.put("stack", sw.toString()); - respondJSON(response, o); + response.setHeader("Content-Type", "application/json"); + respond(response, o.toString()); } catch (JSONException e1) { e.printStackTrace(response.getWriter()); } diff --git a/src/main/java/com/metaweb/gridlock/commands/ComputeFacetsCommand.java b/src/main/java/com/metaweb/gridlock/commands/ComputeFacetsCommand.java index 1b7da0382..a168ff082 100644 --- a/src/main/java/com/metaweb/gridlock/commands/ComputeFacetsCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/ComputeFacetsCommand.java @@ -1,7 +1,6 @@ package com.metaweb.gridlock.commands; import java.io.IOException; -import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -21,8 +20,7 @@ public class ComputeFacetsCommand extends Command { engine.computeFacets(); - Properties options = new Properties(); - respondJSON(response, engine.getJSON(options)); + respondJSON(response, engine); } catch (Exception e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/commands/GetColumnModelCommand.java b/src/main/java/com/metaweb/gridlock/commands/GetColumnModelCommand.java index 478af20e6..dee8f481a 100644 --- a/src/main/java/com/metaweb/gridlock/commands/GetColumnModelCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/GetColumnModelCommand.java @@ -1,7 +1,6 @@ package com.metaweb.gridlock.commands; import java.io.IOException; -import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -17,10 +16,9 @@ public class GetColumnModelCommand extends Command { throws ServletException, IOException { Project project = getProject(request); - Properties options = new Properties(); try { - respondJSON(response, project.columnModel.getJSON(options)); + respondJSON(response, project.columnModel); } catch (JSONException e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/commands/GetHistoryCommand.java b/src/main/java/com/metaweb/gridlock/commands/GetHistoryCommand.java index e6433bdf5..25aa2de29 100644 --- a/src/main/java/com/metaweb/gridlock/commands/GetHistoryCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/GetHistoryCommand.java @@ -1,7 +1,6 @@ package com.metaweb.gridlock.commands; import java.io.IOException; -import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -17,10 +16,8 @@ public class GetHistoryCommand extends Command { throws ServletException, IOException { Project project = getProject(request); - Properties options = new Properties(); - try { - respondJSON(response, project.history.getJSON(options)); + respondJSON(response, project.history); } catch (JSONException e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/commands/GetProcessesCommand.java b/src/main/java/com/metaweb/gridlock/commands/GetProcessesCommand.java index 8f22d0b03..b65101d58 100644 --- a/src/main/java/com/metaweb/gridlock/commands/GetProcessesCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/GetProcessesCommand.java @@ -1,7 +1,6 @@ package com.metaweb.gridlock.commands; import java.io.IOException; -import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -19,9 +18,7 @@ public class GetProcessesCommand extends Command { Project project = getProject(request); try { - Properties options = new Properties(); - - respondJSON(response, project.processManager.getJSON(options)); + respondJSON(response, project.processManager); } catch (JSONException e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/commands/GetProjectMetadataCommand.java b/src/main/java/com/metaweb/gridlock/commands/GetProjectMetadataCommand.java index e238e1816..dfb6c8333 100644 --- a/src/main/java/com/metaweb/gridlock/commands/GetProjectMetadataCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/GetProjectMetadataCommand.java @@ -19,7 +19,7 @@ public class GetProjectMetadataCommand extends Command { Project project = getProject(request); try { - respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id).getJSON()); + respondJSON(response, ProjectManager.singleton.getProjectMetadata(project.id)); } catch (JSONException e) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/commands/GetRowsCommand.java b/src/main/java/com/metaweb/gridlock/commands/GetRowsCommand.java index aa6b7a46e..2eb88fd20 100644 --- a/src/main/java/com/metaweb/gridlock/commands/GetRowsCommand.java +++ b/src/main/java/com/metaweb/gridlock/commands/GetRowsCommand.java @@ -1,8 +1,6 @@ package com.metaweb.gridlock.commands; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import java.util.Properties; import javax.servlet.ServletException; @@ -10,7 +8,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; import com.metaweb.gridlock.browsing.Engine; 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))); Properties options = new Properties(); - JSONObject o = new JSONObject(); + response.setHeader("Content-Type", "application/json"); + + JSONWriter writer = new JSONWriter(response.getWriter()); + writer.object(); - List a = new ArrayList(limit); - try { - FilteredRows filteredRows = engine.getAllFilteredRows(); + { RowAccumulator acc = new RowAccumulator(start, limit) { - List list; - Properties options; + JSONWriter writer; + Properties options; - public RowAccumulator init(List list, Properties options) { - this.list = list; + public RowAccumulator init(JSONWriter writer, Properties options) { + this.writer = writer; this.options = options; return this; } @@ -49,27 +48,28 @@ public class GetRowsCommand extends Command { @Override public boolean internalVisit(int rowIndex, Row row) { try { - JSONObject ro = row.getJSON(options); - ro.put("i", rowIndex); - list.add(ro); + options.put("rowIndex", rowIndex); + row.write(writer, options); } catch (JSONException e) { } return false; } - }.init(a, options); + }.init(writer, options); + FilteredRows filteredRows = engine.getAllFilteredRows(); + + writer.key("rows"); writer.array(); filteredRows.accept(project, acc); + writer.endArray(); - o.put("filtered", acc.total); - } catch (JSONException e) { - respondException(response, e); + writer.key("filtered"); writer.value(acc.total); } - 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) { respondException(response, e); } diff --git a/src/main/java/com/metaweb/gridlock/expr/VariableExpr.java b/src/main/java/com/metaweb/gridlock/expr/VariableExpr.java index 19fe55eef..d4b89125c 100644 --- a/src/main/java/com/metaweb/gridlock/expr/VariableExpr.java +++ b/src/main/java/com/metaweb/gridlock/expr/VariableExpr.java @@ -2,8 +2,6 @@ package com.metaweb.gridlock.expr; import java.util.Properties; -import org.json.JSONObject; - public class VariableExpr implements Evaluable { final protected String _name; diff --git a/src/main/java/com/metaweb/gridlock/history/History.java b/src/main/java/com/metaweb/gridlock/history/History.java index 0663f77c3..18b01b187 100644 --- a/src/main/java/com/metaweb/gridlock/history/History.java +++ b/src/main/java/com/metaweb/gridlock/history/History.java @@ -2,16 +2,18 @@ package com.metaweb.gridlock.history; import java.io.Serializable; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Properties; 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.model.Project; -public class History implements Serializable { +public class History implements Serializable, Jsonizable { private static final long serialVersionUID = -1529783362243627391L; protected long _projectID; @@ -31,7 +33,13 @@ public class History implements Serializable { _futureEntries.clear(); _pastEntries.add(entry); + entry.apply(ProjectManager.singleton.getProject(_projectID)); + setModified(); + } + + protected void setModified() { + ProjectManager.singleton.getProjectMetadata(_projectID).modified = new Date(); } public List getLastPastEntries(int count) { @@ -84,6 +92,7 @@ public class History implements Serializable { _futureEntries.add(0, entry); } + setModified(); } protected void redo(int times) { @@ -97,23 +106,27 @@ public class History implements Serializable { _pastEntries.add(entry); } + setModified(); } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { - List a = new ArrayList(_pastEntries.size()); + writer.object(); + + writer.key("past"); writer.array(); for (HistoryEntry entry : _pastEntries) { - a.add(entry.getJSON(options)); + entry.write(writer, options); } - o.put("past", a); + writer.endArray(); - List b = new ArrayList(_futureEntries.size()); + writer.key("future"); writer.array(); for (HistoryEntry entry : _futureEntries) { - b.add(entry.getJSON(options)); + entry.write(writer, options); } - o.put("future", b); + writer.endArray(); - return o; + writer.endObject(); } } diff --git a/src/main/java/com/metaweb/gridlock/history/HistoryEntry.java b/src/main/java/com/metaweb/gridlock/history/HistoryEntry.java index 1b312546b..b86bd4002 100644 --- a/src/main/java/com/metaweb/gridlock/history/HistoryEntry.java +++ b/src/main/java/com/metaweb/gridlock/history/HistoryEntry.java @@ -12,12 +12,13 @@ import java.util.Date; import java.util.Properties; 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.model.Project; -public class HistoryEntry implements Serializable { +public class HistoryEntry implements Serializable, Jsonizable { private static final long serialVersionUID = 532766467813930262L; public long id; @@ -38,16 +39,17 @@ public class HistoryEntry implements Serializable { saveChange(); } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(); - o.put("id", id); - o.put("description", description); - o.put("time", sdf.format(time)); - - return o; + writer.object(); + writer.key("id"); writer.value(id); + writer.key("description"); writer.value(description); + writer.key("time"); writer.value(sdf.format(time)); + writer.endObject(); } public void apply(Project project) { diff --git a/src/main/java/com/metaweb/gridlock/history/HistoryProcess.java b/src/main/java/com/metaweb/gridlock/history/HistoryProcess.java index b8d7e5846..65980188a 100644 --- a/src/main/java/com/metaweb/gridlock/history/HistoryProcess.java +++ b/src/main/java/com/metaweb/gridlock/history/HistoryProcess.java @@ -3,7 +3,7 @@ package com.metaweb.gridlock.history; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; import com.metaweb.gridlock.model.Project; import com.metaweb.gridlock.process.Process; @@ -49,14 +49,14 @@ public class HistoryProcess extends Process { } @Override - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + public void write(JSONWriter writer, Properties options) + throws JSONException { - o.put("description", _description); - o.put("immediate", true); - o.put("status", _done ? "done" : "pending"); - - return o; + writer.object(); + writer.key("description"); writer.value(_description); + writer.key("immediate"); writer.value(true); + writer.key("status"); writer.value(_done ? "done" : "pending"); + writer.endObject(); } @Override diff --git a/src/main/java/com/metaweb/gridlock/history/MassCellChange.java b/src/main/java/com/metaweb/gridlock/history/MassCellChange.java index 0fada7c87..e57dd2291 100644 --- a/src/main/java/com/metaweb/gridlock/history/MassCellChange.java +++ b/src/main/java/com/metaweb/gridlock/history/MassCellChange.java @@ -1,6 +1,5 @@ package com.metaweb.gridlock.history; -import java.util.ArrayList; import java.util.List; import com.metaweb.gridlock.model.Project; diff --git a/src/main/java/com/metaweb/gridlock/model/Cell.java b/src/main/java/com/metaweb/gridlock/model/Cell.java index 6e0d41d4f..7947b927a 100644 --- a/src/main/java/com/metaweb/gridlock/model/Cell.java +++ b/src/main/java/com/metaweb/gridlock/model/Cell.java @@ -4,27 +4,17 @@ import java.io.Serializable; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; 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; public Object value; 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 public Object getField(String name, Properties bindings) { if ("value".equals(name)) { @@ -34,4 +24,17 @@ public class Cell implements Serializable, HasFields { } 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(); + } } diff --git a/src/main/java/com/metaweb/gridlock/model/Column.java b/src/main/java/com/metaweb/gridlock/model/Column.java index e221837ba..d933d0d33 100644 --- a/src/main/java/com/metaweb/gridlock/model/Column.java +++ b/src/main/java/com/metaweb/gridlock/model/Column.java @@ -4,22 +4,25 @@ import java.io.Serializable; import java.util.Properties; 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; public int cellIndex; public String headerLabel; public Class valueType; - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { - o.put("cellIndex", cellIndex); - o.put("headerLabel", headerLabel); - o.put("valueType", valueType == null ? null : valueType.getSimpleName()); - - return o; + writer.object(); + writer.key("cellIndex"); writer.value(cellIndex); + writer.key("headerLabel"); writer.value(headerLabel); + writer.key("valueType"); writer.value(valueType == null ? null : valueType.getSimpleName()); + writer.endObject(); } } diff --git a/src/main/java/com/metaweb/gridlock/model/ColumnModel.java b/src/main/java/com/metaweb/gridlock/model/ColumnModel.java index 6f2bfdae8..3eb897a53 100644 --- a/src/main/java/com/metaweb/gridlock/model/ColumnModel.java +++ b/src/main/java/com/metaweb/gridlock/model/ColumnModel.java @@ -1,34 +1,23 @@ package com.metaweb.gridlock.model; import java.io.Serializable; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Properties; 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; public List columns = new LinkedList(); transient protected Map _nameToColumn; - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - - List a = new ArrayList(columns.size()); - for (Column column : columns) { - a.add(column.getJSON(options)); - } - o.put("columns", a); - - return o; - } - public Column getColumnByName(String name) { if (_nameToColumn == null) { for (Column column : columns) { @@ -46,4 +35,18 @@ public class ColumnModel implements Serializable { } 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(); + } } diff --git a/src/main/java/com/metaweb/gridlock/model/Recon.java b/src/main/java/com/metaweb/gridlock/model/Recon.java index 23d26b938..54fb88128 100644 --- a/src/main/java/com/metaweb/gridlock/model/Recon.java +++ b/src/main/java/com/metaweb/gridlock/model/Recon.java @@ -8,11 +8,12 @@ import java.util.Map; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; 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; static public enum Judgment { @@ -26,13 +27,6 @@ public class Recon implements Serializable, HasFields { public Judgment judgment = Judgment.None; public ReconCandidate match = null; - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - o.put("j", judgmentToString()); - - return o; - } - @Override public Object getField(String name, Properties bindings) { if ("best".equals(name)) { @@ -65,4 +59,14 @@ public class Recon implements Serializable, HasFields { return features.get(name); } } + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("j"); + writer.value(judgmentToString()); + writer.endObject(); + } } diff --git a/src/main/java/com/metaweb/gridlock/model/ReconCandidate.java b/src/main/java/com/metaweb/gridlock/model/ReconCandidate.java index fd44795a9..137b6f35c 100644 --- a/src/main/java/com/metaweb/gridlock/model/ReconCandidate.java +++ b/src/main/java/com/metaweb/gridlock/model/ReconCandidate.java @@ -4,11 +4,12 @@ import java.io.Serializable; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; 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; public String topicID; @@ -17,18 +18,6 @@ public class ReconCandidate implements Serializable, HasFields { public String[] typeIDs; 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 public Object getField(String name, Properties bindings) { if ("id".equals(name)) { @@ -44,4 +33,23 @@ public class ReconCandidate implements Serializable, HasFields { } 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(); + } } diff --git a/src/main/java/com/metaweb/gridlock/model/Row.java b/src/main/java/com/metaweb/gridlock/model/Row.java index 9dea2f0b1..e71dc5b39 100644 --- a/src/main/java/com/metaweb/gridlock/model/Row.java +++ b/src/main/java/com/metaweb/gridlock/model/Row.java @@ -6,11 +6,12 @@ import java.util.List; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; +import com.metaweb.gridlock.Jsonizable; 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; public boolean flagged; @@ -21,20 +22,6 @@ public class Row implements Serializable, HasFields { cells = new ArrayList(cellCount); } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); - - List a = new ArrayList(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 public Object getField(String name, Properties bindings) { 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(); + } } diff --git a/src/main/java/com/metaweb/gridlock/process/LongRunningProcess.java b/src/main/java/com/metaweb/gridlock/process/LongRunningProcess.java index c2fccb375..924286cfe 100644 --- a/src/main/java/com/metaweb/gridlock/process/LongRunningProcess.java +++ b/src/main/java/com/metaweb/gridlock/process/LongRunningProcess.java @@ -3,7 +3,7 @@ package com.metaweb.gridlock.process; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; abstract public class LongRunningProcess extends Process { final protected String _description; @@ -23,18 +23,17 @@ abstract public class LongRunningProcess extends Process { _thread.interrupt(); } } - + @Override - public - JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + public void write(JSONWriter writer, Properties options) + throws JSONException { - o.put("description", _description); - o.put("immediate", false); - o.put("status", _thread == null ? "pending" : (_thread.isAlive() ? "running" : "done")); - o.put("progress", _progress); - - return o; + writer.object(); + writer.key("description"); writer.value(_description); + writer.key("immediate"); writer.value(false); + writer.key("status"); writer.value(_thread == null ? "pending" : (_thread.isAlive() ? "running" : "done")); + writer.key("progress"); writer.value(_progress); + writer.endObject(); } @Override diff --git a/src/main/java/com/metaweb/gridlock/process/Process.java b/src/main/java/com/metaweb/gridlock/process/Process.java index db118baf6..b8eacc215 100644 --- a/src/main/java/com/metaweb/gridlock/process/Process.java +++ b/src/main/java/com/metaweb/gridlock/process/Process.java @@ -1,11 +1,8 @@ package com.metaweb.gridlock.process; -import java.util.Properties; +import com.metaweb.gridlock.Jsonizable; -import org.json.JSONException; -import org.json.JSONObject; - -public abstract class Process { +public abstract class Process implements Jsonizable { abstract public boolean isImmediate(); abstract public boolean isRunning(); @@ -15,6 +12,4 @@ public abstract class Process { abstract public void startPerforming(ProcessManager manager); abstract public void cancel(); - - public abstract JSONObject getJSON(Properties options) throws JSONException; } diff --git a/src/main/java/com/metaweb/gridlock/process/ProcessManager.java b/src/main/java/com/metaweb/gridlock/process/ProcessManager.java index 2de629d2f..fdab62859 100644 --- a/src/main/java/com/metaweb/gridlock/process/ProcessManager.java +++ b/src/main/java/com/metaweb/gridlock/process/ProcessManager.java @@ -1,32 +1,36 @@ package com.metaweb.gridlock.process; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Properties; 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 _processes = new LinkedList(); public ProcessManager() { } - public JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + + @Override + public void write(JSONWriter writer, Properties options) + throws JSONException { - List a = new ArrayList(_processes.size()); + writer.object(); + writer.key("processes"); writer.array(); 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) { if (process.isImmediate() && _processes.size() == 0) { process.performImmediate(); diff --git a/src/main/java/com/metaweb/gridlock/process/QuickHistoryEntryProcess.java b/src/main/java/com/metaweb/gridlock/process/QuickHistoryEntryProcess.java index f496e8353..3bc83424c 100644 --- a/src/main/java/com/metaweb/gridlock/process/QuickHistoryEntryProcess.java +++ b/src/main/java/com/metaweb/gridlock/process/QuickHistoryEntryProcess.java @@ -3,7 +3,7 @@ package com.metaweb.gridlock.process; import java.util.Properties; import org.json.JSONException; -import org.json.JSONObject; +import org.json.JSONWriter; import com.metaweb.gridlock.history.HistoryEntry; import com.metaweb.gridlock.model.Project; @@ -45,17 +45,17 @@ public class QuickHistoryEntryProcess extends Process { } @Override - public - JSONObject getJSON(Properties options) throws JSONException { - JSONObject o = new JSONObject(); + public void write(JSONWriter writer, Properties options) + throws JSONException { - o.put("description", _historyEntry.description); - o.put("immediate", true); - o.put("status", _done ? "done" : "pending"); - - return o; + writer.object(); + writer.key("description"); writer.value(_historyEntry.description); + writer.key("immediate"); writer.value(true); + writer.key("status"); writer.value(_done ? "done" : "pending"); + writer.endObject(); } + @Override public boolean isDone() { return _done; diff --git a/src/main/webapp/styles/history.css b/src/main/webapp/styles/history.css index 48671dc99..7d24054ce 100644 --- a/src/main/webapp/styles/history.css +++ b/src/main/webapp/styles/history.css @@ -25,7 +25,7 @@ .history-past { padding-bottom: 3px; - border-bottom: 2px solid #aaa; + border-bottom: 2px solid #f88; } .history-future { padding-top: 3px; @@ -33,9 +33,12 @@ a.history-entry { display: block; padding: 3px 5px; - border-bottom: 1px solid #eee; text-decoration: none; color: black; + border-top: 1px solid #eee; +} +a.history-entry:first-child { + border-top: none; } a.history-entry:hover { background: #eee;