Skip to content

Instantly share code, notes, and snippets.

@markleusink
Last active October 6, 2018 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markleusink/20b755287854a8ff2bd0611fab742434 to your computer and use it in GitHub Desktop.
Save markleusink/20b755287854a8ff2bd0611fab742434 to your computer and use it in GitHub Desktop.
// filters
List<String> filterCity = new ArrayList<String>();
List<String> cities = new ArrayList<String>(); // list of typeahead suggestions
LinkedHashSet<Integer> matchingIds = new LinkedHashSet<Integer>();
public ListController3() {
// load city typeahead options: get a list of all cities used in the view
// the list is cached in the applicationScope
if (ExtLibUtil.getApplicationScope().containsKey("cities")) {
this.cities = (List<String>) ExtLibUtil.getApplicationScope().get("cities");
} else {
long start = System.currentTimeMillis();
// get a list of all cities for all contacts
// this uses an optimised view column lookup formula from JNA:
Set<String> cities = getCollection().getColumnValues("city", null);
this.cities.addAll(cities);
ExtLibUtil.getApplicationScope().put("cities", this.cities);
System.out.println("cities now has " + cities.size() + " in " + (System.currentTimeMillis() - start) + "ms");
}
// load first set of entries
loadEntries();
}
public void loadEntries() {
try {
NotesCollection collection = getCollection();
// resort the collection (view) according to the selected column
collection.resortView(this.sortColumn, (this.sortAscending ? Direction.Ascending : Direction.Descending));
// tell the API which data we want to read (in this case note ids and column itemname/value map)
EnumSet<ReadMask> returnData = EnumSet.of(ReadMask.NOTEID, ReadMask.SUMMARYVALUES);
EnumSet<Navigate> returnNavigator;
// if a filter was set (on 'city'), we need to change a couple of things:
// - just read the contacts that match the city/ cities
// - use a different navigator: we loop only over the selected contact
// in the collection
boolean hasFilters = !filterCity.isEmpty();
if (hasFilters) {
// apply the matching Note IDs
collection.select(matchingIds, true);
returnNavigator = EnumSet.of(Navigate.NEXT_SELECTED);
totalEntries = collection.getSelectedList().getCount();
} else {
returnNavigator = EnumSet.of(Navigate.NEXT);
totalEntries = collection.getTopLevelEntries();
}
// get a list of 'NotesViewEntryData' for the current page
List<NotesViewEntryData> viewEntries = collection.getAllEntries("0", skipEntries, returnNavigator, NUM_PER_PAGE, returnData,
new EntriesAsListCallback(NUM_PER_PAGE));
// transform the list into a list of maps (where every entry
// represents a view entry and has a map containing the column values)
entries.clear();
for (NotesViewEntryData entry : viewEntries) {
entries.add(entry.getColumnDataAsMap());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void applyFilters() {
this.skipEntries = 1; // go to first page
// get the contacts view and sort it using the property we're filtering on
NotesCollection collection = getCollection();
collection.resortView("city", Direction.Ascending);
matchingIds.clear();
// for every city entered, we find the Note IDs of the entries in the contacts view
// that match. All IDs are stored in a Set (matchingIds)
for (String city : filterCity) {
Set<Integer> matches = collection.getAllIdsByKey(EnumSet.of(Find.CASE_INSENSITIVE), city);
matchingIds.addAll(matches );
System.out.println("- added " + matches.size() + " for city " + city);
}
loadEntries();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment