2010-02-05 07:29:27 +01:00
|
|
|
package com.metaweb.gridworks.model.operations;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONWriter;
|
|
|
|
|
2010-02-07 04:36:40 +01:00
|
|
|
import com.metaweb.gridworks.expr.ExpressionUtils;
|
2010-02-05 07:29:27 +01:00
|
|
|
import com.metaweb.gridworks.history.Change;
|
|
|
|
import com.metaweb.gridworks.history.HistoryEntry;
|
|
|
|
import com.metaweb.gridworks.model.AbstractOperation;
|
|
|
|
import com.metaweb.gridworks.model.Cell;
|
|
|
|
import com.metaweb.gridworks.model.Column;
|
|
|
|
import com.metaweb.gridworks.model.Project;
|
|
|
|
import com.metaweb.gridworks.model.Row;
|
|
|
|
import com.metaweb.gridworks.model.changes.MassRowChange;
|
|
|
|
import com.metaweb.gridworks.process.Process;
|
|
|
|
import com.metaweb.gridworks.process.QuickHistoryEntryProcess;
|
|
|
|
|
|
|
|
public class MultiValueCellJoinOperation implements AbstractOperation {
|
|
|
|
private static final long serialVersionUID = 3134524625206033285L;
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
final protected String _columnName;
|
|
|
|
final protected String _keyColumnName;
|
2010-02-05 07:29:27 +01:00
|
|
|
final protected String _separator;
|
|
|
|
|
|
|
|
public MultiValueCellJoinOperation(
|
2010-02-17 01:26:38 +01:00
|
|
|
String columnName,
|
|
|
|
String keyColumnName,
|
2010-02-05 07:29:27 +01:00
|
|
|
String separator
|
|
|
|
) {
|
2010-02-17 01:26:38 +01:00
|
|
|
_columnName = columnName;
|
|
|
|
_keyColumnName = keyColumnName;
|
2010-02-05 07:29:27 +01:00
|
|
|
_separator = separator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Process createProcess(Project project, Properties options)
|
|
|
|
throws Exception {
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
Column column = project.columnModel.getColumnByName(_columnName);
|
2010-02-05 07:29:27 +01:00
|
|
|
if (column == null) {
|
2010-02-17 01:26:38 +01:00
|
|
|
throw new Exception("No column named " + _columnName);
|
2010-02-05 07:29:27 +01:00
|
|
|
}
|
2010-02-17 01:26:38 +01:00
|
|
|
int cellIndex = column.getCellIndex();
|
|
|
|
|
|
|
|
Column keyColumn = project.columnModel.getColumnByName(_keyColumnName);
|
|
|
|
if (column == null) {
|
|
|
|
throw new Exception("No key column named " + _keyColumnName);
|
|
|
|
}
|
|
|
|
int keyCellIndex = keyColumn.getCellIndex();
|
2010-02-05 07:29:27 +01:00
|
|
|
|
|
|
|
List<Row> newRows = new ArrayList<Row>();
|
|
|
|
|
|
|
|
int oldRowCount = project.rows.size();
|
|
|
|
for (int r = 0; r < oldRowCount; r++) {
|
|
|
|
Row oldRow = project.rows.get(r);
|
|
|
|
|
2010-02-17 01:26:38 +01:00
|
|
|
if (oldRow.isCellBlank(keyCellIndex)) {
|
2010-02-05 07:29:27 +01:00
|
|
|
newRows.add(oldRow.dup());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int r2 = r + 1;
|
2010-02-17 01:26:38 +01:00
|
|
|
while (r2 < oldRowCount && project.rows.get(r2).isCellBlank(keyCellIndex)) {
|
2010-02-05 07:29:27 +01:00
|
|
|
r2++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r2 == r + 1) {
|
|
|
|
newRows.add(oldRow.dup());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
for (int r3 = r; r3 < r2; r3++) {
|
2010-02-17 01:26:38 +01:00
|
|
|
Object value = project.rows.get(r3).getCellValue(cellIndex);
|
2010-02-07 04:36:40 +01:00
|
|
|
if (!ExpressionUtils.isBlank(value)) {
|
2010-02-05 07:29:27 +01:00
|
|
|
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) {
|
2010-02-17 01:26:38 +01:00
|
|
|
newRow.setCell(cellIndex, new Cell(sb.toString(), null));
|
2010-02-05 07:29:27 +01:00
|
|
|
} else {
|
2010-02-17 01:26:38 +01:00
|
|
|
newRow.setCell(cellIndex, null);
|
2010-02-05 07:29:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!newRow.isEmpty()) {
|
|
|
|
newRows.add(newRow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = r2 - 1; // r will be incremented by the for loop anyway
|
|
|
|
}
|
|
|
|
|
2010-02-17 02:30:09 +01:00
|
|
|
String description = "Join multi-valued cells in column " + column.getHeaderLabel();
|
2010-02-05 07:29:27 +01:00
|
|
|
|
|
|
|
Change change = new MassRowChange(newRows);
|
|
|
|
HistoryEntry historyEntry = new HistoryEntry(
|
|
|
|
project, description, this, change);
|
|
|
|
|
|
|
|
return new QuickHistoryEntryProcess(project, historyEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void write(JSONWriter writer, Properties options)
|
|
|
|
throws JSONException {
|
2010-02-17 02:30:09 +01:00
|
|
|
|
|
|
|
writer.object();
|
|
|
|
writer.key("op"); writer.value("join-multivalued-cells");
|
|
|
|
writer.key("description"); writer.value("Join multi-valued cells in column " + _columnName);
|
|
|
|
writer.key("columnName"); writer.value(_columnName);
|
|
|
|
writer.key("keyColumnName"); writer.value(_keyColumnName);
|
|
|
|
writer.key("separator"); writer.value(_separator);
|
|
|
|
writer.endObject();
|
2010-02-05 07:29:27 +01:00
|
|
|
}
|
|
|
|
}
|