/src/main/scala/rhyskeepence/loader/MongoContentItemStore.scala

https://github.com/rhyskeepence/guardian-travelbook · Scala · 89 lines · 74 code · 15 blank · 0 comment · 0 complexity · fab49198157061139b539f0fe509807b MD5 · raw file

  1. package rhyskeepence.loader
  2. import com.mongodb.casbah.commons.MongoDBObject
  3. import org.joda.time.DateTime
  4. import rhyskeepence.MongoStorage
  5. import com.mongodb.casbah.MongoCollection
  6. import com.mongodb.casbah.query.GeoCoords
  7. import com.mongodb.casbah.Imports._
  8. class MongoContentItemStore(mongoStorage: MongoStorage) {
  9. val initialDate = new DateTime(2008, 10, 1, 0, 0, 0, 0)
  10. def write(items: List[ContentItem]) {
  11. println("MongoWriter: Writing %d items to mongo..." format items.size)
  12. items foreach {
  13. write(_)
  14. }
  15. }
  16. def write(item: ContentItem) = {
  17. val contentItemBuilder = MongoDBObject.newBuilder
  18. contentItemBuilder += "_id" -> item.id
  19. contentItemBuilder += "headline" -> item.headline
  20. contentItemBuilder += "url" -> item.url
  21. contentItemBuilder += "trailText" -> item.trailText
  22. contentItemBuilder += "thumbnail" -> item.thumbnail
  23. contentItemBuilder += "lastModified" -> item.lastModified
  24. item.geolocation.foreach {
  25. loc =>
  26. val geolocation = MongoDBObject.newBuilder
  27. geolocation += "lat" -> loc.lat
  28. geolocation += "lon" -> loc.lon
  29. contentItemBuilder += "geolocation" -> geolocation.result
  30. }
  31. withContent {
  32. _ += contentItemBuilder.result
  33. }
  34. }
  35. def mostRecentLastModified = {
  36. withContent {
  37. _.find()
  38. .sort(MongoDBObject("lastModified" -> -1))
  39. .limit(1)
  40. .toList
  41. .headOption
  42. .map(_.get("lastModified").asInstanceOf[DateTime])
  43. .getOrElse(initialDate)
  44. }
  45. }
  46. def itemsNear(lat: Double, lon: Double, limit: Int, skip: Int) = {
  47. withContent {
  48. contentCollection =>
  49. contentCollection
  50. .find("geolocation" $near GeoCoords(lat, lon))
  51. .skip(skip)
  52. .limit(limit)
  53. .toList.map { toContentItem }
  54. }
  55. }
  56. private def toContentItem: (DBObject) => ContentItem = {
  57. dbObject =>
  58. ContentItem(
  59. dbObject.getAsOrElse[String]("_id", "unknown"),
  60. dbObject.getAsOrElse[String]("url", "unknown"),
  61. dbObject.getAsOrElse[String]("headline", "unknown"),
  62. dbObject.getAs[String]("trailText"),
  63. dbObject.getAs[String]("thumbnail"),
  64. Some(Geolocation(
  65. dbObject.getAs[BasicDBObject]("geolocation").get.getAsOrElse[Double]("lat", 0),
  66. dbObject.getAs[BasicDBObject]("geolocation").get.getAsOrElse[Double]("lon", 0)
  67. )),
  68. dbObject.getAsOrElse[DateTime]("lastModified", initialDate)
  69. )
  70. }
  71. private def withContent[T](doWithContent: MongoCollection => T) = {
  72. mongoStorage.withCollection("content")(doWithContent)
  73. }
  74. }