From 00af0624d41f2b84c79cf0274ba7492fc6b62032 Mon Sep 17 00:00:00 2001 From: dboissin Date: Sun, 24 Jul 2011 19:19:06 +0200 Subject: [PATCH] Evo : add persistance system with squeryl. --- pom.xml | 645 +++++++++--------- .../scala/poc/ServletContextListener.scala | 7 +- src/main/scala/poc/model/Cashier.scala | 8 + src/main/scala/poc/model/Timetable.scala | 9 + src/main/scala/poc/schema/BootStrap.scala | 39 ++ src/main/scala/poc/schema/Schema.scala | 22 + 6 files changed, 410 insertions(+), 320 deletions(-) create mode 100644 src/main/scala/poc/model/Cashier.scala create mode 100644 src/main/scala/poc/model/Timetable.scala create mode 100644 src/main/scala/poc/schema/BootStrap.scala create mode 100644 src/main/scala/poc/schema/Schema.scala diff --git a/pom.xml b/pom.xml index e9f5135..8b95756 100644 --- a/pom.xml +++ b/pom.xml @@ -1,349 +1,358 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 - poc - poc-scalate-guice - TRUNK-SNAPSHOT - war + poc + poc-scalate-guice + TRUNK-SNAPSHOT + war - Poc scalate guice - + Poc scalate guice + A timetable generator for supermarket cashiers. Web Application Using Scalate, Scala and JAXRS/Jersey - - 2.0.9 - + + 2.0.9 + - - - 8080 - ${env.SCALATE_EDITOR} - ${basedir}/target/_scalate - poc.resources - ${basedir}/src - development + + + 8080 + ${env.SCALATE_EDITOR} + ${basedir}/target/_scalate + poc.resources + ${basedir}/src + development - - 1.5 - 7.2.1.v20101111 - 0.9.26 - 2.8.1 - 1.4.1 - 2.5 - 1.8.3 + + 1.5 + 7.2.1.v20101111 + 0.9.26 + 2.8.1 + 1.4.1 + 2.5 + 1.6.4 + 0.9.4 + 1.3.154 - - 7.2.1.v20101111 - 2.15.0 - 2.6 - 1.1 - 2.1.1 - + + 7.2.1.v20101111 + 2.15.0 + 2.6 + 1.1 + 2.1.1 + - - - org.scala-lang - scala-library - ${scala-version} - - - org.scala-lang - scala-compiler - ${scala-version} - - - javax.servlet - servlet-api - ${servlet-api-version} - provided - - - org.fusesource.scalate - scalate-guice - ${scalate-version} - - - ch.qos.logback - logback-classic - ${logback-version} - runtime - true - - - - org.codehaus.jackson - jackson-jaxrs - ${jackson-version} - - - com.sun.jersey - jersey-json - ${jersey-version} - - - org.codehaus.jackson - jackson-core-asl - - - org.codehaus.jackson - jackson-mapper-asl - - - jaxb-impl - com.sun.xml.bind - - - jettison - org.codehaus.jettison - - - org.codehaus.jackson - jackson-jaxrs - - - + + + org.scala-lang + scala-library + ${scala-version} + + + org.scala-lang + scala-compiler + ${scala-version} + + + javax.servlet + servlet-api + ${servlet-api-version} + provided + + + org.fusesource.scalate + scalate-guice + ${scalate-version} + + + ch.qos.logback + logback-classic + ${logback-version} + runtime + true + + + + org.codehaus.jackson + jackson-jaxrs + ${jackson-version} + + + com.sun.jersey + jersey-json + ${jersey-version} + + + org.codehaus.jackson + jackson-core-asl + + + org.codehaus.jackson + jackson-mapper-asl + + + jaxb-impl + com.sun.xml.bind + + + jettison + org.codehaus.jettison + + + org.codehaus.jackson + jackson-jaxrs + + + + + org.squeryl + squeryl_${scala-version} + ${squeryl-version} + + + com.h2database + h2 + ${h2-version} + runtime + - - - org.fusesource.scalate - scalate-test - ${scalate-version} - test - - + + + org.fusesource.scalate + scalate-test + ${scalate-version} + test + + - - - repo1.maven - Maven Central Repo - http://repo1.maven.org/maven2/ - - - fusesource.m2 - FuseSource Community Release Repository - http://repo.fusesource.com/nexus/content/repositories/public - - false - - - true - - - - fusesource.m2-snapshot - FuseSource Community Snapshot Repository - http://repo.fusesource.com/nexus/content/repositories/snapshots - - true - - - false - - - - java.net.m2 - java.net Maven 2 Repo - http://download.java.net/maven/2 - - - openqa-releases - OpenQA Releases - http://archiva.openqa.org/repository/releases - - true - - - false - - + + + repo1.maven + Maven Central Repo + http://repo1.maven.org/maven2/ + + + fusesource.m2 + FuseSource Community Release Repository + http://repo.fusesource.com/nexus/content/repositories/public + + false + + + true + + + + fusesource.m2-snapshot + FuseSource Community Snapshot Repository + http://repo.fusesource.com/nexus/content/repositories/snapshots + + true + + + false + + - - glassfish-repo-archive - Nexus repository collection for Glassfish - http://maven.glassfish.org/content/groups/glassfish - - never - - + + java.net.m2 + java.net Maven 2 Repo + http://download.java.net/maven/2 + + + openqa-releases + OpenQA Releases + http://archiva.openqa.org/repository/releases + + true + + + false + + - - scala - Scala Tools - http://scala-tools.org/repo-releases - - true - - - false - - - - snapshots.scala-tools.org - Scala-Tools Maven2 Snapshot Repository - http://scala-tools.org/repo-snapshots - - true - - fail - - - true - - fail - - - - apache.snapshots - Apache Development Snapshot Repository - https://repository.apache.org/content/repositories/snapshots - - false - - - true - - - + + glassfish-repo-archive + Nexus repository collection for Glassfish + http://maven.glassfish.org/content/groups/glassfish + + never + + - - - - org.scala-tools - maven-scala-plugin - ${scala-plugin-version} - - - - compile - testCompile - - - - - - -Xmx1024m - - - - -deprecation - - ${scala-version} - - + + scala + Scala Tools + http://scala-tools.org/repo-releases + + true + + + false + + + + snapshots.scala-tools.org + Scala-Tools Maven2 Snapshot Repository + http://scala-tools.org/repo-snapshots + + true + + fail + + + true + + fail + + + + apache.snapshots + Apache Development Snapshot Repository + https://repository.apache.org/content/repositories/snapshots + + false + + + true + + + - - maven-surefire-plugin - ${surefire-plugin-version} - - - ${scalate.package.resources} - ${scalate.generate.src} - + + + + org.scala-tools + maven-scala-plugin + ${scala-plugin-version} + + + + compile + testCompile + + + + + + -Xmx1024m + + + + -deprecation + + ${scala-version} + + - once - - false - false - - **/*Test.* - - - + + maven-surefire-plugin + ${surefire-plugin-version} + + + ${scalate.package.resources} + ${scalate.generate.src} + - - maven-war-plugin - ${war-plugin-version} - - true - - + once + + false + false + + **/*Test.* + + + - - org.mortbay.jetty - jetty-maven-plugin - ${jetty-plugin-version} - - - - / - + + maven-war-plugin + ${war-plugin-version} + + true + + - - - - com.sun.management.jmxremote - - - - scalate.editor - ${scalate.editor} - - - scalate.workdir - ${scalate.workdir} - - - scalate.package.resources - ${scalate.package.resources} - - - scalate.mode - ${scalate.mode} - - - 0 - - + + org.mortbay.jetty + jetty-maven-plugin + ${jetty-plugin-version} + + + + / + - - org.codehaus.mojo - tomcat-maven-plugin - ${tomcat-plugin-version} - - / + + + + com.sun.management.jmxremote + + + + scalate.editor + ${scalate.editor} + + + scalate.workdir + ${scalate.workdir} + + + scalate.package.resources + ${scalate.package.resources} + + + scalate.mode + ${scalate.mode} + + + 0 + + - - - + + org.codehaus.mojo + tomcat-maven-plugin + ${tomcat-plugin-version} + + / - - ${scalate.mode} - ${scalateEditor} - ${scalate.workdir} - ${scalate.package.resources} - - - + + + - - org.fusesource.scalate - maven-scalate-plugin - ${scalate-version} - - - - precompile - - - - + + ${scalate.mode} + ${scalateEditor} + ${scalate.workdir} + ${scalate.package.resources} + + + - - + + org.fusesource.scalate + maven-scalate-plugin + ${scalate-version} + + + + precompile + + + + + + + diff --git a/src/main/scala/poc/ServletContextListener.scala b/src/main/scala/poc/ServletContextListener.scala index 5b09675..51cc0bb 100644 --- a/src/main/scala/poc/ServletContextListener.scala +++ b/src/main/scala/poc/ServletContextListener.scala @@ -1,18 +1,21 @@ package poc import org.fusesource.scalate.guice.ScalateModule - import com.google.inject.servlet.GuiceServletContextListener import com.google.inject.Guice - import poc.service.impl.TimetableServiceImpl import poc.service.TimetableService +import poc.schema.BootStrap class ServletContextListener extends GuiceServletContextListener { def getInjector = Guice.createInjector(new ScalateModule() { override def configureServlets() = { super.configureServlets + + BootStrap.startDatabaseSession + BootStrap.testBootStrapDB + bind(classOf[TimetableService]).to(classOf[TimetableServiceImpl]) // bind(classOf[MessageBodyReader[Object]]).to(classOf[JacksonJsonProvider]) // bind(classOf[MessageBodyWriter[Object]]).to(classOf[JacksonJsonProvider]) diff --git a/src/main/scala/poc/model/Cashier.scala b/src/main/scala/poc/model/Cashier.scala new file mode 100644 index 0000000..84431e0 --- /dev/null +++ b/src/main/scala/poc/model/Cashier.scala @@ -0,0 +1,8 @@ +package poc.model + +import org.squeryl.KeyedEntity + +class Cashier ( + val name: String, + val id: Long = 0 +) extends KeyedEntity[Long] diff --git a/src/main/scala/poc/model/Timetable.scala b/src/main/scala/poc/model/Timetable.scala new file mode 100644 index 0000000..00e1d33 --- /dev/null +++ b/src/main/scala/poc/model/Timetable.scala @@ -0,0 +1,9 @@ +package poc.model +import org.squeryl.KeyedEntity +import java.sql.Timestamp + +class Timetable ( + val startWeek: Timestamp, + val cashierId: Long, + val id: Long = 0 +) extends KeyedEntity[Long] diff --git a/src/main/scala/poc/schema/BootStrap.scala b/src/main/scala/poc/schema/BootStrap.scala new file mode 100644 index 0000000..2e88b71 --- /dev/null +++ b/src/main/scala/poc/schema/BootStrap.scala @@ -0,0 +1,39 @@ +package poc.schema +import org.squeryl.SessionFactory +import org.squeryl.Session +import org.squeryl.adapters.H2Adapter +import org.squeryl.PrimitiveTypeMode._ +import poc.model.Cashier +import poc.model.Timetable +import java.sql.Timestamp + +object BootStrap { + + def startDatabaseSession() { + Class.forName("org.h2.Driver"); + SessionFactory.concreteFactory = Some(()=> + Session.create( + java.sql.DriverManager.getConnection("jdbc:h2:mem:example", "sa", ""), + new H2Adapter) + ) + } + + def testBootStrapDB () { + inTransaction { + import poc.schema.Schema._ + + drop // Bad idea in production application !!! + create + printDdl + + cashiers.insert(new Cashier("Blip")) + cashiers.insert(new Cashier("Blop")) + + timetables.insert(new Timetable(new Timestamp(System.currentTimeMillis), 1L)) + val queried = cashiers.where(cashier => cashier.id === 2L).single + println(queried.id + " -- " + queried.name) + + } + } + +} diff --git a/src/main/scala/poc/schema/Schema.scala b/src/main/scala/poc/schema/Schema.scala new file mode 100644 index 0000000..3b80099 --- /dev/null +++ b/src/main/scala/poc/schema/Schema.scala @@ -0,0 +1,22 @@ +package poc.schema + +import org.squeryl._ +import org.squeryl.PrimitiveTypeMode._ +import poc.model.Cashier +import poc.model.Timetable + +object Schema extends Schema { + + val cashiers = table[Cashier] + val timetables = table[Timetable] + + on(cashiers)(cashier => declare( + cashier.id is (autoIncremented), + cashier.name is (unique) + )) + + on(timetables)(timetable => declare( + timetable.id is (autoIncremented) + )) + +}