return c;
}
private Worksheet groupByTopLevel(Worksheet oldws, Workspace workspace, List<String> hnodeIDs, List<HNode> keyhnodes, List<HNode> valuehnodes, RepFactory factory) {
SuperSelection selection = getSuperSelection(oldws);
Worksheet newws = factory.createWorksheet("GroupBy: " + oldws.getTitle(), workspace, oldws.getEncoding());
newws.getMetadataContainer().getWorksheetProperties().setPropertyValue(Property.sourceType, oldws.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.sourceType));
HTable newht = newws.getHeaders();
ArrayList<Row> rows = oldws.getDataTable().getRows(0, oldws.getDataTable().getNumRows(), selection);
HTable oldht = oldws.getHeaders();
Map<String, ArrayList<String>> hash = new TreeMap<String, ArrayList<String>>();
for (Row row : rows) {
String hashValue = HashValueManager.getHashValue(row, hnodeIDs);
ArrayList<String> ids = hash.get(hashValue);
if (ids == null)
ids = new ArrayList<String>();
ids.add(row.getId());
hash.put(hashValue, ids);
//System.out.println("Hash: " + HashValueManager.getHashValue(row, hnodeIDs));
}
//HTable newKeyTable = newht.getHNodeFromColumnName("Keys").addNestedTable("Table for keys", newws, factory);
//newValueTable.addHNode("Values", newws, factory);
//HTable newValueNestedTable = newValueTable.getHNodeFromColumnName("Values").addNestedTable("Table for nested values", newws, factory);
CloneTableUtils.cloneHTable(oldht, newht, newws, factory, keyhnodes, selection);
newht.addHNode("Values", HNodeType.Transformation, newws, factory);
HTable newValueTable = newht.getHNodeFromColumnName("Values").addNestedTable("Table for values", newws, factory);
CloneTableUtils.cloneHTable(oldht, newValueTable, newws, factory, valuehnodes, selection);
for (String key : hash.keySet()) {
//System.out.println("key: " + hash.get(key));
ArrayList<String> r = hash.get(key);
Row lastRow = CloneTableUtils.cloneDataTable(factory.getRow(r.get(0)), newws.getDataTable(), oldws.getHeaders(), newht, keyhnodes, factory, selection);
for (String rowid : r) {
Row cur = factory.getRow(rowid);
Table dataTable = lastRow.getNeighborByColumnName("Values", factory).getNestedTable();
CloneTableUtils.cloneDataTable(cur, dataTable, oldws.getHeaders(), newValueTable, valuehnodes, factory, selection);
}
}
newWorksheetId = newws.getId();
return newws;
}