Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package models
- import database.Database
- import com.orientechnologies.orient.core.exception.ORecordNotFoundException
- import org.mindrot.jbcrypt.BCrypt._
- import com.github.nscala_time.time.Imports._
- import gremlin.scala._
- class User(var login: String, var email: String, var password: String) extends ActiveRecord
- {
- var notificationMail = email
- var phone = ""
- var timezone = DateTimeZone.UTC
- var fullname = ""
- private var vertex: Option[ScalaVertex] = None
- def this(login: String,
- email: String,
- password: String,
- notificationMail: String,
- phone: String,
- timezone: DateTimeZone,
- fullname: String) {
- this(login, email, password)
- this.notificationMail = notificationMail
- this.phone = phone
- this.timezone = timezone
- this.fullname = fullname
- }
- private def this(vertex: ScalaVertex)
- {
- this(vertex.property(User.k_login).value,
- vertex.property(User.k_email).value,
- vertex.property(User.k_password).value,
- vertex.property(User.k_notificationMail).value,
- vertex.property(User.k_phone).value,
- DateTimeZone.forID(vertex.property(User.k_timezone).value),
- vertex.property(User.k_fullname).value)
- this.vertex = Some(vertex)
- }
- private def getAttribute(attribute: String) = attribute match {
- case "login" => login
- case "email" => email
- case "password" => password
- case "notificationMail" => notificationMail
- case "phone" => phone
- case "timezone" => timezone
- case "fullname" => fullname
- }
- def create = {
- val graph = Database.graph.asScala
- vertex = Some(graph +(User.l_User,
- User.k_login -> login,
- User.k_email -> email,
- User.k_password -> hashpw(password, gensalt),
- User.k_notificationMail -> notificationMail,
- User.k_phone -> phone,
- User.k_timezone -> timezone.toString,
- User.k_fullname -> fullname))
- //projects.foreach(p => vertex.get --- "owns" --> Project.getVertex(p.create))
- vertex.get.id.toString
- }
- def update = vertex match {
- case Some(vertex) =>
- vertex.valueMap.foreach(property => vertex.setProperty(Key(property._1), getAttribute(property._1)))
- //projects.foreach(p => vertex --- "owns" --> Project.getVertex(p.create))
- case None => throw new ORecordNotFoundException ("Trying to update User that wasn't created yet")
- }
- }
- object User {
- lazy val l_User = "User"
- lazy val k_login = Key[String]("login")
- lazy val k_email = Key[String]("email")
- lazy val k_password = Key[String]("password")
- lazy val k_notificationMail = Key[String]("notificationMail")
- lazy val k_phone = Key[String]("phone")
- lazy val k_timezone = Key[String]("timezone")
- lazy val k_fullname = Key[String]("fullname")
- def apply(login: String, email: String, password: String) = new User(login, email, password)
- def apply(login: String,
- email: String,
- password: String,
- notificationMail: String,
- phone: String,
- timezone: DateTimeZone,
- fullname: String) = new User(login, email, password, notificationMail, phone, timezone, fullname)
- private def apply(vertex: ScalaVertex) = new User(vertex)
- def getByLogin(login: String) = {
- val vertex = Database.graph.V.hasLabel(l_User).has(k_login -> login).head
- User(vertex)
- }
- def getByMail(email: String) = {
- val vertex = Database.graph.V.hasLabel(l_User).has(k_email -> email).head
- User(vertex)
- }
- def getVertex(id: String) =
- Database.graph.V(id).hasLabel(l_User).head
- def get(id: String) =
- User(Database.graph.V(id).hasLabel(l_User).head)
- def delete(id: String) = {
- get(id).vertex match {
- case Some(vertex) => vertex.head.remove()
- case None => throw new ORecordNotFoundException ("Trying to delete User that wasn't created yet")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement