From 7d01b546a90b481fc15c444e2e2fb0a7142bf650 Mon Sep 17 00:00:00 2001 From: Brian Caruso Date: Thu, 5 Sep 2013 12:53:30 -0400 Subject: [PATCH] Adding ability to edit sameAs from profile page VIVO-111 --- .../dao/jena/ObjectPropertyDaoJena.java | 2 +- .../jena/ObjectPropertyStatementDaoJena.java | 28 +++++++++++---- .../PropertyGroupTemplateModel.java | 34 ++++++++++++------- .../individual/PropertyTemplateModel.java | 26 +++++++++----- .../ObjectPropertyStatementDaoJenaTest.java | 32 +++++++++++++++++ 5 files changed, 94 insertions(+), 28 deletions(-) create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJenaTest.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyDaoJena.java index b1973d45d..351711cb9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyDaoJena.java @@ -831,7 +831,7 @@ public class ObjectPropertyDaoJena extends PropertyDaoJena implements ObjectProp */ protected static final List EXCLUDED_NAMESPACES = Arrays.asList( - "http://www.w3.org/2002/07/owl#" + //"http://www.w3.org/2002/07/owl#" ); /* * This is a hack to throw out properties in the vitro, rdf, rdfs, and owl namespaces. diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJena.java index 073c1189f..0d45ab9c6 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJena.java @@ -2,6 +2,7 @@ package edu.cornell.mannlib.vitro.webapp.dao.jena; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -10,6 +11,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -34,6 +36,7 @@ import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.util.iterator.ClosableIterator; +import com.hp.hpl.jena.vocabulary.OWL; import edu.cornell.mannlib.vitro.webapp.beans.Individual; import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty; @@ -347,7 +350,7 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec return null; } - Model constructedModel = ModelFactory.createDefaultModel(); + Model constructedModel = ModelFactory.createDefaultModel(); for (String queryString : constructQueries) { @@ -402,13 +405,24 @@ public class ObjectPropertyStatementDaoJena extends JenaBaseDao implements Objec w.close(); } } else { - constructedModel.read( - rdfService.sparqlConstructQuery( - queryString, RDFService.ModelSerializationFormat.N3), null, "N3"); - + String parseFormat = "N3"; + RDFService.ModelSerializationFormat resultFormat = RDFService.ModelSerializationFormat.N3; + + /* If the test ObjectPropertyStatementDaoJenaTest.testN3WithSameAs() fails + * this code can be removed: */ + if( OWL.sameAs.getURI().equals( propertyUri )){ + // VIVO-111: owl:sameAs can be represented as = in n3 but Jena's parser does not recognize it. + // Switch to rdf/xml only for sameAs since it seems like n3 would be faster the rest of the time. + parseFormat = "RDF/XML"; + resultFormat = RDFService.ModelSerializationFormat.RDFXML; + } + /* end of removal */ + + InputStream is = rdfService.sparqlConstructQuery(queryString, resultFormat); + constructedModel.read( is, null, parseFormat); } - } catch (Exception e) { - log.error("Error getting constructed model for subject " + subjectUri + " and property " + propertyUri); + } catch (Exception e) { + log.error("Error getting constructed model for subject " + subjectUri + " and property " + propertyUri, e); } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyGroupTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyGroupTemplateModel.java index 357faeb40..0b285fb6e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyGroupTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyGroupTemplateModel.java @@ -22,10 +22,13 @@ public class PropertyGroupTemplateModel extends BaseTemplateModel { private final String name; private final List properties; - + + + PropertyGroupTemplateModel(VitroRequest vreq, PropertyGroup group, Individual subject, boolean editing, - List populatedDataPropertyList, List populatedObjectPropertyList) { + List populatedDataPropertyList, + List populatedObjectPropertyList) { this.name = group.getName(); @@ -49,6 +52,17 @@ public class PropertyGroupTemplateModel extends BaseTemplateModel { properties.remove(ptm); } + + public String toString(){ + String ptmStr =""; + for( int i=0; i < properties.size() ; i ++ ){ + PropertyTemplateModel ptm = properties.get(i); + String spacer = "\n "; + if( ptm != null ) + ptmStr = ptmStr + spacer + ptm.toString(); + } + return String.format("\nPropertyGroupTemplateModel %s[%s] ",name, ptmStr ); + } /* Accessor methods for templates */ // Add this so it's included in dumps for debugging. The templates will want to display @@ -58,18 +72,14 @@ public class PropertyGroupTemplateModel extends BaseTemplateModel { } public String getName(String otherGroupName) { - String displayName = name; - if (displayName == null) { - displayName = ""; - } else if (displayName.isEmpty()) { - displayName = otherGroupName; - } - return displayName; + if (name == null || name.isEmpty()) { + return otherGroupName; + } else { + return name; + } } public List getProperties() { return properties; - } - - + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyTemplateModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyTemplateModel.java index 6569e28d0..496e80a1a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyTemplateModel.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/templatemodels/individual/PropertyTemplateModel.java @@ -36,12 +36,13 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel { private String name; + + PropertyTemplateModel(Property property, Individual subject, VitroRequest vreq) { this.vreq = vreq; subjectUri = subject.getURI(); propertyUri = property.getURI(); - localName = property.getLocalName(); - log.debug("Local name for property " + propertyUri + ": " + localName); + localName = property.getLocalName(); setVerboseDisplayValues(property); addUrl = ""; @@ -53,14 +54,18 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel { protected void setVerboseDisplayValues(Property property) { // No verbose display for vitro and vitro public properties. - // This models previous behavior. In theory the verbose display can be provided, but we may not want - // to give anyone access to these properties, since the application is dependent on them. + // This models previous behavior. In theory the verbose display can be provided, + // but we may not want to give anyone access to these properties, since the + // application is dependent on them. String namespace = property.getNamespace(); - if (VitroVocabulary.vitroURI.equals(namespace) || VitroVocabulary.VITRO_PUBLIC.equals(namespace)) { + if (VitroVocabulary.vitroURI.equals(namespace) + || VitroVocabulary.VITRO_PUBLIC.equals(namespace)) { return; } - Boolean verboseDisplayValue = (Boolean) vreq.getSession().getAttribute("verbosePropertyDisplay"); + Boolean verboseDisplayValue = + (Boolean) vreq.getSession().getAttribute("verbosePropertyDisplay"); + if ( ! Boolean.TRUE.equals(verboseDisplayValue)) { return; } @@ -94,6 +99,12 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel { this.name = name; } + public String toString() { + return String.format("%s on %s", + propertyUri != null ? propertyUri : "null Prop URI", + subjectUri != null ? subjectUri : "null Sub URI" ); + } + /* Template properties */ public abstract String getType(); @@ -117,6 +128,5 @@ public abstract class PropertyTemplateModel extends BaseTemplateModel { public Map getVerboseDisplay() { return verboseDisplay; - } - + } } diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJenaTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJenaTest.java new file mode 100644 index 000000000..9493c3691 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/dao/jena/ObjectPropertyStatementDaoJenaTest.java @@ -0,0 +1,32 @@ +package edu.cornell.mannlib.vitro.webapp.dao.jena; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.hp.hpl.jena.rdf.model.Model; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +public class ObjectPropertyStatementDaoJenaTest { + + /** + * Test if jena lib can parse N3 that it generates. + * owl:sameAs has been a problem when it is represetned + * in N3 with the character = + */ + @Test + public void testN3WithSameAs() { + + String n3WithSameAs = " = ."; + + try{ + Model m = ModelFactory.createDefaultModel(); + m.read(n3WithSameAs, null, "N3"); + fail( "If this test fails it means that jena now correctly parses = when reading N3."); + }catch(Exception ex ){ + + + } + } + +}