Allow comma separated multi-value source in cross() function

Implements support for comma separated multiple-value keys for joining
another project using the cross() function.

See https://github.com/OpenRefine/OpenRefine/issues/1204#issuecomment-326320954
This commit is contained in:
Ralf Claussnitzer 2017-10-26 13:32:00 +02:00
parent e5fdf48680
commit 9aa168633f

View File

@ -68,22 +68,38 @@ public class InterProjectModel {
this.toProjectID = toProjectID; this.toProjectID = toProjectID;
this.toProjectColumnName = toProjectColumnName; this.toProjectColumnName = toProjectColumnName;
} }
public HasFieldsListImpl getRows(Object value) { public HasFieldsListImpl getRows(final Object rowKey) {
if (ExpressionUtils.isNonBlankData(value) && valueToRowIndices.containsKey(value)) { Project toProject = ProjectManager.singleton.getProject(toProjectID);
Project toProject = ProjectManager.singleton.getProject(toProjectID); if (toProject == null) {
if (toProject != null) { return null;
HasFieldsListImpl rows = new HasFieldsListImpl(); }
for (Integer r : valueToRowIndices.get(value)) {
Row row = toProject.rows.get(r); HasFieldsListImpl resultFieldList = null;
rows.add(new WrappedRow(toProject, r, row));
if (ExpressionUtils.isNonBlankData(rowKey)) {
Object[] rowKeys;
if (rowKey instanceof String) {
rowKeys = ((String) rowKey).split(",");
} else {
rowKeys = new Object[]{rowKey};
}
resultFieldList = new HasFieldsListImpl();
for (Object k : rowKeys) {
if (valueToRowIndices.containsKey(k)) {
for (Integer rowIndex : valueToRowIndices.get(k)) {
Row row = toProject.rows.get(rowIndex);
resultFieldList.add(new WrappedRow(toProject, rowIndex, row));
}
} }
return rows;
} }
} }
return null;
// Returning null instead of an empty list is expected
return resultFieldList.isEmpty() ? null : resultFieldList;
} }
} }
protected Map<String, ProjectJoin> _joins = new HashMap<String, ProjectJoin>(); protected Map<String, ProjectJoin> _joins = new HashMap<String, ProjectJoin>();
@ -158,11 +174,21 @@ public class InterProjectModel {
if (fromColumn == null || toColumn == null) { if (fromColumn == null || toColumn == null) {
return; return;
} }
for (Row fromRow : fromProject.rows) { for (Row fromRow : fromProject.rows) {
Object value = fromRow.getCellValue(fromColumn.getCellIndex()); Object fromRowKey = fromRow.getCellValue(fromColumn.getCellIndex());
if (ExpressionUtils.isNonBlankData(value) && !join.valueToRowIndices.containsKey(value)) { if (ExpressionUtils.isNonBlankData(fromRowKey)) {
join.valueToRowIndices.put(value, new ArrayList<Integer>()); Object[] fromRowKeys;
if (fromRowKey instanceof String) {
fromRowKeys = ((String) fromRowKey).split(",");
} else {
fromRowKeys = new Object[]{fromRowKey};
}
for (Object k : fromRowKeys) {
if (!join.valueToRowIndices.containsKey(k)) {
join.valueToRowIndices.put(k, new ArrayList<Integer>());
}
}
} }
} }
@ -176,4 +202,5 @@ public class InterProjectModel {
} }
} }
} }
} }