~ worky POST/GET for runs
authorAlexandre Bertails <bertails@w3.org>
Tue, 23 Aug 2011 16:14:21 -0400
changeset 27 f00302656b5d
parent 17 690206ecf35d
child 28 2f6ec4dcf606
~ worky POST/GET for runs
project/Build.scala
src/main/scala/Main.scala
src/main/scala/Record.scala
src/test/scala/Test.scala
--- a/project/Build.scala	Mon Aug 22 17:43:01 2011 -0400
+++ b/project/Build.scala	Tue Aug 23 16:14:21 2011 -0400
@@ -9,7 +9,9 @@
   val unfiltered_filter = "net.databinder" %% "unfiltered-filter" % "0.4.1"
   val unfiltered_jetty = "net.databinder" %% "unfiltered-jetty" % "0.4.1"
   val unfiltered_scalate = "net.databinder" %% "unfiltered-scalate" % "0.4.1"
+  val unfiltered_json = "net.databinder" %% "unfiltered-json" % "0.4.1"
   val slf4jSimple = "org.slf4j" % "slf4j-simple" % "1.6.1"
+  val salat = "com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
 }
 
 object BuildSettings {
@@ -33,23 +35,26 @@
   import Dependencies._
   import BuildSettings._
 
-  val pubrules_settings =
+  val virtual_trainer_settings =
     Seq(
       resolvers += ScalaToolsReleases,
       resolvers += ScalaToolsSnapshots,
+      resolvers += "repo.novus snaps" at "http://repo.novus.com/snapshots/",
       libraryDependencies += scalatest,
 //      libraryDependencies += antiXML,
       libraryDependencies += dispatch,
       libraryDependencies += unfiltered_filter,
       libraryDependencies += unfiltered_jetty,
       libraryDependencies += unfiltered_scalate,
-      libraryDependencies += slf4jSimple
+      libraryDependencies += unfiltered_json,
+      libraryDependencies += slf4jSimple,
+      libraryDependencies += salat
     )
 
   lazy val virtual_trainer = Project(
     id = "virtualtrainer",
     base = file("."),
-    settings = buildSettings ++ pubrules_settings ++ sbtassembly.Plugin.assemblySettings
+    settings = buildSettings ++ virtual_trainer_settings ++ sbtassembly.Plugin.assemblySettings
   )
   
 
--- a/src/main/scala/Main.scala	Mon Aug 22 17:43:01 2011 -0400
+++ b/src/main/scala/Main.scala	Tue Aug 23 16:14:21 2011 -0400
@@ -5,6 +5,11 @@
 import unfiltered.scalate._
 import unfiltered.jetty._
 
+import org.w3.virtualtrainer.database._
+import com.novus.salat._
+import com.novus.salat.global._
+import com.mongodb.casbah.Imports._
+
 import java.io.{File, FileWriter}
 import org.fusesource.scalate.TemplateEngine
 import java.util.jar._
@@ -64,6 +69,10 @@
     dir
   }
   
+
+  /**
+   * 
+   */
   def main(args: Array[String]) {
     
     val port = args.toList.headOption map { _.toInt } getOrElse 2719
@@ -83,10 +92,27 @@
     val app = unfiltered.filter.Planify {
        case req @ Path(Seg("tracker" :: Nil)) =>
          Ok ~> ContentType("application/xhtml+xml") ~> Scalate(req, "geolocation.ssp")(engine)
-       case req @ POST(Path(Seg("user" :: user :: "run" :: Nil))) => {
-         val body = Body.string(req)
-         logger.info(body)
-         ResponseString(body)
+       case req @ POST(Path(Seg("user" :: user :: "run" :: Nil))) & Host(host) => {
+         val json:MongoDBObject = com.mongodb.util.JSON.parse(Body.string(req)).asInstanceOf[com.mongodb.DBObject]
+         val record = grater[Record].asObject(json)
+         RecordDAO.insert(record) match {
+           case Some(id) => {
+             val path = "%s://%s/user/%s/run/%s" format (req.underlying.getScheme, host, user, id.toString)
+             Created ~> ContentLocation(path) ~> ResponseString("object available at %s" format path)
+           }
+           case None => {
+             InternalServerError ~> ResponseString("couldn't create object")
+           }
+         }
+       }
+       case req @ Path(Seg("user" :: user :: "run" :: id :: Nil)) => req match {
+         case GET(_) => {
+           RecordDAO.findOneByID(id) match {
+             case Some(record) => Ok ~> ContentType("application/json") ~> ResponseString(record.toString)
+             case None => NotFound
+           }
+         }
+         case _ => MethodNotAllowed ~> Allow("GET") ~> ResponseString("MethodNotAllowed...")
        }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/scala/Record.scala	Tue Aug 23 16:14:21 2011 -0400
@@ -0,0 +1,55 @@
+package org.w3.virtualtrainer.database
+
+import com.novus.salat._
+import com.novus.salat.global._
+import com.mongodb.casbah.Imports._
+import com.novus.salat.annotations._
+
+import com.novus.salat.dao._
+import com.mongodb.casbah.MongoConnection
+  
+/*
+{
+ activity:"http://dbpedia.org/resource/Running",
+ events:
+ [
+  {
+   t: 1314049555597,
+   c: [-71.09068298339844,42.361671924591064,66]
+  },
+  {
+   t: 1314049559622,
+   s: "pause"
+  },
+  {
+   t: 1314549555597,
+   c: [-71.09068298339844,42.361671924591064,66]
+  },
+   t: 1315049559622,
+   s: "pause"
+  }
+ ]
+}
+*/
+
+case class Record(@Key("_id") id: ObjectId = new ObjectId,
+                  activity:String,
+                  events:List[Event])
+
+case class Event(@Key("t") timestamp:Long,
+                 @Key("c") coordinates:Option[(Double, Double, Option[Double])],
+                 @Key("s") status:Option[String])
+
+object RecordDAO extends SalatDAO[Record, ObjectId](collection = MongoConnection("gw42.w3.org", 27017)("virtual_trainer")("user/records")) {
+ 
+  def findOneByID(id:String):Option[Record] = {
+    try {
+      val objectId = new ObjectId(id)
+      findOneByID(objectId)
+    } catch {
+      case iae:IllegalArgumentException => None
+    }
+    
+  }
+  
+}
--- a/src/test/scala/Test.scala	Mon Aug 22 17:43:01 2011 -0400
+++ b/src/test/scala/Test.scala	Tue Aug 23 16:14:21 2011 -0400
@@ -1,13 +1,27 @@
-package org.w3.virtualtrainer
+package org.w3.virtualtrainer.database
 
 import org.scalatest.FunSuite
 
+
+
+
 class Test extends FunSuite {
 
   test("") {
 
-    assert(2 == 1+1)
-
+    val record =
+      Record(
+          activity="http://dbpedia.org/resource/Running",
+          events=List(
+              Event(timestamp=1314049555597L, coordinates=Some((-71.09068298339844,42.361671924591064,Some(66))), status=None),
+              Event(timestamp=1314049559622L, coordinates=None, status=Some("pause"))
+          )
+      )
+    
+//    val dbo = grater[Record].asDBObject(record)
+//
+//    println(dbo)
+//    
   }
 
 }