Create java.util.Date TypeMapper. And add the currentDate method.

This commit is contained in:
shimamoto
2013-06-23 20:46:08 +09:00
parent 83ff171782
commit 4326f34ef5
12 changed files with 68 additions and 74 deletions

View File

@@ -38,11 +38,9 @@ trait AccountControllerBase extends ControllerBase {
post("/:userName/_edit", form)(ownerOnly { form => post("/:userName/_edit", form)(ownerOnly { form =>
val userName = params("userName") val userName = params("userName")
val currentDate = new java.sql.Timestamp(System.currentTimeMillis) // TODO make a common function to get the current timestamp.
updateAccount(getAccountByUserName(userName).get.copy( updateAccount(getAccountByUserName(userName).get.copy(
mailAddress = form.mailAddress, mailAddress = form.mailAddress,
url = form.url, url = form.url))
updatedDate = currentDate))
redirect("/%s".format(userName)) redirect("/%s".format(userName))
}) })

View File

@@ -50,11 +50,11 @@ abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFo
/** /**
* ValueType for the Date property. * ValueType for the Date property.
*/ */
def date(constraints: Constraint*): SingleValueType[java.sql.Date] = def date(constraints: Constraint*): SingleValueType[java.util.Date] =
new SingleValueType[java.sql.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){ new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
def convert(value: String): java.sql.Date = { def convert(value: String): java.util.Date = {
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd") val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
new java.sql.Date(formatter.parse(value).getTime) formatter.parse(value)
} }
} }

View File

@@ -4,7 +4,6 @@ import jp.sf.amateras.scalatra.forms._
import service._ import service._
import util.{WritableRepositoryAuthenticator, ReadableRepositoryAuthenticator, UsersOnlyAuthenticator} import util.{WritableRepositoryAuthenticator, ReadableRepositoryAuthenticator, UsersOnlyAuthenticator}
import java.sql.Timestamp
class IssuesController extends IssuesControllerBase class IssuesController extends IssuesControllerBase
with IssuesService with RepositoryService with AccountService with IssuesService with RepositoryService with AccountService
@@ -16,7 +15,7 @@ trait IssuesControllerBase extends ControllerBase {
case class IssueForm(title: String, content: Option[String]) case class IssueForm(title: String, content: Option[String])
case class MilestoneForm(title: String, description: Option[String], dueDate: Option[java.sql.Date]) case class MilestoneForm(title: String, description: Option[String], dueDate: Option[java.util.Date])
val form = mapping( val form = mapping(
"title" -> trim(label("Title", text(required))), "title" -> trim(label("Title", text(required))),
@@ -128,8 +127,8 @@ trait IssuesControllerBase extends ControllerBase {
getMilestone(owner, repository, milestoneId) match { getMilestone(owner, repository, milestoneId) match {
case None => NotFound() case None => NotFound()
case Some(m) => { case Some(m) => {
// TODO make a common function to get the current timestamp. // TODO I want to ban to use the currentDate in Controller.
val currentDate = new Timestamp(System.currentTimeMillis) val currentDate = new java.util.Date()
updateMilestone(m.copy(closedDate = Some(currentDate))) updateMilestone(m.copy(closedDate = Some(currentDate)))
redirect("/%s/%s/issues/milestones".format(owner, repository)) redirect("/%s/%s/issues/milestones".format(owner, repository))
} }

View File

@@ -36,8 +36,8 @@ trait UsersControllerBase extends ControllerBase { self: AccountService with Adm
}) })
post("/admin/users/_new", newForm)(adminOnly { form => post("/admin/users/_new", newForm)(adminOnly { form =>
// TODO make a common function to get the current timestamp. // TODO I want to ban to use the currentDate in Controller.
val currentDate = new java.sql.Timestamp(System.currentTimeMillis) val currentDate = new java.util.Date()
createAccount(Account( createAccount(Account(
userName = form.userName, userName = form.userName,
password = form.password, password = form.password,
@@ -58,14 +58,11 @@ trait UsersControllerBase extends ControllerBase { self: AccountService with Adm
post("/admin/users/:name/_edit", editForm)(adminOnly { form => post("/admin/users/:name/_edit", editForm)(adminOnly { form =>
val userName = params("userName") val userName = params("userName")
// TODO make a common function to get the current timestamp.
val currentDate = new java.sql.Timestamp(System.currentTimeMillis)
updateAccount(getAccountByUserName(userName).get.copy( updateAccount(getAccountByUserName(userName).get.copy(
password = form.password, password = form.password,
mailAddress = form.mailAddress, mailAddress = form.mailAddress,
isAdmin = form.isAdmin, isAdmin = form.isAdmin,
url = form.url, url = form.url))
updatedDate = currentDate))
redirect("/admin/users") redirect("/admin/users")
}) })

View File

@@ -2,15 +2,15 @@ package model
import scala.slick.driver.H2Driver.simple._ import scala.slick.driver.H2Driver.simple._
object Accounts extends Table[Account]("ACCOUNT") { object Accounts extends Table[Account]("ACCOUNT") with Functions {
def userName = column[String]("USER_NAME", O PrimaryKey) def userName = column[String]("USER_NAME", O PrimaryKey)
def mailAddress = column[String]("MAIL_ADDRESS") def mailAddress = column[String]("MAIL_ADDRESS")
def password = column[String]("PASSWORD") def password = column[String]("PASSWORD")
def isAdmin = column[Boolean]("ADMINISTRATOR") def isAdmin = column[Boolean]("ADMINISTRATOR")
def url = column[String]("URL") def url = column[String]("URL")
def registeredDate = column[java.sql.Timestamp]("REGISTERED_DATE") // TODO convert java.util.Date later def registeredDate = column[java.util.Date]("REGISTERED_DATE")
def updatedDate = column[java.sql.Timestamp]("UPDATED_DATE") def updatedDate = column[java.util.Date]("UPDATED_DATE")
def lastLoginDate = column[java.sql.Timestamp]("LAST_LOGIN_DATE") def lastLoginDate = column[java.util.Date]("LAST_LOGIN_DATE")
def * = userName ~ mailAddress ~ password ~ isAdmin ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? <> (Account, Account.unapply _) def * = userName ~ mailAddress ~ password ~ isAdmin ~ url.? ~ registeredDate ~ updatedDate ~ lastLoginDate.? <> (Account, Account.unapply _)
} }
@@ -20,16 +20,7 @@ case class Account(
password: String, password: String,
isAdmin: Boolean, isAdmin: Boolean,
url: Option[String], url: Option[String],
registeredDate: java.sql.Timestamp, registeredDate: java.util.Date,
updatedDate: java.sql.Timestamp, updatedDate: java.util.Date,
lastLoginDate: Option[java.sql.Timestamp] lastLoginDate: Option[java.util.Date]
) )
class AccountDao {
import Database.threadLocalSession
def insert(o: Account): Long = Accounts insert o
def select(key: String): Option[Account] = Query(Accounts) filter(_.userName is key.bind) firstOption
}

View File

@@ -0,0 +1,17 @@
package model
import scala.slick.lifted.MappedTypeMapper
protected[model] trait Functions {
// java.util.Date TypeMapper
implicit val dateTypeMapper = MappedTypeMapper.base[java.util.Date, java.sql.Timestamp](
d => new java.sql.Timestamp(d.getTime),
t => new java.util.Date(t.getTime)
)
/**
* Returns system date.
*/
def currentDate = new java.util.Date()
}

View File

@@ -9,7 +9,7 @@ object IssueId extends Table[(String, String, Int)]("ISSUE_ID") {
def * = userName ~ repositoryName ~ issueId def * = userName ~ repositoryName ~ issueId
} }
object Issues extends Table[Issue]("ISSUE") { object Issues extends Table[Issue]("ISSUE") with Functions {
def userName = column[String]("USER_NAME", O PrimaryKey) def userName = column[String]("USER_NAME", O PrimaryKey)
def repositoryName = column[String]("REPOSITORY_NAME", O PrimaryKey) def repositoryName = column[String]("REPOSITORY_NAME", O PrimaryKey)
def issueId = column[Int]("ISSUE_ID", O PrimaryKey) def issueId = column[Int]("ISSUE_ID", O PrimaryKey)
@@ -19,8 +19,8 @@ object Issues extends Table[Issue]("ISSUE") {
def title = column[String]("TITLE") def title = column[String]("TITLE")
def content = column[String]("CONTENT") def content = column[String]("CONTENT")
def closed = column[Boolean]("CLOSED") def closed = column[Boolean]("CLOSED")
def registeredDate = column[java.sql.Date]("REGISTERED_DATE") // TODO convert java.util.Date later def registeredDate = column[java.util.Date]("REGISTERED_DATE")
def updatedDate = column[java.sql.Date]("UPDATED_DATE") def updatedDate = column[java.util.Date]("UPDATED_DATE")
def * = userName ~ repositoryName ~ issueId ~ openedUserName ~ milestoneId.? ~ assignedUserName.? ~ title ~ content.? ~ closed ~ registeredDate ~ updatedDate <> (Issue, Issue.unapply _) def * = userName ~ repositoryName ~ issueId ~ openedUserName ~ milestoneId.? ~ assignedUserName.? ~ title ~ content.? ~ closed ~ registeredDate ~ updatedDate <> (Issue, Issue.unapply _)
} }
@@ -34,5 +34,5 @@ case class Issue(
title: String, title: String,
content: Option[String], content: Option[String],
closed: Boolean, closed: Boolean,
registeredDate: java.sql.Date, registeredDate: java.util.Date,
updatedDate: java.sql.Date) updatedDate: java.util.Date)

View File

@@ -2,17 +2,17 @@ package model
import scala.slick.driver.H2Driver.simple._ import scala.slick.driver.H2Driver.simple._
object Milestones extends Table[Milestone]("MILESTONE") { object Milestones extends Table[Milestone]("MILESTONE") with Functions {
def userName = column[String]("USER_NAME", O PrimaryKey) def userName = column[String]("USER_NAME", O PrimaryKey)
def repositoryName = column[String]("REPOSITORY_NAME", O PrimaryKey) def repositoryName = column[String]("REPOSITORY_NAME", O PrimaryKey)
def milestoneId = column[Int]("MILESTONE_ID", O PrimaryKey) def milestoneId = column[Int]("MILESTONE_ID", O PrimaryKey, O AutoInc)
def title = column[String]("TITLE") def title = column[String]("TITLE")
def description = column[String]("DESCRIPTION") def description = column[String]("DESCRIPTION")
def dueDate = column[java.sql.Date]("DUE_DATE") // TODO convert java.util.Date later def dueDate = column[java.util.Date]("DUE_DATE")
def closedDate = column[java.sql.Timestamp]("CLOSED_DATE") def closedDate = column[java.util.Date]("CLOSED_DATE")
def ins = userName ~ repositoryName ~ title ~ description.? ~ dueDate.? ~ closedDate.?
def * = userName ~ repositoryName ~ milestoneId ~ title ~ description.? ~ dueDate.? ~ closedDate.? <> (Milestone, Milestone.unapply _) def * = userName ~ repositoryName ~ milestoneId ~ title ~ description.? ~ dueDate.? ~ closedDate.? <> (Milestone, Milestone.unapply _)
def autoInc = userName ~ repositoryName ~ title ~ description.? ~ dueDate.? ~ closedDate.? returning milestoneId
} }
case class Milestone( case class Milestone(
@@ -21,5 +21,5 @@ case class Milestone(
milestoneId: Int, milestoneId: Int,
title: String, title: String,
description: Option[String], description: Option[String],
dueDate: Option[java.sql.Date], dueDate: Option[java.util.Date],
closedDate: Option[java.sql.Timestamp]) closedDate: Option[java.util.Date])

View File

@@ -2,15 +2,15 @@ package model
import scala.slick.driver.H2Driver.simple._ import scala.slick.driver.H2Driver.simple._
object Repositories extends Table[Repository]("REPOSITORY") { object Repositories extends Table[Repository]("REPOSITORY") with Functions {
def repositoryName= column[String]("REPOSITORY_NAME", O PrimaryKey) def repositoryName= column[String]("REPOSITORY_NAME", O PrimaryKey)
def userName = column[String]("USER_NAME", O PrimaryKey) def userName = column[String]("USER_NAME", O PrimaryKey)
def isPrivate = column[Boolean]("PRIVATE") def isPrivate = column[Boolean]("PRIVATE")
def description = column[String]("DESCRIPTION") def description = column[String]("DESCRIPTION")
def defaultBranch = column[String]("DEFAULT_BRANCH") def defaultBranch = column[String]("DEFAULT_BRANCH")
def registeredDate = column[java.sql.Timestamp]("REGISTERED_DATE") // TODO convert java.util.Date later def registeredDate = column[java.util.Date]("REGISTERED_DATE")
def updatedDate = column[java.sql.Timestamp]("UPDATED_DATE") def updatedDate = column[java.util.Date]("UPDATED_DATE")
def lastActivityDate = column[java.sql.Timestamp]("LAST_ACTIVITY_DATE") def lastActivityDate = column[java.util.Date]("LAST_ACTIVITY_DATE")
def * = repositoryName ~ userName ~ isPrivate ~ description.? ~ defaultBranch ~ registeredDate ~ updatedDate ~ lastActivityDate <> (Repository, Repository.unapply _) def * = repositoryName ~ userName ~ isPrivate ~ description.? ~ defaultBranch ~ registeredDate ~ updatedDate ~ lastActivityDate <> (Repository, Repository.unapply _)
} }
@@ -20,7 +20,7 @@ case class Repository(
isPrivate: Boolean, isPrivate: Boolean,
description: Option[String], description: Option[String],
defaultBranch: String, defaultBranch: String,
registeredDate: java.sql.Timestamp, registeredDate: java.util.Date,
updatedDate: java.sql.Timestamp, updatedDate: java.util.Date,
lastActivityDate: java.sql.Timestamp lastActivityDate: java.util.Date
) )

View File

@@ -1,6 +1,7 @@
package service package service
import model._ import model._
import Accounts._
import scala.slick.driver.H2Driver.simple._ import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession import Database.threadLocalSession
@@ -11,7 +12,7 @@ trait AccountService {
def getAllUsers(): List[Account] = Query(Accounts) sortBy(_.userName) list def getAllUsers(): List[Account] = Query(Accounts) sortBy(_.userName) list
def createAccount(account: Account): Unit = Accounts.* insert account def createAccount(account: Account): Unit = Accounts insert account
def updateAccount(account: Account): Unit = def updateAccount(account: Account): Unit =
Query(Accounts) Query(Accounts)
@@ -23,12 +24,11 @@ trait AccountService {
account.isAdmin, account.isAdmin,
account.url, account.url,
account.registeredDate, account.registeredDate,
account.updatedDate, currentDate,
account.lastLoginDate) account.lastLoginDate)
def updateLastLoginDate(userName: String): Unit = def updateLastLoginDate(userName: String): Unit =
// TODO make a common function to get the current timestamp.
Query(Accounts).filter(_.userName is userName.bind).map(_.lastLoginDate) Query(Accounts).filter(_.userName is userName.bind).map(_.lastLoginDate)
.update(new java.sql.Timestamp(System.currentTimeMillis)) .update(currentDate)
} }

View File

@@ -6,6 +6,7 @@ import scala.slick.jdbc.{StaticQuery => Q}
import Q.interpolation import Q.interpolation
import model._ import model._
import Issues._
trait IssuesService { trait IssuesService {
def getIssue(owner: String, repository: String, issueId: String) = def getIssue(owner: String, repository: String, issueId: String) =
@@ -41,8 +42,8 @@ trait IssuesService {
title, title,
content, content,
false, false,
new java.sql.Date(System.currentTimeMillis), // TODO currentDate,
new java.sql.Date(System.currentTimeMillis)) currentDate)
// increment issue id // increment issue id
IssueId.filter { t => IssueId.filter { t =>
@@ -51,9 +52,8 @@ trait IssuesService {
} get } get
def createMilestone(owner: String, repository: String, def createMilestone(owner: String, repository: String,
title: String, description: Option[String], dueDate: Option[java.sql.Date]): Unit = { title: String, description: Option[String], dueDate: Option[java.util.Date]) =
Milestones.ins insert (owner, repository, title, description, dueDate, None) Milestones.autoInc insert (owner, repository, title, description, dueDate, None)
}
def updateMilestone(milestone: Milestone): Unit = def updateMilestone(milestone: Milestone): Unit =
Query(Milestones) Query(Milestones)

View File

@@ -1,13 +1,10 @@
package service package service
import model._ import model._
import Repositories._
import scala.slick.driver.H2Driver.simple._ import scala.slick.driver.H2Driver.simple._
import Database.threadLocalSession import Database.threadLocalSession
import util.JGitUtil import util.JGitUtil
import scala.Some
import model.Repository
import model.Account
import model.Collaborator
trait RepositoryService { self: AccountService => trait RepositoryService { self: AccountService =>
import RepositoryService._ import RepositoryService._
@@ -27,9 +24,6 @@ trait RepositoryService { self: AccountService =>
// TODO insert default labels. // TODO insert default labels.
// TODO make a common function to get the current timestamp.
val currentDate = new java.sql.Timestamp(System.currentTimeMillis)
Repositories insert Repositories insert
Repository( Repository(
repositoryName = repositoryName, repositoryName = repositoryName,
@@ -160,22 +154,20 @@ trait RepositoryService { self: AccountService =>
* Updates the last activity date of the repository. * Updates the last activity date of the repository.
*/ */
def updateLastActivityDate(userName: String, repositoryName: String): Unit = def updateLastActivityDate(userName: String, repositoryName: String): Unit =
// TODO make a common function to get the current timestamp.
Query(Repositories) Query(Repositories)
.filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) } .filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) }
.map { _.lastActivityDate } .map { _.lastActivityDate }
.update (new java.sql.Timestamp(System.currentTimeMillis)) .update (currentDate)
/** /**
* Save repository options. * Save repository options.
*/ */
def saveRepositoryOptions(userName: String, repositoryName: String, def saveRepositoryOptions(userName: String, repositoryName: String,
description: Option[String], defaultBranch: String, isPrivate: Boolean): Unit = description: Option[String], defaultBranch: String, isPrivate: Boolean): Unit =
// TODO make a common function to get the current timestamp.
Query(Repositories) Query(Repositories)
.filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) } .filter { r => (r.userName is userName.bind) && (r.repositoryName is repositoryName.bind) }
.map { r => r.description.? ~ r.defaultBranch ~ r.isPrivate ~ r.updatedDate } .map { r => r.description.? ~ r.defaultBranch ~ r.isPrivate ~ r.updatedDate }
.update (description, defaultBranch, isPrivate, new java.sql.Timestamp(System.currentTimeMillis)) .update (description, defaultBranch, isPrivate, currentDate)
/** /**
* Add collaborator to the repository. * Add collaborator to the repository.