Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VIVO 3828: Remove SDB in preparation to moving to new Jena. #374

Open
wants to merge 4 commits into
base: jena4-upgrade
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public int compare (DataProperty dp1, DataProperty dp2) {
}

public DataPropertyDaoJena(RDFService rdfService,
DatasetWrapperFactory dwf,
DatasetWrapper dw,
WebappDaoFactoryJena wadf) {
super(rdfService, dwf, wadf);
super(rdfService, dw, wadf);
}

public void deleteDataProperty(DataProperty dtp) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/* $This file is distributed under the terms of the license in LICENSE$ */

package edu.cornell.mannlib.vitro.webapp.dao.jena;

import java.util.ArrayList;
import java.util.List;

import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.Dataset;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.shared.Lock;
import org.apache.jena.vocabulary.RDF;

import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatement;
import edu.cornell.mannlib.vitro.webapp.beans.DataPropertyStatementImpl;
import edu.cornell.mannlib.vitro.webapp.beans.Individual;
import edu.cornell.mannlib.vitro.webapp.dao.DataPropertyStatementDao;
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;

/**
* An extension of {@link DataPropertyStatementDaoJena} for databases, such as TDB.
*/
public class DataPropertyStatementDaoDB extends DataPropertyStatementDaoJena implements DataPropertyStatementDao {

/**
* Initialize the data property statement DAO.
*
* @param dw The data wrapper.
* @param wadf The web application DAO factory.
*/
public DataPropertyStatementDaoDB(DatasetWrapper dw, WebappDaoFactoryDB wadf) {
super (dw, wadf);
}

/**
* Fill existing data property statements for an individual.
*
* @param entity The individual.
*
* @return A filled out individual.
*/
public Individual fillExistingDataPropertyStatementsForIndividual(Individual entity) {
if (entity.getURI() == null) {
return entity;
}

String query =
"CONSTRUCT { \n" +
" <" + entity.getURI() + "> ?p ?o . \n" +
"} WHERE { \n" +
" <" + entity.getURI() + "> ?p ?o . \n" +
" FILTER(isLiteral(?o)) \n" +
"}" ;
Model results = null;
Dataset dataset = getDataWrapper().getDataset();
QueryExecution qexec = null;

dataset.getLock().enterCriticalSection(Lock.READ);
try {
qexec = QueryExecutionFactory.create(QueryFactory.create(query), dataset);
results = qexec.execConstruct();
} finally {
if (qexec != null) qexec.close();
dataset.getLock().leaveCriticalSection();
getDataWrapper().close();
}

OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM, results);
ontModel.enterCriticalSection(Lock.READ);
try {
Resource ind = ontModel.getResource(entity.getURI());
List<DataPropertyStatement> edList = new ArrayList<>();
StmtIterator stmtIt = ind.listProperties();

while (stmtIt.hasNext()) {
Statement st = stmtIt.next();
boolean addToList = st.getObject().isLiteral() &&
(
(
RDF.value.equals(st.getPredicate())
|| VitroVocabulary.value.equals(st.getPredicate().getURI()))
|| MONIKER.equals(st.getPredicate())
|| !(NONUSER_NAMESPACES.contains(st.getPredicate().getNameSpace())
)
);

if (addToList) {
DataPropertyStatement ed = new DataPropertyStatementImpl();
Literal lit = (Literal)st.getObject();
fillDataPropertyStatementWithJenaLiteral(ed,lit);
ed.setDatapropURI(st.getPredicate().getURI());
ed.setIndividualURI(ind.getURI());
ed.setIndividual(entity);
edList.add(ed);
}
}

entity.setDataPropertyStatements(edList);
return entity;
} finally {
ontModel.leaveCriticalSection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,22 @@ public class DataPropertyStatementDaoJena extends JenaBaseDao implements DataPro
{
private static final Log log = LogFactory.getLog(DataPropertyStatementDaoJena.class);

private DatasetWrapper dw;

private DatasetWrapperFactory dwf;

public DataPropertyStatementDaoJena(DatasetWrapperFactory dwf,
public DataPropertyStatementDaoJena(DatasetWrapper dw,
WebappDaoFactoryJena wadf) {
super(wadf);
this.dwf = dwf;
this.dw = dw;
}

/**
* Get the data wrapper.
*
* @return The data wrapper.
*/
public DatasetWrapper getDataWrapper() {
return dw;
}

public void deleteDataPropertyStatement( DataPropertyStatement dataPropertyStatement )
{
Expand Down Expand Up @@ -366,13 +373,11 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(String subject

// Run the SPARQL query to get the properties
List<Literal> values = new ArrayList<Literal>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qexec = null;
try {
qexec = QueryExecutionFactory.create(
queryString, dataset);
qexec = QueryExecutionFactory.create(queryString, dataset);
ResultSet results = qexec.execSelect();

while (results.hasNext()) {
Expand All @@ -388,7 +393,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(String subject

} finally {
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -430,8 +435,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(

// Run the SPARQL query to get the properties
List<Literal> values = new ArrayList<Literal>();
DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qexec = null;
try {
Expand Down Expand Up @@ -459,7 +463,7 @@ public List<Literal> getDataPropertyValuesForIndividualByProperty(
return Collections.emptyList();
} finally {
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
if (qexec != null) {
qexec.close();
}
Expand Down Expand Up @@ -497,8 +501,7 @@ private Model constructModelForSelectQueries(String subjectUri,
initialBindings.add(
"property", ResourceFactory.createResource(propertyUri));

DatasetWrapper w = dwf.getDatasetWrapper();
Dataset dataset = w.getDataset();
Dataset dataset = dw.getDataset();
dataset.getLock().enterCriticalSection(Lock.READ);
QueryExecution qe = null;
try {
Expand All @@ -512,7 +515,7 @@ private Model constructModelForSelectQueries(String subjectUri,
qe.close();
}
dataset.getLock().leaveCriticalSection();
w.close();
dw.close();
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena;

/**
* Modes for optimizing database queries.
*/
public enum DatasetMode {

/**
* Only perform assertions.
*/
ASSERTIONS_ONLY,

/**
* Only perform inferences.
*/
INFERENCES_ONLY,

/**
* Perform both assertions and inferences.
*/
ASSERTIONS_AND_INFERENCES,
}
Loading