Including additional generator and field options that enable sorting individuals for an object property form by display rank annotations.

This commit is contained in:
hjkhjk54 2012-10-26 20:25:43 +00:00
parent aac07a1db9
commit 47f37d4213
2 changed files with 77 additions and 9 deletions

View file

@ -4,28 +4,49 @@ package edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields;
import java.text.Collator;
import java.util.Comparator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.QuerySolutionMap;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class IndividualsViaObjectPropertyByRankOptions extends IndividualsViaObjectPropetyOptions {
final static Log log = LogFactory.getLog(IndividualsViaObjectPropertyByRankOptions.class);
private WebappDaoFactory wdf = null;
private Model queryModel = null;
public IndividualsViaObjectPropertyByRankOptions(String subjectUri,
String predicateUri, String objectUri, WebappDaoFactory wdf) throws Exception {
String predicateUri, String objectUri, WebappDaoFactory wdf, Model model) throws Exception {
super(subjectUri, predicateUri, objectUri);
this.wdf = wdf;
this.queryModel = model;
}
public Comparator<String[]> getCustomComparator() {
return new DisplayRankComparator(wdf);
return new DisplayRankComparator(wdf, queryModel);
}
private static class DisplayRankComparator implements Comparator<String[]> {
private WebappDaoFactory wdf = null;
public DisplayRankComparator(WebappDaoFactory wdf) {
private Model queryModel = null;
public DisplayRankComparator(WebappDaoFactory wdf, Model model) {
this.wdf = wdf;
this.queryModel = model;
}
public int compare (String[] s1, String[] s2) {
Collator collator = Collator.getInstance();
if (s2 == null) {
return 1;
} else if (s1 == null) {
@ -41,18 +62,64 @@ public class IndividualsViaObjectPropertyByRankOptions extends IndividualsViaObj
} else if (s1[1] == null){
return -1;
} else {
return compareRanks(collator, s1, s2);
return compareRanks(s1, s2);
}
}
}
private int compareRanks(Collator collator, String[] s1, String[] s2) {
private int compareRanks(String[] s1, String[] s2) {
String uri1 = s1[0];
String uri2 = s2[0];
Individual ind1 = this.wdf.getIndividualDao().getIndividualByURI(uri1);
Individual ind2 = this.wdf.getIndividualDao().getIndividualByURI(uri2);
int displayRank1 = getDisplayRank(ind1);
int displayRank2 = getDisplayRank(ind2);
//Get display ranks
return collator.compare(ind1.getLocalName(), ind2.getLocalName());
return (displayRank1 > displayRank2 ? 1: (displayRank1 == displayRank2? 0: -1));
//TODO: Incorporate sparql query here to retrieve the ranks
//This qualifies as neither a data property or an object property so will need to access
//using sparql query
}
//Run sparql query to get display rank for individual - uses vitro annotation property
private Integer getDisplayRank(Individual ind) {
Integer rankResult = new Integer(0);
String query = getRankQuery();
//Set up bindings: individualURI in query should be individual
QuerySolutionMap initBindings = new QuerySolutionMap();
initBindings.add("individualURI", ResourceFactory.createResource(ind.getURI()));
//Create query
Query rankQuery = QueryFactory.create(query);
this.queryModel.enterCriticalSection(Lock.READ);
try {
QueryExecution qe = QueryExecutionFactory.create(rankQuery, this.queryModel, initBindings);
ResultSet res = qe.execSelect();
try {
while(res.hasNext()) {
QuerySolution qs = res.nextSolution();
//Check for rank
if(qs.get("rank") != null && qs.get("rank").isLiteral()) {
Literal rankLiteral = qs.getLiteral("rank");
rankResult = new Integer(rankLiteral.getInt());
} else {
log.debug("Rank was not returned in query or was not literal");
}
}
}finally{ qe.close(); }
} catch(Exception ex) {
log.error("Error occurred in executing query " + query, ex);
} finally {
this.queryModel.leaveCriticalSection();
}
return rankResult;
}
private String getRankQuery() {
return "PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> " +
"SELECT ?rank WHERE {?individualURI vitro:displayRankAnnot ?rank .} ";
}
}
}

View file

@ -48,7 +48,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.FrontEndEditingUtils.EditMode;
* Generates the edit configuration for a default property form.
* This handles the default object property auto complete.
*
* If a default property form is request and the number of indivdiuals
* If a default property form is request and the number of individuals
* found in the range is too large, the the auto complete setup and
* template will be used instead.
*/
@ -76,7 +76,8 @@ public class IndividualsByRankFormGenerator extends DefaultObjectPropertyFormGen
super.getSubjectUri(),
super.getPredicateUri(),
super.getObjectUri(),
vreq.getWebappDaoFactory()));
vreq.getWebappDaoFactory(),
vreq.getJenaOntModel()));
}else{
field.setOptions(null);
}