NIHVIVO-2617 restored assertions-only filtering for individual lists
This commit is contained in:
parent
f49d3af301
commit
1edae6b651
1 changed files with 136 additions and 65 deletions
|
@ -10,6 +10,7 @@ import java.util.Iterator;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
@ -26,18 +27,14 @@ import com.hp.hpl.jena.query.QuerySolution;
|
||||||
import com.hp.hpl.jena.query.ResultSet;
|
import com.hp.hpl.jena.query.ResultSet;
|
||||||
import com.hp.hpl.jena.rdf.model.AnonId;
|
import com.hp.hpl.jena.rdf.model.AnonId;
|
||||||
import com.hp.hpl.jena.rdf.model.Literal;
|
import com.hp.hpl.jena.rdf.model.Literal;
|
||||||
import com.hp.hpl.jena.rdf.model.Model;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||||
import com.hp.hpl.jena.rdf.model.ResIterator;
|
|
||||||
import com.hp.hpl.jena.rdf.model.Resource;
|
import com.hp.hpl.jena.rdf.model.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||||
import com.hp.hpl.jena.rdf.model.Statement;
|
import com.hp.hpl.jena.rdf.model.Statement;
|
||||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||||
import com.hp.hpl.jena.shared.Lock;
|
import com.hp.hpl.jena.shared.Lock;
|
||||||
import com.hp.hpl.jena.util.iterator.ClosableIterator;
|
import com.hp.hpl.jena.util.iterator.ClosableIterator;
|
||||||
import com.hp.hpl.jena.vocabulary.OWL;
|
|
||||||
import com.hp.hpl.jena.vocabulary.RDF;
|
|
||||||
import com.hp.hpl.jena.vocabulary.RDFS;
|
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||||
|
|
||||||
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
|
||||||
|
@ -105,11 +102,65 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
||||||
ents.addAll(getIndividualsByVClass(vc));
|
ents.addAll(getIndividualsByVClass(vc));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
List<Individual> individualList;
|
||||||
|
|
||||||
|
// Check if there is a graph filter.
|
||||||
|
// If so, we will use it in a slightly strange way. Unfortunately,
|
||||||
|
// performance is quite bad if we add several graph variables in
|
||||||
|
// order to account for the fact that an individual's type
|
||||||
|
// declaration may be in a different graph from its label or
|
||||||
|
// moniker. Thus, we will run two queries: one with a single
|
||||||
|
// graph variable to get the list of URIs, and a second against
|
||||||
|
// the union graph to get individuals with their labels and
|
||||||
|
// monikers. We will then toss out any individual in the second
|
||||||
|
// list that is not also in the first list.
|
||||||
|
// Annoying, yes, but better than the alternative.
|
||||||
|
// Note that both queries need to sort identically or
|
||||||
|
// the results may be very strange.
|
||||||
|
String[] graphVars = {"?g"};
|
||||||
|
String filterStr = WebappDaoFactorySDB.getFilterBlock(
|
||||||
|
graphVars, datasetMode);
|
||||||
|
if (!StringUtils.isEmpty(filterStr)) {
|
||||||
|
List<Individual> graphFilteredIndividualList =
|
||||||
|
getGraphFilteredIndividualList(theClass, filterStr);
|
||||||
|
List<Individual> unfilteredIndividualList = getIndividualList(
|
||||||
|
theClass);
|
||||||
|
Iterator<Individual> unfilteredIt = unfilteredIndividualList
|
||||||
|
.iterator();
|
||||||
|
for (Individual filt : graphFilteredIndividualList) {
|
||||||
|
Individual unfilt = unfilteredIt.next();
|
||||||
|
while (!unfilt.getURI().equals(filt.getURI())) {
|
||||||
|
unfilt = unfilteredIt.next();
|
||||||
|
}
|
||||||
|
ents.add(unfilt);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ents = getIndividualList(theClass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
java.util.Collections.sort(ents);
|
||||||
|
|
||||||
|
if (quantity > 0 && offset > 0) {
|
||||||
|
List<Individual> sublist = new ArrayList<Individual>();
|
||||||
|
for (int i = offset - 1; i < ((offset - 1) + quantity); i++) {
|
||||||
|
sublist.add(ents.get(i));
|
||||||
|
}
|
||||||
|
return sublist;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ents;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Individual> getIndividualList(Resource theClass) {
|
||||||
|
List<Individual> ents = new ArrayList<Individual>();
|
||||||
DatasetWrapper w = getDatasetWrapper();
|
DatasetWrapper w = getDatasetWrapper();
|
||||||
Dataset dataset = w.getDataset();
|
Dataset dataset = w.getDataset();
|
||||||
dataset.getLock().enterCriticalSection(Lock.READ);
|
dataset.getLock().enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
String[] graphVars = {"?g", "?h", "?i"};
|
|
||||||
String query =
|
String query =
|
||||||
"SELECT DISTINCT ?ind ?label ?moniker " +
|
"SELECT DISTINCT ?ind ?label ?moniker " +
|
||||||
"WHERE " +
|
"WHERE " +
|
||||||
|
@ -166,20 +217,40 @@ public class IndividualDaoSDB extends IndividualDaoJena {
|
||||||
dataset.getLock().leaveCriticalSection();
|
dataset.getLock().leaveCriticalSection();
|
||||||
w.close();
|
w.close();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
java.util.Collections.sort(ents);
|
|
||||||
|
|
||||||
if (quantity > 0 && offset > 0) {
|
|
||||||
List<Individual> sublist = new ArrayList<Individual>();
|
|
||||||
for (int i = offset - 1; i < ((offset - 1) + quantity); i++) {
|
|
||||||
sublist.add(ents.get(i));
|
|
||||||
}
|
|
||||||
return sublist;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ents;
|
return ents;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<Individual> getGraphFilteredIndividualList(Resource theClass,
|
||||||
|
String filterStr) {
|
||||||
|
List<Individual> filteredIndividualList = new ArrayList<Individual>();
|
||||||
|
DatasetWrapper w = getDatasetWrapper();
|
||||||
|
Dataset dataset = w.getDataset();
|
||||||
|
dataset.getLock().enterCriticalSection(Lock.READ);
|
||||||
|
try {
|
||||||
|
String query =
|
||||||
|
"SELECT DISTINCT ?ind ?label ?moniker " +
|
||||||
|
"WHERE " +
|
||||||
|
"{ GRAPH ?g { \n" +
|
||||||
|
"{ ?ind a <" + theClass.getURI() + "> } \n" +
|
||||||
|
" } \n" + filterStr +
|
||||||
|
"} ORDER BY ?ind";
|
||||||
|
ResultSet rs =QueryExecutionFactory.create(
|
||||||
|
QueryFactory.create(query), dataset)
|
||||||
|
.execSelect();
|
||||||
|
while (rs.hasNext()) {
|
||||||
|
QuerySolution sol = rs.nextSolution();
|
||||||
|
Resource currRes = sol.getResource("ind");
|
||||||
|
if (currRes.isAnon()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
filteredIndividualList.add(
|
||||||
|
makeIndividual(currRes.getURI(), null, null));
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
dataset.getLock().leaveCriticalSection();
|
||||||
|
w.close();
|
||||||
|
}
|
||||||
|
return filteredIndividualList;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Individual makeIndividual(String uri, String label, String moniker) {
|
private Individual makeIndividual(String uri, String label, String moniker) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue