PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/code/model/Reminder.scala

https://bitbucket.org/ta_jim/lift_userlogin_template
Scala | 120 lines | 94 code | 14 blank | 12 comment | 12 complexity | 867af5017d976e0f135c3de6634b3661 MD5 | raw file
  1. package code.model
  2. import java.text.SimpleDateFormat
  3. import java.util.Date
  4. import net.liftweb.common.Loggable
  5. import net.liftweb.common.Loggable
  6. import net.liftweb.json.JsonAST._
  7. import net.liftweb.json.JsonDSL._
  8. import net.liftweb.mongodb._
  9. import net.liftweb.mongodb.record._
  10. import net.liftweb.mongodb.record.field._
  11. import net.liftweb.record.field._
  12. import net.liftweb.util.Helpers._
  13. import org.bson.types.ObjectId
  14. import java.text.ParsePosition
  15. import com.mongodb.casbah.commons.MongoDBObject
  16. class Reminder extends MongoRecord[Reminder] with ObjectIdPk[Reminder] with Loggable {
  17. def meta = Reminder
  18. object owner extends ObjectIdRefField(this, User)
  19. object friend_name extends StringField(this, 1200)
  20. object created extends DateField(this)
  21. object dob extends DateField(this)
  22. }
  23. object Reminder extends Reminder with MongoMetaRecord[Reminder] {
  24. /**
  25. * For creating Friend's Birthday Reminder.
  26. */
  27. def createReminder(id: String, friend_name: String, dob: String): Either[String, Boolean] = {
  28. if (friend_name.equals(""))
  29. Left("Enter Friend's Name")
  30. else if (dob.equals(""))
  31. Left("Select Date")
  32. else {
  33. try {
  34. val df = new SimpleDateFormat("MM/dd/yyyy");
  35. df.setLenient(false);
  36. val position = new ParsePosition(0);
  37. val stringToDate = df.parse(dob, position);
  38. if (position.getIndex() == dob.length()) {
  39. val reminder = Reminder.createRecord
  40. reminder.created(new Date())
  41. reminder.dob(stringToDate)
  42. reminder.owner(new ObjectId(id))
  43. reminder.friend_name(friend_name)
  44. reminder.save
  45. Right(true)
  46. } else
  47. Left("Enter date in MM/dd/yyyy format")
  48. } catch {
  49. case ex: Throwable => {
  50. Left("Enter date in MM/dd/yyyy format")
  51. }
  52. }
  53. }
  54. }
  55. def findAllNotes(userId: String) = Reminder.findAll((("owner" -> userId)))
  56. def findAllNotes(userId: String, sortOrder: Int, limit: Int, skip: Int) =
  57. {
  58. sortOrder match {
  59. case 0 => Reminder.findAll((("owner" -> userId)), ("created" -> -1), Skip(skip), Limit(limit))
  60. case _ => Reminder.findAll((("owner" -> userId)), ("dob" -> sortOrder), Skip(skip), Limit(limit))
  61. }
  62. }
  63. /**
  64. * For deleting reminders of a user.
  65. */
  66. def deleteReminder(reminder: Reminder) = {
  67. Reminder.getReminder(reminder).delete_!
  68. }
  69. /**
  70. * For getting reminders reference of a user.
  71. */
  72. def getReminder(reminder: Reminder) = {
  73. Reminder.find(reminder.id.toString).get
  74. }
  75. /**
  76. * For updating status of particular getReminder of a user.
  77. */
  78. def updateReminder(reminder: Reminder, friend_name: String, date: String): Either[String, Boolean] = {
  79. if (friend_name.equals(""))
  80. Left("Can not updated. Enter Friend's Name")
  81. else if (date.equals(""))
  82. Left("Can not updated. Select Date")
  83. else {
  84. try {
  85. val df = new SimpleDateFormat("MM/dd/yyyy");
  86. df.setLenient(false);
  87. val position = new ParsePosition(0);
  88. val stringToDate = df.parse(date, position);
  89. if (position.getIndex() == date.length()) {
  90. reminder.friend_name(friend_name).dob(stringToDate).update
  91. Right(true)
  92. } else
  93. Left("Enter date in MM/dd/yyyy format")
  94. } catch {
  95. case ex : Throwable => {
  96. Left("Enter date in MM/dd/yyyy format")
  97. }
  98. }
  99. }
  100. }
  101. def getReminderForToday(userID: String) = {
  102. val start_date = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate())
  103. val end_date = new Date(new Date().getYear(), new Date().getMonth(), new Date().getDate() + 1)
  104. Reminder.findAll(MongoDBObject("dob" -> MongoDBObject("$gte" -> start_date, "$lt" -> end_date), "owner" -> new ObjectId(userID)))
  105. }
  106. }