Get all translations in wikivoyage search

This commit is contained in:
Alexander Sytnyk 2018-03-30 12:40:26 +03:00
parent cdc29f3e85
commit cde978955c

View file

@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import gnu.trove.list.array.TLongArrayList;
import gnu.trove.map.hash.TLongObjectHashMap; import gnu.trove.map.hash.TLongObjectHashMap;
public class WikivoyageDbHelper { public class WikivoyageDbHelper {
@ -78,18 +79,22 @@ public class WikivoyageDbHelper {
@NonNull @NonNull
public List<WikivoyageSearchResult> search(final String searchQuery) { public List<WikivoyageSearchResult> search(final String searchQuery) {
TLongArrayList cityIds = getCityIdsBySearchTerm(searchQuery);
List<WikivoyageSearchResult> res = new ArrayList<>(); List<WikivoyageSearchResult> res = new ArrayList<>();
SQLiteConnection conn = openConnection(); SQLiteConnection conn = openConnection();
if (conn != null) { if (conn != null) {
try { try {
String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE ?"; for (int i = 0; i < cityIds.size(); i++) {
SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[]{searchQuery + "%"}); String dbQuery = SEARCH_TABLE_SELECT + " WHERE " + SEARCH_COL_CITY_ID + " = ?";
SQLiteCursor cursor = conn.rawQuery(dbQuery, new String[]{String.valueOf(cityIds.get(0))});
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
do { do {
res.add(readSearchResult(cursor)); res.add(readSearchResult(cursor));
} while (cursor.moveToNext()); } while (cursor.moveToNext());
} }
cursor.close(); cursor.close();
}
} finally { } finally {
conn.close(); conn.close();
} }
@ -118,6 +123,27 @@ public class WikivoyageDbHelper {
return list; return list;
} }
private TLongArrayList getCityIdsBySearchTerm(String searchTerm) {
TLongArrayList res = new TLongArrayList();
SQLiteConnection conn = openConnection();
if (conn != null) {
try {
String query = "SELECT DISTINCT " + SEARCH_COL_CITY_ID + " FROM " + SEARCH_TABLE_NAME +
" WHERE " + SEARCH_COL_SEARCH_TERM + " LIKE ?";
SQLiteCursor cursor = conn.rawQuery(query, new String[]{searchTerm + "%"});
if (cursor.moveToFirst()) {
do {
res.add(cursor.getLong(0));
} while (cursor.moveToNext());
}
cursor.close();
} finally {
conn.close();
}
}
return res;
}
private Collection<WikivoyageSearchResult> groupSearchResultsByCityId(List<WikivoyageSearchResult> res) { private Collection<WikivoyageSearchResult> groupSearchResultsByCityId(List<WikivoyageSearchResult> res) {
String baseLng = application.getLanguage(); String baseLng = application.getLanguage();
TLongObjectHashMap<WikivoyageSearchResult> wikivoyage = new TLongObjectHashMap<>(); TLongObjectHashMap<WikivoyageSearchResult> wikivoyage = new TLongObjectHashMap<>();