PageRenderTime 151ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/pbc/model/TemplatedDao.scala

https://github.com/nexus49/invsys
Scala | 99 lines | 83 code | 13 blank | 3 comment | 9 complexity | 537a9205c6ec395750121f3f364d721a MD5 | raw file
  1. package pbc.model
  2. import com.mongodb.casbah.{MongoCollection, MongoCursorBase}
  3. import com.mongodb.casbah.Imports._
  4. import com.mongodb.DBObject
  5. import scala.collection.mutable
  6. import pbc.db.CollectionFactory
  7. import net.liftweb.common.Loggable
  8. // Adds dao functionality to a object
  9. trait TemplatedDao extends Loggable {
  10. // Factory method to create a new instance
  11. def fac(dbObject: DBObject, template: Template): Templated
  12. def findAll(): List[Templated] = {
  13. val templates = Template.findAll
  14. val values = new mutable.ListBuffer[Templated]
  15. templates.foreach(t => {
  16. logger.info(t.name)
  17. val collection = CollectionFactory.getCollection(t.collectionName)
  18. for (result <- collection.find) {
  19. val item = fac(result, t)
  20. values += (item)
  21. }
  22. })
  23. return values.toList
  24. }
  25. def findAll(template: Template): List[Templated] = {
  26. val collection = CollectionFactory.getCollection(template.collectionName)
  27. val values = new mutable.ListBuffer[Templated]
  28. val query = MongoDBObject.apply(CollectionFactory.templateIdColumn -> template.id)
  29. val rslt = collection.find(query)
  30. while(rslt.hasNext)
  31. {
  32. val item = fac(rslt.next, template)
  33. values += (item)
  34. }
  35. return values.toList
  36. }
  37. def findFirstById[T](id: ObjectId, template: Template): Templated = {
  38. val collection = CollectionFactory.getCollection(template.collectionName)
  39. val result = collection.findOneByID(id)
  40. if (!result.isEmpty) {
  41. val item = fac(result.get, template)
  42. return item
  43. }
  44. else {
  45. return null
  46. }
  47. }
  48. def save(item: Templated) {
  49. val collection = CollectionFactory.getCollection(item.template.collectionName)
  50. val mongoItem = toDBObject(item)
  51. if (item.id == null) {
  52. val result = collection.save(mongoItem)
  53. item.id = mongoItem._id.get
  54. logger.info("inserted Inventory " + result)
  55. } else {
  56. val result = collection.update(MongoDBObject.apply(CollectionFactory.idColumn -> item.id), mongoItem)
  57. logger.info("updated Inventory " + result)
  58. }
  59. collection += mongoItem
  60. }
  61. // Creates a DBOBject from the template
  62. private def toDBObject(item: Templated): DBObject = {
  63. val builder = MongoDBObject.newBuilder
  64. val values = item.valueMap
  65. values foreach {
  66. case (key, value) =>
  67. if (value.isInstanceOf[Templated]) {
  68. builder += key -> toDBObject(value.asInstanceOf[Templated])
  69. }
  70. else {
  71. builder += key -> value
  72. }
  73. }
  74. builder += CollectionFactory.templateIdColumn -> item.template.id
  75. builder.result.asDBObject
  76. }
  77. def values(dbObject: DBObject): Map[String, Object] = {
  78. val valueMap = mutable.Map.empty[String, Object]
  79. val itr = dbObject.keySet.iterator
  80. while (itr.hasNext) {
  81. val key: String = itr.next
  82. valueMap += (key -> dbObject(key))
  83. }
  84. return valueMap.toMap
  85. }
  86. }