Class DatabaseQuery<T extends DatabaseInstance<T>>

java.lang.Object
com.syzygy.events.database.DatabaseQuery<T>
Type Parameters:
T - The type of instance being returned
All Implemented Interfaces:
Database.Dissolvable, Database.Querrier<DatabaseQuery<T>>, Database.UpdateListener

@Dissolves public class DatabaseQuery<T extends DatabaseInstance<T>> extends Object implements Database.UpdateListener, Database.Querrier<DatabaseQuery<T>>, Database.Dissolvable
Stores a query that is executed on a collection within the database. Loads instances returned from this query in pages
  • Field Details

    • db

      @NonNull private final Database db
      The database
    • collection

      @NonNull private final Database.Collections collection
      The collection which this queries
    • query

      @NonNull private final com.google.firebase.firestore.Query query
      The query to evaluate
    • snapshot

      @Nullable private com.google.firebase.firestore.QuerySnapshot snapshot
      The current document snapshot
    • resultsPerPage

      @Nullable private final Integer resultsPerPage
      The number of results returned per page. If null, all results are returned
    • currentInstances

      private final List<T extends DatabaseInstance<T>> currentInstances
      The current list of loaded instances
    • currentPage

      private com.google.firebase.firestore.Query currentPage
      The current query used to get the page
    • thisPage

      private DatabaseQuery.Page thisPage
      The page
    • updates

      private boolean updates
      If an update has occurred since the last refresh
    • deletes

      private boolean deletes
      If an update has occurred since the last refresh
  • Constructor Details

    • DatabaseQuery

      @MustStir public DatabaseQuery(@NonNull Database db, @NonNull com.google.firebase.firestore.Query query, @NonNull Database.Collections collection, @Nullable Integer resultsPerPage)
      Creates a database query which can be refreshed to load the set of instances which are returned by the query
      Parameters:
      db - The database
      query - The query
      collection - The collection which this queries
  • Method Details

    • refreshData

      @Titrates(what="Result instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") @Observes public void refreshData(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Refreshes the query and loads the new instances at the current page. Loads all instances of the current snapshot at the page. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      Specified by:
      refreshData in interface Database.Querrier<T extends DatabaseInstance<T>>
      Parameters:
      listener - The listener. Once the data is loaded, the listener is notified of success. Otherwise the listener is notified of failure.
    • methods

      Returns an object which contains methods that help notify the user or change the status of the associations. This object must be called with .dissolve once complete
      Parameters:
      query - The query containing the associations
      Returns:
      The set of methods for the associations currently in the list (does not match on change)
    • methods

      Returns an object which contains methods that help notify the user or change the status of the associations. This object must be called with .dissolve once complete
      Parameters:
      querrier - The querrier of the results
      query - The query containing the associations
      eas - The list of event associations to get methods for
      Returns:
      The set of methods for the associations currently in the list (does not match on change)
    • gotoNextPage

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") public void gotoNextPage(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Refreshes the query and loads the new instances starting at the instance following the last instance of the current page. If the result is less the the limit, gets the first page. If the current page does not exist or is empty, gets the first page Loads all instances of the current snapshot at the page. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      This will refresh data even if the given page is the current page

      Parameters:
      listener - The listener. Once the data is loaded, the listener is notified of success. Otherwise the listener is notified of failure.
    • gotoPreviousPage

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") public void gotoPreviousPage(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Refreshes the query and loads the new instances ending at the instance before the first instance of the current page. If the results are empty, gets the last page If the current page does not exist or is empty, gets the last page Loads all instances of the current snapshot at the page. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      This will refresh data even if the given page is the current page

      Parameters:
      listener - The listener. Once the data is loaded, the listener is notified of success. Otherwise the listener is notified of failure.
    • gotoFirstPage

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") public void gotoFirstPage(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Refreshes the query and loads the first page of results Loads all instances of the current snapshot at the page. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      This will refresh data even if the given page is the current page

      Parameters:
      listener - The listener. Once the data is loaded, the listener is notified of success. Otherwise the listener is notified of failure.
    • gotoLastPage

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") public void gotoLastPage(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Refreshes the query and loads the last page of results Loads all instances of the current snapshot at the page. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      This will refresh data even if the given page is the current page

      Parameters:
      listener - The listener. Once the data is loaded, the listener is notified of success. Otherwise the listener is notified of failure.
    • gotoPage

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") public void gotoPage(Database.Querrier.Listener<DatabaseQuery<T>> listener, DatabaseQuery.Page page)
      Goes to the corresponding page
      Parameters:
      listener - The listener
      page - The page to goto
      See Also:
    • isFirstPage

      public boolean isFirstPage()
      Returns:
      false if this thinks there is more data before
    • isLastPage

      public boolean isLastPage()
      Returns:
      true if this thinks there is more data after
    • nextPageQuery

      private void nextPageQuery()
      Sets the currentPage query to the set of resultsPerPage after the last document in the previous result.
    • previousPageQuery

      private void previousPageQuery()
      Sets the currentPage query to the set of resultsPerPage before the first document in the previous result.
    • firstPageQuery

      private void firstPageQuery()
      Gets the first resultsPerPage of the query
    • lastPageQuery

      private void lastPageQuery()
      Gets the last resultsPerPage of the query
    • getResultsPerPage

      @Nullable public Integer getResultsPerPage()
      Returns:
      The number of results per page. null if infinite
    • loadFromSnapshot

      @Titrates(what="Result Instances", when="All success") @StirsDeep(what="Previous Instances", when="All success") @Observes private void loadFromSnapshot(Database.Querrier.Listener<DatabaseQuery<T>> listener)
      Loads all instances of the current snapshot. On completion, dissolves all previous instances and sets the current instances to the new instances. Then notifies the listener of success.

      If any point, and instances errors, dissolves all newly loaded instances and notifies the listener. The current instances remains the same as before the function call.

      Parameters:
      listener - The listener for the refresh
    • setNewInstances

      @Titrates(what="New Instances") @StirsDeep(what="Previous Instances") private void setNewInstances(@Dilutes List<T> newInstances)
      Sets the new instances. Dissolves all previous instances and clears updates/deletions
      Parameters:
      newInstances - the new instances
    • dissolve

      @StirsDeep(what="Previous Instances") public void dissolve()
      Removes references to all instances that have been created and clears the current instance
      Specified by:
      dissolve in interface Database.Dissolvable
    • onUpdate

      public <S extends DatabaseInstance<S>> void onUpdate(@Observes DatabaseInstance<S> instance, Database.UpdateListener.Type type)
      Specified by:
      onUpdate in interface Database.UpdateListener
    • getCurrentInstances

      @NonNull @Observes public @Unmodifiable List<T> getCurrentInstances()
      Returns:
      An unmodifiable list of the current instances loaded
    • getCollection

      public Database.Collections getCollection()
      Returns:
      The collection to which the instances belong
    • outOfDate

      public boolean outOfDate()
      Returns:
      true if one of the items in this query has been updated or deleted since the last refresh. Does not account for additions.
    • updateHasOccurred

      public boolean updateHasOccurred()
      Returns:
      true if one of the items in this query has been updated since the last refresh
    • deletionHasOccurred

      public boolean deletionHasOccurred()
      Returns:
      true if one of the items in this query has been deleted since the last refresh
    • getMyEventsFilter

      @MustStir public static DatabaseQuery<EventAssociation> getMyEventsFilter(Database db, @Observes User u)
    • getFacilityEvents

      @MustStir public static DatabaseQuery<Event> getFacilityEvents(Database db, @Observes Facility facility)
    • getMyNotifications

      @MustStir public static DatabaseQuery<Notification> getMyNotifications(Database db, @Observes User u)
    • getMyNotificationsQuery

      public static com.google.firebase.firestore.Query getMyNotificationsQuery(Database db, @Observes User u)
    • getAttachedUsers

      @MustStir public static DatabaseQuery<EventAssociation> getAttachedUsers(Database db, @Observes Event e, String status, boolean returnAll)
      Parameters:
      status - iF null or blank, returns all
    • getAttachedUsersQuery

      @MustStir public static com.google.firebase.firestore.Query getAttachedUsersQuery(Database db, @Observes Event e, String status)
    • getUsers

      @MustStir public static DatabaseQuery<User> getUsers(Database db)
    • getEvents

      @MustStir public static DatabaseQuery<Event> getEvents(Database db)
    • getImages

      @MustStir public static DatabaseQuery<Image> getImages(Database db)