Started to add the key columns and column groups into the data model.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@32 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-02-03 20:55:48 +00:00
parent fb34d6f507
commit 97fd9422f6
6 changed files with 170 additions and 13 deletions

BIN
graphics/row-groups.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@ -76,10 +76,14 @@ public class CreateProjectFromUploadCommand extends Command {
if ((sep.charAt(0) == ',') ? parseCSVIntoRow(row, line) : parseTSVIntoRow(row, line)) {
project.rows.add(row);
project.columnModel.maxCellIndex =
Math.max(project.columnModel.maxCellIndex, row.cells.size());
}
}
}
project.columnModel.update();
redirect(response, "/project.html?project=" + project.id);
}

View File

@ -0,0 +1,75 @@
package com.metaweb.gridworks.model;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.metaweb.gridworks.Jsonizable;
public class ColumnGroup implements Serializable, Jsonizable {
private static final long serialVersionUID = 2161780779920066118L;
public int[] cellIndices; // must be in order from smallest to largest
public int keyCellIndex; // could be -1 if there is no key cell
transient public ColumnGroup parentGroup;
transient public List<ColumnGroup> subgroups;
public ColumnGroup() {
internalInitialize();
}
@Override
public void write(JSONWriter writer, Properties options)
throws JSONException {
writer.object();
writer.key("cellIndices"); writer.array();
for (int i : cellIndices) {
writer.value(i);
}
writer.endArray();
if (subgroups != null && subgroups.size() > 0) {
writer.key("subgroups"); writer.array();
for (ColumnGroup g : subgroups) {
g.write(writer, options);
}
writer.endArray();
}
writer.endObject();
}
public boolean contains(ColumnGroup g) {
for (int c : g.cellIndices) {
boolean has = false;
for (int d : cellIndices) {
if (c == d) {
has = true;
break;
}
}
if (!has) {
return false;
}
}
return true;
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
internalInitialize();
}
protected void internalInitialize() {
subgroups = new LinkedList<ColumnGroup>();
}
}

View File

@ -1,6 +1,11 @@
package com.metaweb.gridworks.model;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@ -14,26 +19,30 @@ import com.metaweb.gridworks.Jsonizable;
public class ColumnModel implements Serializable, Jsonizable {
private static final long serialVersionUID = 7679639795211544511L;
public List<Column> columns = new LinkedList<Column>();
public List<Column> columns = new LinkedList<Column>();
public int maxCellIndex;
transient protected Map<String, Column> _nameToColumn;
public List<ColumnGroup> columnGroups = new LinkedList<ColumnGroup>();
public int keyCellIndex;
transient protected Map<String, Column> _nameToColumn;
transient protected Map<Integer, Column> _cellIndexToColumn;
transient protected List<ColumnGroup> _rootColumnGroups;
public ColumnModel() {
internalInitialize();
}
public void update() {
generateMaps();
}
public Column getColumnByName(String name) {
if (_nameToColumn == null) {
for (Column column : columns) {
_nameToColumn.put(column.headerLabel, column);
}
}
return _nameToColumn.get(name);
}
public Column getColumnByCellIndex(int cellIndex) {
for (Column column : columns) {
if (column.cellIndex == cellIndex) {
return column;
}
}
return null;
return _cellIndexToColumn.get(cellIndex);
}
@Override
@ -41,12 +50,72 @@ public class ColumnModel implements Serializable, Jsonizable {
throws JSONException {
writer.object();
writer.key("columns");
writer.array();
for (Column column : columns) {
column.write(writer, options);
}
writer.endArray();
writer.key("keyCellIndex"); writer.value(keyCellIndex);
writer.key("columnGroups");
writer.array();
for (ColumnGroup g : _rootColumnGroups) {
g.write(writer, options);
}
writer.endArray();
writer.endObject();
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
internalInitialize();
}
protected void internalInitialize() {
generateMaps();
// Turn the flat list of column groups into a tree
_rootColumnGroups = new LinkedList<ColumnGroup>(columnGroups);
Collections.sort(_rootColumnGroups, new Comparator<ColumnGroup>() {
@Override
public int compare(ColumnGroup o1, ColumnGroup o2) {
int firstDiff = o1.cellIndices[0] - o2.cellIndices[0];
return firstDiff != 0 ?
firstDiff : // whichever group that starts first goes first
(o2.cellIndices.length - o1.cellIndices.length); // otherwise, the larger group goes first
}
});
for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) {
ColumnGroup g = _rootColumnGroups.get(i);
for (int j = i + 1; j < _rootColumnGroups.size(); j++) {
ColumnGroup g2 = _rootColumnGroups.get(j);
if (g2.parentGroup == null && g.contains(g2)) {
g2.parentGroup = g;
g.subgroups.add(g2);
}
}
}
for (int i = _rootColumnGroups.size() - 1; i >= 0; i--) {
if (_rootColumnGroups.get(i).parentGroup != null) {
_rootColumnGroups.remove(i);
}
}
}
protected void generateMaps() {
_nameToColumn = new HashMap<String, Column>();
_cellIndexToColumn = new HashMap<Integer, Column>();
for (Column column : columns) {
_nameToColumn.put(column.headerLabel, column);
_cellIndexToColumn.put(column.cellIndex, column);
}
}
}

View File

@ -34,5 +34,11 @@ public class Project implements Serializable {
protected void internalInitialize() {
processManager = new ProcessManager();
computeContext();
}
protected void computeContext() {
// TODO
}
}

View File

@ -18,6 +18,9 @@ public class Row implements Serializable, HasFields, Jsonizable {
public boolean starred;
public List<Cell> cells;
transient public List<Integer> contextRows;
transient public List<Integer> contextCells;
public Row(int cellCount) {
cells = new ArrayList<Cell>(cellCount);
}