From 6a8c434a84c09a2be8f6f948df54b899d96a9914 Mon Sep 17 00:00:00 2001 From: brianjlowe Date: Thu, 15 Dec 2011 15:08:17 +0000 Subject: [PATCH] NIHVIVO-3510 modified sparql query builder to manufacture prefixes for any namespaces that need them --- .../vitro/webapp/sparql/GetAllPrefix.java | 87 +++++++++++++------ webapp/web/js/sparql/sparql.js | 15 ++-- 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetAllPrefix.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetAllPrefix.java index c2c8047f1..10350062a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetAllPrefix.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetAllPrefix.java @@ -4,8 +4,10 @@ package edu.cornell.mannlib.vitro.webapp.sparql; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; +import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; @@ -13,7 +15,6 @@ import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.mindswap.pellet.jena.vocabulary.SWRL; import org.openrdf.model.vocabulary.OWL; import org.openrdf.model.vocabulary.RDF; @@ -23,10 +24,12 @@ import com.hp.hpl.jena.vocabulary.XSD; import edu.cornell.mannlib.vedit.controller.BaseEditController; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.UseMiscellaneousPages; +import edu.cornell.mannlib.vitro.webapp.beans.BaseResourceBean; import edu.cornell.mannlib.vitro.webapp.beans.Ontology; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; +import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; /** * This servlet gets all the prefix for initizing the sparql query builder. @@ -58,43 +61,75 @@ public class GetAllPrefix extends BaseEditController { return; } - VitroRequest vreq = new VitroRequest(request); - - // EditProcessObject epo = super.createEpo(request); - OntologyDao daoObj = vreq.getFullWebappDaoFactory().getOntologyDao(); - List ontologiesObj = daoObj.getAllOntologies(); - ArrayList prefixList = new ArrayList(); - + VitroRequest vreq = new VitroRequest(request); + Map prefixMap = getPrefixMap(vreq.getFullWebappDaoFactory()); + response.setContentType("text/xml"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); String respo = ""; respo += ""; - if (ontologiesObj != null && ontologiesObj.size() > 0) { - - Iterator ontItr = ontologiesObj.iterator(); - while (ontItr.hasNext()) { - Ontology ont = (Ontology) ontItr.next(); - if (ont.getPrefix() != null) { - respo += makeOption(ont.getPrefix(), ont.getURI()); - } - } - + for (String namespace : prefixMap.keySet()) { + respo += makeOption(prefixMap.get(namespace), namespace); } - ; - respo += makeOption("owl", OWL.NAMESPACE); - respo += makeOption("rdf", RDF.NAMESPACE); - respo += makeOption("rdfs", RDFS.getURI()); - respo += makeOption("swrl", "http://www.w3.org/2003/11/swrl#"); - respo += makeOption("swrlb", "http://www.w3.org/2003/11/swrlb#"); - respo += makeOption("xsd", XSD.getURI()); - respo += makeOption("vitro", VitroVocabulary.vitroURI); respo += ""; out.println(respo); out.flush(); out.close(); } + /** + * Returns a map to prefixes for use in building queries. Will manufacture a + * prefix for any namespace that doesn't have an associated owl:Ontology resource + * with a prefix annotation + * @param wadf + * @return map of namespace URIs to prefix strings + */ + private Map getPrefixMap(WebappDaoFactory wadf) { + Map prefixMap = new HashMap(); + + OntologyDao oDao = wadf.getOntologyDao(); + for(Ontology o : oDao.getAllOntologies()) { + if (o.getPrefix() != null) { + prefixMap.put(o.getPrefix(), o.getURI()); + } + } + + // add standard namespaces + addPrefixIfNecessary("owl", OWL.NAMESPACE, prefixMap); + addPrefixIfNecessary("rdf", RDF.NAMESPACE, prefixMap); + addPrefixIfNecessary("rdfs", RDFS.getURI(), prefixMap); + addPrefixIfNecessary("swrl", "http://www.w3.org/2003/11/swrl#", prefixMap); + addPrefixIfNecessary("swrlb", "http://www.w3.org/2003/11/swrlb#", prefixMap); + addPrefixIfNecessary("xsd", XSD.getURI(), prefixMap); + addPrefixIfNecessary("vitro", VitroVocabulary.vitroURI, prefixMap); + + // we also need to manufacture prefixes for namespaces used by any class or + // property, regardless of whether there's an associated owl:Ontology. + int newPrefixCount = 0; + List ontEntityList = new ArrayList(); + ontEntityList.addAll(wadf.getVClassDao().getAllVclasses()); + ontEntityList.addAll(wadf.getObjectPropertyDao().getAllObjectProperties()); + ontEntityList.addAll(wadf.getDataPropertyDao().getAllDataProperties()); + for (BaseResourceBean ontEntity : ontEntityList) { + if (!ontEntity.isAnonymous() + && !prefixMap.containsKey(ontEntity.getNamespace())) { + newPrefixCount++; + prefixMap.put(ontEntity.getNamespace(), "p." + Integer.toString( + newPrefixCount)); + } + } + + return prefixMap; + } + + private void addPrefixIfNecessary(String prefix, String namespace, + Map prefixMap) { + if (!prefixMap.containsKey(namespace)) { + prefixMap.put(namespace, prefix); + } + } + /** * Makes the markup for a prefix option * @param prefix diff --git a/webapp/web/js/sparql/sparql.js b/webapp/web/js/sparql/sparql.js index d554081af..90c34e16c 100644 --- a/webapp/web/js/sparql/sparql.js +++ b/webapp/web/js/sparql/sparql.js @@ -1,12 +1,13 @@ /* $This file is distributed under the terms of the license in /doc/license.txt$ */ var namespaces = { - rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", - rdfs : "http://www.w3.org/2000/01/rdf-schema#", - xsd : "http://www.w3.org/2001/XMLSchema#", - owl : "http://www.w3.org/2002/07/owl#", - swrl : "http://www.w3.org/2003/11/swrl#", - swrlb : "http://www.w3.org/2003/11/swrlb#", - vitro : "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#" +// now handled in GetAllPrefix.java +// rdf : "http://www.w3.org/1999/02/22-rdf-syntax-ns#", +// rdfs : "http://www.w3.org/2000/01/rdf-schema#", +// xsd : "http://www.w3.org/2001/XMLSchema#", +// owl : "http://www.w3.org/2002/07/owl#", +// swrl : "http://www.w3.org/2003/11/swrl#", +// swrlb : "http://www.w3.org/2003/11/swrlb#", +// vitro : "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#" }; var level = 0;