PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/FileFarmer-Core/src/main/scala/ch/filefarmer/repositories/ArchiveRepository.scala

https://github.com/kufi/Filefarmer
Scala | 64 lines | 53 code | 11 blank | 0 comment | 8 complexity | ca5baac2e73ec7425193af86a8e61e33 MD5 | raw file
  1. package ch.filefarmer.repositories
  2. import com.google.inject.Inject
  3. import ch.filefarmer.database.connection.IConnection
  4. import com.mongodb.casbah.commons.MongoDBObject
  5. import ch.filefarmer.poso._
  6. import com.mongodb.casbah.Implicits._
  7. import ch.filefarmer.logging.Logging
  8. import com.novus.salat._
  9. import com.novus.salat.global._
  10. class ArchiveRepository@Inject()(val conn: IConnection) extends IArchiveRepository with Logging {
  11. private val archiveConnection = conn.connection()("archives")
  12. def getArchive(identity:String) = {
  13. val search = MongoDBObject("identity" -> identity)
  14. archiveConnection.findOne(search) match {
  15. case Some(ret) => {
  16. Option(grater[Archive].asObject(ret))
  17. }
  18. case None => None
  19. }
  20. }
  21. def addArchive(archive:Archive) = {
  22. if(archive.identity == "") {
  23. throw new ArgumentInvalidException("no identity added")
  24. }
  25. if(archive.name == "") {
  26. throw new ArgumentInvalidException("no name added")
  27. }
  28. getArchive(archive.identity) match {
  29. case Some(a) => {
  30. logger.error("archive with identity \"" + archive.identity + "\" already exists")
  31. throw new DuplicateException("archive already exists")
  32. }
  33. case None => {
  34. val obj = grater[Archive].asDBObject(archive)
  35. archiveConnection += obj
  36. }
  37. }
  38. }
  39. def getArchiveTree(parent:String = ""): Option[ArchiveTree] = {
  40. var search = MongoDBObject("parentArchiveId" -> parent)
  41. val tree = new ArchiveTree()
  42. val results = archiveConnection.find(search)
  43. if(results.count != 0) {
  44. for(res <- results) {
  45. val archive = grater[Archive].asObject(res)
  46. tree.archives += archive -> getArchiveTree(archive.identity)
  47. }
  48. } else {
  49. return None
  50. }
  51. Option(tree)
  52. }
  53. }