NIHVIVO-3304 automatic portal renaming with default namespace change, and warning message

This commit is contained in:
brianjlowe 2011-11-11 17:23:21 +00:00
parent 586bf6d01e
commit 2dd79c60c6

View file

@ -4,7 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
@ -14,7 +13,6 @@ import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.Query;
@ -24,6 +22,8 @@ import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax; import com.hp.hpl.jena.query.Syntax;
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;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
@ -36,13 +36,13 @@ import com.hp.hpl.jena.sdb.store.DatabaseType;
import com.hp.hpl.jena.sdb.store.LayoutType; import com.hp.hpl.jena.sdb.store.LayoutType;
import com.hp.hpl.jena.sdb.util.StoreUtils; import com.hp.hpl.jena.sdb.util.StoreUtils;
import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.util.ResourceUtils;
import com.hp.hpl.jena.util.iterator.ClosableIterator; import com.hp.hpl.jena.util.iterator.ClosableIterator;
import com.hp.hpl.jena.vocabulary.RDF; import com.hp.hpl.jena.vocabulary.RDF;
import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties; import edu.cornell.mannlib.vitro.webapp.config.ConfigurationProperties;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary; import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory; import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDaoCon;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils; import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer; import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelSynchronizer;
@ -291,36 +291,63 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
private void checkForNamespaceMismatch(OntModel model, ServletContext ctx) { private void checkForNamespaceMismatch(OntModel model, ServletContext ctx) {
String expectedNamespace = getDefaultNamespace(ctx); String expectedNamespace = getDefaultNamespace(ctx);
List<String> portalURIs = new ArrayList<String>(); List<Resource> portals = getPortal1s(model);
if(!portals.isEmpty() && noPortalForNamespace(
portals, expectedNamespace)) {
// There really should be only one portal 1, but if there happen to
// be multiple, just arbitrarily pick the first in the list.
Resource portal = portals.get(0);
String oldNamespace = portal.getNameSpace();
renamePortal(portal, expectedNamespace, model);
StartupStatus ss = StartupStatus.getBean(ctx);
ss.warning(this, "\nThe default namespace has been changed \n" +
"from " + oldNamespace +
"\nto " + expectedNamespace + ".\n" +
"The application will function normally, but " +
"any individuals in the \n" + oldNamespace + " " +
"namespace will need to have their URIs \n" +
"changed in order to be served as linked data. " +
"You can use the Ingest Tools \nto change the " +
"URIs for a batch of resources.");
}
}
private List<Resource> getPortal1s(Model model) {
List<Resource> portals = new ArrayList<Resource>();
try { try {
model.enterCriticalSection(Lock.READ); model.enterCriticalSection(Lock.READ);
Iterator<Individual> portalIt = model.listIndividuals(PORTAL); ResIterator portalIt = model.listResourcesWithProperty(
RDF.type, PORTAL);
while (portalIt.hasNext()) { while (portalIt.hasNext()) {
portalURIs.add(portalIt.next().getURI()); Resource portal = portalIt.nextResource();
if ("portal1".equals(portal.getLocalName())) {
portals.add(portal);
}
} }
} finally { } finally {
model.leaveCriticalSection(); model.leaveCriticalSection();
} }
return portals;
}
for (String portalUri : portalURIs) { private boolean noPortalForNamespace(List<Resource> portals,
if (portalUri == null) { String expectedNamespace) {
continue; for (Resource portal : portals) {
if(expectedNamespace.equals(portal.getNameSpace())) {
return false;
} }
}
return true;
}
int splitHere = portalUri.lastIndexOf("/") + 1; private void renamePortal(Resource portal, String namespace, Model model) {
String actualNamespace = portalUri.substring(0, splitHere); model.enterCriticalSection(Lock.WRITE);
String portalName = portalUri.substring(splitHere); try {
ResourceUtils.renameResource(
if ("portal1".equals(portalName) portal, namespace + portal.getLocalName());
&& (!expectedNamespace.equals(actualNamespace))) { } finally {
StartupStatus ss = StartupStatus.getBean(ctx); model.leaveCriticalSection();
ss.fatal(this, "Default namespace mis-match. "
+ "The default namespace specified in deploy.properties ('" + expectedNamespace + "') "
+ "does not match the namespace used in the database ('" + actualNamespace + "'). "
+ "Perhaps the 'Vitro.defaultNamespace' property in deploy.properties is incorrect, "
+ "or perhaps the 'VitroConnection.DataSource.url' property in deploy.properties "
+ "specifies the wrong database.");
}
} }
} }