Revision: 1814
Author: [email protected]
Date: Fri Jan  8 09:33:39 2010
Log: Basic Wicket frontend for SPARQL query support (Issue 116) 
http://code.google.com/p/simal/source/detail?r=1814

Added:
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/SparqlQueryPage.html /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/SparqlQueryPage.java /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/data/SparqlQueryInputModel.java
Modified:
/trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.html /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.java

=======================================
--- /dev/null
+++ /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/SparqlQueryPage.html Fri Jan 8 09:33:39 2010
@@ -0,0 +1,44 @@
+<!--
+  Copyright 2010 University of Oxford
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<html>
+<head>
+  <title>Query the Simal Project Registry</title>
+</head>
+<body>
+<wicket:extend>
+  <h2>SPARQL Query</h2>
+
+  <p class="warning">This form allows raw querying of the data</p>
+
+  <form wicket:id="sparqlQueryForm">
+    <fieldset>
+    <legend>SPARQLQuery</legend>
+         <p>
+ <textarea wicket:id="sparqlQueryString" cols="80" rows="15">Type your query here.</textarea>
+         </p>
+       </fieldset>
+
+         <p>
+             <input type="submit" value="Execute" />
+         </p>
+  </form>
+  <span wicket:id="queryResults">
+    <span wicket:id="queryResult" /><br />
+  </span>
+
+</wicket:extend>
+</body>
+</html>
=======================================
--- /dev/null
+++ /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/SparqlQueryPage.java Fri Jan 8 09:33:39 2010
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2008 University of Oxford
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");   *
+ * you may not use this file except in compliance with the License.  *
+ * You may obtain a copy of the License at                           *
+ *                                                                   *
+ *   http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                   *
+ * Unless required by applicable law or agreed to in writing,        *
+ * software distributed under the License is distributed on an       *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY            *
+ * KIND, either express or implied.  See the License for the         *
+ * specific language governing permissions and limitations           *
+ * under the License.                                                *
+ */
+package uk.ac.osswatch.simal.wicket;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Vector;
+
+import org.apache.commons.lang.StringEscapeUtils;
+import org.apache.wicket.PageParameters;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.form.Form;
+import org.apache.wicket.markup.html.form.TextArea;
+import org.apache.wicket.markup.html.list.ListItem;
+import org.apache.wicket.markup.html.list.ListView;
+import org.apache.wicket.model.PropertyModel;
+
+import uk.ac.osswatch.simal.model.jena.SparqlResult;
+import uk.ac.osswatch.simal.model.jena.simal.JenaSimalRepository;
+import uk.ac.osswatch.simal.rdf.ISimalRepository;
+import uk.ac.osswatch.simal.rdf.SimalRepositoryException;
+import uk.ac.osswatch.simal.wicket.data.SparqlQueryInputModel;
+import uk.ac.osswatch.simal.wicket.doap.DoapFormPage;
+
+import com.hp.hpl.jena.rdf.model.RDFNode;
+
+/**
+ * A page for querying the RDF backend using SPARQL
+ *
+ * @author svanderwaal
+ *
+ */
+public class SparqlQueryPage extends BasePage {
+
+ private static final String SPARQL_QUERY_STRING_FIELD = "sparqlQueryString";
+
+ private static final String QUERY_PREFIX = " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> \n"
+      + " PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+      + " PREFIX  doap: <http://usefulinc.com/ns/doap#> \n"
+      + " PREFIX  simal: <http://oss-watch.ac.uk/ns/0.2/simal#> \n\n";
+
+  private final ListView<ListItem<String>> queryListView;
+
+ private static final List<List<String>> queryList = new Vector<List<String>>();
+
+  /**
+   * Create default SPARQL query page
+   */
+  @SuppressWarnings("unchecked")
+  public SparqlQueryPage(PageParameters parameters) {
+    super();
+
+    add(new SparqlQueryForm("sparqlQueryForm"));
+
+    add(queryListView = new ListView("queryResults", queryList) {
+      private static final long serialVersionUID = 2012355500166601921L;
+
+      public void populateItem(final ListItem listItem) {
+ final List<String> resultRow = (List<String>) listItem.getModelObject(); + listItem.add(new Label("queryResult", processResultRow(resultRow)));
+      }
+
+      /**
+ * TODO Query result representation should be a table but don't know how
+       * to create a table with a dynamic nr. of columns in Wicket
+       *
+       * @param resultRow
+       * @return
+       */
+      private String processResultRow(List<String> resultRow) {
+        String result = "";
+        for (String cell : resultRow) {
+          if (!"".equals(result)) {
+            result += ", ";
+          }
+          result += cell;
+        }
+        return result;
+      }
+    });
+  }
+
+  /**
+ * Query the backend db with the queryStr and return result as received from
+   * backend.
+   *
+   * @param queryStr
+   * @return
+   */
+  private SparqlResult getSparqlQueryResults(String queryStr) {
+    String sparqlQuery = StringEscapeUtils.unescapeXml(queryStr);
+    SparqlResult result = null;
+    try {
+      ISimalRepository repo = UserApplication.getRepository();
+      if (repo instanceof JenaSimalRepository) {
+ result = ((JenaSimalRepository) repo).getSparqlQueryResult(sparqlQuery);
+      }
+
+    } catch (SimalRepositoryException e) {
+      setResponsePage(new ErrorReportPage(new UserReportableException(
+ "Unable to add doap using RDF supplied", DoapFormPage.class, e)));
+    }
+    return result;
+  }
+
+  /**
+   * Update display list with new query result.
+   *
+   * @param sparqlResults
+   */
+  private void processQueryResults(SparqlResult sparqlResults) {
+    queryList.clear();
+    queryList.add(sparqlResults.getVarNames());
+ Iterator<List<RDFNode>> resultIter = sparqlResults.getResultsIterator();
+    while (resultIter.hasNext()) {
+      List<RDFNode> result = resultIter.next();
+      List<String> displayRow = new Vector<String>();
+      for (RDFNode cell : result) {
+        displayRow.add(cell.toString());
+      }
+      queryList.add(displayRow);
+    }
+  }
+
+  /**
+   * Form for entering the SPARQL query in the web page.
+   *
+   */
+  private class SparqlQueryForm extends Form<SparqlQueryInputModel> {
+    private static final long serialVersionUID = 1005521653425155426L;
+
+ private final SparqlQueryInputModel inputModel = new SparqlQueryInputModel();
+
+    public SparqlQueryForm(String id) {
+      super(id);
+ TextArea<SparqlQueryInputModel> sparqlQueryField = new TextArea<SparqlQueryInputModel>( + SPARQL_QUERY_STRING_FIELD, new PropertyModel<SparqlQueryInputModel>(
+              inputModel, SPARQL_QUERY_STRING_FIELD));
+      sparqlQueryField.add(new FocusBehaviour());
+      String[] defaultValue = { QUERY_PREFIX };
+      sparqlQueryField.setModelValue(defaultValue);
+      add(sparqlQueryField);
+    }
+
+    @Override
+    protected void onSubmit() {
+      super.onSubmit();
+      String sparqlQuery = inputModel.getSparqlQueryString();
+      if (sparqlQuery != null && !"".equals(sparqlQuery)) {
+        SparqlResult sparqlResults = getSparqlQueryResults(sparqlQuery);
+        processQueryResults(sparqlResults);
+        queryListView.modelChanged();
+      }
+    }
+
+  }
+}
=======================================
--- /dev/null
+++ /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/data/SparqlQueryInputModel.java Fri Jan 8 09:33:39 2010
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2010 University of Oxford
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");   *
+ * you may not use this file except in compliance with the License.  *
+ * You may obtain a copy of the License at                           *
+ *                                                                   *
+ *   http://www.apache.org/licenses/LICENSE-2.0                      *
+ *                                                                   *
+ * Unless required by applicable law or agreed to in writing,        *
+ * software distributed under the License is distributed on an       *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY            *
+ * KIND, either express or implied.  See the License for the         *
+ * specific language governing permissions and limitations           *
+ * under the License.                                                *
+ */
+package uk.ac.osswatch.simal.wicket.data;
+
+import org.apache.wicket.IClusterable;
+
+/**
+ * Provide inputs for the SPARQL query page
+ * @author svanderwaal
+ *
+ */
+public class SparqlQueryInputModel implements IClusterable {
+
+  private static final long serialVersionUID = -638867731076925909L;
+
+  private String sparqlQueryString;
+
+  public void setSparqlQueryString(String sparqlQueryString) {
+    this.sparqlQueryString = sparqlQueryString;
+  }
+
+  public String getSparqlQueryString() {
+    return sparqlQueryString;
+  }
+}
=======================================
--- /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.html Sun Dec 13 15:18:48 2009 +++ /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.html Fri Jan 8 09:33:39 2010
@@ -88,6 +88,7 @@
<p>The tools in this section are pre-alpha and should be used at your own risk.</p>

   <p><a href="#" wicket:id="settingsPageLink">Settings</a></p>
+  <p><a href="#" wicket:id="sparqlQueryPageLink">SPARQL querying</a></p>

     <form wicket:id="importProgrammesFromPimsForm">
       <fieldset>
=======================================
--- /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.java Sun Dec 13 15:18:48 2009 +++ /trunk/uk.ac.osswatch.simal.web/src/main/java/uk/ac/osswatch/simal/wicket/ToolsPage.java Fri Jan 8 09:33:39 2010
@@ -135,7 +135,8 @@
       }
     });

-    add(new BookmarkablePageLink("settingsPageLink", SettingsPage.class));
+ add(new BookmarkablePageLink<SettingsPage>("settingsPageLink", SettingsPage.class)); + add(new BookmarkablePageLink<SparqlQueryPage>("sparqlQueryPageLink", SparqlQueryPage.class));

     add(new ImportFromOhlohForm("importFromOhlohForm"));

-- 
You received this message because you are subscribed to the Google Groups 
"Simal Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/simal-commits?hl=en.


Reply via email to