+ Direct Mapping web application for easy testing
authorAlexandre Bertails <bertails@w3.org>
Wed, 02 Feb 2011 16:18:13 -0500
changeset 315 e0928b57615b
parent 314 238ca9b12a84
child 316 a555d4d071aa
+ Direct Mapping web application for easy testing
directmapping-webapp/.ensime
directmapping-webapp/src/main/scala/Servlet.scala
directmapping-webapp/src/main/webapp/WEB-INF/web.xml
project/build/RDB2RDF.scala
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/directmapping-webapp/.ensime	Wed Feb 02 16:18:13 2011 -0500
@@ -0,0 +1,9 @@
+;; This config was generated using ensime-config-gen. Feel free to customize its contents manually.
+
+(
+
+:project-package "org.w3"
+
+:use-sbt t
+
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/directmapping-webapp/src/main/scala/Servlet.scala	Wed Feb 02 16:18:13 2011 -0500
@@ -0,0 +1,100 @@
+package org.w3.directmapping.servlet
+
+import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse} 
+import scala.xml.XML
+
+import org.w3.rdf._
+import org.w3.rdb.RDB._
+import org.w3.sql
+import org.w3.rdf.turtle.TurtleModule
+import org.w3.directmapping.DirectMappingModule
+import java.io.File
+
+import org.w3.rdf.jena._
+
+class DirectMappingWebapp extends HttpServlet with JenaModel with DirectMappingModule with TurtleModule {
+
+  import DirectMapping._
+
+  val SQLParser = sql.SqlParser()
+
+  val turtleParser = new TurtleParser { }
+
+  val defaultSQL = 
+"""CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
+INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
+CREATE TABLE People (ID INT PRIMARY KEY, fname STRING, addr INT, FOREIGN KEY (addr) REFERENCES Addresses(ID));
+INSERT INTO People (ID, fname, addr) VALUES (7, "Bob", 18);
+INSERT INTO People (ID, fname, addr) VALUES (8, "Sue", NULL);
+"""
+
+  val encoding = "utf-8"
+
+  override def doPost(request:HttpServletRequest, response:HttpServletResponse) = {
+    request.getParameter("sql") match {
+      case null | "" => processIndex(request, response)
+      case sql       => processSQL(request, response, sql)
+    }
+  }
+
+  def jenaSerializer(g:Graph):String = {
+    val m = com.hp.hpl.jena.rdf.model.ModelFactory.createModelForGraph(g.jenaGraph)
+    val s = new java.io.StringWriter
+    m.write(s, "N-TRIPLE")
+    s.toString
+  }
+
+  override def doGet(request:HttpServletRequest, response:HttpServletResponse) =
+    processIndex(request, response)
+
+  def render(sql:String, result:Option[String]) =
+    <html xmlns="http://www.w3.org/1999/xhtml">
+      <head><title>Direct Mapping</title></head>
+      <body>
+        <h1>Try the <a href="http://www.w3.org/2001/sw/rdb2rdf/directMapping/">Direct Mapping</a> application!</h1>
+        { if (result isDefined) <pre name="result" id="result">{ result.get }</pre> }
+        <form method="POST" action="/">
+          <p>
+            <textarea rows="15" cols="80" name="sql" id="sql">{ sql }</textarea>
+            <input type="submit" value="submit SQL" />
+          </p>
+        </form>
+        <hr />
+        <address>
+          <a href="http://www.w3.org/People/Eric/">Eric Prud'hommeaux</a>, <a href="http://www.w3.org/People/Bertails/">Alexandre Bertails</a>, Feb 2011
+        </address>
+      </body>
+    </html>
+
+  def processSQL(request:HttpServletRequest, response:HttpServletResponse, sql:String) {
+
+    val result =
+      try {
+        val db = SQLParser.toDB(sql)
+        DirectMapping.HierarchyDetection = true
+        val computedGraph:Graph = directDB(db)
+        jenaSerializer(computedGraph)
+      } catch {
+        case e => e.getMessage
+      }
+
+    val html = render(sql, Some(result))
+
+    response.setContentType("application/xml; charset='" + encoding + "'")
+
+    XML.write(response.getWriter, html, encoding, false, null) 
+
+  }
+
+  def processIndex(request:HttpServletRequest, response:HttpServletResponse) {
+
+    val index = render(defaultSQL, None)
+
+    response.setContentType("application/xml; charset='" + encoding + "'")
+
+    XML.write(response.getWriter, index, encoding, false, null) 
+
+  }
+
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/directmapping-webapp/src/main/webapp/WEB-INF/web.xml	Wed Feb 02 16:18:13 2011 -0500
@@ -0,0 +1,10 @@
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
+  <servlet>
+    <servlet-name>directmapping-webapp</servlet-name>
+    <servlet-class>org.w3.directmapping.servlet.DirectMappingWebapp</servlet-class>
+  </servlet>
+  <servlet-mapping>
+    <servlet-name>directmapping-webapp</servlet-name>
+    <url-pattern>/*</url-pattern>
+  </servlet-mapping>
+</web-app>
--- a/project/build/RDB2RDF.scala	Wed Feb 02 10:47:33 2011 -0500
+++ b/project/build/RDB2RDF.scala	Wed Feb 02 16:18:13 2011 -0500
@@ -33,6 +33,7 @@
   lazy val jenaTest = project("jena-test", "jena-test", new JenaTest(_), jena, rdfmodelTest, directmappingTest, turtleTest, rdfxmlTest)
   lazy val sharedtestdata = project("sharedtestdata", "sharedtestdata", new SharedTestData(_), rdb, rdf, sql, turtle)
   lazy val directmapping = project("directmapping", "directmapping", new DirectMapping(_), rdb, rdf, sql, sharedtestdata)
+  lazy val directmappingWebapp = project("directmapping-webapp", "directmapping-webapp", new DirectMappingWebapp(_), directmapping, jena)
   lazy val directmappingTest = project("directmapping-test", "directmapping-test", new DirectMappingTest(_), directmapping)
   lazy val sparql = project("sparql", "sparql", new SPARQL(_), rdf)
   lazy val sparql2sql = project("sparql2sql", "sparql2sql", new SPARQL2SQL(_), sparql, sql)
@@ -75,6 +76,12 @@
 
   class DirectMapping(info: ProjectInfo) extends DefaultProject(info) with Common
 
+  class DirectMappingWebapp(info: ProjectInfo) extends DefaultWebProject(info) with Common {
+    val jettyConf = config("jetty")
+    val jettyDep = "org.eclipse.jetty" % "jetty-webapp" % "7.0.2.v20100331" % "compile,jetty"
+    override def jettyClasspath = managedClasspath(jettyConf)
+  }
+
   class DirectMappingTest(info: ProjectInfo) extends DefaultProject(info) with TestSuite
 
   class SPARQL(info: ProjectInfo) extends DefaultProject(info) with Common