use of com.google.refine.model.changes.MassRowChange in project OpenRefine by OpenRefine.
the class DenormalizeOperation method createHistoryEntry.
@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
List<Row> newRows = new ArrayList<Row>();
List<Row> oldRows = project.rows;
for (int r = 0; r < oldRows.size(); r++) {
Row oldRow = oldRows.get(r);
Row newRow = null;
RowDependency rd = project.recordModel.getRowDependency(r);
if (rd.cellDependencies != null) {
newRow = oldRow.dup();
for (CellDependency cd : rd.cellDependencies) {
if (cd != null) {
int contextRowIndex = cd.rowIndex;
int contextCellIndex = cd.cellIndex;
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));
}
use of com.google.refine.model.changes.MassRowChange in project OpenRefine by OpenRefine.
the class MultiValuedCellJoinOperation method createHistoryEntry.
@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
Column column = project.columnModel.getColumnByName(_columnName);
if (column == null) {
throw new Exception("No column named " + _columnName);
}
int cellIndex = column.getCellIndex();
Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
if (keyColumn == null) {
throw new Exception("No key column named " + _keyColumnName);
}
int keyCellIndex = keyColumn.getCellIndex();
List<Row> newRows = new ArrayList<Row>();
int oldRowCount = project.rows.size();
for (int r = 0; r < oldRowCount; r++) {
Row oldRow = project.rows.get(r);
if (oldRow.isCellBlank(keyCellIndex)) {
newRows.add(oldRow.dup());
continue;
}
int r2 = r + 1;
while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) {
r2++;
}
if (r2 == r + 1) {
newRows.add(oldRow.dup());
continue;
}
StringBuffer sb = new StringBuffer();
for (int r3 = r; r3 < r2; r3++) {
Object value = project.rows.get(r3).getCellValue(cellIndex);
if (ExpressionUtils.isNonBlankData(value)) {
if (sb.length() > 0) {
sb.append(_separator);
}
sb.append(value.toString());
}
}
for (int r3 = r; r3 < r2; r3++) {
Row newRow = project.rows.get(r3).dup();
if (r3 == r) {
newRow.setCell(cellIndex, new Cell(sb.toString(), null));
} else {
newRow.setCell(cellIndex, null);
}
if (!newRow.isEmpty()) {
newRows.add(newRow);
}
}
// r will be incremented by the for loop anyway
r = r2 - 1;
}
return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowChange(newRows));
}
use of com.google.refine.model.changes.MassRowChange in project OpenRefine by OpenRefine.
the class MultiValuedCellSplitOperation method createHistoryEntry.
@Override
protected HistoryEntry createHistoryEntry(Project project, long historyEntryID) throws Exception {
Column column = project.columnModel.getColumnByName(_columnName);
if (column == null) {
throw new Exception("No column named " + _columnName);
}
int cellIndex = column.getCellIndex();
Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
if (keyColumn == null) {
throw new Exception("No key column named " + _keyColumnName);
}
int keyCellIndex = keyColumn.getCellIndex();
List<Row> newRows = new ArrayList<Row>();
int oldRowCount = project.rows.size();
for (int r = 0; r < oldRowCount; r++) {
Row oldRow = project.rows.get(r);
if (oldRow.isCellBlank(cellIndex)) {
newRows.add(oldRow.dup());
continue;
}
Object value = oldRow.getCellValue(cellIndex);
String s = value instanceof String ? ((String) value) : value.toString();
String[] values = null;
if (_mode.equals("regex")) {
values = s.split(_separator);
} else {
values = StringUtils.splitByWholeSeparatorPreserveAllTokens(s, _separator);
}
if (values.length < 2) {
newRows.add(oldRow.dup());
continue;
}
// First value goes into the same row
{
Row firstNewRow = oldRow.dup();
firstNewRow.setCell(cellIndex, new Cell(values[0].trim(), null));
newRows.add(firstNewRow);
}
int r2 = r + 1;
for (int v = 1; v < values.length; v++) {
Cell newCell = new Cell(values[v].trim(), null);
if (r2 < project.rows.size()) {
Row oldRow2 = project.rows.get(r2);
if (oldRow2.isCellBlank(cellIndex) && oldRow2.isCellBlank(keyCellIndex)) {
Row newRow = oldRow2.dup();
newRow.setCell(cellIndex, newCell);
newRows.add(newRow);
r2++;
continue;
}
}
Row newRow = new Row(cellIndex + 1);
newRow.setCell(cellIndex, newCell);
newRows.add(newRow);
}
// r will be incremented by the for loop anyway
r = r2 - 1;
}
return new HistoryEntry(historyEntryID, project, getBriefDescription(null), this, new MassRowChange(newRows));
}
Aggregations