SPARQL Query Builder v1.0 with data and object property buttons separately.

This commit is contained in:
yuysun 2010-11-17 00:15:14 +00:00
parent 174c548b27
commit f872582288
10 changed files with 3425 additions and 0 deletions

View file

@ -1292,6 +1292,62 @@
<url-pattern>/generic_delete</url-pattern>
</servlet-mapping>
<!-- ==================== sparql query builder ==================== -->
<servlet>
<servlet-name>SparlQueryBuilder</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.controller.SparqlQueryBuilderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SparlQueryBuilder</servlet-name>
<url-pattern>/admin/sparqlquerybuilder</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetAllClasses</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.sparql.GetAllClasses</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetAllClasses</servlet-name>
<url-pattern>/admin/getAllClasses</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetAllPrefix</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.sparql.GetAllPrefix</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetAllPrefix</servlet-name>
<url-pattern>/admin/getAllPrefix</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetClazzDataProperties</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.sparql.GetClazzDataProperties</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetClazzDataProperties</servlet-name>
<url-pattern>/admin/getClazzDataProperties</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetClazzObjectProperties</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.sparql.GetClazzObjectProperties</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetClazzObjectProperties</servlet-name>
<url-pattern>/admin/getClazzObjectProperties</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>GetObjectClasses</servlet-name>
<servlet-class>edu.cornell.mannlib.vitro.webapp.sparql.GetObjectClasses</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetObjectClasses</servlet-name>
<url-pattern>/admin/getObjectClasses</url-pattern>
</servlet-mapping>
<!-- ==================== tag libraries ============================== -->
<jsp-config>
<taglib>

View file

@ -0,0 +1,175 @@
/*
Copyright (c) 2010, Cornell University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of Cornell University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package edu.cornell.mannlib.vitro.webapp.controller;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.DataSource;
import com.hp.hpl.jena.query.DatasetFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.query.ResultSetFormatter;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelMaker;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.sparql.resultset.ResultSetFormat;
import com.hp.hpl.jena.vocabulary.XSD;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.Portal;
/* @author ass92 */
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
/**
* This servlet works as a RequestDispatcher to direct to the sparl query builder page.
* @author yuysun
*/
public class SparqlQueryBuilderServlet extends BaseEditController {
private static final Log log = LogFactory.getLog(SparqlQueryBuilderServlet.class.getName());
protected static final Syntax SYNTAX = Syntax.syntaxARQ;
protected static HashMap<String,ResultSetFormat>formatSymbols = new HashMap<String,ResultSetFormat>();
static{
formatSymbols.put( ResultSetFormat.syntaxXML.getSymbol(), ResultSetFormat.syntaxXML);
formatSymbols.put( ResultSetFormat.syntaxRDF_XML.getSymbol(), ResultSetFormat.syntaxRDF_XML);
formatSymbols.put( ResultSetFormat.syntaxRDF_N3.getSymbol(), ResultSetFormat.syntaxRDF_N3);
formatSymbols.put( ResultSetFormat.syntaxText.getSymbol() , ResultSetFormat.syntaxText);
formatSymbols.put( ResultSetFormat.syntaxJSON.getSymbol() , ResultSetFormat.syntaxJSON);
formatSymbols.put( "vitro:csv", null);
}
protected static HashMap<String,String> rdfFormatSymbols = new HashMap<String,String>();
static {
rdfFormatSymbols.put( "RDF/XML", "application/rdf+xml" );
rdfFormatSymbols.put( "RDF/XML-ABBREV", "application/rdf+xml" );
rdfFormatSymbols.put( "N3", "text/n3" );
rdfFormatSymbols.put( "N-TRIPLE", "text/plain" );
rdfFormatSymbols.put( "TTL", "application/x-turtle" );
}
protected static HashMap<String, String>mimeTypes = new HashMap<String,String>();
static{
mimeTypes.put( ResultSetFormat.syntaxXML.getSymbol() , "text/xml" );
mimeTypes.put( ResultSetFormat.syntaxRDF_XML.getSymbol(), "application/rdf+xml" );
mimeTypes.put( ResultSetFormat.syntaxRDF_N3.getSymbol(), "text/plain" );
mimeTypes.put( ResultSetFormat.syntaxText.getSymbol() , "text/plain");
mimeTypes.put( ResultSetFormat.syntaxJSON.getSymbol(), "application/javascript" );
mimeTypes.put( "vitro:csv", "text/csv");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request,response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
super.doGet(request, response);
if( !checkLoginStatus(request, response) )
return;
VitroRequest vreq = new VitroRequest(request);
Model model = vreq.getJenaOntModel(); // getModel()
if( model == null ){
doNoModelInContext(request,response);
return;
}
doHelp(request,response);
return;
}
private void doNoModelInContext(HttpServletRequest request, HttpServletResponse res){
try {
res.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
ServletOutputStream sos = res.getOutputStream();
sos.println("<html><body>this service is not supporeted by the current " +
"webapp configuration. A jena model is required in the servlet context.</body></html>" );
} catch (IOException e) {
log.error("Could not write to ServletOutputStream");
}
}
private void doHelp(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
//res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
VitroRequest vreq = new VitroRequest(req);
Portal portal = vreq.getPortal();
/* Code change completed */
req.setAttribute("portalBean",portal);
// nac26: 2009-09-25 - this was causing problems in safari on localhost installations because the href did not include the context. The edit.css is not being used here anyway (or anywhere else for that matter)
// req.setAttribute("css", "<link rel=\"stylesheet\" type=\"text/css\" href=\""+portal.getThemeDir()+"css/edit.css\"/>");
req.setAttribute("title","SPARQL Query Builder");
req.setAttribute("bodyJsp", "/admin/sparql.jsp");
RequestDispatcher rd = req.getRequestDispatcher("/"+Controllers.BASIC_JSP);
rd.forward(req,res);
}
}

View file

@ -0,0 +1,144 @@
package edu.cornell.mannlib.vitro.webapp.sparql;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.EditProcessObject;
import edu.cornell.mannlib.vedit.beans.FormObject;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.edit.SiteAdminController;
/**
* This servlet gets all the classes for initizing the sparql query builder.
*
* @author yuysun
*/
public class GetAllClasses extends BaseEditController {
private static final Log log = LogFactory.getLog(SiteAdminController.class
.getName());
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
super.doGet(request, response);
} catch (Exception e) {
e.printStackTrace();
}
VitroRequest vreq = new VitroRequest(request);
Object obj = vreq.getSession().getAttribute("loginHandler");
LoginFormBean loginHandler = null;
if (obj != null && obj instanceof LoginFormBean)
loginHandler = ((LoginFormBean) obj);
if (loginHandler == null
|| !"authenticated".equalsIgnoreCase(loginHandler
.getLoginStatus()) ||
// rjy7 Allows any editor (including self-editors) access to
// this servlet.
// This servlet is now requested via Ajax from some custom
// forms, so anyone
// using the custom form needs access rights.
Integer.parseInt(loginHandler.getLoginRole()) < LoginFormBean.NON_EDITOR) {
HttpSession session = request.getSession(true);
session.setAttribute("postLoginRequest", vreq.getRequestURI()
+ (vreq.getQueryString() != null ? ('?' + vreq
.getQueryString()) : ""));
String redirectURL = request.getContextPath()
+ Controllers.SITE_ADMIN + "?login=block";
response.sendRedirect(redirectURL);
return;
}
// EditProcessObject epo = super.createEpo(request);
List classGroups = vreq.getFullWebappDaoFactory().getVClassGroupDao()
.getPublicGroupsWithVClasses(true, true, false); // order by
// displayRank,
// include
// uninstantiated
// classes,
// don't get
// the
// counts of
// individuals
Iterator classGroupIt = classGroups.iterator();
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
respo += "<options>";
while (classGroupIt.hasNext()) {
VClassGroup group = (VClassGroup) classGroupIt.next();
List classes = group.getVitroClassList();
Iterator classIt = classes.iterator();
while (classIt.hasNext()) {
VClass clazz = (VClass) classIt.next();
respo += "<option>" + "<key>" + clazz.getPickListName()
+ "</key>" + "<value>" + clazz.getURI() + "</value>"
+ "</option>";
}
}
respo += "</options>";
out.println(respo);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View file

@ -0,0 +1,139 @@
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 javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.EditProcessObject;
import edu.cornell.mannlib.vedit.beans.FormObject;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.Ontology;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.edit.SiteAdminController;
import edu.cornell.mannlib.vitro.webapp.dao.OntologyDao;
/**
* This servlet gets all the prefix for initizing the sparql query builder.
*
* @author yuysun
*/
public class GetAllPrefix extends BaseEditController {
private static final Log log = LogFactory.getLog(SiteAdminController.class
.getName());
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
super.doGet(request, response);
} catch (Exception e) {
e.printStackTrace();
}
VitroRequest vreq = new VitroRequest(request);
Object obj = vreq.getSession().getAttribute("loginHandler");
LoginFormBean loginHandler = null;
if (obj != null && obj instanceof LoginFormBean)
loginHandler = ((LoginFormBean) obj);
if (loginHandler == null
|| !"authenticated".equalsIgnoreCase(loginHandler
.getLoginStatus()) ||
// rjy7 Allows any editor (including self-editors) access to
// this servlet.
// This servlet is now requested via Ajax from some custom
// forms, so anyone
// using the custom form needs access rights.
Integer.parseInt(loginHandler.getLoginRole()) < LoginFormBean.NON_EDITOR) {
HttpSession session = request.getSession(true);
session.setAttribute("postLoginRequest", vreq.getRequestURI()
+ (vreq.getQueryString() != null ? ('?' + vreq
.getQueryString()) : ""));
String redirectURL = request.getContextPath()
+ Controllers.SITE_ADMIN + "?login=block";
response.sendRedirect(redirectURL);
return;
}
// EditProcessObject epo = super.createEpo(request);
OntologyDao daoObj = vreq.getFullWebappDaoFactory().getOntologyDao();
List ontologiesObj = daoObj.getAllOntologies();
ArrayList prefixList = new ArrayList();
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
respo += "<options>";
if (ontologiesObj != null && ontologiesObj.size() > 0) {
Iterator ontItr = ontologiesObj.iterator();
while (ontItr.hasNext()) {
Ontology ont = (Ontology) ontItr.next();
if (ont.getPrefix() != null) {
respo += "<option>" + "<key>" + ont.getPrefix() + "</key>"
+ "<value>" + ont.getURI() + "</value>"
+ "</option>";
}
}
}
respo += "</options>";
out.println(respo);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View file

@ -0,0 +1,147 @@
package edu.cornell.mannlib.vitro.webapp.sparql;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.DataProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.PropertyInstance;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.edit.SiteAdminController;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.DatatypeDao;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyGroupDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyInstanceDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
/**
* This servlet gets all the data properties for a given subject.
*
* @param vClassURI
* @author yuysun
*/
public class GetClazzDataProperties extends BaseEditController {
private static final Log log = LogFactory.getLog(SiteAdminController.class
.getName());
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
super.doGet(request, response);
} catch (Exception e) {
e.printStackTrace();
}
VitroRequest vreq = new VitroRequest(request);
Object obj = vreq.getSession().getAttribute("loginHandler");
LoginFormBean loginHandler = null;
if (obj != null && obj instanceof LoginFormBean)
loginHandler = ((LoginFormBean) obj);
if (loginHandler == null
|| !"authenticated".equalsIgnoreCase(loginHandler
.getLoginStatus()) ||
// rjy7 Allows any editor (including self-editors) access to
// this servlet.
// This servlet is now requested via Ajax from some custom
// forms, so anyone
// using the custom form needs access rights.
Integer.parseInt(loginHandler.getLoginRole()) < LoginFormBean.NON_EDITOR) {
HttpSession session = request.getSession(true);
session.setAttribute("postLoginRequest", vreq.getRequestURI()
+ (vreq.getQueryString() != null ? ('?' + vreq
.getQueryString()) : ""));
String redirectURL = request.getContextPath()
+ Controllers.SITE_ADMIN + "?login=block";
response.sendRedirect(redirectURL);
return;
}
String vClassURI = vreq.getParameter("vClassURI");
if (vClassURI == null || vClassURI.trim().equals("")) {
return;
}
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
respo += "<options>";
// Add rdfs:label to the list
respo += "<option>" + "<key>" + "label" + "</key>" + "<value>"
+ "http://www.w3.org/2000/01/rdf-schema#label" + "</value>"
+ "</option>";
DataPropertyDao ddao = vreq.getFullWebappDaoFactory()
.getDataPropertyDao();
Collection<DataProperty> dataProps = ddao
.getDataPropertiesForVClass(vClassURI);
Iterator<DataProperty> dataPropIt = dataProps.iterator();
HashSet<String> dpropURIs = new HashSet<String>();
while (dataPropIt.hasNext()) {
DataProperty dp = dataPropIt.next();
if (!(dpropURIs.contains(dp.getURI()))) {
dpropURIs.add(dp.getURI());
DataProperty dprop = (DataProperty) ddao
.getDataPropertyByURI(dp.getURI());
if (dprop != null) {
respo += "<option>" + "<key>" + dprop.getLocalName()
+ "</key>" + "<value>" + dprop.getURI()
+ "</value>" + "</option>";
}
}
}
respo += "</options>";
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(respo);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View file

@ -0,0 +1,159 @@
package edu.cornell.mannlib.vitro.webapp.sparql;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.PropertyInstance;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.edit.SiteAdminController;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.PropertyInstanceDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
/**
* This servlet gets all the object properties for a given subject.
*
* @param vClassURI
* @author yuysun
*/
public class GetClazzObjectProperties extends BaseEditController {
private static final Log log = LogFactory.getLog(SiteAdminController.class
.getName());
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
super.doGet(request, response);
} catch (Exception e) {
e.printStackTrace();
}
VitroRequest vreq = new VitroRequest(request);
Object obj = vreq.getSession().getAttribute("loginHandler");
LoginFormBean loginHandler = null;
if (obj != null && obj instanceof LoginFormBean)
loginHandler = ((LoginFormBean) obj);
if (loginHandler == null
|| !"authenticated".equalsIgnoreCase(loginHandler
.getLoginStatus()) ||
// rjy7 Allows any editor (including self-editors) access to
// this servlet.
// This servlet is now requested via Ajax from some custom
// forms, so anyone
// using the custom form needs access rights.
Integer.parseInt(loginHandler.getLoginRole()) < LoginFormBean.NON_EDITOR) {
HttpSession session = request.getSession(true);
session.setAttribute("postLoginRequest", vreq.getRequestURI()
+ (vreq.getQueryString() != null ? ('?' + vreq
.getQueryString()) : ""));
String redirectURL = request.getContextPath()
+ Controllers.SITE_ADMIN + "?login=block";
response.sendRedirect(redirectURL);
return;
}
String vClassURI = vreq.getParameter("vClassURI");
if (vClassURI == null || vClassURI.trim().equals("")) {
return;
}
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
respo += "<options>";
ObjectPropertyDao odao = vreq.getFullWebappDaoFactory()
.getObjectPropertyDao();
PropertyInstanceDao piDao = vreq.getFullWebappDaoFactory()
.getPropertyInstanceDao();
VClassDao vcDao = vreq.getFullWebappDaoFactory().getVClassDao();
// incomplete list of classes to check, but better than before
List<String> superclassURIs = vcDao.getAllSuperClassURIs(vClassURI);
superclassURIs.add(vClassURI);
superclassURIs.addAll(vcDao.getEquivalentClassURIs(vClassURI));
Map<String, PropertyInstance> propInstMap = new HashMap<String, PropertyInstance>();
for (String classURI : superclassURIs) {
Collection<PropertyInstance> propInsts = piDao
.getAllPropInstByVClass(classURI);
try {
for (PropertyInstance propInst : propInsts) {
propInstMap.put(propInst.getPropertyURI(), propInst);
}
} catch (NullPointerException ex) {
continue;
}
}
List<PropertyInstance> propInsts = new ArrayList<PropertyInstance>();
propInsts.addAll(propInstMap.values());
Collections.sort(propInsts);
Iterator propInstIt = propInsts.iterator();
HashSet opropURIs = new HashSet();
while (propInstIt.hasNext()) {
PropertyInstance pi = (PropertyInstance) propInstIt.next();
if (!(opropURIs.contains(pi.getPropertyURI()))) {
opropURIs.add(pi.getPropertyURI());
ObjectProperty oprop = (ObjectProperty) odao
.getObjectPropertyByURI(pi.getPropertyURI());
if (oprop != null) {
respo += "<option>" + "<key>" + oprop.getLocalName()
+ "</key>" + "<value>" + oprop.getURI()
+ "</value>" + "</option>";
}
}
}
respo += "</options>";
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println(respo);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

View file

@ -0,0 +1,181 @@
package edu.cornell.mannlib.vitro.webapp.sparql;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vedit.beans.LoginFormBean;
import edu.cornell.mannlib.vedit.controller.BaseEditController;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
import edu.cornell.mannlib.vitro.webapp.beans.VClassGroup;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.edit.SiteAdminController;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
/**
* This servlet gets all the range classes for a given predicate.
*
* @param predicate
* @author yuysun
*/
public class GetObjectClasses extends BaseEditController {
private static final Log log = LogFactory.getLog(SiteAdminController.class
.getName());
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
super.doGet(request, response);
} catch (Exception e) {
e.printStackTrace();
}
VitroRequest vreq = new VitroRequest(request);
Object obj = vreq.getSession().getAttribute("loginHandler");
LoginFormBean loginHandler = null;
if (obj != null && obj instanceof LoginFormBean)
loginHandler = ((LoginFormBean) obj);
if (loginHandler == null
|| !"authenticated".equalsIgnoreCase(loginHandler
.getLoginStatus()) ||
// rjy7 Allows any editor (including self-editors) access to
// this servlet.
// This servlet is now requested via Ajax from some custom
// forms, so anyone
// using the custom form needs access rights.
Integer.parseInt(loginHandler.getLoginRole()) < LoginFormBean.NON_EDITOR) {
HttpSession session = request.getSession(true);
session.setAttribute("postLoginRequest", vreq.getRequestURI()
+ (vreq.getQueryString() != null ? ('?' + vreq
.getQueryString()) : ""));
String redirectURL = request.getContextPath()
+ Controllers.SITE_ADMIN + "?login=block";
response.sendRedirect(redirectURL);
return;
}
String predicate = vreq.getParameter("predicate");
if (predicate == null || predicate.trim().equals("")) {
return;
}
ObjectPropertyDao odao = vreq.getFullWebappDaoFactory()
.getObjectPropertyDao();
ObjectProperty oprop = (ObjectProperty) odao
.getObjectPropertyByURI(predicate);
VClassDao vcDao = vreq.getFullWebappDaoFactory().getVClassDao();
VClass vClass = (oprop.getRangeVClassURI() != null) ? vcDao
.getVClassByURI(oprop.getRangeVClassURI()) : null;
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String respo = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
respo += "<options>";
if (vClass != null) {
respo += "<option>" + "<key>" + vClass.getPickListName() + "</key>"
+ "<value>" + vClass.getURI() + "</value>" + "</option>";
} else {
List classGroups = vreq.getFullWebappDaoFactory()
.getVClassGroupDao().getPublicGroupsWithVClasses(true,
true, false); // order by displayRank, include
// uninstantiated classes, don't get
// the counts of individuals
Iterator classGroupIt = classGroups.iterator();
while (classGroupIt.hasNext()) {
VClassGroup group = (VClassGroup) classGroupIt.next();
List classes = group.getVitroClassList();
Iterator classIt = classes.iterator();
while (classIt.hasNext()) {
VClass clazz = (VClass) classIt.next();
respo += "<option>" + "<key>" + clazz.getPickListName()
+ "</key>" + "<value>" + clazz.getURI()
+ "</value>" + "</option>";
}
}
}
/*
* VClass clazz = oprop.getRangeVClass();
* response.setContentType("text/xml");
* response.setCharacterEncoding("UTF-8"); PrintWriter out =
* response.getWriter(); String respo =
* "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; respo += "<options>";
*
* if (clazz != null) { respo += "<option>" + "<key>" +
* clazz.getPickListName() + "</key>" + "<value>" + clazz.getURI() +
* "</value>" + "</option>"; } else{ List classGroups =
* vreq.getFullWebappDaoFactory
* ().getVClassGroupDao().getPublicGroupsWithVClasses(true,true,false);
* // order by displayRank, include uninstantiated classes, don't get
* the counts of individuals Iterator classGroupIt =
* classGroups.iterator(); while (classGroupIt.hasNext()) { VClassGroup
* group = (VClassGroup)classGroupIt.next(); List classes =
* group.getVitroClassList(); Iterator classIt = classes.iterator();
* while (classIt.hasNext()) { clazz = (VClass) classIt.next();
* System.out.println(clazz.getPickListName()); respo += "<option>" +
* "<key>" + clazz.getPickListName() + "</key>" + "<value>" +
* clazz.getURI() + "</value>" + "</option>"; } }
*
* }
*/
respo += "</options>";
out.println(respo);
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}

165
webapp/web/admin/sparql.jsp Normal file
View file

@ -0,0 +1,165 @@
<%@page import="com.hp.hpl.jena.rdf.model.ModelMaker"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<script type="text/javascript" src="../js/sparql/prototype.js">
</script>
<script type="text/javascript" src="../js/sparql/sparql.js">
</script>
<style type="text/css">
td {
vertical-align: top;
border: 1px solid #b2b2b2;
}
</style>
<BODY onload="init()">
<div id="content" class="sparqlform"
style="width: 900px; margin: 0 auto;">
<h1>
SPARQL Query Builder
</h1>
<table id="builder" width="100%">
<tr>
<td width="33%">
Subject
</td>
<td width="33%">
Predicate
</td>
<td width="34%">
Object
</td>
</tr>
<tr id="clazz(0)">
<td id="subject(0)" width="33%">
<select id="subject(0,0)">
<option value="">
Thing
</option>
</select>
</td>
<td id="predicate(0)">
</td>
<td id="object(0)" width="34%">
</td>
</tr>
</table>
<div>
<input type="button" value="Generate Query" onclick="genQuery();" />
</div>
<div id="sparqlquery" style="visibility: hidden;">
<form action="sparqlquery">
<div>
<textarea id="query" name="query" rows="20" cols="111">
</textarea>
</div>
<p>
<h4>
Format for SELECT query results:
</h4>
<input id='RS_XML_BUTTON' type='radio' name='resultFormat'
value='RS_XML'>
<label for='RS_XML_BUTTON'>
RS_XML
</label>
<input id='RS_TEXT_BUTTON' type='radio' name='resultFormat'
value='RS_TEXT' checked='checked'>
<label for='RS_TEXT_BUTTON'>
RS_TEXT
</label>
<input id='RS_CSV_BUTTON' type='radio' name='resultFormat'
value='vitro:csv'>
<label for='RS_CSV_BUTTON'>
CSV
</label>
<input id='RS_RDF_N3_BUTTON' type='radio' name='resultFormat'
value='RS_RDF/N3'>
<label for='RS_RDF_N3_BUTTON'>
RS_RDF/N3
</label>
<input id='RS_JSON_BUTTON' type='radio' name='resultFormat'
value='RS_JSON'>
<label for='RS_JSON_BUTTON'>
RS_JSON
</label>
<input id='RS_RDF_BUTTON' type='radio' name='resultFormat'
value='RS_RDF'>
<label for='RS_RDF_BUTTON'>
RS_RDF
</label>
</p>
<p>
<h4>
Format for CONSTRUCT and DESCRIBE query results:
</h4>
<input id='RR_RDFXML_BUTTON' type='radio' name='rdfResultFormat'
value='RDF/XML'>
<label for='RR_RDFXML_BUTTON'>
RDF/XML
</label>
<input id='RR_RDFXMLABBREV_BUTTON' type='radio'
name='rdfResultFormat' value='RDF/XML-ABBREV' checked='checked'>
<label for='RR_RDFXMLABBREV_BUTTON'>
RDF/XML-ABBREV
</label>
<input id='RR_N3_BUTTON' type='radio' name='rdfResultFormat'
value='N3'>
<label for='RR_N3_BUTTON'>
N3
</label>
<input id='RR_NTRIPLE_BUTTON' type='radio' name='rdfResultFormat'
value='N-TRIPLE'>
<label for='RR_NTRIPLE_BUTTON'>
N-Triples
</label>
<input id='RR_TURTLE_BUTTON' type='radio' name='rdfResultFormat'
value='TTL'>
<label for='RR_TURTLE_BUTTON'>
Turtle
</label>
</p>
<div>
<ul class="clean">
<%
try {
if (request.getSession() != null
&& application.getAttribute("vitroJenaModelMaker") != null) {
ModelMaker maker = (ModelMaker) application
.getAttribute("vitroJenaModelMaker");
for (Iterator it = maker.listModels(); it.hasNext();) {
String modelName = (String) it.next();
%>
<li>
<input type="checkbox" name="sourceModelName"
value="<%=modelName%>" /><%=modelName%></li>
<%
}
} else {
%><li>
could not find named models in session
</li>
<%
}
} catch (Exception ex) {
%><li>
could not find named models in ModelMaker
</li>
<%
}
%>
</ul>
</div>
<input type="submit" value="Run Query">
</form>
</div>
</div>
</BODY>
</HTML>

1785
webapp/web/js/sparql/prototype.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,474 @@
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#"
};
var level = 0;
function init(){
var url = "getAllClasses";
var preurl = "getAllPrefix";
var base = document.getElementById("subject(0,0)");
base.level = 0;
base.count = 0;
var myAjax = new Ajax.Request( url, {method: "get", parameters: "", onComplete: function(originalRequest){
var response = originalRequest.responseXML;
var options = response.getElementsByTagName("option");
if (options == null || options.length == 0){
alert("Error: Cannot get all the classes.");
return;
}
for(i=0; i<options.length; i++){
base[base.length] = new Option(options[i].childNodes[0].firstChild.data, options[i].childNodes[1].firstChild.data);
}
var subdiv = document.getElementById("subject(0)");
subdiv.appendChild(document.createElement("br"));
var adddprop = document.createElement("input");
adddprop.type = "button";
adddprop.value = "Add Data Property";
adddprop.level = 0;
adddprop.onclick = function() {
return getDataProperty(this);
}
subdiv.appendChild(adddprop);
var addoprop = document.createElement("input");
addoprop.type = "button";
addoprop.value = "Add Object Property";
addoprop.level = 0;
addoprop.onclick = function() {
return getObjectProperty(this);
}
subdiv.appendChild(addoprop);
level ++;
}
}
);
var myPrefixAjax = new Ajax.Request( preurl, {method: "get", parameters: "", onComplete: function(originalRequest){
var response = originalRequest.responseXML;
var options = response.getElementsByTagName("option");
if (options == null || options.length == 0){
alert("Error: Cannot get all the prefix.");
return;
}
for(i=0; i<options.length; i++)
namespaces[options[i].childNodes[0].firstChild.data] = options[i].childNodes[1].firstChild.data;
}
}
);
}
function getDataProperty(addprop){
var url = "getClazzDataProperties";
var base = document.getElementById("subject(" + addprop.level + ",0)");
var subject = base.value;
if (subject == ""){
alert("Please select a class.");
}
else{
var params = "vClassURI=" + subject.replace('#', '%23');
var myAjax = new Ajax.Request( url, {method: "get", parameters: params, onComplete: function(originalRequest){
var response = originalRequest.responseXML;
var property = document.createElement("select");
property.id = "predicate(" + base.level + "," + base.count + ")";
property[property.length] = new Option("Properties", "");
var options = response.getElementsByTagName('option');
if (options == null || options.length == 0){
alert("Error: Cannot get data properties for " + subject + ".");
return;
}
for(i=0; i<options.length; i++){
property[property.length] = new Option(options[i].childNodes[0].firstChild.data, options[i].childNodes[1].firstChild.data);
}
property.level = base.level;
property.count = base.count;
property.onchange = function() {
return getData(this);
}
var prediv = document.getElementById("predicate(" + base.level + ")");
if (prediv.innerHTML.trim() != "") {
var lastNode = prediv.lastChild.previousSibling;
if (lastNode.selectedIndex == 0){
alert("You have a undefined property, please make sure it has been initialized.");
return;
}
}
prediv.appendChild(property);
base.count += 1
prediv.appendChild(document.createElement("br"));
}
}
);
}
}
function getData(property){
var base = document.getElementById("subject(" + property.level + ",0)")
var subject = base.value;
//Disable the selection
property.disabled = true;
//DEL PROPERTY
var delprop = document.createElement("input");
delprop.type = "button";
delprop.value = "Delete";
delprop.count = base.count - 1;
delprop.level = base.level;
delprop.onclick = function() {
return delProperty(this);
}
var prediv = document.getElementById("predicate(" + base.level + ")");
prediv.insertBefore(delprop, property.nextSibling);
var objdiv = document.getElementById("object(" + base.level + ")");
var dataprop = document.createElement("input");
dataprop.type = "text";
dataprop.size = 50;
dataprop.count = base.count - 1;
dataprop.level = base.level;
dataprop.id = "object(" + base.level + "," + (base.count - 1) + ")";
objdiv.appendChild(dataprop);
objdiv.appendChild(document.createElement("br"));
}
function getObjectProperty(addprop){
var url = "getClazzObjectProperties";
var base = document.getElementById("subject(" + addprop.level + ",0)");
var subject = base.value;
if (subject == ""){
alert("Please select a class.");
}
else{
var params = "vClassURI=" + subject.replace('#', '%23');
var myAjax = new Ajax.Request( url, {method: "get", parameters: params, onComplete: function(originalRequest){
var response = originalRequest.responseXML;
var property = document.createElement("select");
property.id = "predicate(" + base.level + "," + base.count + ")";
property[property.length] = new Option("Properties", "");
var options = response.getElementsByTagName('option');
if (options == null || options.length == 0){
alert("Error: Cannot get object properties for " + subject + ".");
return;
}
for(i=0; i<options.length; i++){
property[property.length] = new Option(options[i].childNodes[0].firstChild.data, options[i].childNodes[1].firstChild.data);
}
property.level = base.level;
property.count = base.count;
property.onchange = function() {
return getObject(this);
}
var prediv = document.getElementById("predicate(" + base.level + ")");
if (prediv.innerHTML.trim() != "") {
var lastNode = prediv.lastChild.previousSibling;
if (lastNode.selectedIndex == 0){
alert("You have a undefined property, please make sure it has been initialized.");
return;
}
}
prediv.appendChild(property);
base.count += 1
prediv.appendChild(document.createElement("br"));
}
}
);
}
}
function getObject(property){
var url = "getObjectClasses";
var base = document.getElementById("subject(" + property.level + ",0)")
var subject = base.value;
//Disable the selection
property.disabled = true;
//DEL PROPERTY
var delprop = document.createElement("input");
delprop.type = "button";
delprop.value = "Delete";
delprop.count = base.count - 1;
delprop.level = base.level;
delprop.onclick = function() {
return delProperty(this);
}
var prediv = document.getElementById("predicate(" + base.level + ")");
prediv.insertBefore(delprop, property.nextSibling);
var predicate = property.value;
var params = "predicate=" + predicate.replace('#', '%23');
var myAjax = new Ajax.Request( url, {method: "get", parameters: params, onComplete: function(originalRequest){
var response = originalRequest.responseXML;
var objdiv = document.getElementById("object(" + base.level + ")");
var options = response.getElementsByTagName('option');
if (options == null || options.length == 0){
alert("Error: Cannot get range classes for " + predicate + ".");
return;
}
var obj = document.getElementById("object(" + base.level + "," + base.count + ")");
if (obj == null){
if (options.length > 0){
obj = document.createElement("select");
obj[obj.length] = new Option("Classes", "");
for(i=0; i<options.length; i ++){
obj[obj.length] = new Option(options[i].childNodes[0].firstChild.data, options[i].childNodes[1].firstChild.data);
}
obj.onchange = function(){
return addClass(this);
}
}
else{
obj = document.createElement("input");
obj.type = "text";
}
obj.id = "object(" + base.level + "," + (base.count - 1) + ")";
obj.level = base.level;
obj.count = base.count - 1;
objdiv.appendChild(obj);
objdiv.appendChild(document.createElement("br"));
}
else{
var objpar = obj.parentNode;
if (options.length > 0){
var newobj = document.createElement("select");
newobj[newobj.length] = new Option("Classes", "");
for(i=0; i<options.length; i ++){
newobj[newobj.length] = new Option(options[i].firstChild.data, options[i].firstChild.data);
}
newobj.onchange = function(){
return addClass(this);
}
}
else{
newobj = document.createElement("input");
newobj.type = "text";
}
newobj.id = "object(" + base.level + "," + base.count + ")";
newobj.level = base.level;
newobj.count = base.count;
objpar.replaceChild(newobj, obj);
}
}
}
);
}
function addClass(obj){
addClazz();
//disable the selection
obj.disabled = true;
var subject = document.createElement("select");
subject[subject.length] = new Option(obj.options[obj.selectedIndex].text, obj.value);
subject.disabled = true;
subject.level = level;
level ++;
subject.count = 0;
subject.id = "subject(" + subject.level + "," + subject.count + ")";
var subdiv = document.getElementById("subject(" + subject.level +")");
subdiv.appendChild(subject);
var delclazz = document.createElement("input");
delclazz.type = "button";
delclazz.value = "Delete";
delclazz.count = subject.count;
delclazz.level = subject.level;
delclazz.onclick = function() {
return delClazz(this.level);
}
subdiv.appendChild(delclazz);
subdiv.appendChild(document.createElement("br"));
var adddprop = document.createElement("input");
adddprop.type = "button";
adddprop.value = "Add Data Property";
adddprop.level = subject.level;
adddprop.onclick = function() {
return getDataProperty(this);
}
subdiv.appendChild(adddprop);
var addoprop = document.createElement("input");
addoprop.type = "button";
addoprop.value = "Add Object Property";
addoprop.level = subject.level;
addoprop.onclick = function() {
return getObjectProperty(this);
}
subdiv.appendChild(addoprop);
}
function addClazz(){
var builder = document.getElementById("builder");
var clazz = document.createElement("tr");
clazz.id = "clazz(" + level + ")";
var subject = document.createElement("td");
subject.id = "subject(" + level + ")";
var predicate = document.createElement("td");
predicate.id = "predicate(" + level + ")";
var object = document.createElement("td");
object.id = "object(" + level + ")";
clazz.appendChild(subject);
clazz.appendChild(predicate);
clazz.appendChild(object);
builder.appendChild(clazz);
}
function delClazz(level){
var clazz = document.getElementById("clazz(" + level +")");
var builder = document.getElementById("builder");
builder.removeChild(clazz);
}
function delProperty(delprop){
var sub = document.getElementById("predicate(" + delprop.level + "," + delprop.count + ")");
var obj = document.getElementById("object(" + delprop.level + "," + delprop.count + ")");
var subdiv = document.getElementById("predicate(" + delprop.level +")");
var objdiv = document.getElementById("object(" + delprop.level +")");
subdiv.removeChild(sub.nextSibling.nextSibling);
subdiv.removeChild(sub.nextSibling);
subdiv.removeChild(sub);
objdiv.removeChild(obj.nextSibling);
objdiv.removeChild(obj);
}
function genQuery(){
var items = new Array();
var criterias = new Array();
var clazz = new Array();
var number = 0;
var _sub;
var _obj;
// namespaces shown in the SPARQL query box
var namespace = getNamespace();
for (i=0; i < level; i++){
var subjects = document.getElementById("subject(" + i + ")");
if (subjects == null){
continue;
}
var subNodes = subjects.getElementsByTagName("select");
var sub = subNodes[0].value;
sub = getNameWithPrefix(sub);
if (!clazz[sub.substring(sub.indexOf(":") + 1)]){
clazz[sub.substring(sub.indexOf(":") + 1)] = 1;
_sub = sub.substring(sub.indexOf(":") + 1) + 1;
}
else{
_sub = sub.substring(sub.indexOf(":") + 1) + clazz[sub.substring(sub.indexOf(":") + 1)];
}
var subname = "?" + _sub;
criterias[criterias.length] = subname + " rdf:type " + sub + " .";
var predicates = document.getElementById("predicate(" + i + ")");
var preNodes = predicates.getElementsByTagName("select");
var num = preNodes.length;
for (j=0; j<num; j++){
var pre = preNodes[j];
obj = document.getElementById("object(" + pre.level + "," + pre.count + ")");
if (obj == null){
alert("You have a undefined property, please make sure it has been initialized.");
return;
}
pre = pre.value;
pre = getNameWithPrefix(pre);
if (obj.tagName == "INPUT"){
var objname = subname + "_" + pre.substring(pre.indexOf(":") + 1);
criterias[criterias.length] = subname + " " + pre + " " + objname + " .";
items[items.length] = objname;
if (obj.value != ""){
criterias[criterias.length] = "FILTER REGEX (str(" + objname + "), '" + obj.value + "', 'i')";
}
}
else{
obj = obj.value;
obj = getNameWithPrefix(obj);
if (!clazz[obj.substring(obj.indexOf(":") + 1)]){
clazz[obj.substring(obj.indexOf(":") + 1)] = 1;
_obj = obj.substring(obj.indexOf(":") + 1) + 1;
}
else{
number = clazz[obj.substring(obj.indexOf(":") + 1)] + 1;
clazz[obj.substring(obj.indexOf(":") + 1)] = number
_obj = obj.substring(obj.indexOf(":") + 1) + number;
}
var objname = "?" + _obj;
criterias[criterias.length] = subname + " " + pre + " " + objname + " .";
}
}
}
var item = "distinct " + items.join(" ");
var criteria = criterias.join("\n");
var query = namespace+ "SELECT " + item + "\nWHERE{\n" + criteria + "\n}\n";
var quediv = document.getElementById("sparqlquery");
var quetextarea = document.getElementById("query");
quediv.style.visibility = "visible";
quetextarea.value = query;
}
function getNamespace(){
var namespace = "";
for (key in namespaces){
namespace += "PREFIX " + key + ": <" + namespaces[key] + ">\n";
}
namespace += "\n";
return namespace;
}
function getNameWithPrefix(name){
for (key in namespaces){
var index = name.indexOf(namespaces[key]);
if (index == 0){
return key + ":" + name.slice(namespaces[key].length);
}
}
return name;
}