updates to include method that checks whether uri exists or not

This commit is contained in:
hudajkhan 2013-09-16 14:07:52 -04:00
parent d52b939743
commit 8fa8702462
4 changed files with 66 additions and 30 deletions

View file

@ -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<String> getNonuserNamespaces();

View file

@ -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);

View file

@ -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);
}

View file

@ -155,6 +155,12 @@ return this.objectPropertyStatementDao; }
"WebappDaoFactory.checkURI() not implemented.");
}
@Override
public boolean hasExistingURI(String uriStr) {
throw new RuntimeException(
"WebappDaoFactory.hasExistingURI() not implemented.");
}
@Override
public Set<String> getNonuserNamespaces() {
throw new RuntimeException(