NIHVIVO-2466 Attempting to address the problem of label display for an individual with multiple labels by using the same methods to get the label that are used by Individual.getName(). However, these are tied to Jena DAOs so the solution doesn't quite work yet. We are still making a better guess by sorting the labels alphabetically, but this will not work in all cases.

This commit is contained in:
ryounes 2011-06-03 21:47:33 +00:00
parent fb67a5de19
commit 7cd1467786
2 changed files with 63 additions and 19 deletions

View file

@ -6,6 +6,8 @@ import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
@ -729,7 +731,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
}
private String getLabel(String lang, List<RDFNode>labelList) {
private Literal getLabel(String lang, List<RDFNode>labelList) {
Iterator<RDFNode> labelIt = labelList.iterator();
while (labelIt.hasNext()) {
RDFNode label = labelIt.next();
@ -737,10 +739,10 @@ public class JenaBaseDao extends JenaBaseDaoCon {
Literal labelLit = ((Literal)label);
String labelLanguage = labelLit.getLanguage();
if ( (labelLanguage==null) && (lang==null) ) {
return labelLit.getLexicalForm();
return labelLit;
}
if ( (lang != null) && (lang.equals(labelLanguage)) ) {
return labelLit.getLexicalForm();
return labelLit;
}
}
}
@ -766,26 +768,34 @@ public class JenaBaseDao extends JenaBaseDaoCon {
return label;
}
protected String getLabel(OntResource r){
String label = null;
Literal labelLiteral = getLabelLiteral(r);
if (labelLiteral != null) {
label = labelLiteral.getLexicalForm();
}
return label;
}
/**
* works through list of PREFERRED_LANGUAGES to find an appropriate
* label, or NULL if not found.
*/
protected String getLabel(OntResource r){
String label = null;
public Literal getLabelLiteral(OntResource r) {
Literal labelLiteral = null;
r.getOntModel().enterCriticalSection(Lock.READ);
try {
// try rdfs:label with preferred languages
label = tryPropertyForPreferredLanguages( r, RDFS.label, ALSO_TRY_NO_LANG );
labelLiteral = tryPropertyForPreferredLanguages( r, RDFS.label, ALSO_TRY_NO_LANG );
// try vitro:label with preferred languages
// Commenting out for NIHVIVO-1962
/* if ( label == null ) {
label = tryPropertyForPreferredLanguages( r, r.getModel().getProperty(VitroVocabulary.label), ALSO_TRY_NO_LANG );
} */
labelLiteral = tryPropertyForPreferredLanguages( r, r.getModel().getProperty(VitroVocabulary.label), ALSO_TRY_NO_LANG );
} */
} finally {
r.getOntModel().leaveCriticalSection();
}
return label;
return labelLiteral;
}
/**
@ -809,9 +819,24 @@ public class JenaBaseDao extends JenaBaseDaoCon {
return label;
}
private String tryPropertyForPreferredLanguages( OntResource r, Property p, boolean alsoTryNoLang ) {
String label = null;
List<RDFNode> labels = (List<RDFNode>) r.listPropertyValues(p).toList();
private Literal tryPropertyForPreferredLanguages( OntResource r, Property p, boolean alsoTryNoLang ) {
Literal label = null;
List<RDFNode> labels = r.listPropertyValues(p).toList();
// Sort by lexical value to guarantee consistent results
Collections.sort(labels, new Comparator<RDFNode>() {
public int compare(RDFNode left, RDFNode right) {
if (left == null) {
return (right == null) ? 0 : -1;
}
if ( left.isLiteral() && right.isLiteral()) {
return ((Literal) left).getLexicalForm().compareTo(((Literal) right).getLexicalForm());
}
// Can't sort meaningfully if both are not literals
return 0;
}
});
for (int i=0; i<PREFERRED_LANGUAGES.length; i++) {
String lang = PREFERRED_LANGUAGES[i];
label = getLabel(lang,labels);

View file

@ -9,11 +9,16 @@ import org.apache.commons.logging.LogFactory;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.Literal;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDao;
import edu.cornell.mannlib.vitro.webapp.dao.jena.WebappDaoFactoryJena;
public class NameStatementTemplateModel extends
DataPropertyStatementTemplateModel {
@ -29,15 +34,29 @@ public class NameStatementTemplateModel extends
NameStatementTemplateModel(String subjectUri, VitroRequest vreq, EditingPolicyHelper policyHelper) {
super(subjectUri, VitroVocabulary.LABEL, vreq, policyHelper);
DataPropertyStatementDao dpsDao = vreq.getWebappDaoFactory().getDataPropertyStatementDao();
List<Literal> literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, VitroVocabulary.LABEL);
Literal literal = null;
WebappDaoFactory wdf = vreq.getWebappDaoFactory();
// Make sure the subject has a value for this property
if (literals.size() > 0) {
Literal literal = literals.get(0);
// Use the same methods to get the label that are used elsewhere in the application, to
// guarantee consistent results for individuals with multiple labels.
// RY The problem here is we have a WebappDaoFactoryFiltering instead of WebappDaoFactoryJena.
if (wdf instanceof WebappDaoFactoryJena) {
WebappDaoFactoryJena wdfj = (WebappDaoFactoryJena) wdf;
OntResource resource = wdfj.getOntModel().createOntResource(subjectUri);
JenaBaseDao baseDao = wdfj.getJenaBaseDao();
literal = baseDao.getLabelLiteral(resource);
} else {
DataPropertyStatementDao dpsDao = vreq.getWebappDaoFactory().getDataPropertyStatementDao();
List<Literal> literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, VitroVocabulary.LABEL);
// Make sure the subject has a value for this property
if (literals.size() > 0) {
literal = literals.get(0);
}
}
if (literal != null) {
value = literal.getLexicalForm();
setEditAccess(literal, policyHelper);
} else {
// If the individual has no rdfs:label, use the local name. It will not be editable (this replicates previous behavior;
// perhaps we would want to allow a label to be added. But such individuals do not usually have their profiles viewed or