PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/dsbw/mongo/MongoDAO.scala

https://bitbucket.org/fludwig/dsbw
Scala | 78 lines | 49 code | 18 blank | 11 comment | 2 complexity | 1f8358637075a064f85f90c0b5ed0add MD5 | raw file
  1. package dsbw.mongo
  2. import com.mongodb.casbah.MongoCollection
  3. import com.novus.salat.dao.SalatDAO
  4. import org.bson.types.ObjectId
  5. import com.mongodb.casbah.Imports._
  6. import com.mongodb.MongoException.DuplicateKey
  7. import com.novus.salat.Context
  8. import com.mongodb.casbah.commons.MongoDBObject
  9. import dsbw.mongo.SalatContext.ctx
  10. /** Generic DAO class used as a superclass of MongoDB DAOs */
  11. class MongoDao[ObjectType <: AnyRef](collection: MongoCollection)(implicit mot: Manifest[ObjectType]) {
  12. val salatDao = new SalatDAO[ObjectType,ObjectId](collection){}
  13. /** Check the last operation performend and, in case of error, throw an exception */
  14. def checkLastError() {
  15. val lastError = collection.getLastError()
  16. try{
  17. lastError.throwOnError()
  18. }catch{
  19. case m:DuplicateKey => {
  20. val errorMsg = lastError.getString("err")
  21. val index = collection.indexInfo.find(ii => (ii.get("unique") == true) && errorMsg.contains("%s.$%s".format(ii.get("ns"),ii.get("name"))))
  22. index.map(ii => throw new DuplicateKeyException(collection.name,ii.get("name").asInstanceOf[String]))
  23. throw m
  24. }
  25. case t:Throwable => throw t
  26. }
  27. }
  28. /** Find all documents that match a specific query*/
  29. def findByQuery[T<: AnyRef](query: Map[String, T]) = salatDao.find(query)
  30. /** Find a document using a query and return it or None if it wasn't found */
  31. def findOne[T<: AnyRef](query:Map[String,T]): Option[ObjectType] = salatDao.findOne(query)
  32. /** Find a document by id and return it or None if it wasn't found */
  33. def findOneByID(id:ObjectId): Option[ObjectType] = salatDao.findOneById(id)
  34. /** Find a collection of documents given their ids */
  35. def findByIds(ids:Set[ObjectId]) = salatDao.find("_id" $in ids).toSet
  36. /** Find all the documents in a collection */
  37. def findAll = salatDao.find((MongoDBObject.empty))
  38. /** Update the documents matching a query with the given update */
  39. def update[T<: AnyRef](query:Map[String,T],update:MongoDBObject,multi:Boolean=true) {
  40. salatDao.update(query,update,multi=multi)
  41. }
  42. /** Update a document identified by its id with the given update */
  43. def updateById(id:ObjectId,update:MongoDBObject){
  44. this.update(Map("_id"->id),update,multi=false)
  45. }
  46. /** Updatea collection of documents identified by their ids with the given update */
  47. def updateByIds(ids:Set[ObjectId],update:MongoDBObject){
  48. salatDao.update("_id" $in ids,update,multi=true)
  49. }
  50. /** Save a document */
  51. def save(obj:ObjectType) {
  52. salatDao.save(obj)
  53. }
  54. def insert(obj:ObjectType) : Option[ObjectId] = {
  55. return salatDao.insert(obj)
  56. }
  57. }
  58. case class DuplicateKeyException(collection:String, key:String) extends RuntimeException{
  59. override def toString = """DuplicateKeyException(collection="%s",key="%s")""".format(collection,key)
  60. }