NIHVIVO-1761 bugfix for backend property deletion

This commit is contained in:
bjl23 2011-01-17 21:09:32 +00:00
parent 46739ea3cd
commit aad1ea806b
8 changed files with 139 additions and 54 deletions

View file

@ -215,7 +215,10 @@ public class OperationController extends BaseEditController {
Object newObj = null; Object newObj = null;
if (epo.getOriginalBean() != null) { // we're updating or deleting an existing bean if (epo.getOriginalBean() != null) { // we're updating or deleting an existing bean
if (epo.getImplementationClass() != null) { if (epo.getImplementationClass() != null) {
newObj = OperationUtils.cloneBean(epo.getOriginalBean(), epo.getImplementationClass()); newObj = OperationUtils.cloneBean(
epo.getOriginalBean(),
epo.getImplementationClass(),
epo.getBeanClass());
} else { } else {
newObj = OperationUtils.cloneBean(epo.getOriginalBean()); newObj = OperationUtils.cloneBean(epo.getOriginalBean());
} }

View file

@ -67,7 +67,7 @@ public class OperationUtils{
* @return * @return
*/ */
public static Object cloneBean (Object bean) { public static Object cloneBean (Object bean) {
return cloneBean(bean, bean.getClass()); return cloneBean(bean, bean.getClass(), bean.getClass());
} }
/** /**
@ -76,39 +76,61 @@ public class OperationUtils{
* @param bean * @param bean
* @return * @return
*/ */
public static Object cloneBean (Object bean, Class beanClass){ public static Object cloneBean (Object bean, Class beanClass, Class iface){
Object newBean = null; Object newBean = null;
try { try {
newBean = beanClass.newInstance(); newBean = beanClass.newInstance();
Method[] beanMeths = beanClass.getMethods(); Method[] beanMeths = iface.getMethods();
for (int i=0; i<beanMeths.length ; ++i) { for (int i=0; i<beanMeths.length ; ++i) {
String methName = beanMeths[i].getName(); Method beanMeth = beanMeths[i];
if (methName.indexOf("get")==0){ String methName = beanMeth.getName();
if (methName.startsWith("get")
&& beanMeth.getParameterTypes().length == 0 ) {
String fieldName = methName.substring(3,methName.length()); String fieldName = methName.substring(3,methName.length());
Class returnType = beanMeths[i].getReturnType(); Class returnType = beanMeth.getReturnType();
try { try {
Class[] args = new Class[1]; Class[] args = new Class[1];
args[0] = returnType; args[0] = returnType;
Method setterMethod = beanClass.getMethod("set"+fieldName,args); Method setterMethod = iface.getMethod("set"+fieldName,args);
try { try {
Object fieldVal = beanMeths[i].invoke(bean,(Object[])null); Object fieldVal = beanMeth.invoke(bean,(Object[])null);
try { try {
Object[] setArgs = new Object[1]; Object[] setArgs = new Object[1];
setArgs[0] = fieldVal; setArgs[0] = fieldVal;
setterMethod.invoke(newBean,setArgs); setterMethod.invoke(newBean,setArgs);
} catch (IllegalAccessException iae) { } catch (IllegalAccessException iae) {
log.error("edu.cornell.mannlib.vitro.edit.utils.OperationUtils encountered IllegalAccessException invoking "+setterMethod.getName()); log.error(OperationUtils.class.getName() +
".cloneBean() " +
" encountered IllegalAccessException " +
" invoking " +
setterMethod.getName(), iae);
throw new RuntimeException(iae);
} catch (InvocationTargetException ite) { } catch (InvocationTargetException ite) {
log.error("edu.cornell.mannlib.vitro.edit.utils.OperationUtils encountered InvocationTargetException invoking "+setterMethod.getName()); log.error(OperationUtils.class.getName() +
log.error(ite.getTargetException().getClass().toString()); ".cloneBean() " +
" encountered InvocationTargetException"
+ " invoking "
+ setterMethod.getName(), ite);
throw new RuntimeException(ite);
} }
} catch (IllegalAccessException iae) { } catch (IllegalAccessException iae) {
log.error(OperationUtils.class.getName()+" encountered IllegalAccessException invoking "+beanMeths[i].getName()); log.error(OperationUtils.class.getName() +
".cloneBean() encountered " +
" IllegalAccessException invoking " +
beanMeths[i].getName(), iae);
throw new RuntimeException(iae);
} catch (InvocationTargetException ite) { } catch (InvocationTargetException ite) {
log.error(OperationUtils.class.getName()+" encountered InvocationTargetException invoking "+beanMeths[i].getName()); log.error(OperationUtils.class.getName() +
log.error(ite.getTargetException().getClass().toString()); ".cloneBean() encountered " +
" InvocationTargetException invoking " +
beanMeths[i].getName(), ite);
throw new RuntimeException(ite);
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
// log.error(OperationUtils.class.getName()+" found that "+beanMeths[i].getName()+" requires one or more arguments. Skipping."); log.error(OperationUtils.class.getName() +
".cloneBean() encountered " +
" IllegalArgumentException invoking " +
beanMeths[i].getName(), iae);
throw new RuntimeException(iae);
} }
} catch (NoSuchMethodException nsme){ } catch (NoSuchMethodException nsme){
// ignore this field because there is no setter method // ignore this field because there is no setter method

View file

@ -65,25 +65,39 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
this.dwf = dwf; this.dwf = dwf;
} }
public void deleteDataPropertyStatement(DataPropertyStatement dataPropertyStmt) {
deleteDataPropertyStatement(dataPropertyStmt, getOntModelSelector().getABoxModel());
}
public void deleteDataPropertyStatement( DataPropertyStatement dataPropertyStatement, OntModel ontModel ) public void deleteDataPropertyStatement( DataPropertyStatement dataPropertyStatement )
{ {
OntModel ontModel = getOntModelSelector().getABoxModel();
try { try {
ontModel.enterCriticalSection(Lock.WRITE); ontModel.enterCriticalSection(Lock.WRITE);
getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(),true,dataPropertyStatement.getIndividualURI())); getOntModel().getBaseModel().notifyEvent(
com.hp.hpl.jena.ontology.Individual ind = ontModel.getIndividual(dataPropertyStatement.getIndividualURI()); new IndividualUpdateEvent(
Property prop = ontModel.getProperty(dataPropertyStatement.getDatapropURI()); getWebappDaoFactory().getUserURI(),
Literal l = jenaLiteralFromDataPropertyStatement(dataPropertyStatement, ontModel); true,
dataPropertyStatement.getIndividualURI()));
com.hp.hpl.jena.ontology.Individual ind = ontModel.getIndividual(
dataPropertyStatement.getIndividualURI());
OntModel tboxModel = getOntModelSelector().getTBoxModel();
tboxModel.enterCriticalSection(Lock.READ);
try {
Property prop = tboxModel.getProperty(
dataPropertyStatement.getDatapropURI());
Literal l = jenaLiteralFromDataPropertyStatement(
dataPropertyStatement, ontModel);
if (ind != null && prop != null && l != null) { if (ind != null && prop != null && l != null) {
ontModel.getBaseModel().remove(ind, prop, l); ontModel.getBaseModel().remove(ind, prop, l);
} }
} finally { } finally {
getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(),false,dataPropertyStatement.getIndividualURI())); tboxModel.leaveCriticalSection();
}
} finally {
getOntModel().getBaseModel().notifyEvent(
new IndividualUpdateEvent(
getWebappDaoFactory().getUserURI(),
false,
dataPropertyStatement.getIndividualURI()));
ontModel.leaveCriticalSection(); ontModel.leaveCriticalSection();
} }
} }
@ -143,19 +157,29 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
deleteDataPropertyStatementsForIndividualByDataProperty(individualURI, dataPropertyURI, getOntModelSelector().getABoxModel()); deleteDataPropertyStatementsForIndividualByDataProperty(individualURI, dataPropertyURI, getOntModelSelector().getABoxModel());
} }
public void deleteDataPropertyStatementsForIndividualByDataProperty(String individualURI, String dataPropertyURI, OntModel ontModel) { public void deleteDataPropertyStatementsForIndividualByDataProperty(
String individualURI,
String dataPropertyURI,
OntModel ontModel) {
ontModel.enterCriticalSection(Lock.WRITE); ontModel.enterCriticalSection(Lock.WRITE);
getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(),true,individualURI)); getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(
getWebappDaoFactory().getUserURI(),
true,
individualURI));
try { try {
Resource indRes = ontModel.getResource(individualURI); Resource indRes = ResourceFactory.createResource(individualURI);
DatatypeProperty datatypeProperty = ontModel.getDatatypeProperty(dataPropertyURI); Property datatypeProperty = ResourceFactory.createProperty(
if (indRes != null && datatypeProperty != null) { dataPropertyURI);
ontModel.removeAll(indRes, datatypeProperty, (Literal)null); ontModel.removeAll(indRes, datatypeProperty, (Literal)null);
}
} finally { } finally {
getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(getWebappDaoFactory().getUserURI(),false,individualURI)); getOntModel().getBaseModel().notifyEvent(new IndividualUpdateEvent(
getWebappDaoFactory().getUserURI(),
false,
individualURI));
ontModel.leaveCriticalSection(); ontModel.leaveCriticalSection();
} }
} }
public void deleteDataPropertyStatementsForIndividualByDataProperty(Individual individual, DataProperty dataProperty) { public void deleteDataPropertyStatementsForIndividualByDataProperty(Individual individual, DataProperty dataProperty) {

View file

@ -24,6 +24,7 @@ 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.ontology.OntResource; import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.query.Dataset; import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QueryFactory;
@ -427,7 +428,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (sunrise != null) { if (sunrise != null) {
return sunrise; return sunrise;
} else { } else {
constructProperty(ind, VitroVocabulary.SUNRISE);
ind.getOntModel().enterCriticalSection(Lock.READ); ind.getOntModel().enterCriticalSection(Lock.READ);
try { try {
sunrise = webappDaoFactory.getJenaBaseDao() sunrise = webappDaoFactory.getJenaBaseDao()
@ -435,7 +436,6 @@ public class IndividualSDB extends IndividualImpl implements Individual {
ind,webappDaoFactory.getJenaBaseDao().SUNRISE); ind,webappDaoFactory.getJenaBaseDao().SUNRISE);
return sunrise; return sunrise;
} finally { } finally {
ind.getOntModel().leaveCriticalSection(); ind.getOntModel().leaveCriticalSection();
} }
} }
@ -445,7 +445,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (sunset != null) { if (sunset != null) {
return sunset; return sunset;
} else { } else {
constructProperty(ind, VitroVocabulary.SUNSET);
ind.getOntModel().enterCriticalSection(Lock.READ); ind.getOntModel().enterCriticalSection(Lock.READ);
try { try {
sunset = webappDaoFactory.getJenaBaseDao() sunset = webappDaoFactory.getJenaBaseDao()
@ -463,7 +463,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (timekey != null) { if (timekey != null) {
return timekey; return timekey;
} else { } else {
constructProperty(ind, VitroVocabulary.TIMEKEY);
ind.getOntModel().enterCriticalSection(Lock.READ); ind.getOntModel().enterCriticalSection(Lock.READ);
try { try {
timekey = webappDaoFactory.getJenaBaseDao() timekey = webappDaoFactory.getJenaBaseDao()
@ -554,7 +554,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (this.blurb != null) { if (this.blurb != null) {
return blurb; return blurb;
} else { } else {
constructProperty(ind, VitroVocabulary.BLURB);
ind.getOntModel().enterCriticalSection(Lock.READ); ind.getOntModel().enterCriticalSection(Lock.READ);
try { try {
blurb = webappDaoFactory.getJenaBaseDao().getPropertyStringValue(ind,webappDaoFactory.getJenaBaseDao().BLURB); blurb = webappDaoFactory.getJenaBaseDao().getPropertyStringValue(ind,webappDaoFactory.getJenaBaseDao().BLURB);
@ -570,7 +570,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (this.description != null) { if (this.description != null) {
return description; return description;
} else { } else {
constructProperty(ind, VitroVocabulary.DESCRIPTION);
ind.getOntModel().enterCriticalSection(Lock.READ); ind.getOntModel().enterCriticalSection(Lock.READ);
try { try {
description = webappDaoFactory.getJenaBaseDao() description = webappDaoFactory.getJenaBaseDao()
@ -584,6 +584,24 @@ public class IndividualSDB extends IndividualImpl implements Individual {
} }
} }
private synchronized void constructProperty(OntResource ind, String propertyURI) {
DatasetWrapper w = getDatasetWrapper();
Dataset dataset = w.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
try {
String queryStr = "CONSTRUCT { ?ind <" +
propertyURI + "> ?value } \n" +
"WHERE { GRAPH ?g { ?ind <" +
propertyURI + "> ?value } } \n";
Query query = QueryFactory.create(queryStr);
QueryExecution qe = QueryExecutionFactory.create(
query, dataset);
qe.execConstruct(ind.getModel());
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
}
}
public Float getSearchBoost(){ public Float getSearchBoost(){
if( this._searchBoostJena != null ){ if( this._searchBoostJena != null ){

View file

@ -142,7 +142,6 @@ public class JenaBaseDao extends JenaBaseDaoCon {
protected void addPropertyStringValue(Resource res, Property dataprop, String value, Model model) { protected void addPropertyStringValue(Resource res, Property dataprop, String value, Model model) {
if (res != null && dataprop != null && value != null && value.length()>0) { if (res != null && dataprop != null && value != null && value.length()>0) {
model.add(res, dataprop, value, XSDDatatype.XSDstring); model.add(res, dataprop, value, XSDDatatype.XSDstring);
System.out.println("JenaBaseDao" + value);
} }
} }

View file

@ -48,7 +48,7 @@ public class PropertyDaoJena extends JenaBaseDao implements PropertyDao {
"PREFIX owl: <http://www.w3.org/2002/07/owl#> \n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#> \n" +
"PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>"; "PREFIX afn: <http://jena.hpl.hp.com/ARQ/function#>";
private DatasetWrapperFactory dwf; protected DatasetWrapperFactory dwf;
public PropertyDaoJena(DatasetWrapperFactory dwf, public PropertyDaoJena(DatasetWrapperFactory dwf,
WebappDaoFactoryJena wadf) { WebappDaoFactoryJena wadf) {

View file

@ -118,6 +118,7 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
this.userURI = userURI; this.userURI = userURI;
this.flag2ValueMap = base.flag2ValueMap; this.flag2ValueMap = base.flag2ValueMap;
this.flag2ClassLabelMap = base.flag2ClassLabelMap; this.flag2ClassLabelMap = base.flag2ClassLabelMap;
this.dwf = base.dwf;
} }
public WebappDaoFactoryJena(OntModelSelector ontModelSelector, public WebappDaoFactoryJena(OntModelSelector ontModelSelector,

View file

@ -18,6 +18,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao; import edu.cornell.mannlib.vitro.webapp.dao.IndividualDao;
import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao; import edu.cornell.mannlib.vitro.webapp.dao.ObjectPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.VClassDao; import edu.cornell.mannlib.vitro.webapp.dao.VClassDao;
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
public class WebappDaoFactorySDB extends WebappDaoFactoryJena { public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
@ -63,6 +64,18 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
this.dwf = new ReconnectingDatasetFactory(bds, storeDesc); this.dwf = new ReconnectingDatasetFactory(bds, storeDesc);
} }
public WebappDaoFactorySDB(WebappDaoFactorySDB base, String userURI) {
super(base.ontModelSelector);
this.ontModelSelector = base.ontModelSelector;
this.defaultNamespace = base.defaultNamespace;
this.nonuserNamespaces = base.nonuserNamespaces;
this.preferredLanguages = base.preferredLanguages;
this.userURI = userURI;
this.flag2ValueMap = base.flag2ValueMap;
this.flag2ClassLabelMap = base.flag2ClassLabelMap;
this.dwf = base.dwf;
}
@Override @Override
public IndividualDao getIndividualDao() { public IndividualDao getIndividualDao() {
if (entityWebappDao != null) if (entityWebappDao != null)
@ -95,6 +108,11 @@ public class WebappDaoFactorySDB extends WebappDaoFactoryJena {
return vClassDao = new VClassDaoSDB(dwf, this); return vClassDao = new VClassDaoSDB(dwf, this);
} }
public WebappDaoFactory getUserAwareDaoFactory(String userURI) {
// TODO: put the user-aware factories in a hashmap so we don't keep re-creating them
return new WebappDaoFactorySDB(this, userURI);
}
private class ReconnectingDatasetFactory implements DatasetWrapperFactory { private class ReconnectingDatasetFactory implements DatasetWrapperFactory {
private BasicDataSource _bds; private BasicDataSource _bds;