various and sundry small DAO improvements

This commit is contained in:
brianjlowe 2012-03-08 20:54:29 +00:00
parent 5fea873ed2
commit d7da587aa7
17 changed files with 130 additions and 116 deletions

View file

@ -441,14 +441,14 @@ public class OperationController extends BaseEditController {
} catch (InvocationTargetException e) {
log.error(this.getClass().getName()+" encountered exception performing two-stage update");
Throwable innerE = e.getTargetException();
log.error(innerE);
log.error(innerE, innerE);
if (innerE.getMessage()!=null) {
log.error(innerE.getMessage());
//log.error(innerE.getMessage());
epo.setAttribute("globalErrorMsg",innerE.getMessage());
}
return FAILURE;
} catch (IllegalAccessException iae) {
log.error(iae);
log.error(iae, iae);
epo.setAttribute("globalErrorMessage", "Illegal access - see error logs.");
return FAILURE;
}
@ -459,15 +459,15 @@ public class OperationController extends BaseEditController {
log.error(this.getClass().getName()+" encountered exception performing edit action");
Throwable innerE = e.getTargetException();
//innerE.printStackTrace();
log.error(innerE);
log.error(innerE, innerE);
if (innerE.getMessage()!=null) {
//System.out.println(innerE.getMessage());
log.error(innerE.getMessage());
//log.error(innerE.getMessage());
epo.setAttribute("globalErrorMsg",innerE.getMessage());
}
return FAILURE;
} catch (IllegalAccessException iae) {
log.error(iae);
log.error(iae, iae);
epo.setAttribute("globalErrorMessage", "Illegal access - see error logs.");
return FAILURE;
}

View file

@ -7,10 +7,6 @@ public interface ObjectPropertyStatement {
public String toString();
public boolean isSubjectOriented();
public void setSubjectOriented(boolean subjectOriented);
public String getSubjectURI();
public void setSubjectURI(String subjectURI);

View file

@ -18,9 +18,7 @@ public class ObjectPropertyStatementImpl implements ObjectPropertyStatement
private String propertyURI = null;
private ObjectProperty property = null;
private String qualifier = null;
private boolean subjectOriented = true; //is the range the item of interest?
private String description = null; //generated desc based on subjectOriented during sql query.
private boolean subjectOriented = true; //is the range the item of interest?
public ObjectPropertyStatementImpl() { }
@ -37,23 +35,7 @@ public class ObjectPropertyStatementImpl implements ObjectPropertyStatement
String prop = (getProperty()!=null)?getProperty().getDomainPublic():"by propURI"+getPropertyURI();
String ran = (getObject()!= null)?getObject().getName():"objectURI:"+getObjectURI();
String dom = (getSubject()!= null)?getSubject().getName():"subjectURI:"+getSubjectURI();
String orent = (isSubjectOriented() )?"subject oriented":"object oriented";
return "Object Property Statements: "+dom+" "+prop+" to "+ran+" "+orent;
}
/* (non-Javadoc)
* @see edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement#isSubjectOriented()
*/
public boolean isSubjectOriented() {
return subjectOriented;
}
/* (non-Javadoc)
* @see edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement#setSubjectOriented(boolean)
*/
public void setSubjectOriented(boolean subjectOriented) {
this.subjectOriented = subjectOriented;
return "Object Property Statements: "+dom+" "+prop+" to "+ran+" ";
}
/* (non-Javadoc)
@ -99,7 +81,12 @@ public class ObjectPropertyStatementImpl implements ObjectPropertyStatement
* @see edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement#setSubject(edu.cornell.mannlib.vitro.webapp.beans.Individual)
*/
public void setSubject(Individual subject) {
this.subject = subject;
this.subject = subject;
if (subject == null || subject.isAnonymous()) {
setSubjectURI(null);
} else {
setSubjectURI(subject.getURI());
}
}
@ -135,7 +122,12 @@ public class ObjectPropertyStatementImpl implements ObjectPropertyStatement
* @see edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement#setObject(edu.cornell.mannlib.vitro.webapp.beans.Individual)
*/
public void setObject(Individual object) {
this.object = object;
this.object = object;
if (object == null || object.isAnonymous()) {
setObjectURI(null);
} else {
setObjectURI(object.getURI());
}
}

View file

@ -133,8 +133,14 @@ public class VClass extends BaseResourceBean implements Comparable<VClass>
* Sorts alphabetically by name
*/
public int compareTo (VClass o1) {
Collator collator = Collator.getInstance();
return collator.compare(this.getName(),o1.getName());
if (this.getName() == null) {
return 1;
} else if (o1.getName() == null) {
return -1;
} else {
Collator collator = Collator.getInstance();
return collator.compare(this.getName(),o1.getName());
}
}
/**

View file

@ -39,7 +39,7 @@ public interface WebappDaoFactory {
public Set<String> getNonuserNamespaces();
public String[] getPreferredLanguages();
public List<String> getPreferredLanguages();
/**
* BJL23 2008-05-20: Putting this here for lack of a more logical place.

View file

@ -2,30 +2,29 @@
package edu.cornell.mannlib.vitro.webapp.dao;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class WebappDaoFactoryConfig {
private String[] preferredLanguages;
private List<String> preferredLanguages;
private String defaultNamespace;
private Set<String> nonUserNamespaces;
public WebappDaoFactoryConfig() {
preferredLanguages = new String[3];
preferredLanguages[0] = "en-US";
preferredLanguages[1] = "en";
preferredLanguages[2] = "EN";
preferredLanguages = Arrays.asList("en-US", "en", "EN");
defaultNamespace = "http://vitro.mannlib.cornell.edu/ns/default#";
nonUserNamespaces = new HashSet<String>();
nonUserNamespaces.add(VitroVocabulary.vitroURI);
}
public String[] getPreferredLanguages() {
public List<String> getPreferredLanguages() {
return this.preferredLanguages;
}
public void setPreferredLanguages(String[] pl) {
public void setPreferredLanguages(List<String> pl) {
this.preferredLanguages = pl;
}

View file

@ -56,10 +56,6 @@ public class ObjectPropertyStatementFiltering implements ObjectPropertyStatement
return innerStmt.getSubjectURI();
}
public boolean isSubjectOriented() {
return innerStmt.isSubjectOriented();
}
public void setObject(Individual object) {
innerStmt.setObject(object);
}
@ -80,10 +76,6 @@ public class ObjectPropertyStatementFiltering implements ObjectPropertyStatement
innerStmt.setSubject(subject);
}
public void setSubjectOriented(boolean subjectOriented) {
innerStmt.setSubjectOriented(subjectOriented);
}
public void setSubjectURI(String subjectURI) {
innerStmt.setSubjectURI(subjectURI);
}

View file

@ -101,7 +101,7 @@ public class WebappDaoFactoryFiltering implements WebappDaoFactory {
return innerWebappDaoFactory.getNonuserNamespaces();
}
public String[] getPreferredLanguages() {
public List<String> getPreferredLanguages() {
return innerWebappDaoFactory.getPreferredLanguages();
}

View file

@ -187,9 +187,10 @@ public class IndividualDaoSDB extends IndividualDaoJena {
continue;
}
if (uri != null && !uri.equals(currRes.getURI())) {
Individual ent = makeIndividual(uri, label);
if (ent != null) {
ents.add(ent);
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
uri = currRes.getURI();
label = null;
@ -201,9 +202,10 @@ public class IndividualDaoSDB extends IndividualDaoJena {
label = labelLit.getLexicalForm();
}
if (!rs.hasNext()) {
Individual ent = makeIndividual(uri, label);
if (ent != null) {
ents.add(ent);
try {
ents.add(makeIndividual(uri, label));
} catch (IndividualNotFoundException e) {
// don't add
}
}
}
@ -237,8 +239,12 @@ public class IndividualDaoSDB extends IndividualDaoJena {
if (currRes.isAnon()) {
continue;
}
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null));
try {
filteredIndividualList.add(
makeIndividual(currRes.getURI(), null));
} catch (IndividualNotFoundException e) {
// don't add
}
}
} finally {
dataset.getLock().leaveCriticalSection();
@ -247,7 +253,7 @@ public class IndividualDaoSDB extends IndividualDaoJena {
return filteredIndividualList;
}
private Individual makeIndividual(String uri, String label) {
private Individual makeIndividual(String uri, String label) throws IndividualNotFoundException {
Individual ent = new IndividualSDB(uri,
this.dwf, datasetMode, getWebappDaoFactory(),
SKIP_INITIALIZATION);

View file

@ -109,7 +109,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
DatasetWrapperFactory datasetWrapperFactory,
SDBDatasetMode datasetMode,
WebappDaoFactoryJena wadf,
boolean skipInitialization) {
boolean skipInitialization) throws IndividualNotFoundException {
this.individualURI = individualURI;
this.datasetMode = datasetMode;
this.dwf = datasetWrapperFactory;
@ -176,7 +176,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
public IndividualSDB(String individualURI,
DatasetWrapperFactory datasetWrapperFactory,
SDBDatasetMode datasetMode,
WebappDaoFactoryJena wadf) {
WebappDaoFactoryJena wadf) throws IndividualNotFoundException {
this(individualURI,
datasetWrapperFactory,
datasetMode,
@ -184,7 +184,7 @@ public class IndividualSDB extends IndividualImpl implements Individual {
!SKIP_INITIALIZATION);
}
public class IndividualNotFoundException extends RuntimeException {}
public class IndividualNotFoundException extends Exception {}
private void setUpURIParts(OntResource ind) {
if (ind != null) {
@ -464,8 +464,24 @@ public class IndividualSDB extends IndividualImpl implements Individual {
if (!s.getSubject().canAs(OntResource.class) || !s.getObject().canAs(OntResource.class)) {
continue;
}
Individual subj = new IndividualSDB(((OntResource) s.getSubject().as(OntResource.class)).getURI(), this.dwf, datasetMode, webappDaoFactory);
Individual obj = new IndividualSDB(((OntResource) s.getObject().as(OntResource.class)).getURI(), this.dwf, datasetMode, webappDaoFactory);
Individual subj = null;
try {
subj = new IndividualSDB(
((OntResource) s.getSubject().as(OntResource.class))
.getURI(),
this.dwf, datasetMode, webappDaoFactory);
} catch (IndividualNotFoundException e) {
// leave null subject
}
Individual obj = null;
try {
obj = new IndividualSDB(
((OntResource) s.getObject().as(OntResource.class))
.getURI(),
this.dwf, datasetMode, webappDaoFactory);
} catch (IndividualNotFoundException e) {
// leave null object
}
ObjectProperty op = webappDaoFactory.getObjectPropertyDao().getObjectPropertyByURI(s.getPredicate().getURI());
// We don't want to filter out statements simply because we
// can't find a type for the property, so we'll just make a
@ -517,15 +533,19 @@ public class IndividualSDB extends IndividualImpl implements Individual {
while (values.hasNext()) {
result = values.next();
RDFNode value = result.get("object");
if (value.canAs(OntResource.class)) {
relatedIndividuals.add(
new IndividualSDB(
((OntResource) value.as(OntResource.class))
.getURI(),
this.dwf,
datasetMode,
webappDaoFactory) );
}
try {
if (value.canAs(OntResource.class)) {
relatedIndividuals.add(
new IndividualSDB(
((OntResource) value.as(OntResource.class))
.getURI(),
this.dwf,
datasetMode,
webappDaoFactory) );
}
} catch (IndividualNotFoundException e) {
// don't add to the list
}
}
} finally {
dataset.getLock().leaveCriticalSection();
@ -555,9 +575,13 @@ public class IndividualSDB extends IndividualImpl implements Individual {
QuerySolution result = results.next();
RDFNode value = result.get("object");
if (value != null && value.canAs(OntResource.class)) {
return new IndividualSDB(
((OntResource) value.as(OntResource.class)).getURI(),
dwf, datasetMode, webappDaoFactory);
try {
return new IndividualSDB(
((OntResource) value.as(OntResource.class)).getURI(),
dwf, datasetMode, webappDaoFactory);
} catch (IndividualNotFoundException e) {
return null;
}
}
}
return null;

View file

@ -80,7 +80,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
protected String DEFAULT_NAMESPACE;
protected Set<String> NONUSER_NAMESPACES;
protected String[] PREFERRED_LANGUAGES;
protected List<String> PREFERRED_LANGUAGES;
/* ******************* constructor ************************* */
@ -501,7 +501,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
}
} catch (Exception e) {
log.error("Error in updatePropertyDateValue");
log.error("Error in updatePropertyDateValue", e);
}
}
@ -552,8 +552,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
}
} catch (Exception e) {
log.error("Error in updatePropertyDateTimeValue");
log.error(e, e);
log.error("Error in updatePropertyDateTimeValue", e);
}
}
@ -767,7 +766,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
if (label.isLiteral()) {
Literal labelLit = ((Literal)label);
String labelLanguage = labelLit.getLanguage();
if ( (labelLanguage==null) && (lang==null) ) {
if ( (labelLanguage == null) && (lang == null || lang.isEmpty()) ) {
return labelLit;
}
if ( (lang != null) && (lang.equals(labelLanguage)) ) {
@ -856,6 +855,10 @@ public class JenaBaseDao extends JenaBaseDaoCon {
private Literal tryPropertyForPreferredLanguages( OntResource r, Property p, boolean alsoTryNoLang ) {
Literal label = null;
List<RDFNode> labels = r.listPropertyValues(p).toList();
if (labels.size() == 0) {
return null;
}
// Sort by lexical value to guarantee consistent results
Collections.sort(labels, new Comparator<RDFNode>() {
@ -871,8 +874,7 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
});
for (int i=0; i<PREFERRED_LANGUAGES.length; i++) {
String lang = PREFERRED_LANGUAGES[i];
for (String lang : PREFERRED_LANGUAGES) {
label = getLabel(lang,labels);
if (label != null) {
break;
@ -880,12 +882,21 @@ public class JenaBaseDao extends JenaBaseDaoCon {
}
if ( label == null && alsoTryNoLang ) {
label = getLabel("", labels);
// accept any label as a last resort
if (label == null) {
for (RDFNode labelNode : labels) {
if (labelNode instanceof Literal) {
label = ((Literal) labelNode);
break;
}
}
}
}
return label;
}
protected String getDefaultLanguage() {
return PREFERRED_LANGUAGES[0];
return PREFERRED_LANGUAGES.get(0);
}
/**

View file

@ -132,7 +132,7 @@ public class OntologyDaoJena extends JenaBaseDao implements OntologyDao {
try {
com.hp.hpl.jena.ontology.Ontology o = ontModel.createOntology(adjustOntologyURI(ontology.getURI()));
if (ontology.getName() != null && ontology.getName().length()>0) {
o.setLabel(ontology.getName(), PREFERRED_LANGUAGES[0]);
o.setLabel(ontology.getName(), getDefaultLanguage());
}
if (ontology.getPrefix() != null && ontology.getPrefix().length()>0) {
addPropertyStringValue(o,ONTOLOGY_PREFIX_ANNOT,ontology.getPrefix(),ontModel);

View file

@ -9,6 +9,9 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.ontology.AllValuesFromRestriction;
import com.hp.hpl.jena.ontology.AnnotationProperty;
import com.hp.hpl.jena.ontology.CardinalityRestriction;
@ -59,6 +62,8 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.pellet.PelletListener;
public class VClassDaoJena extends JenaBaseDao implements VClassDao {
protected static final Log log = LogFactory.getLog(VClassDaoJena.class);
public VClassDaoJena(WebappDaoFactoryJena wadf) {
super(wadf);
}
@ -446,11 +451,12 @@ public class VClassDaoJena extends JenaBaseDao implements VClassDao {
List<VClass> classes = new ArrayList<VClass>();
getOntModel().enterCriticalSection(Lock.READ);
try {
ClosableIterator<OntClass> classIt = getOntModel().listClasses();
ClosableIterator<Individual> classIt = getOntModel().listIndividuals(OWL.Class);
try {
while (classIt.hasNext()) {
try {
OntClass cls = classIt.next();
Individual classInd = classIt.next();
OntClass cls = (OntClass) classInd.as(OntClass.class);
if (!cls.isAnon() && !(NONUSER_NAMESPACES.contains(cls.getNameSpace()))) {
classes.add(new VClassJena(cls,getWebappDaoFactory()));
}
@ -481,11 +487,15 @@ public class VClassDaoJena extends JenaBaseDao implements VClassDao {
*/
private Iterator<OntClass> smarterListHierarchyRootClasses(OntModel ontModel, String ontologyURI) {
List<OntClass> rootClassList = new ArrayList<OntClass>();
ClosableIterator ci = ontModel.listClasses();
ClosableIterator<Individual> ci = ontModel.listIndividuals(OWL.Class);
try {
for (ClosableIterator i = ci ; i.hasNext(); ) {
for (ClosableIterator<Individual> i = ci ; i.hasNext(); ) {
try {
OntClass ontClass = (OntClass) i.next();
Individual classInd = i.next();
// if (!classInd.canAs(OntClass.class)) {
// continue;
// }
OntClass ontClass = (OntClass) classInd.as(OntClass.class);
boolean isRoot = true;
for (Iterator<RDFNode> j = ontClass.listPropertyValues(RDFS.subClassOf); j.hasNext(); ) {
Resource res = (Resource) j.next();
@ -510,7 +520,7 @@ public class VClassDaoJena extends JenaBaseDao implements VClassDao {
rootClassList.add(ontClass);
}
} catch (ClassCastException cce) {
log.error(cce);
log.error(cce, cce);
}
}
} finally {

View file

@ -233,7 +233,7 @@ public class WebappDaoFactoryJena implements WebappDaoFactory {
return config.getDefaultNamespace();
}
public String[] getPreferredLanguages() {
public List<String> getPreferredLanguages() {
return config.getPreferredLanguages();
}

View file

@ -638,18 +638,6 @@ public class IndividualFilteringByStatementTest extends AbstractTestClass {
// Un-implemented methods
// ----------------------------------------------------------------------
@Override
public boolean isSubjectOriented() {
throw new RuntimeException(
"ObjectPropertyStatement.isSubjectOriented() not implemented.");
}
@Override
public void setSubjectOriented(boolean subjectOriented) {
throw new RuntimeException(
"ObjectPropertyStatement.setSubjectOriented() not implemented.");
}
@Override
public void setSubjectURI(String subjectURI) {
throw new RuntimeException(

View file

@ -55,16 +55,6 @@ public class ObjectPropertyStatementDaoStub implements
.getObjectURI());
}
@Override
public boolean isSubjectOriented() {
return false;
}
@Override
public void setSubjectOriented(boolean subjectOriented) {
throw new UnsupportedOperationException();
}
@Override
public String getSubjectURI() {
return s;

View file

@ -169,7 +169,7 @@ return this.objectPropertyStatementDao; }
}
@Override
public String[] getPreferredLanguages() {
public List<String> getPreferredLanguages() {
throw new RuntimeException(
"WebappDaoFactory.getPreferredLanguages() not implemented.");
}