Class AbstractJongoRepository<T>

java.lang.Object
org.craftercms.commons.mongo.AbstractJongoRepository<T>
All Implemented Interfaces:
CrudRepository<T>

public abstract class AbstractJongoRepository<T> extends Object implements CrudRepository<T>
Simple interface to interact with Jongo/MongoDB.
Changes MongoException in to MongoRepositoryException (which is a checked exception).As well if Command result is not ok (CommandResult#isOk) return false a exception will be thrown the message for that exception will be CommandResult#getErrorMessage.
Some of the find and insert methods use a template queryName. this means that the string can contain placeholders ('#') this will allow the user to have predefine json strings that will be substitute with the given params.Params are not Name therefor if the same value is needed multiple times for now it has to be send multiple times.Order of the params should match the same in the json string.

This class is mark as abstract to force inheritance , so we can get in runtime the class of the generic parameter with this we can simplify the mapping.

Author:
Carlos Ortiz.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected Class<? extends T>
     
    protected String
     
    protected com.mongodb.gridfs.GridFS
     
    protected org.jongo.Jongo
     
    protected JongoQueries
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of all documents in the collection.
    long
    count(String query)
    Returns the number of documents that match the query
    long
    count(String query, Object... queryParams)
    Returns the number of documents that match the query
    protected String
    createSortQuery(List<org.apache.commons.collections4.keyvalue.DefaultKeyValue<String,Boolean>> fields)
    Creates a Sort query based on the fields.
    Key of the map is the field False=Desc,True=asc for the field, Respect order of the keys
    void
    deleteFile(String storeName)
    Deletes the File with the given name.
    void
    deleteFile(org.bson.types.ObjectId fileId)
    Deletes the File with the given Id.
    find(String query)
    Search all documents in the given collection that match the query.
    find(String query, Object... queryParams)
    Finds all documents of the given collection that match the template query
    §§
    Gets all documents of a the given collection.
    Finds by the Id.
    Finds by the Id.
    findOne(String query)
    Search for documents of the given collection that match the query.
    In only return the first Document
    findOne(String query, Object... queryParams)
    Search for documents of the given collection that match the query.
    In only return the first Document
    protected org.jongo.MongoCollection
    Gets the Jongo Document.
    getFileInfo(String storeName)
    Gets the file information based on its name..
    getFileInfo(org.bson.types.ObjectId fileId)
    Gets the file information based on its id..
    protected String
    Get the query string for a given key
    void
     
    void
    insert(T document)
    Inserts the document into the collection.
    void
    insert(T... documents)
    Inserts multiple documents into the collection.
     
    readFile(String storeName)
    Returns the InputStream of the file with the given name.
    readFile(org.bson.types.ObjectId fileId)
    Returns the InputStream of the file with the given id.
    void
    remove(String query)
    Removes all Documents that are found using the given query.
    void
    remove(String query, Object... queryParams)
    Removes all Documents that are found using the given query.
    void
    Removes a Document with the given id
    void
    Removes by the Id.
    protected Iterable<T>
    returnList(org.jongo.Find find)
    Actually makes the transformation form Jongo to List of Objects.
    protected T
    returnSimple(org.jongo.FindOne findOne)
    Actually makes the transformation form Jongo to the Object.
    void
    save(String query, Object... queryParams)
    Inserts in the given collection the json "As Is"
    Json String can contain placeholders this will allow the user to have predefine json strings
    that will be substitute with the given queryParams.Params are not Name therefor if the same value is needed multiple times for now it has to be send multiple times.Order of the queryParams should match the same in the json string.
    Example
    String Json ="{name:#,address: #,age:#}"
    save("testCollection",Json,"Dr.John Z.", new Address(), 125);
    void
    save(T document)
    Saves the document into the collection.
    saveFile(InputStream inputStream, String storeName, String contentType)
    Saves the given InputStream as with the given name.
    saveFile(InputStream inputStream, String storeName, String contentType, org.bson.types.ObjectId fileId)
    Saves the given InputStream as with the given name.
    void
    setJongo(org.jongo.Jongo jongo)
     
    void
     
    void
    update(String id, Object updateObject)
    Updates the object with the given id with the given Object information.
    void
    update(String id, Object updateObject, boolean multi, boolean upsert)
    Updates the object with the given id with the given Object information.
    void
    update(String id, String modifier, boolean multi, boolean upsert)
    Updates the given
    void
    update(String id, String modifier, boolean multi, boolean upsert, Object... params)
    Updates the given
    updateFile(InputStream inputStream, String storeName, String contentType)
    "Updates" the file with the new information (A name change is NOT valid )
    updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType)
    "Updates" the file with the new information (A name change is valid as long a file with new name does not exists)
    updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType, boolean sameFileId)
    "Updates" the file with the new information (A name change is valid as long a file with new name does not exists)
    protected com.mongodb.gridfs.GridFSDBFile
     
    protected com.mongodb.gridfs.GridFSDBFile
    validateObject(org.bson.types.ObjectId fileId)
     

    Methods inherited from class java.lang.Object

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

    • clazz

      protected Class<? extends T> clazz
    • jongo

      protected org.jongo.Jongo jongo
    • collectionName

      protected String collectionName
    • queries

      protected JongoQueries queries
    • gridfs

      protected com.mongodb.gridfs.GridFS gridfs
  • Constructor Details

    • AbstractJongoRepository

      public AbstractJongoRepository()
  • Method Details

    • init

      public void init() throws Exception
      Throws:
      Exception
    • insert

      public void insert(T document) throws MongoDataException
      Description copied from interface: CrudRepository
      Inserts the document into the collection.
      Specified by:
      insert in interface CrudRepository<T>
      Parameters:
      document - document to be inserted (aka Pojo).
      Throws:
      MongoDataException - if the document can't be inserted.
    • getCollection

      protected org.jongo.MongoCollection getCollection()
      Gets the Jongo Document.
      Returns:
      a Jongo Document to interact with the Mongo.
    • insert

      public void insert(T... documents) throws MongoDataException
      Description copied from interface: CrudRepository
      Inserts multiple documents into the collection.
      Specified by:
      insert in interface CrudRepository<T>
      Parameters:
      documents - documents to be inserted (aka Pojo).
      Throws:
      MongoDataException - if the documents can't be inserted.
    • save

      public void save(T document) throws MongoDataException
      Description copied from interface: CrudRepository
      Saves the document into the collection.
      Specified by:
      save in interface CrudRepository<T>
      Parameters:
      document - Document to be save (aka Pojo).
      Throws:
      MongoDataException - if Document can't be save.
    • save

      public void save(String query, Object... queryParams) throws MongoDataException
      Description copied from interface: CrudRepository
      Inserts in the given collection the json "As Is"
      Json String can contain placeholders this will allow the user to have predefine json strings
      that will be substitute with the given queryParams.Params are not Name therefor if the same value is needed multiple times for now it has to be send multiple times.Order of the queryParams should match the same in the json string.
      Example
      String Json ="{name:#,address: #,age:#}"
      save("testCollection",Json,"Dr.John Z.", new Address(), 125);
      Specified by:
      save in interface CrudRepository<T>
      Parameters:
      query - Name of the Query to be look in default-queries.xml or custom-queries.properties
      queryParams - Params of the json.
      Throws:
      MongoDataException
    • update

      public void update(String id, Object updateObject, boolean multi, boolean upsert) throws MongoDataException
      Description copied from interface: CrudRepository
      Updates the object with the given id with the given Object information.
      Specified by:
      update in interface CrudRepository<T>
      Parameters:
      id - Id of the object to be updated.
      updateObject - Object to be use to updated.
      multi - If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
      upsert - If set to true, creates a new document when no document matches the query criteria.
      Throws:
      MongoDataException - if document can't be save.
    • update

      public void update(String id, Object updateObject) throws MongoDataException
      Description copied from interface: CrudRepository

      Updates the object with the given id with the given Object information.

      Should be equals to CrudRepository.update(String, Object, boolean, boolean) with String,Object,false,false

      Specified by:
      update in interface CrudRepository<T>
      Parameters:
      id - Id of the object to be updated.
      updateObject - Object to be use to updated.
      Throws:
      MongoDataException - if document can't be save.
    • update

      public void update(String id, String modifier, boolean multi, boolean upsert) throws MongoDataException
      Description copied from interface: CrudRepository
      Updates the given
      Specified by:
      update in interface CrudRepository<T>
      Parameters:
      id - Id of the object to be updated.
      modifier - The modifications to apply.
      multi - If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
      upsert - If set to true, creates a new document when no document matches the query criteria.
      Throws:
      MongoDataException - if document can't be save.
    • update

      public void update(String id, String modifier, boolean multi, boolean upsert, Object... params) throws MongoDataException
      Description copied from interface: CrudRepository
      Updates the given
      Specified by:
      update in interface CrudRepository<T>
      Parameters:
      id - Id of the object to be updated.
      modifier - The modifications to apply.
      multi - If set to true, updates multiple documents that meet the query criteria. If set to false, updates one document.
      upsert - If set to true, creates a new document when no document matches the query criteria.
      params - Params of the modifier query.
      Throws:
      MongoDataException - if document can't be save.
    • count

      public long count() throws MongoDataException
      Description copied from interface: CrudRepository
      Returns the number of all documents in the collection.
      Specified by:
      count in interface CrudRepository<T>
      Returns:
      the count of all documents
      Throws:
      MongoDataException - if an error occurs
    • count

      public long count(String query) throws MongoDataException
      Description copied from interface: CrudRepository
      Returns the number of documents that match the query
      Specified by:
      count in interface CrudRepository<T>
      Parameters:
      query - the query the documents should match
      Returns:
      the count of documents that match the query
      Throws:
      MongoDataException - if an error occurs
    • count

      public long count(String query, Object... queryParams) throws MongoDataException
      Description copied from interface: CrudRepository
      Returns the number of documents that match the query
      Specified by:
      count in interface CrudRepository<T>
      Parameters:
      query - the query the documents should match
      queryParams - the query parameters
      Returns:
      the count of documents that match the query
      Throws:
      MongoDataException - if an error occurs
    • findAll

      public Iterable<T> findAll() throws MongoDataException
      Description copied from interface: CrudRepository
      Gets all documents of a the given collection. Tries to convert them in to Instances of the given class. Order of the params should match the same in the json string.
      Example
      String Json ="{name:#,address: #,age:#}"
      find("testCollection",Json,"Dr. John.Z", new Address(), 125);
      Specified by:
      findAll in interface CrudRepository<T>
      Returns:
      A Iterable Instance of all the documents.This is lazy loaded
      Throws:
      MongoDataException - If couldn't search for the documents. or a mapping exception happen.
    • find

      public Iterable<T> find(String query) throws MongoDataException
      Description copied from interface: CrudRepository
      Search all documents in the given collection that match the query.
      Specified by:
      find in interface CrudRepository<T>
      Parameters:
      query - Name of the Query to be look in default-queries.xml or custom-queries.properties
      Returns:
      A Iterable Instance of all the documents that match the query.This is lazy loaded
      Throws:
      MongoDataException
    • find

      public Iterable<T> find(String query, Object... queryParams) throws MongoDataException
      Description copied from interface: CrudRepository
      Finds all documents of the given collection that match the template query
      §§
      Specified by:
      find in interface CrudRepository<T>
      Parameters:
      query - Name of the Template Query to be look in default-queries.xml or custom-queries .properties
      queryParams - Params to be use in the template query. Must match the order of the templates .
      Template queryParams are not named, therefor they have to be send multiple times if needed
      Returns:
      A Iterable Instance of all the documents.This is lazy loaded
      Throws:
      MongoDataException - If couldn't search for the documents. or a mapping exception happen.
    • findOne

      public T findOne(String query) throws MongoDataException
      Description copied from interface: CrudRepository
      Search for documents of the given collection that match the query.
      In only return the first Document
      Specified by:
      findOne in interface CrudRepository<T>
      Parameters:
      query - Name of the Template Query to be look in default-queries.xml or custom-queries .properties
      Returns:
      A instance of the given class. Null if nothing is found.
      Throws:
      MongoDataException - If couldn't search for the documents. or a mapping exception happen.
    • findOne

      public T findOne(String query, Object... queryParams) throws MongoDataException
      Description copied from interface: CrudRepository
      Search for documents of the given collection that match the query.
      In only return the first Document
      Specified by:
      findOne in interface CrudRepository<T>
      Parameters:
      query - Name of the Template Query to be look in default-queries.xml or custom-queries .properties
      queryParams - Params to be use in the template query. Must match the order of the templates .
      Template queryParams are not named, therefor they have to be send multiple times if needed
      Returns:
      A instance of the given class. Null if nothing is found.
      Throws:
      MongoDataException - If couldn't search for the documents. or a mapping exception happen.
    • remove

      public void remove(String query, Object... queryParams) throws MongoDataException
      Description copied from interface: CrudRepository
      Removes all Documents that are found using the given query.
      Specified by:
      remove in interface CrudRepository<T>
      Parameters:
      query - Name of the Template Query to be look in default-queries.xml or custom-queries .properties
      queryParams - Params to be use in the template query. Must match the order of the templates
      Throws:
      MongoDataException
    • findById

      public T findById(String id) throws MongoDataException
      Description copied from interface: CrudRepository

      Finds by the Id.

      Internal transforms the given String to and Object Id

      Specified by:
      findById in interface CrudRepository<T>
      Parameters:
      id - String representation of the Id.
      Returns:
      A instance of the object with the given Id.
      Throws:
      MongoDataException
    • remove

      public void remove(String query) throws MongoDataException
      Description copied from interface: CrudRepository
      Removes all Documents that are found using the given query.
      Specified by:
      remove in interface CrudRepository<T>
      Parameters:
      query - Name of the Template Query to be look in default-queries.xml or custom-queries .properties
      Throws:
      MongoDataException
    • removeById

      public void removeById(String id) throws MongoDataException
      Description copied from interface: CrudRepository
      Removes a Document with the given id
      Specified by:
      removeById in interface CrudRepository<T>
      Parameters:
      id - Id of the object to be remove
      Throws:
      MongoDataException
    • saveFile

      public FileInfo saveFile(InputStream inputStream, String storeName, String contentType, org.bson.types.ObjectId fileId) throws MongoDataException, org.apache.commons.io.FileExistsException
      Description copied from interface: CrudRepository
      Saves the given InputStream as with the given name. Closes the Stream after its done.
      Specified by:
      saveFile in interface CrudRepository<T>
      Parameters:
      inputStream - InputStream to be Save.
      storeName - File name for the inputStream.
      Returns:
      FileInfo with all the information of the file.
      Throws:
      MongoDataException - If Can't save the file.
      org.apache.commons.io.FileExistsException - If a file with the given file name already exists.
    • saveFile

      public FileInfo saveFile(InputStream inputStream, String storeName, String contentType) throws MongoDataException, org.apache.commons.io.FileExistsException
      Description copied from interface: CrudRepository
      Saves the given InputStream as with the given name. Closes the Stream after its done.
      Specified by:
      saveFile in interface CrudRepository<T>
      Parameters:
      inputStream - InputStream to be Save.
      storeName - File name for the inputStream.
      Returns:
      FileInfo with all the information of the file.
      Throws:
      MongoDataException - If Can't save the file.
      org.apache.commons.io.FileExistsException - If a file with the given file name already exists.
    • getFileInfo

      public FileInfo getFileInfo(org.bson.types.ObjectId fileId) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Gets the file information based on its id..
      Specified by:
      getFileInfo in interface CrudRepository<T>
      Parameters:
      fileId - file Id to look up the information.
      Returns:
      File Information of the file.
      Throws:
      FileNotFoundException - If file with the given id does not exist.
    • getFileInfo

      public FileInfo getFileInfo(String storeName) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Gets the file information based on its name..
      Specified by:
      getFileInfo in interface CrudRepository<T>
      Parameters:
      storeName - file name to look up the information.
      Returns:
      File Information of the file.
      Throws:
      FileNotFoundException - If file with the given id does not exist.
    • readFile

      public FileInfo readFile(org.bson.types.ObjectId fileId) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Returns the InputStream of the file with the given id.
      Specified by:
      readFile in interface CrudRepository<T>
      Parameters:
      fileId - File Id to read.
      Returns:
      A InputStream with that file Information.
      Throws:
      FileNotFoundException - If there is no file with that Id.
    • readFile

      public FileInfo readFile(String storeName) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Returns the InputStream of the file with the given name.
      Specified by:
      readFile in interface CrudRepository<T>
      Parameters:
      storeName - File Id to read.
      Returns:
      A InputStream with that file Information.
      Throws:
      FileNotFoundException - If there is no file with that name.
    • deleteFile

      public void deleteFile(org.bson.types.ObjectId fileId) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Deletes the File with the given Id.
      Specified by:
      deleteFile in interface CrudRepository<T>
      Parameters:
      fileId - Id of the file to delete.
      Throws:
      FileNotFoundException - If there is no file with that id.
    • deleteFile

      public void deleteFile(String storeName) throws FileNotFoundException
      Description copied from interface: CrudRepository
      Deletes the File with the given name.
      Specified by:
      deleteFile in interface CrudRepository<T>
      Parameters:
      storeName - Name of the file to delete.
      Throws:
      FileNotFoundException - If there is no file with that name.
    • updateFile

      public FileInfo updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
      Description copied from interface: CrudRepository

      "Updates" the file with the new information (A name change is valid as long a file with new name does not exists)

      Mongodb Does not actually support any update Operation in GridFs therefor Calling this method should be as calling CrudRepository.deleteFile(org.bson.types.ObjectId) and then CrudRepository.saveFile(java.io.InputStream, String,String) It will generate also new FileInfo Including It's Id

      Specified by:
      updateFile in interface CrudRepository<T>
      Parameters:
      fileId - File id to be Updated
      inputStream - new InputStream of the file.
      storeName - File name of the inputStream (can differ from the original).
      Returns:
      The new FileInfo of the "Updated" file
      Throws:
      FileNotFoundException - If File with Given Id Does not exists.
      MongoDataException - If unable to save the File.
      org.apache.commons.io.FileExistsException - If a file name exists with the new storeName (this should Only happen if you change the file name)
    • updateFile

      public FileInfo updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType, boolean sameFileId) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
      Description copied from interface: CrudRepository

      "Updates" the file with the new information (A name change is valid as long a file with new name does not exists)

      Mongodb Does not actually support any update Operation in GridFs therefor Calling this method should be as calling CrudRepository.deleteFile(org.bson.types.ObjectId) and then CrudRepository.saveFile(java.io.InputStream, String,String) It will generate also new FileInfo Including It's Id

      Specified by:
      updateFile in interface CrudRepository<T>
      Parameters:
      fileId - File id to be Updated
      inputStream - new InputStream of the file.
      storeName - File name of the inputStream (can differ from the original).
      Returns:
      The new FileInfo of the "Updated" file
      Throws:
      FileNotFoundException - If File with Given Id Does not exists.
      MongoDataException - If unable to save the File.
      org.apache.commons.io.FileExistsException - If a file name exists with the new storeName (this should Only happen if you change the file name)
    • updateFile

      public FileInfo updateFile(InputStream inputStream, String storeName, String contentType) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
      Description copied from interface: CrudRepository

      "Updates" the file with the new information (A name change is NOT valid )

      Mongodb Does not actually support any update Operation in GridFs therefor Calling this method should be as calling CrudRepository.deleteFile(org.bson.types.ObjectId) and then CrudRepository.saveFile(java.io.InputStream, String,String) It will generate also new FileInfo Including It's Id

      Specified by:
      updateFile in interface CrudRepository<T>
      Parameters:
      inputStream - new InputStream of the file.
      storeName - File id to be Updated. (Have to the the same as the original of not it will throw a FileNotFoundException)
      Returns:
      The new FileInfo of the "Updated" file
      Throws:
      FileNotFoundException - If File with Given Id Does not exists.
      MongoDataException - If unable to save the File.
      org.apache.commons.io.FileExistsException - this exception is highly unlikely to happen but posible.
    • findByStringId

      public T findByStringId(String id) throws MongoDataException
      Description copied from interface: CrudRepository

      Finds by the Id.

      No internal Modification is done, uses Mongodb default '_id' field name

      Specified by:
      findByStringId in interface CrudRepository<T>
      Parameters:
      id - String representation of the Id.
      Returns:
      A instance of the object with the given Id.
      Throws:
      MongoDataException
    • listFilesByName

      public List<FileInfo> listFilesByName(String filename)
      Specified by:
      listFilesByName in interface CrudRepository<T>
    • removeByStringId

      public void removeByStringId(String id) throws MongoDataException
      Description copied from interface: CrudRepository

      Removes by the Id.

      No internal Modification is done, uses Mongodb default '_id' field name

      Specified by:
      removeByStringId in interface CrudRepository<T>
      Parameters:
      id - String representation of the Id.
      Throws:
      MongoDataException
    • validateObject

      protected com.mongodb.gridfs.GridFSDBFile validateObject(String storeName) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • validateObject

      protected com.mongodb.gridfs.GridFSDBFile validateObject(org.bson.types.ObjectId fileId) throws FileNotFoundException
      Throws:
      FileNotFoundException
    • returnSimple

      protected T returnSimple(org.jongo.FindOne findOne)
      Actually makes the transformation form Jongo to the Object.
      Parameters:
      findOne - Find object to transform.
      Returns:
      a Object with the results null if nothing is found.
    • returnList

      protected Iterable<T> returnList(org.jongo.Find find)
      Actually makes the transformation form Jongo to List of Objects.
      Parameters:
      find - Find object to transform.
      Returns:
      a Iterable with the results null if nothing is found.
    • setJongo

      public void setJongo(org.jongo.Jongo jongo)
    • setQueries

      public void setQueries(JongoQueries queries)
    • getQueryFor

      protected String getQueryFor(String key)
      Get the query string for a given key
      Parameters:
      key - Key of the query,
      Returns:
      Query with the given key. IllegalArgumentException if query does not exist
      Throws:
      IllegalArgumentException - if q
    • createSortQuery

      protected String createSortQuery(List<org.apache.commons.collections4.keyvalue.DefaultKeyValue<String,Boolean>> fields)
      Creates a Sort query based on the fields.
      Key of the map is the field False=Desc,True=asc for the field, Respect order of the keys
      Parameters:
      fields - Keys are fields, true if asc, false desc
      Returns:
      Sorted Json string with the query.