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

@ -69,21 +69,37 @@ public class InterProjectModel {
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) {
HasFieldsListImpl rows = new HasFieldsListImpl();
for (Integer r : valueToRowIndices.get(value)) {
Row row = toProject.rows.get(r);
rows.add(new WrappedRow(toProject, r, row));
}
return rows;
}
}
return null; return null;
} }
HasFieldsListImpl resultFieldList = null;
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));
}
}
}
}
// 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>();
@ -160,9 +176,19 @@ public class InterProjectModel {
} }
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 {
} }
} }
} }
} }