PageRenderTime 244ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/Dann/src/com/minosiants/dann/repository/MongoRepository.scala

http://tratata.googlecode.com/
Scala | 42 lines | 37 code | 3 blank | 2 comment | 0 complexity | b0b1a92f9d2f51aa68e85678e82acfe9 MD5 | raw file
  1. package com.minosiants.dann.repository
  2. import com.mongodb.casbah.MongoConnection
  3. import com.mongodb.util.JSON
  4. import com.minosiants.dann.repository.Gson._
  5. import com.minosiants.dann.data.BasicData
  6. import scala.collection.JavaConverters._
  7. import com.mongodb.casbah.commons.conversions.scala._
  8. import org.scala_tools.time.Imports._
  9. import com.mongodb.DBObject
  10. import com.mongodb.casbah.commons.MongoDBObject; import com.mongodb.casbah.Imports._; object MongoDataSource {
  11. //val mongodbConfigPrefix = "akka.remote.server.server.client.storage.mongodb"
  12. val hostName="127.0.0.1" //config.getString(mongodbConfigPrefix+".hostname", "127.0.0.1")
  13. val dataBaseName = "dann" //config.getString(mongodbConfigPrefix+".dbname", "statistics")
  14. val port =27017; //config.getInt(mongodbConfigPrefix+".port", 27017) lazy val mongo= MongoConnection(hostName,port)
  15. def getDb()={
  16. mongo(dataBaseName)
  17. }
  18. }
  19. class MongoRepository() extends Repository{
  20. def findOne[T](qr:Query[T]):Option[BasicData]={
  21. collection(qr.collectionName).findOne(qr.queryObject).map(create(_,qr.clazz))
  22. };
  23. def find[T](qr:Query[T]):List[BasicData]={
  24. collection(qr.collectionName).find(qr.queryObject).map(create(_,qr.clazz)).toList
  25. }; private def create[T](o:DBObject,clazz:Class[T]):BasicData={ Gson().fromJson(toJson(o),clazz) }
  26. def save(o:BasicData){
  27. val r=collection(o.collectionName).save(createDBObject(o))
  28. }
  29. def delete(o:BasicData){
  30. collection(o.collectionName).remove(createDBObject(o))
  31. }
  32. def delete(id:String){
  33. //collection(collectionName(o.getClass)).remove()
  34. }
  35. def drop(collectionName:String){ collection(collectionName).drop }
  36. private def collection(name:String)={
  37. MongoDataSource.getDb()(name)
  38. }; private def createDBObject(o:BasicData)={ JSON.parse(toJson(o)).asInstanceOf[DBObject]; }
  39. }