PageRenderTime 120ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/syndor-common/src/main/scala/syndor/model/Feed.scala

http://github.com/HendraWijaya/syndor
Scala | 92 lines | 72 code | 16 blank | 4 comment | 0 complexity | 0095ce3ea3642289893638a687fd3a8d MD5 | raw file
  1. package syndor.model
  2. import java.util.concurrent.atomic.AtomicInteger
  3. import scala.collection.JavaConverters._
  4. import org.bson.types.ObjectId
  5. import com.novus.salat._
  6. import com.novus.salat.global._
  7. import com.novus.salat.annotations._
  8. import com.novus.salat.dao.SalatDAO
  9. import com.mongodb.casbah.Imports._
  10. import com.sun.syndication.feed.synd.SyndFeed
  11. import com.sun.syndication.feed.synd.SyndEntry
  12. case class Feed(
  13. @Key("_id") id: ObjectId = new ObjectId,
  14. sourceId: ObjectId,
  15. url: String,
  16. title: String,
  17. description: String = "",
  18. etag: Option[String] = None,
  19. lastModified: Option[Long] = None,
  20. fetchStatus: FetchStatus = FetchStatus())
  21. object Feed extends SalatDAO[Feed, ObjectId](collection = MongoConfig.collection("feed")) {
  22. def grabForFetching(size: Int): Seq[Feed] = {
  23. // Seems like this is the simplest way now and the most stable
  24. // Although it might be slow.
  25. // TODO: Look for faster way
  26. for{
  27. i <- 1 to size
  28. feed <- grabForFetching
  29. } yield {
  30. feed
  31. }
  32. }
  33. def grabForFetching: Option[Feed] = {
  34. return collection.findAndModify(
  35. query = MongoDBObject("fetchStatus.fetching" -> false),
  36. fields = MongoDBObject(),
  37. sort = MongoDBObject(),
  38. remove = false,
  39. update = $set("fetchStatus.fetching" -> true, "fetchStatus.fetchStart" -> System.currentTimeMillis),
  40. returnNew = true,
  41. upsert = false) map {
  42. grater[Feed].asObject(_)
  43. }
  44. }
  45. def finishFetchingModified(feed: Feed, syndFeed: SyndFeed, etag: Option[String] = None, lastModified: Option[Long] = None) {
  46. processSyndEntries(feed, syndFeed.getEntries.asInstanceOf[java.util.List[SyndEntry]].asScala)
  47. Feed.save(feed.copy(
  48. title = syndFeed.getTitle,
  49. description = syndFeed.getDescription,
  50. etag = etag orElse feed.etag, // etag might be missing
  51. lastModified = lastModified orElse feed.lastModified, // lastModified might be missing
  52. fetchStatus = feed.fetchStatus.copy(success = true, fetching = false, fetchEnd = System.currentTimeMillis)
  53. ))
  54. }
  55. def finishFetchingNotModified(feed: Feed) {
  56. Feed.save(feed.copy(
  57. fetchStatus = feed.fetchStatus.copy(success = true, fetching = false, fetchEnd = System.currentTimeMillis)
  58. ))
  59. }
  60. def finishFetchingWithFailure(feed: Feed, message: String) {
  61. Feed.save(feed.copy(
  62. fetchStatus = feed.fetchStatus.copy(success = false, fetching = false, fetchEnd = System.currentTimeMillis, message = Option(message))
  63. ))
  64. }
  65. private def processSyndEntries(feed: Feed, entries: Seq[SyndEntry]) {
  66. entries.foreach { entry =>
  67. FeedItem.findOne(MongoDBObject("link" -> entry.getLink)) orElse {
  68. FeedItem.insert(FeedItem.make(feed, entry))
  69. }
  70. }
  71. }
  72. def reset() {
  73. // Resetting fetching status
  74. update(MongoDBObject("fetchStatus.fetching" -> true), $set("fetchStatus.fetching" -> false))
  75. }
  76. }