|
36 | 36 |
|
37 | 37 | import static com.mongodb.client.model.Aggregates.*; |
38 | 38 | import static com.mongodb.client.model.Projections.*; |
| 39 | +import static org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.Accumulator.*; |
39 | 40 | import static org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.Accumulator.bucket; |
40 | 41 | import static org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.Accumulator.count; |
41 | | -import static org.opencb.commons.datastore.mongodb.MongoDBQueryUtils.Accumulator.*; |
42 | 42 |
|
43 | 43 | /** |
44 | 44 | * Created by imedina on 17/01/16. |
@@ -178,6 +178,53 @@ public static Bson createFilter(String mongoDbField, String queryParam, Query qu |
178 | 178 | return filter; |
179 | 179 | } |
180 | 180 |
|
| 181 | + /** |
| 182 | + * Splits a string by the given separator, handling quoted values properly. |
| 183 | + * Quoted values can contain the separator character without being split. |
| 184 | + * Removes surrounding quotes and trims whitespace from each value. |
| 185 | + * |
| 186 | + * @param input the input string to split |
| 187 | + * @param separator the separator to split by ("," or ";") |
| 188 | + * @return list of trimmed, unquoted values |
| 189 | + */ |
| 190 | + public static List<String> smartSplit(String input, String separator) { |
| 191 | + List<String> result = new ArrayList<>(); |
| 192 | + if (input == null || input.isEmpty()) { |
| 193 | + return result; |
| 194 | + } |
| 195 | + |
| 196 | + boolean inQuotes = false; |
| 197 | + StringBuilder currentValue = new StringBuilder(); |
| 198 | + |
| 199 | + for (int i = 0; i < input.length(); i++) { |
| 200 | + char c = input.charAt(i); |
| 201 | + |
| 202 | + if (c == '"') { |
| 203 | + inQuotes = !inQuotes; |
| 204 | + } else if (!inQuotes && input.substring(i).startsWith(separator)) { |
| 205 | + // Found separator outside quotes |
| 206 | + String value = currentValue.toString().trim(); |
| 207 | + if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { |
| 208 | + value = value.substring(1, value.length() - 1); |
| 209 | + } |
| 210 | + result.add(value.trim()); |
| 211 | + currentValue = new StringBuilder(); |
| 212 | + i += separator.length() - 1; // Skip the separator |
| 213 | + } else { |
| 214 | + currentValue.append(c); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + // Add the last value |
| 219 | + String value = currentValue.toString().trim(); |
| 220 | + if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) { |
| 221 | + value = value.substring(1, value.length() - 1); |
| 222 | + } |
| 223 | + result.add(value.trim()); |
| 224 | + |
| 225 | + return result; |
| 226 | + } |
| 227 | + |
181 | 228 | private static String getLogicalSeparator(LogicalOperator operator) { |
182 | 229 | return (operator != null && operator.equals(LogicalOperator.AND)) ? AND : OR; |
183 | 230 | } |
@@ -258,8 +305,13 @@ protected static String getOp2(String op, String value) { |
258 | 305 |
|
259 | 306 | public static Bson createAutoFilter(String mongoDbField, String queryParam, Query query, QueryParam.Type type, LogicalOperator operator) |
260 | 307 | throws NumberFormatException { |
261 | | - |
262 | | - List<String> queryParamList = query.getAsStringList(queryParam, getLogicalSeparator(operator)); |
| 308 | + List<String> queryParamList; |
| 309 | + String value = query.getString(queryParam); |
| 310 | + if (StringUtils.isNotEmpty(value) && value.contains("\"")) { |
| 311 | + queryParamList = smartSplit(value, getLogicalSeparator(operator)); |
| 312 | + } else { |
| 313 | + queryParamList = query.getAsStringList(queryParam, getLogicalSeparator(operator)); |
| 314 | + } |
263 | 315 | return createAutoFilter(mongoDbField, queryParam, type, operator, queryParamList); |
264 | 316 | } |
265 | 317 |
|
|
0 commit comments