Advertisement
Guest User

Untitled

a guest
Jan 19th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. package models
  2.  
  3. import database.Database
  4. import com.orientechnologies.orient.core.exception.ORecordNotFoundException
  5. import org.mindrot.jbcrypt.BCrypt._
  6. import com.github.nscala_time.time.Imports._
  7. import gremlin.scala._
  8.  
  9. class User(var login: String, var email: String, var password: String) extends ActiveRecord
  10. {
  11. var notificationMail = email
  12. var phone = ""
  13. var timezone = DateTimeZone.UTC
  14. var fullname = ""
  15.  
  16. private var vertex: Option[ScalaVertex] = None
  17.  
  18. def this(login: String,
  19. email: String,
  20. password: String,
  21. notificationMail: String,
  22. phone: String,
  23. timezone: DateTimeZone,
  24. fullname: String) {
  25. this(login, email, password)
  26. this.notificationMail = notificationMail
  27. this.phone = phone
  28. this.timezone = timezone
  29. this.fullname = fullname
  30. }
  31.  
  32. private def this(vertex: ScalaVertex)
  33. {
  34. this(vertex.property(User.k_login).value,
  35. vertex.property(User.k_email).value,
  36. vertex.property(User.k_password).value,
  37. vertex.property(User.k_notificationMail).value,
  38. vertex.property(User.k_phone).value,
  39. DateTimeZone.forID(vertex.property(User.k_timezone).value),
  40. vertex.property(User.k_fullname).value)
  41. this.vertex = Some(vertex)
  42. }
  43.  
  44. private def getAttribute(attribute: String) = attribute match {
  45. case "login" => login
  46. case "email" => email
  47. case "password" => password
  48. case "notificationMail" => notificationMail
  49. case "phone" => phone
  50. case "timezone" => timezone
  51. case "fullname" => fullname
  52. }
  53.  
  54. def create = {
  55. val graph = Database.graph.asScala
  56. vertex = Some(graph +(User.l_User,
  57. User.k_login -> login,
  58. User.k_email -> email,
  59. User.k_password -> hashpw(password, gensalt),
  60. User.k_notificationMail -> notificationMail,
  61. User.k_phone -> phone,
  62. User.k_timezone -> timezone.toString,
  63. User.k_fullname -> fullname))
  64.  
  65. //projects.foreach(p => vertex.get --- "owns" --> Project.getVertex(p.create))
  66.  
  67. vertex.get.id.toString
  68. }
  69.  
  70. def update = vertex match {
  71. case Some(vertex) =>
  72. vertex.valueMap.foreach(property => vertex.setProperty(Key(property._1), getAttribute(property._1)))
  73. //projects.foreach(p => vertex --- "owns" --> Project.getVertex(p.create))
  74. case None => throw new ORecordNotFoundException ("Trying to update User that wasn't created yet")
  75. }
  76. }
  77.  
  78. object User {
  79.  
  80. lazy val l_User = "User"
  81. lazy val k_login = Key[String]("login")
  82. lazy val k_email = Key[String]("email")
  83. lazy val k_password = Key[String]("password")
  84. lazy val k_notificationMail = Key[String]("notificationMail")
  85. lazy val k_phone = Key[String]("phone")
  86. lazy val k_timezone = Key[String]("timezone")
  87. lazy val k_fullname = Key[String]("fullname")
  88.  
  89. def apply(login: String, email: String, password: String) = new User(login, email, password)
  90. def apply(login: String,
  91. email: String,
  92. password: String,
  93. notificationMail: String,
  94. phone: String,
  95. timezone: DateTimeZone,
  96. fullname: String) = new User(login, email, password, notificationMail, phone, timezone, fullname)
  97.  
  98. private def apply(vertex: ScalaVertex) = new User(vertex)
  99.  
  100. def getByLogin(login: String) = {
  101. val vertex = Database.graph.V.hasLabel(l_User).has(k_login -> login).head
  102. User(vertex)
  103. }
  104.  
  105. def getByMail(email: String) = {
  106. val vertex = Database.graph.V.hasLabel(l_User).has(k_email -> email).head
  107. User(vertex)
  108. }
  109.  
  110. def getVertex(id: String) =
  111. Database.graph.V(id).hasLabel(l_User).head
  112.  
  113. def get(id: String) =
  114. User(Database.graph.V(id).hasLabel(l_User).head)
  115.  
  116. def delete(id: String) = {
  117. get(id).vertex match {
  118. case Some(vertex) => vertex.head.remove()
  119. case None => throw new ORecordNotFoundException ("Trying to delete User that wasn't created yet")
  120. }
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement