Class GenericRepository<T extends DatabaseObject>

java.lang.Object
com.example.skeddly.business.database.repository.GenericRepository<T>
Type Parameters:
T - The class of objects that this repository shall handle.
Direct Known Subclasses:
EventRepository, NotificationRepository, TicketRepository, UserRepository

public abstract class GenericRepository<T extends DatabaseObject> extends Object
Represents a generic repository that can be extended to retrieve POJOs from Firestore. This class should NOT be used on its own. Derived classes should be used.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Class<T>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Create a new GenericRepository with the given class as the type of POJO that it shall handle.
  • Method Summary

    Modifier and Type
    Method
    Description
    com.google.android.gms.tasks.Task<Long>
    Counts the number of documents that reside within the collection we are tracking.
    com.google.android.gms.tasks.Task<Void>
    Deletes a document by its ID.
    com.google.android.gms.tasks.Task<T>
    get(String id)
    Retrieves a document by its ID.
    com.google.android.gms.tasks.Task<List<T>>
    Retrieves all documents present in the collection managed by this repository.
    protected com.google.android.gms.tasks.Task<List<T>>
    getAllByQuery(com.google.firebase.firestore.Query query)
    Retrieves all documents matching a given query.
    protected abstract com.google.firebase.firestore.CollectionReference
    This method retrieves the reference to the collection of documents it handles
    protected com.google.firebase.firestore.Query
    This method retrieves a query for getting all the documents handled by this repository.
    com.google.firebase.firestore.ListenerRegistration
    Listens to the document given by the ID.
    com.google.firebase.firestore.ListenerRegistration
    Listens to all documents in this repository.
    com.google.android.gms.tasks.Task<Void>
    set(T object)
    Given an object, either update or add it to the repository.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • GenericRepository

      protected GenericRepository(Class<T> clazz)
      Create a new GenericRepository with the given class as the type of POJO that it shall handle.
      Parameters:
      clazz - The class corresponding to the POJO we're dealing with.
  • Method Details

    • getCollectionPath

      protected abstract com.google.firebase.firestore.CollectionReference getCollectionPath()
      This method retrieves the reference to the collection of documents it handles
      Returns:
      A CollectionReference to the collection of documents it handles.
    • getQuery

      protected com.google.firebase.firestore.Query getQuery()
      This method retrieves a query for getting all the documents handled by this repository. This is commonly overridden in repositories that filter out documents by fields.
      Returns:
      A query that can retrieve multiple documents.
    • get

      public com.google.android.gms.tasks.Task<T> get(String id)
      Retrieves a document by its ID. A task is returned that retrieves the object as converted to the POJO. Retrieving a document that does not exist will result in the task failing.

      Common usage includes adding an onCompleteListener. The status of the task should be checked with a call to .isSuccessful() before attempting to retrieve the object.

      Parameters:
      id - The ID of the document we want to retrieve.
      Returns:
      A task that returns the requested object.
    • getAll

      public com.google.android.gms.tasks.Task<List<T>> getAll()
      Retrieves all documents present in the collection managed by this repository. The documents are converted to their underlying POJOs and then returned as a list. The list shall be empty if the collection does not exist or has nothing in it.
      Returns:
      A task that returns all the objects in a list.
    • getAllByQuery

      protected com.google.android.gms.tasks.Task<List<T>> getAllByQuery(com.google.firebase.firestore.Query query)
      Retrieves all documents matching a given query. The documents are converted to their underlying POJOs and then returned as a list. The list shall be empty if the collection does not exist or has nothing in it.
      Returns:
      A task that returns all the objects in a list.
    • set

      public com.google.android.gms.tasks.Task<Void> set(T object)
      Given an object, either update or add it to the repository. The ID of the object will be queried and used as the Document ID in the database.
      Parameters:
      object - The object to store in the repository.
      Returns:
      A task that will complete once the object has been saved.
    • listen

      public com.google.firebase.firestore.ListenerRegistration listen(String id, SingleListenUpdate<T> callback)
      Listens to the document given by the ID. The caller is called back with the initial state of the document as converted to a POJO. Any new changes afterwards will notify the provided callback function.

      If the document does not exist when initially calling this, the returned object is NULL. The listener remains active and will notify if the document is created later on.

      If the document is deleted, the returned object is NULL. The listener still remains active for if a document with the same ID is created.

      Warning: The caller is responsible for removing the ListenerRegistration once they no longer require updates. Only use this method if real time updates are necessary.

      Parameters:
      id - The ID of the document we want to listen to.
      callback - Who we should call back upon document changes.
      Returns:
      A ListenerRegistration object that can be used to cancel the listener at any time.
    • listenAll

      public com.google.firebase.firestore.ListenerRegistration listenAll(SingleListenUpdate<List<T>> callback)
      Listens to all documents in this repository. The caller is called back with the initial state of all documents as converted to POJOs. Any new changes afterwards will notify the provided callback function. Note that each call provides a list of every object, even those that did not change.

      If the collection does not exist when initially calling this, the list is empty. The listener remains active and will notify if the collection is added to later on.

      Warning: The caller is responsible for removing the ListenerRegistration once they no longer require updates. Only use this method if real time updates are necessary.

      Parameters:
      callback - Who we should call back upon document changes.
      Returns:
      A ListenerRegistration object that can be used to cancel the listener at any time.
    • delete

      public com.google.android.gms.tasks.Task<Void> delete(String id)
      Deletes a document by its ID. A task is returned that can be listened to so that the caller knows when the deletion has finished being processed by the DB.

      If the ID does not exist in the database, the task is still successful and nothing changes.

      Parameters:
      id - The ID of the document that we want to delete.
      Returns:
      A listenable Task that represents the deletion on the DB side.
    • count

      public com.google.android.gms.tasks.Task<Long> count()
      Counts the number of documents that reside within the collection we are tracking. If the collection does not exist, a count of 0 is returned.
      Returns:
      A task that retrieves the count as a long.