~ the webapp features the full chain: DM + CONSTRUCT
authorAlexandre Bertails <bertails@w3.org>
Tue, 03 May 2011 13:43:48 -0400
changeset 370 3a9aa3b600f8
parent 369 b06800284060
child 371 25da059442b5
~ the webapp features the full chain: DM + CONSTRUCT
directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala
directmapping-webapp/src/main/webapp/index.html
project/build/RDB2RDF.scala
rdf2rdf/src/main/scala/RDF2RDF.scala
--- a/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Tue May 03 10:23:36 2011 -0400
+++ b/directmapping-webapp/src/main/scala/code/snippet/HelloWorld.scala	Tue May 03 13:43:48 2011 -0400
@@ -25,11 +25,11 @@
 import org.w3.directmapping.DirectMappingModule
 import org.w3.rdf.jena._
 
-
+import org.w3.rdf2rdf._
 
 import net.liftweb.http.rest.RestHelper
 
-object Services extends RestHelper {
+object Services extends RestHelper with RDF2RDFModule with DirectMappingModule {
   serve {
     case "ddl" :: ddl :: Nil Get _ => JsonResponse(JsRaw(QueryManager.getQueries(ddl)))
     case "ddls" :: Nil Get _ => {
@@ -43,15 +43,38 @@
         val db = SQLParser.toDB(S.param("ddl").openOr(""))
         PlainTextResponse("DDL successfully parsed")
       } catch {
-        case e => PlainTextResponse(e.toString)
+        case e => PlainTextResponse(e.getMessage, 500)
       }
     }
+    case "parse-construct" :: Nil Post _ => {
+      try {
+        val construct = Construct(S.param("construct").openOr(""))
+        PlainTextResponse("SPARQL CONSTRUCT successfully parsed")
+      } catch {
+        case e => PlainTextResponse(e.getMessage, 500)
+      }
+    }
+    case "dm-then-construct" :: Nil Post _ => {
+      try {
+        import DirectMapping._
+        val SQLParser = sql.SqlParser()
+        val db = SQLParser.toDB(S.param("ddl").openOr(""))
+        DirectMapping.MinEncode = false
+        val computedGraph:Graph = databaseSemantics(db)
+        val construct = Construct(S.param("construct").openOr(""))
+        val resultGraph = RDF2RDF(computedGraph) | construct
+        PlainTextResponse(this.toString(resultGraph))
+      } catch {
+        case e => PlainTextResponse(e.getMessage, 500)
+      }
+    }
+
   }
 }
 
 
 
-class DirectMapping extends JenaModel with DirectMappingModule with TurtleModule {
+class DirectMapping extends JenaModel with RDF2RDFModule with DirectMappingModule with TurtleModule {
 
   import DirectMapping._
 
--- a/directmapping-webapp/src/main/webapp/index.html	Tue May 03 10:23:36 2011 -0400
+++ b/directmapping-webapp/src/main/webapp/index.html	Tue May 03 13:43:48 2011 -0400
@@ -14,19 +14,32 @@
     .interaction-area {position:absolute; top:0; right:0; width:39%; height:100%}
     .query-box input[type=text] {width:100%; margin:0; padding:2px}
     div.message {width:100%; position:absolute; top:1.6em; bottom:6px}
-    div.message textarea {width:100%; height:100%; resize:none; margin:0; padding:2px}
+    div.message textarea {width:100%; height:100%; resize:none; margin:0; padding:2px; color:black}
+    div.message textarea.error {color:red}
+    .error {color:red}
     </style>
 
 <script type="text/javascript">
 // <![CDATA[
 jQuery(document).ready(function() {
 
+  var dmThenConstruct = function() {
+    var ddl = $("#ddl").val();
+    var construct = $("#construct").val();
+    jQuery.ajax({type: "POST",
+                 url: '/dm-then-construct',
+                 data: 'ddl='+ddl+'&construct='+construct,
+                 success: function(data) { jQuery("#result").removeClass("error"); jQuery("#result").val(data); },
+                 error: function(xhr) { jQuery("#result").addClass("error"); jQuery("#result").val(xhr.responseText); }
+                })
+  }
+
   jQuery("#ddl").watermark(jQuery("#ddl").val());
 
   jQuery("#ddl-name").autocomplete("/ddls", {minChars:0,matchContains:true}).result(function(event, dt, ddlname) {
     jQuery("#ddl-name").val(ddlname);
     jQuery.ajax({url: '/ddl/'+ddlname,
-                 success: function(data) { jQuery("#ddl").val(data); jQuery("#ddl-message").val("DDL successfully loaded") }
+                 success: function(data) { jQuery("#ddl").val(data); jQuery("#ddl-message").val("DDL successfully loaded"); dmThenConstruct(); }
                 });
   });
 
@@ -35,7 +48,22 @@
       jQuery.ajax({type: "POST",
                    url: '/parse-ddl',
                    data: 'ddl='+$('#ddl').val(),
-                   success: function(data) { jQuery("#ddl-message").val(data); }
+                   success: function(data) { jQuery("#ddl-message").removeClass("error"); jQuery("#ddl-message").val(data); dmThenConstruct(); },
+                   error: function(xhr) { jQuery("#ddl-message").addClass("error"); jQuery("#ddl-message").val(xhr.responseText); }
+                  })
+    },
+    wait: 1000,
+    highlight: false,
+    captureLength: 1
+  });
+
+  jQuery("#construct").typeWatch({
+    callback: function() {
+      jQuery.ajax({type: "POST",
+                   url: '/parse-construct',
+                   data: 'construct='+$('#construct').val(),
+                   success: function(data) { jQuery("#construct-message").removeClass("error"); jQuery("#construct-message").val(data); dmThenConstruct(); },
+                   error: function(xhr) { jQuery("#construct-message").addClass("error"); jQuery("#construct-message").val(xhr.responseText); }
                   })
     },
     wait: 1000,
@@ -78,7 +106,7 @@
     If empty or non-valid SPARQL CONSTRUCT, this will be ignored.
 
     <div class="area">
-      <textarea rows="15" cols="80" id="sparql" class="query">CONSTRUCT {
+      <textarea rows="15" cols="80" id="construct" class="query">CONSTRUCT {
   ?s ?p ?o .
 } WHERE {
   ?s ?p ?o .
@@ -86,10 +114,10 @@
       <div class="interaction-area">
         <div class="query-management">
           <div class="query-box" >
-            <input type="text" value="" id="sparql-name" class="ac_input"/>
+            <input type="text" value="" id="construct-name" class="ac_input"/>
           </div>
-          <div id="sparql-message" class="message">
-            <textarea id="sparql-message" rows="5" cols="40" disabled="disabled"></textarea>
+          <div class="message">
+            <textarea id="construct-message" class="error" rows="5" cols="40" disabled="disabled"></textarea>
           </div>
         </div>
       </div>
--- a/project/build/RDB2RDF.scala	Tue May 03 10:23:36 2011 -0400
+++ b/project/build/RDB2RDF.scala	Tue May 03 13:43:48 2011 -0400
@@ -34,7 +34,7 @@
   lazy val rdf2rdf = project("rdf2rdf", "rdf2rdf", new RDF2RDF(_), jena, directmapping)
   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 directmappingWebapp = project("directmapping-webapp", "directmapping-webapp", new DirectMappingWebapp(_), directmapping, jena, rdf2rdf)
   lazy val directmappingTest = project("directmapping-test", "directmapping-test", new DirectMappingTest(_), directmapping, jena)
   lazy val sparql = project("sparql", "sparql", new SPARQL(_), rdf)
   lazy val sparql2sql = project("sparql2sql", "sparql2sql", new SPARQL2SQL(_), sparql, sql)
--- a/rdf2rdf/src/main/scala/RDF2RDF.scala	Tue May 03 10:23:36 2011 -0400
+++ b/rdf2rdf/src/main/scala/RDF2RDF.scala	Tue May 03 13:43:48 2011 -0400
@@ -8,22 +8,22 @@
   val base = "dm:"
 }
 
+case class Construct(query:Query) {
+  assert(query.getQueryType == Query.QueryTypeConstruct, "You must provide a valid SPARQL 1.1 Construct query")
+}
+
+object Construct {
+  def apply(query:String, base:String = RDF2RDFModule.base):Construct = {
+    val construct = QueryFactory.create(query, base, Syntax.syntaxSPARQL_11)
+    Construct(construct)
+  }
+}
+
+
 import RDF2RDFModule._
 
 trait RDF2RDFModule extends JenaModel {
 
-  case class Construct(query:Query) {
-    assert(query.getQueryType == Query.QueryTypeConstruct, "You must provide a valid SPARQL 1.1 Construct query")
-  }
-
-  object Construct {
-    def apply(query:String, base:String = base):Construct = {
-      val construct = QueryFactory.create(query, base, Syntax.syntaxSPARQL_11)
-      Construct(construct)
-    }
-  }
-
-
   case class RDF2RDF(graph:Graph) {
 
     val model = {
@@ -58,11 +58,16 @@
     return new Graph(model.getGraph)
   }
 
-  def dumpCutingBase(g:Graph):Unit = {
+  def toString(g:Graph):String = {
     val model = ModelFactory.createModelForGraph(g.jenaGraph)
     val buffer = new java.io.ByteArrayOutputStream
     model.write(buffer, "TURTLE", base)
     val result = buffer.toString("UTF-8").replaceAll("<"+base, "<")
+    result
+  }
+
+  def dumpCutingBase(g:Graph):Unit = {
+    val result = toString(g)
     println(result)
   }