diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactory.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactory.java index 05eccbccf..5cfa5d278 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactory.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactory.java @@ -28,6 +28,14 @@ public interface WebappDaoFactory { */ public String checkURI(String uriStr, boolean checkUniqueness); + /** + * Check if a given URI string exists in the system: + * checks for the following conditions: URI found as subject in a statement or an object or as a property + * @param uriStr + * @return + */ + public boolean hasExistingURI(String uriStr); + public String getDefaultNamespace(); public Set getNonuserNamespaces(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/WebappDaoFactoryFiltering.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/WebappDaoFactoryFiltering.java index 7ebe23b0d..828757a2d 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/WebappDaoFactoryFiltering.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/filtering/WebappDaoFactoryFiltering.java @@ -83,6 +83,10 @@ public class WebappDaoFactoryFiltering implements WebappDaoFactory { return innerWebappDaoFactory.checkURI(uriStr, checkUniqueness); } + public boolean hasExistingURI(String uriStr) { + return innerWebappDaoFactory.hasExistingURI(uriStr); + } + public WebappDaoFactory getUserAwareDaoFactory(String userURI) { //TODO: need to clone the filtering factory return innerWebappDaoFactory.getUserAwareDaoFactory(userURI); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java index 143f93c61..a002b7ea9 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/WebappDaoFactoryJena.java @@ -186,40 +186,58 @@ public class WebappDaoFactoryJena implements WebappDaoFactory { errorMsg += (iri.violations(false).next()) .getShortMessage() + " "; } else if (checkUniqueness) { - OntModel ontModel = ontModelSelector.getFullModel(); - ontModel.enterCriticalSection(Lock.READ); - try { - Resource newURIAsRes = ResourceFactory.createResource(uriStr); - Property newURIAsProp = ResourceFactory.createProperty(uriStr); - StmtIterator closeIt = ontModel.listStatements( - newURIAsRes, null, (RDFNode)null); - if (closeIt.hasNext()) { - validURI = false; - errorMsg+="Not a valid URI. Please enter another URI. "; - errorMsg+=duplicateMsg; - } - if (validURI) { - closeIt = ontModel.listStatements(null, null, newURIAsRes); - if (closeIt.hasNext()) { - validURI = false; - errorMsg+=duplicateMsg; - } - } - if (validURI) { - closeIt = ontModel.listStatements( - null, newURIAsProp, (RDFNode)null); - if (closeIt.hasNext()) { - validURI = false; - errorMsg+=duplicateMsg; - } - } - } finally { - ontModel.leaveCriticalSection(); - } + boolean existingURI = this.hasExistingURI(uriStr); + if(existingURI) { + errorMsg+="Not a valid URI. Please enter another URI. "; + errorMsg+=duplicateMsg; + //the original code included an extra line "Not a valid URI. Please enter another URI. " + //in the error message in addition to the duplicate error message in the case where the uri + //is in the subject position of any of the statements in the system - but not so where the + //uri was only in the object position or was a propery. In this code, the same error message + //is returned for all duplicate uris + } } return (errorMsg.length()>0) ? errorMsg : null; } + + + //Check if URI already in use or not either as resource OR as property + public boolean hasExistingURI(String uriStr) { + boolean existingURI = false; + OntModel ontModel = ontModelSelector.getFullModel(); + ontModel.enterCriticalSection(Lock.READ); + try { + Resource newURIAsRes = ResourceFactory.createResource(uriStr); + Property newURIAsProp = ResourceFactory.createProperty(uriStr); + StmtIterator closeIt = ontModel.listStatements( + newURIAsRes, null, (RDFNode)null); + if (closeIt.hasNext()) { + existingURI = true; + + } + //if not in the subject position, check in object position + if (!existingURI) { + closeIt = ontModel.listStatements(null, null, newURIAsRes); + if (closeIt.hasNext()) { + existingURI= true; + } + } + //Check for property + if (!existingURI) { + closeIt = ontModel.listStatements( + null, newURIAsProp, (RDFNode)null); + if (closeIt.hasNext()) { + existingURI = true; + } + } + } finally { + ontModel.leaveCriticalSection(); + } + + return existingURI; + } + public WebappDaoFactory getUserAwareDaoFactory(String userURI) { return new WebappDaoFactoryJena(this, userURI); } diff --git a/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactoryStub.java b/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactoryStub.java index e96dc1c6a..809aba701 100644 --- a/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactoryStub.java +++ b/webapp/test/stubs/edu/cornell/mannlib/vitro/webapp/dao/WebappDaoFactoryStub.java @@ -154,6 +154,12 @@ return this.objectPropertyStatementDao; } throw new RuntimeException( "WebappDaoFactory.checkURI() not implemented."); } + + @Override + public boolean hasExistingURI(String uriStr) { + throw new RuntimeException( + "WebappDaoFactory.hasExistingURI() not implemented."); + } @Override public Set getNonuserNamespaces() {