fix for NIHVIVO-2533 (broken changeURI function)

This commit is contained in:
stellamit 2011-05-23 20:14:19 +00:00
parent 58dcccfa7e
commit 1c975919c5
2 changed files with 99 additions and 55 deletions

View file

@ -21,6 +21,11 @@ import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource; import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
@ -45,7 +50,10 @@ import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.Actions;
import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditOntology; import edu.cornell.mannlib.vitro.webapp.auth.requestedAction.usepages.EditOntology;
import edu.cornell.mannlib.vitro.webapp.controller.Controllers; import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.FileGraphSetup;
import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
public class RefactorOperationController extends BaseEditController { public class RefactorOperationController extends BaseEditController {
@ -69,16 +77,6 @@ public class RefactorOperationController extends BaseEditController {
ontModel.enterCriticalSection(Lock.WRITE); ontModel.enterCriticalSection(Lock.WRITE);
ArrayList<String> results = new ArrayList<String>(); ArrayList<String> results = new ArrayList<String>();
/* Debugging code thats inserts invalid triples into model
Property hasTitle = ontModel.getProperty("http://www.owl-ontologies.com/Ontology1209425965.owl#hasTitleee");
Resource product = ontModel.createResource("http://www.owl-ontologies.com/Ontology1209425965.owl#someBook14");
Resource product2 = ontModel.createResource("http://www.owl-ontologies.com/Ontology1209425965.owl#someBook15");
Literal illegalLiteral = ontModel.createTypedLiteral(134);
Literal illegalLiteral2 = ontModel.createTypedLiteral(234);
ontModel.add(product, hasTitle, illegalLiteral);
ontModel.add(product2, hasTitle, illegalLiteral2);
*/
try try
{ {
ExtendedIterator dataProperties = ontModel.listDatatypeProperties(); ExtendedIterator dataProperties = ontModel.listDatatypeProperties();
@ -182,10 +180,8 @@ public class RefactorOperationController extends BaseEditController {
} }
private String doRenameResource(VitroRequest request, HttpServletResponse response, EditProcessObject epo) { private String doRenameResource(VitroRequest request, HttpServletResponse response, EditProcessObject epo) {
String userURI = LoginStatusBean.getBean(request).getUserURI(); String userURI = LoginStatusBean.getBean(request).getUserURI();
OntModel ontModel = (OntModel) getServletContext().getAttribute("baseOntModel");
String oldURIStr = (String) epo.getAttribute("oldURI"); String oldURIStr = (String) epo.getAttribute("oldURI");
String newURIStr = request.getParameter("newURI"); String newURIStr = request.getParameter("newURI");
@ -214,39 +210,43 @@ public class RefactorOperationController extends BaseEditController {
return "STOP"; return "STOP";
} }
ontModel.enterCriticalSection(Lock.WRITE); // find the models that the resource is referred to in and change
ontModel.getBaseModel().notifyEvent(new EditEvent(userURI,true)); // the name in each of those models.
try { String queryStr = "SELECT distinct ?graph WHERE {{ GRAPH ?graph { ?subj <" + oldURIStr + "> ?obj }} ";
Property prop = ontModel.getProperty(oldURIStr); queryStr += " union { GRAPH ?graph { <" + oldURIStr + "> ?prop ?obj }} ";
if(prop != null) queryStr += " union { GRAPH ?graph { ?subj ?prop <" + oldURIStr + ">}}}";
{ Dataset dataset = request.getDataset();
try {
Property newProp = ontModel.createProperty(newURIStr); dataset.getLock().enterCriticalSection(Lock.READ);
StmtIterator statements = ontModel.listStatements(null, prop, (RDFNode)null); try {
try { ResultSet resultSet = QueryExecutionFactory.create(QueryFactory.create(queryStr), dataset).execSelect();
while(statements.hasNext()) {
Statement statement = (Statement)statements.next(); while (resultSet.hasNext()) {
Resource subj = statement.getSubject(); QuerySolution qs = resultSet.next();
RDFNode obj = statement.getObject(); String graphURI = qs.get("graph").asNode().toString();
Statement newStatement = ontModel.createStatement(subj, newProp, obj);
ontModel.add(newStatement); if (graphURI.startsWith(FileGraphSetup.FILEGRAPH_URI_ROOT)) {
} continue;
} finally { }
if (statements != null) {
statements.close(); boolean doNotify = false;
} Model model = null;
}
ontModel.remove(ontModel.listStatements(null, prop, (RDFNode)null)); if (JenaDataSourceSetupBase.JENA_TBOX_ASSERTIONS_MODEL.equals(graphURI)) {
} catch (InvalidPropertyURIException ipue) { model = ModelContext.getBaseOntModelSelector(getServletContext()).getTBoxModel();
/* if it can't be a property, don't bother with predicates */ doNotify = true;
} } else if (JenaDataSourceSetupBase.JENA_DB_MODEL.equals(graphURI)) {
Resource res = ontModel.getResource(oldURIStr); model = ModelContext.getBaseOntModelSelector(getServletContext()).getABoxModel();
ResourceUtils.renameResource(res,newURIStr); doNotify = true;
} } else {
} finally { model = dataset.getNamedModel(graphURI);
ontModel.getBaseModel().notifyEvent(new EditEvent(userURI,false)); }
ontModel.leaveCriticalSection();
} renameResourceInModel(model, userURI, oldURIStr, newURIStr, doNotify);
}
} finally {
dataset.getLock().leaveCriticalSection();
}
// there are no statements to delete, but we want indexes updated appropriately // there are no statements to delete, but we want indexes updated appropriately
request.getFullWebappDaoFactory().getIndividualDao().deleteIndividual(oldURIStr); request.getFullWebappDaoFactory().getIndividualDao().deleteIndividual(oldURIStr);
@ -275,6 +275,50 @@ public class RefactorOperationController extends BaseEditController {
} }
private void renameResourceInModel(Model model, String userURI, String oldURIStr, String newURIStr, boolean doNotify) {
model.enterCriticalSection(Lock.WRITE);
if (doNotify) {
model.notifyEvent(new EditEvent(userURI,true));
}
try {
Property prop = model.getProperty(oldURIStr); // this will create a resource if there isn't
// one by this URI (we don't expect this to happen
// and will also return a resource if the given
// URI is the URI of a class.
try {
Property newProp = model.createProperty(newURIStr);
StmtIterator statements = model.listStatements(null, prop, (RDFNode)null);
try {
while(statements.hasNext()) {
Statement statement = (Statement)statements.next();
Resource subj = statement.getSubject();
RDFNode obj = statement.getObject();
Statement newStatement = model.createStatement(subj, newProp, obj);
model.add(newStatement);
}
} finally {
if (statements != null) {
statements.close();
}
}
model.remove(model.listStatements(null, prop, (RDFNode)null));
} catch (InvalidPropertyURIException ipue) {
/* if it can't be a property, don't bother with predicates */
}
Resource res = model.getResource(oldURIStr);
ResourceUtils.renameResource(res,newURIStr);
} finally {
if (doNotify) {
model.notifyEvent(new EditEvent(userURI,false));
}
model.leaveCriticalSection();
}
}
private void doMovePropertyStatements(VitroRequest request, HttpServletResponse response, EditProcessObject epo) { private void doMovePropertyStatements(VitroRequest request, HttpServletResponse response, EditProcessObject epo) {
String userURI = LoginStatusBean.getBean(request).getUserURI(); String userURI = LoginStatusBean.getBean(request).getUserURI();

View file

@ -32,10 +32,10 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelectorImpl;
public class FileGraphSetup implements ServletContextListener { public class FileGraphSetup implements ServletContextListener {
private static String ABOX = "abox"; private static final String ABOX = "abox";
private static String TBOX = "tbox"; private static final String TBOX = "tbox";
private static String PATH_ROOT = "/WEB-INF/filegraph/"; private static final String PATH_ROOT = "/WEB-INF/filegraph/";
private static String URI_ROOT = "http://vitro.mannlib.cornell.edu/filegraph/"; public static final String FILEGRAPH_URI_ROOT = "http://vitro.mannlib.cornell.edu/filegraph/";
private static final Log log = LogFactory.getLog(FileGraphSetup.class); private static final Log log = LogFactory.getLog(FileGraphSetup.class);
@ -189,7 +189,7 @@ public class FileGraphSetup implements ServletContextListener {
*/ */
public void cleanupDB(Store kbStore, Set<String> uriSet, String type) { public void cleanupDB(Store kbStore, Set<String> uriSet, String type) {
Pattern graphURIPat = Pattern.compile("^" + URI_ROOT + type); Pattern graphURIPat = Pattern.compile("^" + FILEGRAPH_URI_ROOT + type);
Iterator<Node> iter = StoreUtils.storeGraphNames(kbStore); Iterator<Node> iter = StoreUtils.storeGraphNames(kbStore);
@ -237,7 +237,7 @@ public class FileGraphSetup implements ServletContextListener {
if (path != null) { if (path != null) {
File file = new File(path); File file = new File(path);
uri = URI_ROOT + type + "/" + file.getName(); uri = FILEGRAPH_URI_ROOT + type + "/" + file.getName();
} }
return uri; return uri;