From 1077f283d671771e3a2e8c0359b2f1eabbf57ce9 Mon Sep 17 00:00:00 2001 From: ryounes Date: Fri, 10 Jun 2011 21:37:50 +0000 Subject: [PATCH] NIHVIVO-2466 For individuals with multiple labels, ensure the same label is displayed on the profile page as elsewhere in the application (individuallist pages, menu pages, back end) by using the same method to get the label. --- .../vitro/webapp/dao/IndividualDao.java | 3 + .../dao/filtering/IndividualDaoFiltering.java | 7 +++ .../webapp/dao/jena/IndividualDaoJena.java | 16 +++++ .../vitro/webapp/dao/jena/JenaBaseDao.java | 7 ++- .../DataPropertyStatementTemplateModel.java | 4 +- .../NameStatementTemplateModel.java | 61 +++++++++++-------- .../vitro/webapp/dao/IndividualDaoStub.java | 7 +++ 7 files changed, 77 insertions(+), 28 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java index 87d94c262..d32fa0407 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/IndividualDao.java @@ -10,6 +10,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.Keyword; import edu.cornell.mannlib.vitro.webapp.beans.VClass; +import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral; import edu.cornell.mannlib.vitro.webapp.search.beans.ObjectSourceIface; public interface IndividualDao extends ObjectSourceIface { @@ -135,6 +136,8 @@ public interface IndividualDao extends ObjectSourceIface { * @throws InsertException Could not create a URI */ String getUnusedURI(Individual individual) throws InsertException; + + EditLiteral getLabelEditLiteral(String individualUri); @Deprecated public abstract Individual getIndividualByExternalId(int externalIdType, diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/IndividualDaoFiltering.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/IndividualDaoFiltering.java index ad813013d..7f530c04e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/IndividualDaoFiltering.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/IndividualDaoFiltering.java @@ -19,6 +19,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.dao.InsertException; import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters; +import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral; class IndividualDaoFiltering extends BaseFiltering implements IndividualDao{ @@ -242,4 +243,10 @@ class IndividualDaoFiltering extends BaseFiltering implements IndividualDao{ return innerIndividualDao.getUnusedURI(individual); } + + @Override + public EditLiteral getLabelEditLiteral(String individualUri) { + return innerIndividualDao.getLabelEditLiteral(individualUri); + } + } \ No newline at end of file diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java index 29b3b8397..50dd064f9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/IndividualDaoJena.java @@ -58,6 +58,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualCreationEvent; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualDeletionEvent; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualUpdateEvent; +import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral; public class IndividualDaoJena extends JenaBaseDao implements IndividualDao { @@ -1047,5 +1048,20 @@ public class IndividualDaoJena extends JenaBaseDao implements IndividualDao { return uri; } + + @Override + // This method returns an EditLiteral rather than a Jena Literal, since IndividualDao + // should not reference Jena objects. (However, the problem isn't really solved + // because EditLiteral currently references the Jena API.) + public EditLiteral getLabelEditLiteral(String individualUri) { + Literal literal = getLabelLiteral(individualUri); + if (literal == null) { + return null; + } + String value = literal.getLexicalForm(); + String datatype = literal.getDatatypeURI(); + String lang = literal.getLanguage(); + return new EditLiteral(value, datatype, lang); + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/JenaBaseDao.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/JenaBaseDao.java index 4b7d3c7d2..6b9a40079 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/JenaBaseDao.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/JenaBaseDao.java @@ -776,12 +776,17 @@ public class JenaBaseDao extends JenaBaseDaoCon { } return label; } + + protected Literal getLabelLiteral(String individualUri) { + OntResource resource = webappDaoFactory.getOntModel().createOntResource(individualUri); + return getLabelLiteral(resource); + } /** * works through list of PREFERRED_LANGUAGES to find an appropriate * label, or NULL if not found. */ - public Literal getLabelLiteral(OntResource r) { + protected Literal getLabelLiteral(OntResource r) { Literal labelLiteral = null; r.getOntModel().enterCriticalSection(Lock.READ); try { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/DataPropertyStatementTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/DataPropertyStatementTemplateModel.java index 98316e222..da631e0bc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/DataPropertyStatementTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/DataPropertyStatementTemplateModel.java @@ -29,7 +29,7 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat protected String value = null; // Used for editing - private String dataPropHash = null; + protected String dataPropHash = null; //Useful in case additional params to be retrieved for URL private VitroRequest vitroRequest= null; @@ -84,7 +84,7 @@ public class DataPropertyStatementTemplateModel extends PropertyStatementTemplat } // Determine whether the statement can be deleted - // Hack for rdfs:label - the policy doesn't prevent deletion + // Hack for rdfs:label - the policy doesn't prevent deletion. if ( ! propertyUri.equals(VitroVocabulary.LABEL) ) { action = new DropDataPropStmt(dps); if (policyHelper.isAuthorizedAction(action)) { diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/NameStatementTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/NameStatementTemplateModel.java index b5e6c16e2..d00e5041f 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/NameStatementTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/NameStatementTemplateModel.java @@ -9,16 +9,21 @@ 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.auth.requestedAction.ifaces.RequestedAction; +import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.DropDataPropStmt; +import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.propstmt.EditDataPropStmt; +import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; +import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao; +import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; 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; +import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral; +import edu.cornell.mannlib.vitro.webapp.edit.n3editing.processEdit.RdfLiteralHash; public class NameStatementTemplateModel extends DataPropertyStatementTemplateModel { @@ -27,43 +32,49 @@ public class NameStatementTemplateModel extends /* * This method handles the special case where we are creating a DataPropertyStatementTemplateModel outside the GroupedPropertyList. - * Specifically, it allows rdfs:label to be treated like a data property statement and thus have editing links. It is not possible - * to handle rdfs:label like vitro links and vitroPublic image, because it is not possible to construct a DataProperty from - * rdfs:label. + * Specifically, it allows rdfs:label to be treated like a data property statement and thus have editing links. */ NameStatementTemplateModel(String subjectUri, VitroRequest vreq, EditingPolicyHelper policyHelper) { super(subjectUri, VitroVocabulary.LABEL, vreq, policyHelper); - Literal literal = null; WebappDaoFactory wdf = vreq.getWebappDaoFactory(); - // 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 literals = dpsDao.getDataPropertyValuesForIndividualByProperty(subjectUri, VitroVocabulary.LABEL); - // Make sure the subject has a value for this property - if (literals.size() > 0) { - literal = literals.get(0); - } - } + // NIHVIVO-2466 Use the same methods to get the label that are used elsewhere in the + // application, to guarantee consistent results for individuals with multiple labels + // across the application. + IndividualDao iDao = wdf.getIndividualDao(); + EditLiteral literal = iDao.getLabelEditLiteral(subjectUri); 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; + // 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 - // edited directly. + // edited directly.) URI uri = new URIImpl(subjectUri); value = uri.getLocalName(); } } + + protected void setEditAccess(EditLiteral value, EditingPolicyHelper policyHelper) { + + if (policyHelper != null) { // we're editing + DataPropertyStatement dps = new DataPropertyStatementImpl(subjectUri, propertyUri, value.getLexicalForm()); + // Language and datatype are needed to get the correct hash value + dps.setLanguage(value.getLanguage()); + dps.setDatatypeURI(value.getDatatypeURI()); + this.dataPropHash = String.valueOf(RdfLiteralHash.makeRdfLiteralHash(dps)); + + // Determine whether the statement can be edited + RequestedAction action = new EditDataPropStmt(dps); + if (policyHelper.isAuthorizedAction(action)) { + markEditable(); + } + + // The label cannot be deleted, so we don't need to check + // the policy for the delete action. + } + } } diff --git a/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/IndividualDaoStub.java b/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/IndividualDaoStub.java index 4caf12cbe..cdbb4d31b 100644 --- a/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/IndividualDaoStub.java +++ b/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/IndividualDaoStub.java @@ -14,6 +14,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.Keyword; import edu.cornell.mannlib.vitro.webapp.beans.VClass; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.dao.InsertException; +import edu.cornell.mannlib.vitro.webapp.edit.EditLiteral; /** * A minimal implementation of the IndividualDao. @@ -237,4 +238,10 @@ public class IndividualDaoStub implements IndividualDao { "IndividualDaoStub.getIndividualByExternalId() not implemented."); } + @Override + public EditLiteral getLabelEditLiteral(String individualUri) { + throw new RuntimeException( + "IndividualDaoStub.getLabelLiteral() not implemented."); + } + }