Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1585018
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: 架构设计与优化

2015-04-21 00:30:43

三个版本了已经,都是原型。

coffee_scala.tgz 
	
	
	
	
	
build.sbt
name := """play-scala-intro"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayScala)

scalaVersion := "2.10.1"

libraryDependencies ++= Seq(
  jdbc,
  anorm,
  cache,
  ws,
  "com.google.inject" % "guice" % "3.0",
  "org.sorm-framework" % "sorm" % "0.3.8",
  "com.h2database" % "h2" % "1.3.168",
  "org.webjars" %% "webjars-play" % "2.3.0-2",
  "org.webjars" % "bootstrap" % "3.1.1-2"
)
	
	
	
	
	
Ticket.scala
package models
import play.api.libs.json._
import play.api.libs.json.Writes._
import play.api.libs.functional.syntax._
case class Ticket(id: Int, ticket_number: String, detail: String)

object Ticket {

  implicit val ticketFormat = Json.format[Ticket]

  implicit val ticketReader = Json.reads[Ticket]

  val writesWithNewField: Writes[Ticket] =
    (
      (__ \ "id").write[Int] ~
        (__ \ "ticket_number").write[String] ~
        (__ \ "school_bucket").write[String] ~
        (__ \ "school_number").write[String] ~
        (__ \ "school_name").write[String]
      )(
        (t: Ticket) => {

          // Do some parsing and transformation to extract school_id, school_name, school_bucket
          val bucket_pattern = """.*school_bucket:\s*(\d).*""".r
          val number_pattern = """.*school_id:\s*(\d+).*""".r
          val name_pattern = """.*school_name:\s*([^<]+).*""".r

          var school_bucket = "Missing"
          var school_number = "Missing"
          var school_name = "Missing"

          t.detail match {
            case bucket_pattern(school_match) => school_bucket = school_match
            case _ => 0
          }
          t.detail match {
            case number_pattern(school_match) => school_number = school_match
            case _ => 0
          }
          t.detail match {
            case name_pattern(school_match) => school_name = school_match
            case _ => 0
          }

          (t.id, t.ticket_number, school_bucket, school_number, school_name)
        }
      )

  val listWritesWithNewFields: Writes[List[Ticket]] = Writes.traversableWrites[Ticket](writesWithNewField)
}
	
	
	
	
	
	
	
	
	
routes.scala
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~

# Home page
GET         /                     controllers.Application.index
POST        /person               controllers.Application.addPerson
GET         /persons              controllers.Application.getPersons

# Map static resources from the /public folder to the /assets URL path
GET         /assets/*file         controllers.Assets.at(path="/public", file)
GET         /webjars/*file        controllers.WebJarAssets.at(file)
# My Edu ticket system
POST        /login                controllers.Application.login
GET         /credential           controllers.TicketController.getCredential
GET         /tickets              controllers.TicketController.getTickets
	
TicketController.scala
	
	
	
	
	
	
	
	
	
package controllers import controllers.Application._ import models._ import play.api.data.Form import play.api.data.Forms._ import play.api.libs.json.Json import play.api.libs.ws._ import play.api.mvc._ import scala.concurrent.duration.Duration import scala.util.{Failure, Success} import scala.concurrent._ import play.api.Play.current import play.api.libs.concurrent.Execution.Implicits.defaultContext object TicketController extends Controller {   def index = Action {     Ok(views.html.index("Hello world."))   }   val credentialForm: Form[Credential] = Form {     mapping("username" -> text, "password" -> text)(Credential.apply)(Credential.unapply)   }   def login = Action.async(parse.urlFormEncoded) {     implicit request =>       val credential = credentialForm.bindFromRequest.get       WS.url("")         .withHeaders("Content-Type" -> "application/json", "Accept" -> "application/json")         .post(Json.toJson(credential))         .map(           response => {             val token = (response.json \ "return_body" \ "token").as[String]             val result =  WS.url("")               .withQueryString("token" -> token, "_pageSize_" -> "1000", "_queue_" -> "14").get()               .map(                 response => {                   val tickets = (response.json \ "return_body" \ "items").as[List[Ticket]]                   Ok(Json.toJson(tickets)(Ticket.listWritesWithNewFields))                 }               )             Await.result(result, Duration.Inf)           }         )   } ......................未完待续 }
阅读(1410) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~