public abstract class AbstractJongoRepository<T> extends Object implements CrudRepository<T>
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.
| Modifier and Type | Field and Description |
|---|---|
protected Class<? extends T> |
clazz |
protected String |
collectionName |
protected com.mongodb.gridfs.GridFS |
gridfs |
protected org.jongo.Jongo |
jongo |
protected JongoQueries |
queries |
| Constructor and Description |
|---|
AbstractJongoRepository() |
| Modifier and Type | Method and Description |
|---|---|
long |
count()
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(org.bson.types.ObjectId fileId)
Deletes the File with the given Id.
|
void |
deleteFile(String storeName)
Deletes the File with the given name.
|
Iterable<T> |
find(String query)
Search all documents in the given collection that match the query.
|
Iterable<T> |
find(String query,
Object... queryParams)
Finds all documents of the given collection that match the template query
§§ |
Iterable<T> |
findAll()
Gets all documents of a the given collection.
|
T |
findById(String id)
Finds by the Id.
|
T |
findByStringId(String id)
Finds by the Id.
|
T |
findOne(String query)
Search for documents of the given collection that match the query.
In only return the first Document |
T |
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 |
getCollection()
Gets the Jongo Document.
|
FileInfo |
getFileInfo(org.bson.types.ObjectId fileId)
Gets the file information based on its id..
|
FileInfo |
getFileInfo(String storeName)
Gets the file information based on its name..
|
protected String |
getQueryFor(String key)
Get the query string for a given key
|
void |
init() |
void |
insert(T... documents)
Inserts multiple documents into the collection.
|
void |
insert(T document)
Inserts the document into the collection.
|
List<FileInfo> |
listFilesByName(String filename) |
FileInfo |
readFile(org.bson.types.ObjectId fileId)
Returns the InputStream of the file with the given id.
|
FileInfo |
readFile(String storeName)
Returns the InputStream of the file with the given name.
|
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 |
removeById(String id)
Removes a Document with the given id
|
void |
removeByStringId(String id)
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:#}" |
void |
save(T document)
Saves the document into the collection.
|
FileInfo |
saveFile(InputStream inputStream,
String storeName,
String contentType)
Saves the given InputStream as with the given name.
|
FileInfo |
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 |
setQueries(JongoQueries queries) |
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
|
FileInfo |
updateFile(InputStream inputStream,
String storeName,
String contentType)
"Updates" the file with the new information (A name change is NOT valid )
|
FileInfo |
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)
|
FileInfo |
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 |
validateObject(org.bson.types.ObjectId fileId) |
protected com.mongodb.gridfs.GridFSDBFile |
validateObject(String storeName) |
protected org.jongo.Jongo jongo
protected String collectionName
protected JongoQueries queries
protected com.mongodb.gridfs.GridFS gridfs
public void insert(T document) throws MongoDataException
CrudRepositoryinsert in interface CrudRepository<T>document - document to be inserted (aka Pojo).MongoDataException - if the document can't be inserted.protected org.jongo.MongoCollection getCollection()
public void insert(T... documents) throws MongoDataException
CrudRepositoryinsert in interface CrudRepository<T>documents - documents to be inserted (aka Pojo).MongoDataException - if the documents can't be inserted.public void save(T document) throws MongoDataException
CrudRepositorysave in interface CrudRepository<T>document - Document to be save (aka Pojo).MongoDataException - if Document can't be save.public void save(String query, Object... queryParams) throws MongoDataException
CrudRepository
String Json ="{name:#,address: #,age:#}"
save("testCollection",Json,"Dr.John Z.", new Address(), 125);
save in interface CrudRepository<T>query - Name of the Query to be look in default-queries.xml or custom-queries.propertiesqueryParams - Params of the json.MongoDataExceptionpublic void update(String id, Object updateObject, boolean multi, boolean upsert) throws MongoDataException
CrudRepositoryupdate in interface CrudRepository<T>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.MongoDataException - if document can't be save.public void update(String id, Object updateObject) throws MongoDataException
CrudRepositoryUpdates 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
update in interface CrudRepository<T>id - Id of the object to be updated.updateObject - Object to be use to updated.MongoDataException - if document can't be save.public void update(String id, String modifier, boolean multi, boolean upsert) throws MongoDataException
CrudRepositoryupdate in interface CrudRepository<T>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.MongoDataException - if document can't be save.public void update(String id, String modifier, boolean multi, boolean upsert, Object... params) throws MongoDataException
CrudRepositoryupdate in interface CrudRepository<T>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.MongoDataException - if document can't be save.public long count()
throws MongoDataException
CrudRepositorycount in interface CrudRepository<T>MongoDataException - if an error occurspublic long count(String query) throws MongoDataException
CrudRepositorycount in interface CrudRepository<T>query - the query the documents should matchMongoDataException - if an error occurspublic long count(String query, Object... queryParams) throws MongoDataException
CrudRepositorycount in interface CrudRepository<T>query - the query the documents should matchqueryParams - the query parametersMongoDataException - if an error occurspublic Iterable<T> findAll() throws MongoDataException
CrudRepository
String Json ="{name:#,address: #,age:#}"
find("testCollection",Json,"Dr. John.Z", new Address(), 125);
findAll in interface CrudRepository<T>MongoDataException - If couldn't search for the documents. or a mapping
exception happen.public Iterable<T> find(String query) throws MongoDataException
CrudRepositoryfind in interface CrudRepository<T>query - Name of the Query to be look in default-queries.xml or custom-queries.propertiesMongoDataExceptionpublic Iterable<T> find(String query, Object... queryParams) throws MongoDataException
CrudRepositoryfind in interface CrudRepository<T>query - Name of the Template Query to be look in default-queries.xml or custom-queries
.propertiesqueryParams - Params to be use in the template query. Must match the order of the templates
.MongoDataException - If couldn't search for the documents. or a mapping
exception happen.public T findOne(String query) throws MongoDataException
CrudRepositoryfindOne in interface CrudRepository<T>query - Name of the Template Query to be look in default-queries.xml or custom-queries
.propertiesMongoDataException - If couldn't search for the documents. or a mapping
exception happen.public T findOne(String query, Object... queryParams) throws MongoDataException
CrudRepositoryfindOne in interface CrudRepository<T>query - Name of the Template Query to be look in default-queries.xml or custom-queries
.propertiesqueryParams - Params to be use in the template query. Must match the order of the templates
.MongoDataException - If couldn't search for the documents. or a mapping
exception happen.public void remove(String query, Object... queryParams) throws MongoDataException
CrudRepositoryremove in interface CrudRepository<T>query - Name of the Template Query to be look in default-queries.xml or custom-queries
.propertiesqueryParams - Params to be use in the template query. Must match the order of the templatesMongoDataExceptionpublic T findById(String id) throws MongoDataException
CrudRepositoryFinds by the Id.
Internal transforms the given String to and Object Id
findById in interface CrudRepository<T>id - String representation of the Id.MongoDataExceptionpublic void remove(String query) throws MongoDataException
CrudRepositoryremove in interface CrudRepository<T>query - Name of the Template Query to be look in default-queries.xml or custom-queries
.propertiesMongoDataExceptionpublic void removeById(String id) throws MongoDataException
CrudRepositoryremoveById in interface CrudRepository<T>id - Id of the object to be removeMongoDataExceptionpublic FileInfo saveFile(InputStream inputStream, String storeName, String contentType, org.bson.types.ObjectId fileId) throws MongoDataException, org.apache.commons.io.FileExistsException
CrudRepositorysaveFile in interface CrudRepository<T>inputStream - InputStream to be Save.storeName - File name for the inputStream.MongoDataException - If Can't save the file.org.apache.commons.io.FileExistsException - If a file with the given file name already exists.public FileInfo saveFile(InputStream inputStream, String storeName, String contentType) throws MongoDataException, org.apache.commons.io.FileExistsException
CrudRepositorysaveFile in interface CrudRepository<T>inputStream - InputStream to be Save.storeName - File name for the inputStream.MongoDataException - If Can't save the file.org.apache.commons.io.FileExistsException - If a file with the given file name already exists.public FileInfo getFileInfo(org.bson.types.ObjectId fileId) throws FileNotFoundException
CrudRepositorygetFileInfo in interface CrudRepository<T>fileId - file Id to look up the information.FileNotFoundException - If file with the given id does not exist.public FileInfo getFileInfo(String storeName) throws FileNotFoundException
CrudRepositorygetFileInfo in interface CrudRepository<T>storeName - file name to look up the information.FileNotFoundException - If file with the given id does not exist.public FileInfo readFile(org.bson.types.ObjectId fileId) throws FileNotFoundException
CrudRepositoryreadFile in interface CrudRepository<T>fileId - File Id to read.FileNotFoundException - If there is no file with that Id.public FileInfo readFile(String storeName) throws FileNotFoundException
CrudRepositoryreadFile in interface CrudRepository<T>storeName - File Id to read.FileNotFoundException - If there is no file with that name.public void deleteFile(org.bson.types.ObjectId fileId)
throws FileNotFoundException
CrudRepositorydeleteFile in interface CrudRepository<T>fileId - Id of the file to delete.FileNotFoundException - If there is no file with that id.public void deleteFile(String storeName) throws FileNotFoundException
CrudRepositorydeleteFile in interface CrudRepository<T>storeName - Name of the file to delete.FileNotFoundException - If there is no file with that name.public FileInfo updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
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
updateFile in interface CrudRepository<T>fileId - File id to be UpdatedinputStream - new InputStream of the file.storeName - File name of the inputStream (can differ from the original).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)public FileInfo updateFile(org.bson.types.ObjectId fileId, InputStream inputStream, String storeName, String contentType, boolean sameFileId) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
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
updateFile in interface CrudRepository<T>fileId - File id to be UpdatedinputStream - new InputStream of the file.storeName - File name of the inputStream (can differ from the original).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)public FileInfo updateFile(InputStream inputStream, String storeName, String contentType) throws FileNotFoundException, MongoDataException, org.apache.commons.io.FileExistsException
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
updateFile in interface CrudRepository<T>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)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.public T findByStringId(String id) throws MongoDataException
CrudRepositoryFinds by the Id.
No internal Modification is done, uses Mongodb default '_id' field name
findByStringId in interface CrudRepository<T>id - String representation of the Id.MongoDataExceptionpublic List<FileInfo> listFilesByName(String filename)
listFilesByName in interface CrudRepository<T>public void removeByStringId(String id) throws MongoDataException
CrudRepositoryRemoves by the Id.
No internal Modification is done, uses Mongodb default '_id' field name
removeByStringId in interface CrudRepository<T>id - String representation of the Id.MongoDataExceptionprotected com.mongodb.gridfs.GridFSDBFile validateObject(String storeName) throws FileNotFoundException
FileNotFoundExceptionprotected com.mongodb.gridfs.GridFSDBFile validateObject(org.bson.types.ObjectId fileId)
throws FileNotFoundException
FileNotFoundExceptionprotected T returnSimple(org.jongo.FindOne findOne)
findOne - Find object to transform.protected Iterable<T> returnList(org.jongo.Find find)
find - Find object to transform.public void setJongo(org.jongo.Jongo jongo)
public void setQueries(JongoQueries queries)
protected String getQueryFor(String key)
key - Key of the query,IllegalArgumentException - if qprotected String createSortQuery(List<org.apache.commons.collections4.keyvalue.DefaultKeyValue<String,Boolean>> fields)
fields - Keys are fields, true if asc, false descCopyright © 2020 CrafterCMS. All rights reserved.