Permalink
Browse files

Do some parsing & add in Mongo stuff

  • Loading branch information...
1 parent 2d4f4a5 commit a5ff17e352e59307446bc096587ccc5b35810d15 @rupertbates committed May 21, 2012
Showing with 28,838 additions and 3 deletions.
  1. +3 −2 README.md
  2. +12 −0 build.sbt
  3. +28,694 −0 files/matches.json
  4. +1 −1 project/plugins.sbt
  5. +10 −0 src/main/scala/Match.scala
  6. +8 −0 src/main/scala/MatchRepository.scala
  7. +102 −0 src/main/scala/ParseJson.scala
  8. +8 −0 src/main/scala/Team.scala
View
@@ -1,2 +1,3 @@
-1. Add in Idea Scala plugin + Idea SBT plugin
-2. From SBT console in Idea type gen-idea to generate project files and to regenerate after adding in new SBT dependencies
+1. Add in Idea Scala plugin + Idea SBT plugin through IntelliJ plugins dialog
+2. Add SBT Idea plugin (https://github.com/mpeltonen/sbt-idea) to ~/.sbt/plugins/build.sbt
+3. From SBT console in Idea type gen-idea to generate project files and to regenerate after adding in new SBT dependencies
View
@@ -1,5 +1,17 @@
+resolvers += "repo.novus rels" at "http://repo.novus.com/releases/"
+
+resolvers += "repo.novus snaps" at "http://repo.novus.com/snapshots/"
+
libraryDependencies += "org.codehaus.jackson" % "jackson-core-asl" % "1.9.2"
libraryDependencies += "org.codehaus.jackson" % "jackson-mapper-asl" % "1.9.2"
libraryDependencies += "com.fasterxml" % "jackson-module-scala" % "1.9.1"
+
+libraryDependencies += "org.apache.httpcomponents" % "httpclient" % "4.1.3"
+
+libraryDependencies += "com.mongodb.casbah" %% "casbah" % "2.1.5-1"
+
+libraryDependencies += "com.novus" %% "salat-core" % "0.0.8-SNAPSHOT"
+
+
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -1,3 +1,3 @@
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
-addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.0.0")
+addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "0.11.0")
View
@@ -0,0 +1,10 @@
+package models
+
+import java.util.Date
+import com.mongodb.casbah.commons.MongoDBObject
+import com.mongodb.BasicDBObject
+import com.novus.salat.annotations.raw.Key
+
+case class Match(kickOffTime: String, homeTeam: Team, awayTeam: Team){
+}
+
@@ -0,0 +1,8 @@
+import com.mongodb.casbah.MongoConnection
+import com.novus.salat.dao.SalatDAO
+import com.novus.salat.global._
+import models.Match
+
+object MatchRepository extends SalatDAO[Match, Int](collection = MongoConnection()("test")("match")) {
+
+}
@@ -0,0 +1,102 @@
+import _root_.models.Match
+import _root_.models.Team
+import com.mongodb.casbah.Imports._
+
+//import com.fasterxml.jackson.databind.ObjectMapper
+import com.fasterxml.jackson.module.scala.{CaseClassModule, OptionModule}
+import java.io.File
+import java.util.UUID
+import org.apache.http.client.methods.HttpPut
+import org.apache.http.entity.StringEntity
+import org.apache.http.impl.client.DefaultHttpClient
+import org.codehaus.jackson.map.ObjectMapper
+
+object ParseJson {
+ def main(args: Array[String]) {
+ saveGames(saveMongo)
+ }
+
+ def saveMongo(game : Match){
+ println("saving game " + game)
+
+ //val stuff = MongoConnection()("test")("match")
+ MatchRepository.insert(game)
+
+ }
+
+ def saveGames(saveGame : Match => Unit){
+ val om = new ObjectMapper
+ var filename = System.getProperty("user.dir") + "\\files\\matches.json";
+ val file = new File(filename)
+
+ var node = om.readTree(file)
+ val months = node.get("months")
+
+ var i = 0
+ while (i < months.size()) {
+ //Console.out.print(months.get(i).get("startDate"))
+ var days = months.get(i).get("days")
+ var j = 0
+ while (j < days.size()) {
+ var matches = days.get(j).get("matches")
+ var k = 0
+ while (k < matches.size()) {
+ var gameNode = matches.get(k)
+ var home = new Team(gameNode.get("homeTeam").findValue("name").asText(), gameNode.get("homeTeam").get("score").asInt(0))
+ var away = new Team(gameNode.get("awayTeam").findValue("name").asText(), gameNode.get("awayTeam").get("score").asInt(0))
+ var game = new Match(gameNode.get("dateTime").asText(), home, away)
+ saveGame(game)
+ k = k + 1
+ }
+ j = j + 1
+ }
+ i = i + 1
+ }
+ }
+
+ def saveCouch(game: Match) = {
+ var httpClient = new DefaultHttpClient();
+ val postRequest = new HttpPut("http://localhost:5984/lms/game_" + UUID.randomUUID().toString);
+
+ var om = new ObjectMapper();
+
+ val module = new OptionModule with CaseClassModule
+ om.registerModule(module)
+
+ var g = om.writeValueAsString(game);
+ val input = new StringEntity(g);
+
+ // val cp = new ContentProducer() {
+ // def writeTo(outStream : OutputStream): Unit = {
+ // var om = new ObjectMapper();
+ // //om.toString
+ // om.writeValue(outStream, game)
+ // }
+ // };
+
+
+ // var entity = new EntityTemplate(cp);
+ input.setContentType("application/json");
+ postRequest.setEntity(input);
+
+ val response = httpClient.execute(postRequest);
+
+ if (response.getStatusLine().getStatusCode() != 201) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + response.getStatusLine().getStatusCode());
+ }
+
+ // val br = new BufferedReader(
+ // new InputStreamReader((response.getEntity().getContent())));
+
+ // var output: String = ""
+ // System.out.println("Output from Server .... \n");
+ // while ((output = br.readLine()) != null) {
+ // System.out.println(output);
+ // }
+
+ httpClient.getConnectionManager().shutdown();
+
+ }
+
+}
@@ -0,0 +1,8 @@
+package models
+
+import com.mongodb.casbah.commons.MongoDBObject
+import com.mongodb.BasicDBObject
+import com.novus.salat.annotations.raw.Key
+
+case class Team(name : String, score : Int) {
+}

0 comments on commit a5ff17e

Please sign in to comment.