From 62aae75893b640736a0f11a4065be56294cb763b Mon Sep 17 00:00:00 2001 From: yuysun Date: Wed, 17 Nov 2010 01:15:12 +0000 Subject: [PATCH] SPARQL Query Builder with one property button. --- webapp/config/web.xml | 9 + .../webapp/sparql/GetClazzAllProperties.java | 177 ++++++++++++++++++ webapp/web/js/sparql/sparql.js | 151 ++++----------- 3 files changed, 222 insertions(+), 115 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetClazzAllProperties.java diff --git a/webapp/config/web.xml b/webapp/config/web.xml index d1f602bee..23a727523 100644 --- a/webapp/config/web.xml +++ b/webapp/config/web.xml @@ -1329,6 +1329,15 @@ GetClazzDataProperties /admin/getClazzDataProperties + + + GetClazzAllProperties + edu.cornell.mannlib.vitro.webapp.sparql.GetClazzAllProperties + + + GetClazzAllProperties + /admin/getClazzAllProperties + GetClazzObjectProperties diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetClazzAllProperties.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetClazzAllProperties.java new file mode 100644 index 000000000..92371c1cc --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/sparql/GetClazzAllProperties.java @@ -0,0 +1,177 @@ +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 properties for a given subject. + * + * @param vClassURI + * @author yuysun + */ + +public class GetClazzAllProperties 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(); + } + if (!checkLoginStatus(request, response)) + return; + VitroRequest vreq = new VitroRequest(request); + + String vClassURI = vreq.getParameter("vClassURI"); + if (vClassURI == null || vClassURI.trim().equals("")) { + return; + } + + String respo = ""; + respo += ""; + + // Get Data Properties + // Add rdfs:label to the list + respo += ""; + + DataPropertyDao ddao = vreq.getFullWebappDaoFactory() + .getDataPropertyDao(); + + Collection dataProps = ddao + .getDataPropertiesForVClass(vClassURI); + Iterator dataPropIt = dataProps.iterator(); + HashSet dpropURIs = new HashSet(); + 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 += ""; + } + } + } + + // Get Object Properties + + 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 superclassURIs = vcDao.getAllSuperClassURIs(vClassURI); + superclassURIs.add(vClassURI); + superclassURIs.addAll(vcDao.getEquivalentClassURIs(vClassURI)); + + Map propInstMap = new HashMap(); + for (String classURI : superclassURIs) { + Collection propInsts = piDao + .getAllPropInstByVClass(classURI); + try { + for (PropertyInstance propInst : propInsts) { + propInstMap.put(propInst.getPropertyURI(), propInst); + } + } catch (NullPointerException ex) { + continue; + } + } + List propInsts = new ArrayList(); + 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 += ""; + } + } + } + + respo += ""; + 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.
+ * + * 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); + } +} diff --git a/webapp/web/js/sparql/sparql.js b/webapp/web/js/sparql/sparql.js index 905de1823..6e8ea4b86 100644 --- a/webapp/web/js/sparql/sparql.js +++ b/webapp/web/js/sparql/sparql.js @@ -32,22 +32,14 @@ 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); + var addprop = document.createElement("input"); + addprop.type = "button"; + addprop.value = "Add Property"; + addprop.level = 0; + addprop.onclick = function() { + return getProperty(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); + subdiv.appendChild(addprop); level ++; } } @@ -68,9 +60,9 @@ } - function getDataProperty(addprop){ + function getProperty(addprop){ - var url = "getClazzDataProperties"; + var url = "getClazzAllProperties"; var base = document.getElementById("subject(" + addprop.level + ",0)"); var subject = base.value; if (subject == ""){ @@ -89,89 +81,8 @@ return; } for(i=0; i