BagQueryRunner { public BagQueryResult search(String type, Collection<String> input, String extraFieldValue, boolean doWildcards, boolean caseSensitive, boolean ignoreConfig) throws ClassNotFoundException, InterMineException { Map<String, String> lowerCaseInput = new HashMap<String, String>(); List<String> cleanInput = new ArrayList<String>(); List<String> wildcardInput = new ArrayList<String>(); Map<String, Pattern> patterns = new HashMap<String, Pattern>(); for (String inputString : input) { if (StringUtils.isNotEmpty(inputString)) { if (!doWildcards || "*".equals(inputString) || inputString.indexOf('*') == -1) { if (!lowerCaseInput.containsKey(inputString.toLowerCase())) { cleanInput.add(inputString); lowerCaseInput.put(inputString.toLowerCase(), inputString); } } else { wildcardInput.add(inputString); String patternString = inputString.toLowerCase().replaceAll("\\*", "\\.\\*"); patternString = patternString.replaceAll("\\(", "\\.\\*"); patternString = patternString.replaceAll("\\)", "\\.\\*"); patterns.put(inputString, Pattern.compile(patternString)); } } } Class<?> typeCls = Class.forName(model.getPackageName() + "." + type); List<BagQuery> queries = getBagQueriesForType(bagQueryConfig, typeCls.getName()); Set<String> unresolved = new LinkedHashSet<String>(cleanInput); Set<String> wildcardUnresolved = new LinkedHashSet<String>(wildcardInput); Set<String> unresolvedOriginal = new LinkedHashSet<String>(cleanInput); Set<String> wildcardUnresolvedOriginal = new LinkedHashSet<String>(wildcardInput); BagQueryResult bqr = new BagQueryResult(); boolean matchOnFirst = true; if (!ignoreConfig) { matchOnFirst = bagQueryConfig.getMatchOnFirst(); } for (BagQuery bq : queries) { if (!unresolved.isEmpty() || !matchOnFirst) { Map<String, Set<Integer>> resMap = new HashMap<String, Set<Integer>>(); try { Set<String> toProcess = (matchOnFirst) ? unresolved : unresolvedOriginal; Query q = bq.getQuery(toProcess, extraFieldValue); Results res = os.execute(q, 10000, true, true, false); for (Object rowObj : res) { ResultsRow<?> row = (ResultsRow<?>) rowObj; Integer id = (Integer) row.get(0); for (int i = 1; i < row.size(); i++) { final Object fieldObject = row.get(i); if (fieldObject != null) { String field = String.valueOf(fieldObject); String lowerField = field.toLowerCase(); if (caseSensitive) { if (cleanInput.contains(field)) { processMatch(resMap, unresolved, id, field); } } else if (lowerCaseInput.containsKey(lowerField)) { String originalInput = lowerCaseInput.get(lowerField); processMatch(resMap, unresolved, id, originalInput); } } } } } catch (IllegalArgumentException e) { } addResults(resMap, unresolved, bqr, bq.getMessage(), typeCls, false, matchOnFirst, bq.matchesAreIssues()); } if (!wildcardInput.isEmpty()) { Map<String, Set<Integer>> resMap = new HashMap<String, Set<Integer>>(); Query q = bq.getQueryForWildcards(wildcardInput, extraFieldValue); Results res = os.execute(q, ResultsBatches.DEFAULT_BATCH_SIZE, true, true, false); for (Object rowObj : res) { ResultsRow<?> row = (ResultsRow<?>) rowObj; Integer id = (Integer) row.get(0); for (int i = 1; i < row.size(); i++) { String field = "" + row.get(i); String lowerField = field.toLowerCase(); for (String wildcard : wildcardInput) { Pattern pattern = patterns.get(wildcard); if (pattern.matcher(lowerField).matches()) { processMatch(resMap, wildcardUnresolved, id, wildcard); } } } } for (Map.Entry<String, Set<Integer>> entry : resMap.entrySet()) { bqr.addIssue(BagQueryResult.WILDCARD, bq.getMessage(), entry.getKey(), new ArrayList<Object>(entry.getValue())); if (matchOnFirst) { addResults(resMap, wildcardUnresolved, bqr, bq.getMessage(), typeCls, true, matchOnFirst, bq.matchesAreIssues()); } else { addResults(resMap, wildcardUnresolvedOriginal, bqr, bq.getMessage(), typeCls, true, matchOnFirst, bq.matchesAreIssues()); } } } } unresolved.addAll(wildcardUnresolved); bqr.addUnresolved(unresolved); return bqr; } BagQueryRunner(ObjectStore os, Map<String, List<FieldDescriptor>> classKeys,
            BagQueryConfig bagQueryConfig, TemplateManager templateManager); BagQueryResult searchForBag(String type, List<String> input, String extraFieldValue,
            boolean doWildcards); BagQueryResult searchForBag(String type, List<String> input, String extraFieldValue,
            boolean doWildcards, boolean ignoreConfig); BagQueryResult search(String type, Collection<String> input, String extraFieldValue,
            boolean doWildcards, boolean caseSensitive, boolean ignoreConfig);  }
@Test public void testSearchForBagMatches() throws Exception { List input = Arrays.asList(new Object[] {"EmployeeA1", "EmployeeA2"}); BagQueryResult res = runner.search("Employee", input, null, true, true, false); Assert.assertEquals(2, res.getMatches().values().size()); Assert.assertTrue(res.getIssues().isEmpty()); Assert.assertTrue(res.getUnresolved().isEmpty()); }
@Test public void testCase() throws Exception { List input = Arrays.asList(new Object[] {"employeeA1", "employeeA2"}); BagQueryResult res = runner.search("Employee", input, null, true, true, false); Assert.assertTrue(res.getMatches().isEmpty()); Assert.assertTrue(res.getIssues().isEmpty()); Assert.assertEquals(2, res.getUnresolved().size()); }