From 3d527acce9e0264109c4cd9e75fd0709f787e83a Mon Sep 17 00:00:00 2001 From: tworrall Date: Wed, 18 Sep 2013 17:01:26 -0400 Subject: [PATCH] VIVO-112 and VIVO-298 --- .../listViewConfig-authorInAuthorship.xml | 13 +- ...Config-informationResourceInAuthorship.xml | 2 +- ...AuthorsToInformationResourceGenerator.java | 153 ++++++++++-------- .../AddPublicationToPersonGenerator.java | 48 ++++-- src/org/vivoweb/webapp/util/ModelUtils.java | 4 +- 5 files changed, 139 insertions(+), 81 deletions(-) diff --git a/productMods/config/listViewConfig-authorInAuthorship.xml b/productMods/config/listViewConfig-authorInAuthorship.xml index 6a4b9aad..51f0a127 100644 --- a/productMods/config/listViewConfig-authorInAuthorship.xml +++ b/productMods/config/listViewConfig-authorInAuthorship.xml @@ -10,6 +10,7 @@ PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#> PREFIX bibo: <http://purl.org/ontology/bibo/> PREFIX vitro: <http://vitro.mannlib.cornell.edu/ns/vitro/0.7#> + PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT DISTINCT ?subclass ?authorship @@ -41,7 +42,8 @@ OPTIONAL { ?infoResource core:publisher ?publisherObj . ?publisherObj rdfs:label ?publisher } - OPTIONAL { ?infoResource core:editor ?editorObj . + OPTIONAL { ?infoResource core:relatedBy ?editorship . + ?editorObj core:relatedBy ?editorship . ?editorObj rdfs:label ?editor } OPTIONAL { ?infoResource core:partOf ?partOfObj . @@ -118,6 +120,7 @@ PREFIX core: <http://vivoweb.org/ontology/core#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX bibo: <http://purl.org/ontology/bibo/> + PREFIX foaf: <http://xmlns.com/foaf/0.1/> CONSTRUCT { ?subject ?property ?authorship . ?authorship ?authorshipProperty ?authorshipValue . @@ -125,7 +128,10 @@ ?infoResource ?infoResourceProperty ?infoResourceValue . ?infoResource bibo:reproducedIn ?appearsInObj . ?infoResource core:publisher ?publisherObj . - ?infoResource core:editor ?editorObj . + ?infoResource core:relatedBy ?editorship . + ?editorship a core:Editorship . + ?editorship core:relates ?editorObj . + ?editorObj a foaf:Person . ?infoResource core:partOf ?partOfObj . ?appearsInObj rdfs:label ?appearsIn . ?publisherObj rdfs:label ?publisher . @@ -159,7 +165,8 @@ } UNION { ?subject ?property ?authorship . ?authorship core:relates ?infoResource . - ?infoResource core:editor ?editorObj . + ?infoResource core:relatedBy ?editorship . + ?editorship core:relates ?editorObj . ?editorObj rdfs:label ?editor } UNION { ?subject ?property ?authorship . diff --git a/productMods/config/listViewConfig-informationResourceInAuthorship.xml b/productMods/config/listViewConfig-informationResourceInAuthorship.xml index d7af9d7f..43d2c320 100644 --- a/productMods/config/listViewConfig-informationResourceInAuthorship.xml +++ b/productMods/config/listViewConfig-informationResourceInAuthorship.xml @@ -17,7 +17,7 @@ WHERE { ?subject ?property ?authorship . ?authorship a core:Authorship - OPTIONAL { ?authorship core:authorRank ?rank } + OPTIONAL { ?authorship core:rank ?rank } OPTIONAL { ?authorship core:relates ?person . ?person a foaf:Person . ?person rdfs:label ?personName diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAuthorsToInformationResourceGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAuthorsToInformationResourceGenerator.java index be5d8f05..1f8227cf 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAuthorsToInformationResourceGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddAuthorsToInformationResourceGenerator.java @@ -8,12 +8,16 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import javax.servlet.http.HttpSession; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import com.hp.hpl.jena.query.QuerySolution; +import com.hp.hpl.jena.query.ResultSet; +import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.vocabulary.RDFS; import com.hp.hpl.jena.vocabulary.XSD; @@ -22,6 +26,7 @@ import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyComparator; import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; +import edu.cornell.mannlib.vitro.webapp.dao.jena.QueryUtils; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.PublicationHasAuthorValidator; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.DateTimeIntervalValidationVTwo; import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils; @@ -334,99 +339,117 @@ public class AddAuthorsToInformationResourceGenerator extends VivoBaseGenerator public void addFormSpecificData(EditConfigurationVTwo editConfiguration, VitroRequest vreq) { HashMap formSpecificData = new HashMap(); //Get the existing authorships - formSpecificData.put("existingAuthorInfo", getExistingAuthorships(vreq)); - formSpecificData.put("newRank", getMaxRank(vreq) + 1); - formSpecificData.put("rankPredicate", authorRankPredicate); + formSpecificData.put("existingAuthorInfo", getExistingAuthorships(editConfiguration.getSubjectUri(), vreq)); + formSpecificData.put("newRank", getMaxRank(editConfiguration.getSubjectUri(), vreq) + 1); + formSpecificData.put("rankPredicate", "http://vivoweb.org/ontology/core#rank"); editConfiguration.setFormSpecificData(formSpecificData); } - - private List getExistingAuthorships(VitroRequest vreq) { - Individual infoResource = EditConfigurationUtils.getSubjectIndividual(vreq); - List authorships = infoResource.getRelatedIndividuals( - EditConfigurationUtils.getPredicateUri(vreq)); - //TODO: Check if sorted correctly - - log.debug("authorships = " + authorships); - - sortAuthorshipIndividuals(authorships); - - return getAuthorshipInfo(authorships); - } -/* + private static String AUTHORSHIPS_QUERY = "" + "PREFIX core: \n" + "PREFIX afn: \n" + "PREFIX rdfs: \n" + "PREFIX foaf: \n" - + "SELECT ?authorshipURI (afn:localname(?authorshipURI) AS ?authorshipName) ?authorURI ?authorName \n" + + "SELECT ?authorshipURI (afn:localname(?authorshipURI) AS ?authorshipName) ?authorURI ?authorName ?rank \n" + "WHERE { \n" + "?subject core:relatedBy ?authorshipURI . \n" + + "?authorshipURI a core:Authorship . \n" + "?authorshipURI core:relates ?authorURI . \n" + "?authorURI a foaf:Person . \n" - + "?authorURI rdfs:label ?authorName \n" - + "}"; + + "OPTIONAL { ?authorURI rdfs:label ?authorName } \n" + + "OPTIONAL { ?authorshipURI core:rank ?rank } \n" + + "} ORDER BY ?rank"; - private List> getExistingAuthorships(String subjectUri, VitroRequest vreq) { + private List getExistingAuthorships(String subjectUri, VitroRequest vreq) { String queryStr = QueryUtils.subUriForQueryVar(this.getAuthorshipsQuery(), "subject", subjectUri); log.debug("Query string is: " + queryStr); - List> authorshipss = new ArrayList>(); + List> authorships = new ArrayList>(); try { ResultSet results = QueryUtils.getQueryResults(queryStr, vreq); while (results.hasNext()) { QuerySolution soln = results.nextSolution(); - RDFNode node = soln.get("link"); + RDFNode node = soln.get("authorshipURI"); if (node.isURIResource()) { - webpages.add(QueryUtils.querySolutionToStringValueMap(soln)); + authorships.add(QueryUtils.querySolutionToStringValueMap(soln)); } } } catch (Exception e) { log.error(e, e); } - log.debug("webpages = " + webpages); - return webpages; + log.debug("authorships = " + authorships); + return getAuthorshipInfo(authorships); } -*/ + + private static String MAX_RANK_QUERY = "" + + "PREFIX core: \n" + + "SELECT DISTINCT ?rank WHERE { \n" + + " ?subject core:relatedBy ?authorship . \n" + + " ?authorship a core:Authorship . \n" + + " ?authorship core:rank ?rank .\n" + + "} ORDER BY DESC(?rank) LIMIT 1"; + + private int getMaxRank(String subjectUri, VitroRequest vreq) { + + int maxRank = 0; // default value + String queryStr = QueryUtils.subUriForQueryVar(this.getMaxRankQueryStr(), "subject", subjectUri); + log.debug("maxRank query string is: " + queryStr); + try { + ResultSet results = QueryUtils.getQueryResults(queryStr, vreq); + if (results != null && results.hasNext()) { // there is at most one result + QuerySolution soln = results.next(); + RDFNode node = soln.get("rank"); + if (node != null && node.isLiteral()) { + // node.asLiteral().getInt() won't return an xsd:string that + // can be parsed as an int. + int rank = Integer.parseInt(node.asLiteral().getLexicalForm()); + if (rank > maxRank) { + log.debug("setting maxRank to " + rank); + maxRank = rank; + } + } + } + } catch (NumberFormatException e) { + log.error("Invalid rank returned from query: not an integer value."); + } catch (Exception e) { + log.error(e, e); + } + log.debug("maxRank is: " + maxRank); + return maxRank; + } + private List getAuthorshipInfo( - List authorships) { + List> authorships) { List info = new ArrayList(); - for ( Individual authorship : authorships ) { - String authorshipUri = authorship.getURI(); - String authorshipName = authorship.getName(); - String authorUri = ""; - String authorName = ""; - Individual author = authorship.getRelatedIndividual(linkedAuthorPredicate); - if(author != null) { - authorUri = author.getURI(); - authorName = author.getName(); - } - AuthorshipInfo aaInfo = new AuthorshipInfo(authorshipUri, authorshipName, authorUri, authorName); - info.add(aaInfo); + String authorshipUri = ""; + String authorshipName = ""; + String authorUri = ""; + String authorName = ""; + + for ( Map authorship : authorships ) { + for (Entry entry : authorship.entrySet() ) { + if ( entry.getKey().equals("authorshipURI") ) { + authorshipUri = entry.getValue(); + } + else if ( entry.getKey().equals("authorshipName") ) { + authorshipName = entry.getValue(); + } + else if ( entry.getKey().equals("authorURI") ) { + authorUri = entry.getValue(); + } + else if ( entry.getKey().equals("authorName") ) { + authorName = entry.getValue(); + } + } + + AuthorshipInfo aaInfo = new AuthorshipInfo(authorshipUri, authorshipName, authorUri, authorName); + info.add(aaInfo); } + log.debug("info = " + info); return info; } - private int getMaxRank(VitroRequest vreq) { - Individual infoResource = EditConfigurationUtils.getSubjectIndividual(vreq); - List authorships = infoResource.getRelatedIndividuals( - EditConfigurationUtils.getPredicateUri(vreq)); - sortAuthorshipIndividuals(authorships); - int maxRank = 0; - for(Individual authorship: authorships) { - DataPropertyStatement rankStmt = authorship.getDataPropertyStatement(authorRankPredicate); - if (rankStmt != null) { - maxRank = Integer.parseInt(rankStmt.getData()); - } - } - return maxRank; - } - - private void sortAuthorshipIndividuals(List authorships) { - DataPropertyComparator comp = new DataPropertyComparator(authorRankPredicate); - Collections.sort(authorships, comp); - } - //This is the information about authors the form will require public class AuthorshipInfo { //This is the authorship node information @@ -467,8 +490,12 @@ public class AddAuthorsToInformationResourceGenerator extends VivoBaseGenerator static final String DEFAULT_NS_TOKEN=null; //null forces the default NS -// protected String getAuthorshipsQuery() { -// return AUTHORSHIPS_QUERY; -// } + protected String getMaxRankQueryStr() { + return MAX_RANK_QUERY; + } + + protected String getAuthorshipsQuery() { + return AUTHORSHIPS_QUERY; + } } diff --git a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddPublicationToPersonGenerator.java b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddPublicationToPersonGenerator.java index f30cc908..3f26de70 100644 --- a/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddPublicationToPersonGenerator.java +++ b/src/edu/cornell/mannlib/vitro/webapp/edit/n3editing/configuration/generators/AddPublicationToPersonGenerator.java @@ -308,15 +308,24 @@ public class AddPublicationToPersonGenerator extends VivoBaseGenerator implement private String getN3ForNewBookNewEditor() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?newBook vivo:editor ?newEditor . \n " + - "?newEditor vivo:editorOf ?newBook . \n" + + "?newBook vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?newBook . \n" + + "?newBook <" + label + "> ?book . \n " + + "?editorship a vivo:Editorship . \n" + + "?editorship vivo:relates ?newEditor . \n" + + "?newEditor a <" + editorClass + "> . \n" + + "?newEditor vivo:relatedBy ?editorship . \n" + "?newEditor <" + label + "> ?editor ."; } private String getN3ForNewBookEditor() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?newBook vivo:editor ?editorUri . \n" + - "?editorUri vivo:editorOf ?newBook . " ; + "?newBook vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?newBook . \n" + + "?newBook <" + label + "> ?book . \n " + + "?editorship a vivo:Editorship . \n" + + "?editorship vivo:relates ?editorUri . \n" + + "?editorUri vivo:relatedBy ?editorship . "; } private String getN3ForNewBookNewPublisher() { @@ -396,30 +405,44 @@ public class AddPublicationToPersonGenerator extends VivoBaseGenerator implement private String getN3ForNewEditor() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?pubUri vivo:editor ?newEditor . \n" + + "?pubUri vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?pubUri . \n" + + "?editorship a vivo:Editorship . \n" + + "?editorship vivo:relates ?newEditor . \n" + "?newEditor a <" + editorClass + "> . \n" + - "?newEditor vivo:editorOf ?pubUri . \n" + + "?newEditor vivo:relatedBy ?editorship . \n" + "?newEditor <" + label + "> ?editor ."; } private String getN3ForEditor() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?pubUri vivo:editor ?editorUri . \n" + - "?editorUri vivo:editorOf ?pubUri . "; + "?pubUri vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?pubUri . \n" + + "?editorship a vivo:Editorship . \n" + + "?editorship vivo:relates ?editorUri . \n" + + "?editorUri vivo:relatedBy ?editorship . "; } private String getN3ForNewEditorNewPub() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?newPublication vivo:editor ?newEditor . \n" + + "?newPublication vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?newPublication . \n" + + "?newPublication <" + label + "> ?title ." + + "?editorship a vivo:Editorship . \n" + + "?editorship vivo:relates ?newEditor . \n" + "?newEditor a <" + editorClass + "> . \n" + - "?newEditor vivo:editorOf ?newPublication . \n" + + "?newEditor vivo:relatedBy ?editorship . \n" + "?newEditor <" + label + "> ?editor ."; } private String getN3ForEditorNewPub() { return "@prefix vivo: <" + vivoCore + "> . \n" + - "?newPublication vivo:editor ?editorUri . \n" + - "?editorUri vivo:editorOf ?newPublication . "; + "?newPublication vivo:relatedBy ?editorship . \n" + + "?editorship vivo:relates ?newPublication . \n" + + "?newPublication <" + label + "> ?title ." + + "?editorship vivo:relates ?editorUri . \n" + + "?editorship a vivo:Editorship . \n" + + "?editorUri vivo:relatedBy ?editorship . "; } private String getN3ForNewPublisher() { @@ -525,6 +548,7 @@ public class AddPublicationToPersonGenerator extends VivoBaseGenerator implement newResources.put("newConference", DEFAULT_NS_TOKEN); newResources.put("newEvent", DEFAULT_NS_TOKEN); newResources.put("newEditor", DEFAULT_NS_TOKEN); + newResources.put("editorship", DEFAULT_NS_TOKEN); newResources.put("vcardEditor", DEFAULT_NS_TOKEN); newResources.put("vcardName", DEFAULT_NS_TOKEN); newResources.put("newPublisher", DEFAULT_NS_TOKEN); diff --git a/src/org/vivoweb/webapp/util/ModelUtils.java b/src/org/vivoweb/webapp/util/ModelUtils.java index b3df1d6b..629d9f52 100644 --- a/src/org/vivoweb/webapp/util/ModelUtils.java +++ b/src/org/vivoweb/webapp/util/ModelUtils.java @@ -24,8 +24,8 @@ public class ModelUtils { private static final String processPropertyInverseURI = "http://purl.obolibrary.org/obo/BFO_0000055"; private static final String nonProcessPropertyURI = "http://vivoweb.org/ontology/core#roleContributesTo"; private static final String nonProcessPropertyInverseURI = "http://vivoweb.org/ontology/core#contributingRole"; - private static final String grantPropertyURI = "http://vivoweb.org/ontology/core#relates"; - private static final String grantPropertyInverseURI = "http://vivoweb.org/ontology/core#relatedBy"; + private static final String grantPropertyURI = "http://vivoweb.org/ontology/core#relatedBy"; + private static final String grantPropertyInverseURI = "http://vivoweb.org/ontology/core#relates"; private static Set processClass = new HashSet(); static {