From 9aa168633f4ddc410bdd5c646ab4581f49e28257 Mon Sep 17 00:00:00 2001 From: Ralf Claussnitzer Date: Thu, 26 Oct 2017 13:32:00 +0200 Subject: [PATCH] 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 --- .../com/google/refine/InterProjectModel.java | 59 ++++++++++++++----- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/main/src/com/google/refine/InterProjectModel.java b/main/src/com/google/refine/InterProjectModel.java index 295042612..1d5e14382 100644 --- a/main/src/com/google/refine/InterProjectModel.java +++ b/main/src/com/google/refine/InterProjectModel.java @@ -68,22 +68,38 @@ public class InterProjectModel { this.toProjectID = toProjectID; this.toProjectColumnName = toProjectColumnName; } - - public HasFieldsListImpl getRows(Object value) { - if (ExpressionUtils.isNonBlankData(value) && valueToRowIndices.containsKey(value)) { - Project toProject = ProjectManager.singleton.getProject(toProjectID); - 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)); + + public HasFieldsListImpl getRows(final Object rowKey) { + Project toProject = ProjectManager.singleton.getProject(toProjectID); + if (toProject == 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)); + } } - - return rows; } } - return null; + + // Returning null instead of an empty list is expected + return resultFieldList.isEmpty() ? null : resultFieldList; } + } protected Map _joins = new HashMap(); @@ -158,11 +174,21 @@ public class InterProjectModel { if (fromColumn == null || toColumn == null) { return; } - + for (Row fromRow : fromProject.rows) { - Object value = fromRow.getCellValue(fromColumn.getCellIndex()); - if (ExpressionUtils.isNonBlankData(value) && !join.valueToRowIndices.containsKey(value)) { - join.valueToRowIndices.put(value, new ArrayList()); + Object fromRowKey = fromRow.getCellValue(fromColumn.getCellIndex()); + if (ExpressionUtils.isNonBlankData(fromRowKey)) { + 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()); + } + } } } @@ -176,4 +202,5 @@ public class InterProjectModel { } } } + }