diff --git a/src/main/java/com/metaweb/gridworks/GridworksServlet.java b/src/main/java/com/metaweb/gridworks/GridworksServlet.java index ebb73adb1..b95000d3a 100644 --- a/src/main/java/com/metaweb/gridworks/GridworksServlet.java +++ b/src/main/java/com/metaweb/gridworks/GridworksServlet.java @@ -21,6 +21,7 @@ import com.metaweb.gridworks.commands.edit.DeleteProjectCommand; import com.metaweb.gridworks.commands.edit.ExportProjectCommand; import com.metaweb.gridworks.commands.edit.ExtendDataCommand; import com.metaweb.gridworks.commands.edit.ImportProjectCommand; +import com.metaweb.gridworks.commands.edit.RenameColumnCommand; import com.metaweb.gridworks.commands.edit.TextTransformCommand; import com.metaweb.gridworks.commands.edit.EditOneCellCommand; import com.metaweb.gridworks.commands.edit.MassEditCommand; @@ -96,6 +97,7 @@ public class GridworksServlet extends HttpServlet { _commands.put("add-column", new AddColumnCommand()); _commands.put("remove-column", new RemoveColumnCommand()); + _commands.put("rename-column", new RenameColumnCommand()); _commands.put("extend-data", new ExtendDataCommand()); _commands.put("reconcile", new ReconcileCommand()); diff --git a/src/main/java/com/metaweb/gridworks/commands/edit/RenameColumnCommand.java b/src/main/java/com/metaweb/gridworks/commands/edit/RenameColumnCommand.java new file mode 100644 index 000000000..c2e0e8ce0 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/commands/edit/RenameColumnCommand.java @@ -0,0 +1,35 @@ +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.ColumnRenameOperation; +import com.metaweb.gridworks.process.Process; + +public class RenameColumnCommand extends Command { + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + + try { + Project project = getProject(request); + + String oldColumnName = request.getParameter("oldColumnName"); + String newColumnName = request.getParameter("newColumnName"); + + AbstractOperation op = new ColumnRenameOperation(oldColumnName, newColumnName); + 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/model/changes/ColumnRenameChange.java b/src/main/java/com/metaweb/gridworks/model/changes/ColumnRenameChange.java new file mode 100644 index 000000000..ec2c7d786 --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/model/changes/ColumnRenameChange.java @@ -0,0 +1,61 @@ +package com.metaweb.gridworks.model.changes; + +import java.io.IOException; +import java.io.LineNumberReader; +import java.io.Writer; +import java.util.Properties; + +import com.metaweb.gridworks.history.Change; +import com.metaweb.gridworks.model.Project; + +public class ColumnRenameChange extends ColumnChange { + final protected String _oldColumnName; + final protected String _newColumnName; + + public ColumnRenameChange(String oldColumnName, String newColumnName) { + _oldColumnName = oldColumnName; + _newColumnName = newColumnName; + } + + public void apply(Project project) { + synchronized (project) { + project.columnModel.getColumnByName(_oldColumnName).setName(_newColumnName); + project.columnModel.update(); + } + } + + public void revert(Project project) { + synchronized (project) { + project.columnModel.getColumnByName(_newColumnName).setName(_oldColumnName); + project.columnModel.update(); + } + } + + public void save(Writer writer, Properties options) throws IOException { + writer.write("oldColumnName="); writer.write(_oldColumnName); writer.write('\n'); + writer.write("newColumnName="); writer.write(_newColumnName); writer.write('\n'); + writer.write("/ec/\n"); // end of change marker + } + + static public Change load(LineNumberReader reader) throws Exception { + String oldColumnName = null; + String newColumnName = null; + + String line; + while ((line = reader.readLine()) != null && !"/ec/".equals(line)) { + int equal = line.indexOf('='); + CharSequence field = line.subSequence(0, equal); + String value = line.substring(equal + 1); + + if ("oldColumnName".equals(field)) { + oldColumnName = value; + } else if ("newColumnName".equals(field)) { + newColumnName = value; + } + } + + ColumnRenameChange change = new ColumnRenameChange(oldColumnName, newColumnName); + + return change; + } +} diff --git a/src/main/java/com/metaweb/gridworks/operations/ColumnRenameOperation.java b/src/main/java/com/metaweb/gridworks/operations/ColumnRenameOperation.java new file mode 100644 index 000000000..eafc531cb --- /dev/null +++ b/src/main/java/com/metaweb/gridworks/operations/ColumnRenameOperation.java @@ -0,0 +1,62 @@ +package com.metaweb.gridworks.operations; + +import java.util.Properties; + +import org.json.JSONException; +import org.json.JSONObject; +import org.json.JSONWriter; + +import com.metaweb.gridworks.history.Change; +import com.metaweb.gridworks.history.HistoryEntry; +import com.metaweb.gridworks.model.AbstractOperation; +import com.metaweb.gridworks.model.Project; +import com.metaweb.gridworks.model.changes.ColumnRenameChange; + +public class ColumnRenameOperation extends AbstractOperation { + final protected String _oldColumnName; + final protected String _newColumnName; + + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { + return new ColumnRenameOperation( + obj.getString("oldColumnName"), + obj.getString("newColumnName") + ); + } + + public ColumnRenameOperation( + String oldColumnName, + String newColumnName + ) { + _oldColumnName = oldColumnName; + _newColumnName = newColumnName; + } + + 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("Rename column " + _oldColumnName + " to " + _newColumnName); + writer.key("oldColumnName"); writer.value(_oldColumnName); + writer.key("newColumnName"); writer.value(_newColumnName); + writer.endObject(); + } + + + protected String getBriefDescription(Project project) { + return "Rename column " + _oldColumnName + " to " + _newColumnName; + } + + protected HistoryEntry createHistoryEntry(Project project) throws Exception { + if (project.columnModel.getColumnByName(_oldColumnName) == null) { + throw new Exception("No column named " + _oldColumnName); + } + if (project.columnModel.getColumnByName(_newColumnName) != null) { + throw new Exception("Another column already named " + _newColumnName); + } + + Change change = new ColumnRenameChange(_oldColumnName, _newColumnName); + + return new HistoryEntry(project, getBriefDescription(null), ColumnRenameOperation.this, change); + } +} diff --git a/src/main/webapp/scripts/views/data-table-column-header-ui.js b/src/main/webapp/scripts/views/data-table-column-header-ui.js index 91246deac..a252cd537 100644 --- a/src/main/webapp/scripts/views/data-table-column-header-ui.js +++ b/src/main/webapp/scripts/views/data-table-column-header-ui.js @@ -312,10 +312,14 @@ DataTableColumnHeaderUI.prototype._createMenuForColumnHeader = function(elmt) { click: function() { self._doAddColumnFromFreebase(); } }, {}, + { + label: "Rename This Column", + click: function() { self._doRenameColumn(); } + }, { label: "Remove This Column", click: function() { self._doRemoveColumn(); } - }, + } ] }, {}, @@ -889,6 +893,21 @@ DataTableColumnHeaderUI.prototype._doRemoveColumn = function() { ); }; +DataTableColumnHeaderUI.prototype._doRenameColumn = function() { + var newColumnName = window.prompt("Enter new column name", this._column.name); + if (newColumnName != null) { + Gridworks.postProcess( + "rename-column", + { + oldColumnName: this._column.name, + newColumnName: newColumnName + }, + null, + { modelsChanged: true } + ); + } +}; + DataTableColumnHeaderUI.prototype._doJoinMultiValueCells = function() { var separator = window.prompt("Enter separator to use between values", ", "); if (separator != null) {