Merge 4725 and most of 4732 into the trunk (IndexBuilder.java would not merge).

This commit is contained in:
jeb228 2010-04-14 14:42:26 +00:00
parent 5579c1a9ac
commit 4965958602
3 changed files with 141 additions and 93 deletions

View file

@ -69,7 +69,8 @@ public class VitroVocabulary {
public static final String SUNRISE = vitroURI+"sunrise";
public static final String SUNSET = vitroURI+"sunset";
public static final String DEPENDENT_RESORUCE = "http://vivoweb.org/ontology/core#DependentResource";
//////////////////////////////////////////

View file

@ -2,145 +2,144 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelChangedListener;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
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.vocabulary.RDF;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualCreationEvent;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualDeletionEvent;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualEditEvent;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.IndividualUpdateEvent;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent;
import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder;
public class SearchReindexingListener implements ModelChangedListener {
public class SearchReindexingListener implements ModelChangedListener {
private ServletContext context;
private HashSet<String> changedUris;
private static final Log log = LogFactory.getLog(SearchReindexingListener.class.getName());
private OntModel ontModel;
private ServletContext servletContext;
protected DateFormat xsdDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
private boolean dirty = false;
private boolean indexing = false;
public SearchReindexingListener(OntModel ontModel, ServletContext sc) {
this.ontModel = ontModel;
this.servletContext = sc;
public SearchReindexingListener(OntModel ontModel, ServletContext sc) {
this.context = sc;
this.changedUris = new HashSet<String>();
}
public void notifyEvent(Model arg0, Object arg1) {
if ( (arg1 instanceof EditEvent) ){
EditEvent editEvent = (EditEvent)arg1;
if( editEvent.getBegin() ){
}else{ // editEvent is the end of an edit
log.debug("doing search index build");
IndexBuilder builder = (IndexBuilder) context.getAttribute(IndexBuilder.class.getName());
if( builder != null ){
for( String uri: getAndClearChangedUris()){
builder.addToChangedUris(uri);
}
new Thread(builder).start();
}else{
log.debug("Could not get IndexBuilder from servlet context, cannot create index for full text seraching.");
getAndClearChangedUris(); //clear list of changes because they cannot be indexed.
}
}
}
}
private class Reindexer implements Runnable {
public void run() {
while(dirty) {
dirty = false;
IndexBuilder builder = (IndexBuilder) servletContext.getAttribute(IndexBuilder.class.getName());
indexing = true;
try {
builder.run();
} finally {
indexing = false;
}
}
private boolean isNormalPredicate(Property p) {
if( p == null ) return false;
/* currently the only predicate that is filtered out is rdf:type.
* It may be useful to improve this so that it may be configured
* at run time.*/
if( RDF.type.equals( p ))
return false;
else
return true;
}
private synchronized Set<String> getAndClearChangedUris(){
log.debug("getting and clearing changed URIs.");
Set<String> out = changedUris;
changedUris = new HashSet<String>();
return out;
}
private synchronized void addChange(Statement stmt){
if( stmt == null ) return;
if( stmt.getSubject().isURIResource() ){
changedUris.add( stmt.getSubject().getURI());
log.debug(stmt.getSubject().getURI());
}
if( stmt.getObject().isURIResource() && isNormalPredicate( stmt.getPredicate() ) ){
changedUris.add( ((Resource) stmt.getObject().as(Resource.class)).getURI() );
log.debug(((Resource) stmt.getObject().as(Resource.class)).getURI());
}
}
public void addedStatement(Statement arg0) {
public void addedStatement(Statement stmt) {
addChange(stmt);
}
public void addedStatements(Statement[] arg0) {
// TODO Auto-generated method stub
for(Statement stmt : arg0)
addChange(stmt);
}
public void addedStatements(List arg0) {
// TODO Auto-generated method stub
public void addedStatements(List arg0) {
for(Statement stmt : (List<Statement>)arg0)
addChange(stmt);
}
public void addedStatements(StmtIterator arg0) {
// TODO Auto-generated method stub
}
public void addedStatements(Model arg0) {
// TODO Auto-generated method stub
}
public void notifyEvent(Model arg0, Object arg1) {
if ((arg1 instanceof IndividualCreationEvent) || (arg1 instanceof IndividualUpdateEvent)) {
IndividualEditEvent ee = (IndividualEditEvent) arg1;
if (!ee.getBegin()) {
dirty=true;
if (!indexing) {
new Thread(new Reindexer()).start();
}
if( arg0 != null ){
while( arg0.hasNext() ){
addChange(arg0.nextStatement());
}
} else if (arg1 instanceof IndividualDeletionEvent) {
IndividualEditEvent ee = (IndividualEditEvent) arg1;
IndexBuilder builder = (IndexBuilder) servletContext.getAttribute(IndexBuilder.class.getName());
if (builder != null) {
builder.entityDeleted(ee.getIndividualURI());
} else {
log.warn("Unable to remove individual from search index: no attribute " + IndexBuilder.class.getName() + " in servlet context");
}
}
}
public void removedStatement(Statement arg0) {
// TODO Auto-generated method stub
public void addedStatements(Model arg0) {
if( arg0 != null)
addedStatements(arg0.listStatements());
}
public void removedStatement(Statement stmt){
addChange(stmt);
}
public void removedStatements(Statement[] arg0) {
// TODO Auto-generated method stub
for(Statement stmt : arg0)
addChange(stmt);
}
public void removedStatements(List arg0) {
// TODO Auto-generated method stub
public void removedStatements(List arg0) {
for(Statement stmt : (List<Statement>)arg0)
addChange(stmt);
}
public void removedStatements(StmtIterator arg0) {
// TODO Auto-generated method stub
if( arg0 != null ){
while( arg0.hasNext() ){
addChange(arg0.nextStatement());
}
}
}
public void removedStatements(Model arg0) {
// TODO Auto-generated method stub
if( arg0 != null)
removedStatements(arg0.listStatements());
}
private static final Log log = LogFactory.getLog(SearchReindexingListener.class.getName());
}

View file

@ -2,14 +2,27 @@
package edu.cornell.mannlib.vitro.webapp.edit.n3editing;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.shared.Lock;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.IdentifierBundle;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.RoleIdentifier;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.SelfEditingIdentifierFactory;
import edu.cornell.mannlib.vitro.webapp.auth.identifier.ServletIdentifierBundleFactory;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
import edu.cornell.mannlib.vitro.webapp.filters.VitroRequestPrep;
public class EditN3Utils {
@ -27,4 +40,39 @@ public class EditN3Utils {
return editorUri;
}
// public static void addModTimes( Model additions, Model retractions, Model contextModel ){
// Property modtime = ResourceFactory.createProperty(VitroVocabulary.MODTIME);
// Date time = Calendar.getInstance().getTime();
//
// //get all resources in additions and retractions that are not types
// additions.listStatements()
//
// Lock lock = contextModel.getLock();
// try {
//
// String existingValue = null;
// Statement stmt = res.getProperty(modtime);
// if (stmt != null) {
// RDFNode object = stmt.getObject();
// if (object != null && object.isLiteral()){
// existingValue = ((Literal)object).getString();
// }
// }
// String formattedDateStr = xsdDateTimeFormat.format(time);
// if ( (existingValue!=null && value == null) || (existingValue!=null && value != null && !(existingValue.equals(formattedDateStr)) ) ) {
// model.removeAll(res, modtime, null);
// }
// if ( (existingValue==null && value != null) || (existingValue!=null && value != null && !(existingValue.equals(formattedDateStr)) ) ) {
// model.add(res, modtime, formattedDateStr, XSDDatatype.XSDdateTime);
// }
//
// } catch (Exception e) {
// log.error("Error in updatePropertyDateTimeValue");
// log.error(e);
// }
// }
//
// private static List<URIResource>
}