// Method to return a list of matches according to fields for a tab, using // the cached results // this does not store the result in the cache - this behavior is deferred // to the caller private int searchTabResults( int WindowNo, UITab tab, List<Integer> fieldIds, int queryResultID, int searchResultID, String query, int rowCount) { ArrayList<String[]> results = m_results.get(queryResultID); if (query.trim().equals("")) { m_results.put(searchResultID, results); return results.size(); } ScoreStrategy scorer = new ScoreStrategy(query); ScoreCell[] scores = new ScoreCell[results.size()]; // first initialize score cells int j = 0; for (String[] result : results) { // initialize score cells ScoreCell score = new ScoreCell(); score.row = result; score.score = scorer.createScore(); scores[j++] = score; } for (int id : fieldIds) { UIField field = getField(id, WindowNo); final int idx = tab.getFieldIndex(id); if (field.isLookup()) { ArrayList<String> fieldValues = new ArrayList<String>(results.size()); for (String[] row : results) { fieldValues.add(row[idx]); } ArrayList<String> sorts = getLookupValueOnlyDirect(id, fieldValues, true); int i = 0; for (String value : sorts) { scorer.getScore(value, scores[i].score); i++; } } else { int i = 0; for (String[] row : results) { String value = row[idx]; scorer.getScore(value, scores[i].score); i++; } } } ArrayList<ScoreCell> matchingScores = new ArrayList<ScoreCell>(); for (ScoreCell cell : scores) { if (cell.score.isMatch) { matchingScores.add(cell); } } Collections.sort(matchingScores, scorer); ArrayList<String[]> matches = new ArrayList<String[]>(); for (ScoreCell score : matchingScores) { matches.add(score.row); } m_results.put(searchResultID, matches); return matches.size(); }
public void sortResults( int WindowNo, int AD_Tab_ID, int AD_Field_ID, int queryResultID, final boolean ascending) { class SortCell { String[] row; String sort; } ArrayList<String[]> results = m_results.get(queryResultID); if (results == null) log.severe("cannot sort. results non-existent for queryResultID:" + queryResultID); UITab tab = getTab(AD_Tab_ID); final UIField field = getField(AD_Field_ID, WindowNo); final int displayType = field.getAD_Reference_ID(); final int idx = tab.getFieldIndex(AD_Field_ID); // if not a lookup, directly sort if (!field.isLookup()) { if (FieldType.isNumeric(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; BigDecimal s1 = new BigDecimal(o1[idx].equals("") ? "-1e-10" : o1[idx]); BigDecimal s2 = new BigDecimal(o2[idx].equals("") ? "-1e-10" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else if (FieldType.isDate(displayType)) { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; Long s1 = new Long(o1[idx].equals("") ? "-1000000" : o1[idx]); Long s2 = new Long(o2[idx].equals("") ? "-1000000" : o2[idx]); return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } else { Collections.sort( results, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { if (o1[idx] == null) o1[idx] = ""; if (o2[idx] == null) o2[idx] = ""; String s1 = o1[idx]; String s2 = o2[idx]; return ascending ? s1.compareTo(s2) : s2.compareTo(s1); } }); } return; } Comparator<SortCell> c = new Comparator<SortCell>() { public int compare(SortCell o1, SortCell o2) { if (ascending) return o1.sort.compareTo(o2.sort); else return o2.sort.compareTo(o1.sort); } }; // for look up, first get id values ArrayList<String> fieldValues = new ArrayList<String>(results.size()); for (String[] row : results) { fieldValues.add(row[idx]); } // then translate into real values ArrayList<String> sorts = getLookupValueOnlyDirect(AD_Field_ID, fieldValues, true); ArrayList<SortCell> toBeSorteds = new ArrayList<SortCell>(sorts.size()); for (int i = 0; i < sorts.size(); i++) { SortCell toBeSorted = new SortCell(); toBeSorted.row = results.get(i); toBeSorted.sort = sorts.get(i); toBeSorteds.add(toBeSorted); } // sort Collections.sort(toBeSorteds, c); // after sorting, replace col with original values int i = 0; for (SortCell toBeSorted : toBeSorteds) { results.set(i, toBeSorted.row); i++; } }