From 3f40195ea1fa23381ba8cc250f3f85d990b80e11 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Thu, 29 Apr 2010 22:07:07 +0000 Subject: [PATCH] Implemented but disabled the denormalize operation. git-svn-id: http://google-refine.googlecode.com/svn/trunk@571 7d457c2a-affb-35e4-300a-418c747d4874 --- .../metaweb/gridworks/GridworksServlet.java | 3 + .../commands/edit/DenormalizeCommand.java | 32 ++++++++ .../operations/DenormalizeOperation.java | 75 +++++++++++++++++++ .../operations/OperationRegistry.java | 2 + src/main/webapp/scripts/project/menu-bar.js | 21 ++++++ 5 files changed, 133 insertions(+) create mode 100644 src/main/java/com/metaweb/gridworks/commands/edit/DenormalizeCommand.java create mode 100644 src/main/java/com/metaweb/gridworks/operations/DenormalizeOperation.java diff --git a/src/main/java/com/metaweb/gridworks/GridworksServlet.java b/src/main/java/com/metaweb/gridworks/GridworksServlet.java index ef0923dd8..feb387922 100644 --- a/src/main/java/com/metaweb/gridworks/GridworksServlet.java +++ b/src/main/java/com/metaweb/gridworks/GridworksServlet.java @@ -25,6 +25,7 @@ import com.metaweb.gridworks.commands.edit.AnnotateRowsCommand; import com.metaweb.gridworks.commands.edit.ApplyOperationsCommand; import com.metaweb.gridworks.commands.edit.CreateProjectCommand; import com.metaweb.gridworks.commands.edit.DeleteProjectCommand; +import com.metaweb.gridworks.commands.edit.DenormalizeCommand; import com.metaweb.gridworks.commands.edit.EditOneCellCommand; import com.metaweb.gridworks.commands.edit.ExportProjectCommand; import com.metaweb.gridworks.commands.edit.ExtendDataCommand; @@ -119,6 +120,8 @@ public class GridworksServlet extends HttpServlet { _commands.put("split-column", new SplitColumnCommand()); _commands.put("extend-data", new ExtendDataCommand()); + _commands.put("denormalize", new DenormalizeCommand()); + _commands.put("reconcile", new ReconcileCommand()); _commands.put("recon-match-best-candidates", new ReconMatchBestCandidatesCommand()); _commands.put("recon-mark-new-topics", new ReconMarkNewTopicsCommand()); diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/DenormalizeCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/DenormalizeCommand.java new file mode 100644 index 000000000..e96961ae5 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/commands/edit/DenormalizeCommand.java @@ -0,0 +1,32 @@ +package com.metaweb.gridworks.commands.edit; + +import java.io.IOException; +import java.util.Properties; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.metaweb.gridworks.commands.Command; +import com.metaweb.gridworks.model.AbstractOperation; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.operations.DenormalizeOperation; +import com.metaweb.gridworks.process.Process; + +public class DenormalizeCommand extends Command { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + AbstractOperation op = new DenormalizeOperation(); + Process process = op.createProcess(project, new Properties()); + + performProcessAndRespond(request, response, project, process); + } catch (Exception e) { + respondException(response, e); + } + } +} diff --git a/src/main/java/com/metaweb/gridworks/operations/DenormalizeOperation.java b/src/main/java/com/metaweb/gridworks/operations/DenormalizeOperation.java new file mode 100644 index 000000000..a930468b8 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/operations/DenormalizeOperation.java @@ -0,0 +1,75 @@ +package com.metaweb.gridworks.operations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.metaweb.gridworks.history.HistoryEntry; +import com.metaweb.gridworks.model.AbstractOperation; +import com.metaweb.gridworks.model.Cell; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.Row; +import com.metaweb.gridworks.model.changes.MassRowChange; + +public class DenormalizeOperation extends AbstractOperation { + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { + return new DenormalizeOperation(); + } + + public DenormalizeOperation() { + } + + public void write(JSONWriter writer, Properties options) + throws JSONException { + + writer.object(); + writer.key("op"); writer.value(OperationRegistry.s_opClassToName.get(this.getClass())); + writer.key("description"); writer.value("Denormalize"); + writer.endObject(); + } + + + protected String getBriefDescription(Project project) { + return "Denormalize"; + } + + protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception { + List newRows = new ArrayList(); + + List oldRows = project.rows; + for (int r = 0; r < oldRows.size(); r++) { + Row oldRow = oldRows.get(r); + Row newRow = null; + + if (oldRow.contextCellSlots != null && oldRow.contextRowSlots != null) { + newRow = oldRow.dup(); + + for (int c = 0; c < oldRow.contextCellSlots.length && c < oldRow.contextRowSlots.length; c++) { + int contextRowIndex = oldRow.contextRowSlots[c]; + int contextCellIndex = oldRow.contextCellSlots[c]; + + if (contextRowIndex >= 0 && contextRowIndex < oldRows.size()) { + Row contextRow = oldRows.get(contextRowIndex); + Cell contextCell = contextRow.getCell(contextCellIndex); + + newRow.setCell(contextCellIndex, contextCell); + } + } + } + + newRows.add(newRow != null ? newRow : oldRow); + } + + return new HistoryEntry( + historyEntryID, + project, + getBriefDescription(project), + DenormalizeOperation.this, + new MassRowChange(newRows) + ); + } +} diff --git a/src/main/java/com/metaweb/gridworks/operations/OperationRegistry.java b/src/main/java/com/metaweb/gridworks/operations/OperationRegistry.java index 90d53cff7..57407e3db 100644 --- a/src/main/java/com/metaweb/gridworks/operations/OperationRegistry.java +++ b/src/main/java/com/metaweb/gridworks/operations/OperationRegistry.java @@ -43,6 +43,8 @@ public abstract class OperationRegistry { register("save-protograph", SaveProtographOperation.class); register("text-transform", TextTransformOperation.class); register("mass-edit", MassEditOperation.class); + + register("denormalize", DenormalizeOperation.class); } static public AbstractOperation reconstruct(Project project, JSONObject obj) { diff --git a/src/main/webapp/scripts/project/menu-bar.js b/src/main/webapp/scripts/project/menu-bar.js index 0c7bdf0c9..f92cd358f 100644 --- a/src/main/webapp/scripts/project/menu-bar.js +++ b/src/main/webapp/scripts/project/menu-bar.js @@ -16,6 +16,18 @@ MenuBar.prototype._initializeUI = function() { var self = this; this._createTopLevelMenuItem("Project", [ + /* + { + "label": "Data Model", + "submenu": [ + { + "label": "Denormalize Records", + "click": function() { self._doDenormalizeRecords(); } + } + ] + }, + {}, + */ { "label": "Export Filtered Rows", "submenu": [ @@ -148,6 +160,15 @@ MenuBar.prototype._deactivateMenu = function() { this._mode = "inactive"; }; +MenuBar.prototype._doDenormalizeRecords = function() { + Gridworks.postProcess( + "denormalize", + {}, + null, + { modelsChanged: true } + ); +}; + MenuBar.prototype._doExportTripleloader = function() { if (!theProject.protograph) { alert(