diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/listing/VClassWebappsListingController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/listing/VClassWebappsListingController.java index b354acca9..1ae653357 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/listing/VClassWebappsListingController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/edit/listing/VClassWebappsListingController.java @@ -5,7 +5,6 @@ package edu.cornell.mannlib.vitro.webapp.controller.edit.listing; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -23,6 +22,7 @@ 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.dao.OntologyDao; +import edu.cornell.mannlib.vitro.webapp.dao.PropertyDao; import edu.cornell.mannlib.vitro.webapp.dao.VClassDao; import edu.cornell.mannlib.vitro.webapp.dao.VClassGroupDao; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; @@ -43,20 +43,28 @@ public class VClassWebappsListingController extends BaseEditController { } catch (Throwable t) { t.printStackTrace(); } - + //need to figure out how to structure the results object to put the classes underneath - - VClassDao dao = getWebappDaoFactory().getVClassDao(); - - List classes = (request.getParameter("iffRoot") != null) - ? dao.getRootClasses() - : dao.getAllVclasses(); + + List classes = null; + + if (request.getParameter("showPropertyRestrictions") != null) { + PropertyDao pdao = getWebappDaoFactory().getObjectPropertyDao(); + classes = pdao.getClassesRestrictedOn(request.getParameter("propertyURI")); + } else { + VClassDao vcdao = getWebappDaoFactory().getVClassDao(); + + if (request.getParameter("iffRoot") != null) { + classes = vcdao.getRootClasses(); + } else { + classes = vcdao.getAllVclasses(); + } + + } String ontologyURI = vrequest.getParameter("ontologyUri"); - Collections.sort(classes); - - ArrayList results = new ArrayList(); + ArrayList results = new ArrayList(); results.add("XX"); results.add("Class"); results.add("short definition"); @@ -68,7 +76,8 @@ public class VClassWebappsListingController extends BaseEditController { results.add("update level"); if (classes != null) { - Iterator classesIt = classes.iterator(); + Collections.sort(classes); + Iterator classesIt = classes.iterator(); while (classesIt.hasNext()) { VClass cls = (VClass) classesIt.next(); if ( (ontologyURI==null) || ( (ontologyURI != null) && (cls.getNamespace()!=null) && (ontologyURI.equals(cls.getNamespace())) ) ) { @@ -157,7 +166,7 @@ public class VClassWebappsListingController extends BaseEditController { } } - + public void doPost(HttpServletRequest request, HttpServletResponse response) { doGet(request,response); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/PropertyDao.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/PropertyDao.java index 1f3cc9c2f..668e4f05f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/PropertyDao.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/PropertyDao.java @@ -4,8 +4,8 @@ package edu.cornell.mannlib.vitro.webapp.dao; import java.util.List; -import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.Property; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; public interface PropertyDao { @@ -43,4 +43,6 @@ public interface PropertyDao { List getEquivalentPropertyURIs(String propertyURI); + List getClassesRestrictedOn(String propertyURI); + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/DataPropertyDaoFiltering.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/DataPropertyDaoFiltering.java index 75c038816..0da8eba02 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/DataPropertyDaoFiltering.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/DataPropertyDaoFiltering.java @@ -9,6 +9,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.DataProperty; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.Property; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyDao; import edu.cornell.mannlib.vitro.webapp.dao.InsertException; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; @@ -183,5 +184,9 @@ class DataPropertyDaoFiltering extends BaseFiltering implements DataPropertyDao{ Property equivalentProperty) { innerDataPropertyDao.removeEquivalentProperty(property, equivalentProperty); } + + public List getClassesRestrictedOn(String propertyURI) { + return innerDataPropertyDao.getClassesRestrictedOn(propertyURI); + } } \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/ObjectPropertyDaoFiltering.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/ObjectPropertyDaoFiltering.java index 6730af455..b7d36872e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/ObjectPropertyDaoFiltering.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/ObjectPropertyDaoFiltering.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.filtering; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -12,6 +13,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.Property; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.InsertException; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyDao; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; @@ -196,4 +198,8 @@ class ObjectPropertyDaoFiltering extends BaseFiltering implements ObjectProperty return innerObjectPropertyDao.skipEditForm(predicateURI); } + public List getClassesRestrictedOn(String propertyURI) { + return innerObjectPropertyDao.getClassesRestrictedOn(propertyURI); + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/PropertyDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/PropertyDaoJena.java index e9ea4796b..20114f868 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/PropertyDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/PropertyDaoJena.java @@ -2,11 +2,15 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena; +import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntProperty; @@ -20,12 +24,15 @@ import com.hp.hpl.jena.vocabulary.RDFS; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; import edu.cornell.mannlib.vitro.webapp.beans.Property; +import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.PropertyDao; -import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; +import edu.cornell.mannlib.vitro.webapp.dao.VClassDao; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent; public class PropertyDaoJena extends JenaBaseDao implements PropertyDao { + protected static final Log log = LogFactory.getLog(PropertyDaoJena.class.getName()); + public PropertyDaoJena(WebappDaoFactoryJena wadf) { super(wadf); } @@ -266,5 +273,100 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao { } } - + + /** + * Finds the classes that have a definition involving a restriction + * on the given property. + * + * @param propertyURI identifier of a property + * @return a list of VClass objects representing the classes that have + * definitions involving a restriction on the given property. + */ + + public List getClassesRestrictedOn(String propertyURI) { + + if (propertyURI == null) { + log.info("getClassesRestrictedOn: called with null propertyURI"); + return null; + } + + OntModel ontModel = getOntModel(); + ontModel.enterCriticalSection(Lock.READ); + + HashSet classURISet = new HashSet(); + + try { + Resource targetProp = ontModel.getResource(propertyURI); + + if (targetProp != null) { + + StmtIterator stmtIter = ontModel.listStatements((Resource) null, OWL.onProperty, (RDFNode) targetProp); + + while (stmtIter.hasNext()) { + Statement statement = stmtIter.next(); + + if ( statement.getSubject().canAs(OntClass.class) ) { + classURISet.addAll(getRelatedClasses((OntClass) statement.getSubject().as(OntClass.class))); + } else { + log.warn("getClassesRestrictedOn: Unexpected use of onProperty: it is not applied to a restriction"); + } + } + } else { + log.error("getClassesRestrictedOn: Error: didn't find a Property in the ontology model for the URI: " + propertyURI); + } + } finally { + ontModel.leaveCriticalSection(); + } + + List classes = new ArrayList(); + Iterator iter = classURISet.iterator(); + + VClassDao vcd = getWebappDaoFactory().getVClassDao(); + + while (iter.hasNext()) { + + String curi = iter.next(); + VClass vc = vcd.getVClassByURI(curi); + + if (vc != null) { + classes.add(vc); + } else { + log.error("getClassesRestrictedOn: Error: no VClass found for URI: " + curi); + } + } + + return (classes.size()>0) ? classes : null; + } + + /** + * Finds all named superclasses, subclasses and equivalent classes of + * the given class. + * + * @param resourceURI identifier of a class + * @return set of class URIs + * + * Note: this method assumes that the caller holds a read lock on + * the ontology model. + */ + + public HashSet getRelatedClasses(OntClass ontClass) { + + HashSet classSet = new HashSet(); + + List classList = ontClass.listEquivalentClasses().toList(); + classList.addAll(ontClass.listSubClasses().toList()); + classList.addAll(ontClass.listSuperClasses().toList()); + + Iterator it = classList.iterator(); + + while (it.hasNext()) { + OntClass oc = it.next(); + + if (!oc.isAnon()) { + classSet.add(oc.getURI()); + } + } + + return classSet; + } }