Merge commit '69a45e573a
' into dev-isf
Conflicts: productMods/WEB-INF/filegraph/tbox/geopolitical.tbox.ver1.1-11-18-11.owl productMods/WEB-INF/filegraph/tbox/isDefinedBy-1.5.owl productMods/WEB-INF/filegraph/tbox/scires-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-bibo-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-c4o-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-core-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-dcelements-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-dcterms-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-event-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-fabio-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-foaf-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-pws-1.5.owl productMods/WEB-INF/filegraph/tbox/vivo-skos-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/geopolitical.tbox.ver1.1-11-18-11.owl productMods/WEB-INF/ontologies/update/oldVersion/isDefinedBy-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/scires-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-bibo-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-c4o-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-core-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-dcelements-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-dcterms-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-event-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-fabio-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-foaf-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-pws-1.5.owl productMods/WEB-INF/ontologies/update/oldVersion/vivo-skos-1.5.owl productMods/templates/freemarker/body/partials/individual/individual-positions.ftl rdf/abox/filegraph/us-states.rdf rdf/tbox/filegraph/geopolitical.tbox.ver1.1-11-18-11.owl rdf/tbox/filegraph/isDefinedBy-1.5.owl rdf/tbox/filegraph/scires-1.5.owl rdf/tbox/filegraph/vivo-bibo-1.5.owl rdf/tbox/filegraph/vivo-c4o-1.5.owl rdf/tbox/filegraph/vivo-core-1.5.owl rdf/tbox/filegraph/vivo-dcelements-1.5.owl rdf/tbox/filegraph/vivo-dcterms-1.5.owl rdf/tbox/filegraph/vivo-event-1.5.owl rdf/tbox/filegraph/vivo-fabio-1.5.owl rdf/tbox/filegraph/vivo-foaf-1.5.owl rdf/tbox/filegraph/vivo-pws-1.5.owl rdf/tbox/filegraph/vivo-skos-1.5.owl
This commit is contained in:
commit
5c14c1355d
135 changed files with 3010 additions and 607 deletions
28
src/edu/cornell/mannlib/semservices/bo/BaseObject.java
Normal file
28
src/edu/cornell/mannlib/semservices/bo/BaseObject.java
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
package edu.cornell.mannlib.semservices.bo;
|
||||
|
||||
public class BaseObject {
|
||||
/**
|
||||
* Simple JavaBean domain object with an id property.
|
||||
* Used as a base class for objects needing this property.
|
||||
*
|
||||
* @author Ken Krebs
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
private Integer id;
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public boolean isNew() {
|
||||
return (this.id == null);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -2,6 +2,9 @@
|
|||
|
||||
package edu.cornell.mannlib.semservices.bo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Concept {
|
||||
|
||||
private String definedBy;
|
||||
|
@ -11,13 +14,23 @@ public class Concept {
|
|||
private String type;
|
||||
private String definition;
|
||||
private String uri;
|
||||
|
||||
private String schemeURI;
|
||||
private List<String> broaderURIList;
|
||||
private List<String> narrowerURIList;
|
||||
private List<String> exactMatchURIList;
|
||||
private List<String> closeMatchURIList;
|
||||
private List<String> altLabelList;
|
||||
|
||||
/**
|
||||
* default constructor
|
||||
*/
|
||||
public Concept() {
|
||||
|
||||
this.broaderURIList = new ArrayList<String>();
|
||||
this.narrowerURIList = new ArrayList<String>();
|
||||
this.exactMatchURIList = new ArrayList<String>();
|
||||
this.closeMatchURIList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the conceptId
|
||||
*/
|
||||
|
@ -90,6 +103,18 @@ public class Concept {
|
|||
public void setDefinedBy(String definedBy) {
|
||||
this.definedBy = definedBy;
|
||||
}
|
||||
/**
|
||||
* @return the schemeURI
|
||||
*/
|
||||
public String getSchemeURI() {
|
||||
return schemeURI;
|
||||
}
|
||||
/**
|
||||
* @param schemeURI the schemeURI to set
|
||||
*/
|
||||
public void setSchemeURI(String schemeURI) {
|
||||
this.schemeURI = schemeURI;
|
||||
}
|
||||
/**
|
||||
* @return the bestMatch
|
||||
*/
|
||||
|
@ -102,5 +127,37 @@ public class Concept {
|
|||
public void setBestMatch(String bestMatch) {
|
||||
this.bestMatch = bestMatch;
|
||||
}
|
||||
public List<String> getBroaderURIList() {
|
||||
return broaderURIList;
|
||||
}
|
||||
public void setBroaderURIList(List<String> broaderURIList) {
|
||||
this.broaderURIList = broaderURIList;
|
||||
}
|
||||
public List<String> getNarrowerURIList() {
|
||||
return narrowerURIList;
|
||||
}
|
||||
public void setNarrowerURIList(List<String> narrowerURIList) {
|
||||
this.narrowerURIList = narrowerURIList;
|
||||
}
|
||||
public List<String> getExactMatchURIList() {
|
||||
return exactMatchURIList;
|
||||
}
|
||||
public void setExactMatchURIList(List<String> exactMatchURIList) {
|
||||
this.exactMatchURIList = exactMatchURIList;
|
||||
}
|
||||
public List<String> getCloseMatchURIList() {
|
||||
return closeMatchURIList;
|
||||
}
|
||||
public void setCloseMatchURIList(List<String> closeMatchURIList) {
|
||||
this.closeMatchURIList = closeMatchURIList;
|
||||
}
|
||||
|
||||
public List<String> getAltLabelList() {
|
||||
return altLabelList;
|
||||
}
|
||||
|
||||
public void setAltLabelList(List<String> altLabelList) {
|
||||
this.altLabelList = altLabelList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,8 +8,7 @@ import java.util.List;
|
|||
|
||||
public class ConceptInfo extends SemanticServicesInfoBase {
|
||||
|
||||
private List<Concept> conceptList;
|
||||
|
||||
private List<?> conceptList;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -18,17 +17,16 @@ public class ConceptInfo extends SemanticServicesInfoBase {
|
|||
}
|
||||
|
||||
/**
|
||||
* @return the vivoDepartmentList
|
||||
* @return the conceptList
|
||||
*/
|
||||
public List<Concept> getConceptList() {
|
||||
public List<?> getConceptList() {
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param vivoDepartmentList the vivoDepartmentList to set
|
||||
* @param conceptList the conceptList to set
|
||||
*/
|
||||
public void setConceptList(List<Concept> inputConceptList) {
|
||||
this.conceptList = inputConceptList;
|
||||
public void setConceptList(List<?> conceptList) {
|
||||
this.conceptList = conceptList;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.semservices.exceptions;
|
||||
|
||||
public class ConceptsNotFoundException extends Exception {
|
||||
/**
|
||||
* An exception that indicates a service could not find a Concept
|
||||
*/
|
||||
private static final long serialVersionUID = -4729465393290022840L;
|
||||
public ConceptsNotFoundException() { }
|
||||
public ConceptsNotFoundException(String message) { super(message); }
|
||||
public ConceptsNotFoundException(Throwable cause) { super(cause); }
|
||||
public ConceptsNotFoundException(String message, Throwable cause) { super(message, cause); }
|
||||
|
||||
}
|
|
@ -7,7 +7,24 @@ import java.util.List;
|
|||
import edu.cornell.mannlib.semservices.bo.Concept;
|
||||
|
||||
public interface ExternalConceptService {
|
||||
// this is the only method that needs to be exposed
|
||||
List<Concept> processResults(String term) throws Exception;
|
||||
|
||||
/**
|
||||
* @param term
|
||||
* @return
|
||||
*/
|
||||
List<Concept> processResults(String term) throws Exception;
|
||||
|
||||
/**
|
||||
* @param term
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
List<Concept> getConcepts(String term) throws Exception;
|
||||
|
||||
/**
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
List<Concept> getConceptsByURIWithSparql(String uri) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -8,18 +8,30 @@ import java.io.IOException;
|
|||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URL;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.rpc.ServiceException;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.fao.gilw.aims.webservices.AgrovocWS;
|
||||
import org.fao.gilw.aims.webservices.AgrovocWSServiceLocator;
|
||||
import org.fao.www.webservices.AgrovocWS.ACSWWebService;
|
||||
import org.fao.www.webservices.AgrovocWS.ACSWWebServiceServiceLocator;
|
||||
import org.semanticweb.skos.SKOSAnnotation;
|
||||
import org.semanticweb.skos.SKOSConcept;
|
||||
import org.semanticweb.skos.SKOSDataset;
|
||||
import org.semanticweb.skos.SKOSEntity;
|
||||
import org.semanticweb.skos.SKOSLiteral;
|
||||
import org.semanticweb.skos.SKOSUntypedLiteral;
|
||||
import org.semanticweb.skosapibinding.SKOSManager;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
|
@ -27,95 +39,215 @@ import org.w3c.dom.Node;
|
|||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import com.hp.hpl.jena.query.Query;
|
||||
import com.hp.hpl.jena.query.QueryExecution;
|
||||
import com.hp.hpl.jena.query.QueryExecutionFactory;
|
||||
import com.hp.hpl.jena.query.QueryFactory;
|
||||
import com.hp.hpl.jena.query.QuerySolution;
|
||||
import com.hp.hpl.jena.query.ResultSet;
|
||||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
|
||||
import edu.cornell.mannlib.semservices.bo.Concept;
|
||||
import edu.cornell.mannlib.semservices.service.ExternalConceptService;
|
||||
import edu.cornell.mannlib.semservices.util.XMLUtils;
|
||||
|
||||
public class AgrovocService implements ExternalConceptService {
|
||||
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
private java.lang.String AgrovocWS_address = "http://www.fao.org/webservices/AgrovocWS";
|
||||
private java.lang.String AgrovocWS_address = "http://agrovoc.fao.org/axis/services/SKOSWS";
|
||||
private final String schemeUri = "http://aims.fao.org/aos/agrovoc/agrovocScheme";
|
||||
private final String baseUri = "http://aims.fao.org/aos/agrovoc/";
|
||||
private final String ontologyName = "agrovoc";
|
||||
private final String format = "SKOS";
|
||||
private final String lang = "en";
|
||||
private final String codeName = "hasCodeAgrovoc";
|
||||
private final String searchMode = "Exact Match";
|
||||
protected final String dbpedia_endpoint = " http://dbpedia.org/sparql";
|
||||
|
||||
|
||||
@Override
|
||||
public List<Concept> getConcepts(String term) throws Exception {
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
//System.out.println("Searching for term: "+ term);
|
||||
String result = getURIByTermAndLangXML(this.ontologyName, term, this.searchMode, this.format, this.lang);
|
||||
// return empty conceptList if conceptUri is empty
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
// First create a new SKOSManager
|
||||
String conceptUri = getConceptURIFromRDF(result);
|
||||
SKOSManager manager = new SKOSManager();
|
||||
|
||||
// return empty conceptList if conceptUri is empty
|
||||
if (StringUtils.isEmpty(conceptUri)) {
|
||||
return conceptList;
|
||||
}
|
||||
URI uri = null;
|
||||
try {
|
||||
uri = new URI(conceptUri);
|
||||
} catch (URISyntaxException e) {
|
||||
e.printStackTrace();
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
//System.out.println("uri: "+uri);
|
||||
SKOSDataset dataset = manager.loadDataset(uri);
|
||||
|
||||
for (SKOSConcept skosConcept : dataset.getSKOSConcepts()) {
|
||||
Concept concept = new Concept();
|
||||
//System.out.println("Concept: " + skosConcept.getURI());
|
||||
concept.setUri(skosConcept.getURI().toString());
|
||||
concept.setConceptId(stripConceptId(skosConcept.getURI().toString()));
|
||||
concept.setBestMatch("true");
|
||||
concept.setDefinedBy(schemeUri);
|
||||
concept.setSchemeURI(this.schemeUri);
|
||||
concept.setType("");
|
||||
String lang = "";
|
||||
|
||||
for (SKOSLiteral literal : skosConcept.getSKOSRelatedConstantByProperty(dataset, manager.getSKOSDataFactory().getSKOSPrefLabelProperty())) {
|
||||
|
||||
if (!literal.isTyped()) {
|
||||
// if it has language
|
||||
SKOSUntypedLiteral untypedLiteral = literal.getAsSKOSUntypedLiteral();
|
||||
if (untypedLiteral.hasLang()) {
|
||||
lang = untypedLiteral.getLang();
|
||||
} else {
|
||||
lang = "";
|
||||
}
|
||||
}
|
||||
if (lang.equals("en")) {
|
||||
//System.out.println("prefLabel: " + literal.getLiteral());
|
||||
|
||||
concept.setLabel(literal.getLiteral());
|
||||
}
|
||||
}
|
||||
|
||||
// get altLabels
|
||||
List<String> altLabelList = new ArrayList<String>();
|
||||
for (SKOSLiteral literal : skosConcept
|
||||
.getSKOSRelatedConstantByProperty(dataset, manager
|
||||
.getSKOSDataFactory().getSKOSAltLabelProperty())) {
|
||||
|
||||
if (!literal.isTyped()) {
|
||||
// if it has language
|
||||
SKOSUntypedLiteral untypedLiteral = literal
|
||||
.getAsSKOSUntypedLiteral();
|
||||
if (untypedLiteral.hasLang()) {
|
||||
lang = untypedLiteral.getLang();
|
||||
} else {
|
||||
lang = "";
|
||||
}
|
||||
}
|
||||
//System.out.println("literal: "+ literal.getLiteral());
|
||||
if (lang.equals("en")) {
|
||||
//System.out.println("altLabel: " + literal.getLiteral());
|
||||
altLabelList.add(literal.getLiteral());
|
||||
}
|
||||
}
|
||||
concept.setAltLabelList(altLabelList);
|
||||
|
||||
|
||||
|
||||
// get the broader property URI
|
||||
List<String> broaderURIList = new ArrayList<String>();
|
||||
for (SKOSAnnotation annotation: skosConcept.getSKOSAnnotationsByURI(dataset, manager.getSKOSDataFactory().getSKOSBroaderProperty().getURI())) {
|
||||
String value = new String();
|
||||
if (annotation.isAnnotationByConstant()) {
|
||||
SKOSLiteral literal = annotation.getAnnotationValueAsConstant();
|
||||
value = literal.getLiteral();
|
||||
//System.out.println("broder uri: "+ value);
|
||||
} else {
|
||||
// annotation is some resource
|
||||
SKOSEntity entity = annotation.getAnnotationValue();
|
||||
value = entity.getURI().toString();
|
||||
}
|
||||
//System.out.println("broader uri: "+value);
|
||||
broaderURIList.add(value);
|
||||
}
|
||||
concept.setBroaderURIList(broaderURIList);
|
||||
|
||||
// get the narrower property URI
|
||||
List<String> narrowerURIList = new ArrayList<String>();
|
||||
for (SKOSAnnotation annotation: skosConcept.getSKOSAnnotationsByURI(dataset, manager.getSKOSDataFactory().getSKOSNarrowerProperty().getURI())) {
|
||||
String value = new String();
|
||||
if (annotation.isAnnotationByConstant()) {
|
||||
SKOSLiteral literal = annotation.getAnnotationValueAsConstant();
|
||||
value = literal.getLiteral();
|
||||
//System.out.println("narrower uri: "+ value);
|
||||
} else {
|
||||
// annotation is some resource
|
||||
SKOSEntity entity = annotation.getAnnotationValue();
|
||||
value = entity.getURI().toString();
|
||||
}
|
||||
//System.out.println("narrower uri: "+value);
|
||||
narrowerURIList.add(value);
|
||||
}
|
||||
concept.setNarrowerURIList(narrowerURIList);
|
||||
|
||||
// exact match
|
||||
List<String> exactMatchURIList = new ArrayList<String>();
|
||||
for (SKOSAnnotation annotation: skosConcept.getSKOSAnnotationsByURI(dataset, manager.getSKOSDataFactory().getSKOSExactMatchProperty().getURI())) {
|
||||
String value = new String();
|
||||
if (annotation.isAnnotationByConstant()) {
|
||||
SKOSLiteral literal = annotation.getAnnotationValueAsConstant();
|
||||
value = literal.getLiteral();
|
||||
//System.out.println("exact match: "+ value);
|
||||
} else {
|
||||
// annotation is some resource
|
||||
SKOSEntity entity = annotation.getAnnotationValue();
|
||||
value = entity.getURI().toString();
|
||||
}
|
||||
//System.out.println("exact match: "+value);
|
||||
exactMatchURIList.add(value);
|
||||
}
|
||||
concept.setExactMatchURIList(exactMatchURIList);
|
||||
|
||||
// close match
|
||||
List<String> closeMatchURIList = new ArrayList<String>();
|
||||
for (SKOSAnnotation annotation: skosConcept.getSKOSAnnotationsByURI(dataset, manager.getSKOSDataFactory().getSKOSCloseMatchProperty().getURI())) {
|
||||
String value = new String();
|
||||
if (annotation.isAnnotationByConstant()) {
|
||||
SKOSLiteral literal = annotation.getAnnotationValueAsConstant();
|
||||
value = literal.getLiteral();
|
||||
//System.out.println("close match: "+ value);
|
||||
} else {
|
||||
// annotation is some resource
|
||||
SKOSEntity entity = annotation.getAnnotationValue();
|
||||
value = entity.getURI().toString();
|
||||
}
|
||||
//System.out.println("close match: "+value);
|
||||
closeMatchURIList.add(value);
|
||||
if (value.startsWith("http://dbpedia.org")) {
|
||||
String description = getDbpediaDescription(value);
|
||||
//System.out.println("description: "+ description);
|
||||
concept.setDefinition(description);
|
||||
}
|
||||
}
|
||||
concept.setCloseMatchURIList(closeMatchURIList);
|
||||
|
||||
conceptList.add(concept);
|
||||
|
||||
|
||||
}
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
public List<Concept> processResults(String term) throws Exception {
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
|
||||
String termcode;
|
||||
try {
|
||||
termcode = getTermcodeByTerm(term);
|
||||
} catch (Exception e1) {
|
||||
logger.error("Could not get termcode from service", e1);
|
||||
throw e1;
|
||||
}
|
||||
|
||||
String format = "SKOS";
|
||||
// if the termcode is null it means that either the service is not responding
|
||||
// or there was not a match for the string
|
||||
//System.out.println("Got termcode: "+termcode);
|
||||
|
||||
String results = getConceptInfoByTermcodeXML(termcode, format);
|
||||
|
||||
//XMLUtils.prettyPrint(results);
|
||||
|
||||
try {
|
||||
Document doc = XMLUtils.parse(results);
|
||||
String prefLabelQuery = "child::*[@xml:lang='EN']";
|
||||
NodeList nodes = doc.getElementsByTagName("skos:Concept");
|
||||
//System.out.println("Found this many nodes: "+ nodes.getLength());
|
||||
for (int i=0; i < nodes.getLength(); i++) {
|
||||
|
||||
Node node = nodes.item(i);
|
||||
//XMLUtils.serializeNode(node); System.out.println();
|
||||
|
||||
Concept concept = new Concept();
|
||||
concept.setDefinedBy("http://aims.fao.org/aos/agrovoc/agrovocScheme");
|
||||
concept.setConceptId(termcode);
|
||||
|
||||
NamedNodeMap attrs = node.getAttributes();
|
||||
Attr idAttr = (Attr) attrs.getNamedItem("rdf:about");
|
||||
String conceptUri = idAttr.getTextContent();
|
||||
concept.setUri(conceptUri);
|
||||
|
||||
Node prefLabelNode = XMLUtils.getNodeWithXpath(node, prefLabelQuery);
|
||||
if (prefLabelNode != null) {
|
||||
String prefLabel = prefLabelNode.getTextContent();
|
||||
concept.setLabel(prefLabel);
|
||||
}
|
||||
conceptList.add(concept);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return conceptList;
|
||||
return getConcepts(term);
|
||||
}
|
||||
|
||||
|
||||
protected String getAgrovocLanguages() {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getAgrovocLanguages();
|
||||
} catch (ServiceException e) {
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Deprecated
|
||||
protected String getTermcodeByTerm(String term) throws Exception {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
ACSWWebServiceServiceLocator locator = new ACSWWebServiceServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
ACSWWebService agrovoc_service = locator.getACSWWebService(url);
|
||||
result = agrovoc_service.getTermcodeByTerm(term);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
|
@ -130,275 +262,58 @@ public class AgrovocService implements ExternalConceptService {
|
|||
return result;
|
||||
}
|
||||
|
||||
protected String getTermcodeByTermXML(String term, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermcodeByTermXML(term, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
protected String getTermCodeByTermAndLangXML(String ontologyName, String searchString, String lang, String codeName, String format) {
|
||||
String result = new String();
|
||||
ACSWWebServiceServiceLocator locator = new ACSWWebServiceServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
ACSWWebService agrovoc_service = locator.getACSWWebService(url);
|
||||
result = agrovoc_service.getTermCodeByTermAndLangXML(ontologyName, searchString, lang, codeName, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getTermByLanguage(int termcode, String language) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermByLanguage(termcode, language);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
protected String getTermByLanguageXML(int termcode, String language, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermByLanguageXML(termcode, language, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
protected String getURIByTermAndLangXML(String ontologyName, String term,
|
||||
String searchMode, String format, String lang) {
|
||||
String result = new String();
|
||||
ACSWWebServiceServiceLocator locator = new ACSWWebServiceServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
ACSWWebService agrovoc_service = locator.getACSWWebService(url);
|
||||
result = agrovoc_service.getURIByTermAndLangXML(ontologyName, term,
|
||||
searchMode, format, lang);
|
||||
} catch (ServiceException e) {
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getTermsListByLanguage2(String termcodes, String language, String sep) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermsListByLanguage2(termcodes, language, sep);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getTermsListByLanguageXML(String termcodes, String language, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermsListByLanguageXML(termcodes, language, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getAllLabelsByTermcode2(int termcode, String sep) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getAllLabelsByTermcode2(termcode, sep);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getAllLabelsByTermcodeXML(int termcode, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getAllLabelsByTermcodeXML(termcode, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String simpleSearchByMode2(String term, String mode, String sep ) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.simpleSearchByMode2(term, mode, sep);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String simpleSearchByModeXML(String term, String mode, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.simpleSearchByModeXML(term, mode, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String searchByTerm2(String term, String sep) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.searchByTerm2(term, sep);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String searchByTermXML(String term, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.searchByTermXML(term, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String searchCategoryByMode(String term, String lang, String schemeid, String mode, String sep) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.searchCategoryByMode(term, lang, schemeid, mode, sep);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String searchCategoryByModeXML(String term, String mode, String schemeid, String lang, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.searchCategoryByModeXML(term, mode, schemeid, lang, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String[] getConceptInfoByTermcode(String termcode) {
|
||||
String result[] = null;
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getConceptInfoByTermcode(termcode);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected String getConceptInfoByTermcodeXML(String termcode, String format) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
ACSWWebServiceServiceLocator locator = new ACSWWebServiceServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
ACSWWebService agrovoc_service = locator.getACSWWebService(url);
|
||||
result = agrovoc_service.getConceptInfoByTermcodeXML(termcode, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
|
@ -411,70 +326,34 @@ public class AgrovocService implements ExternalConceptService {
|
|||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getConceptByKeyword(String ontologyName, String searchString, String format, String searchMode, String lang) {
|
||||
String result = new String();
|
||||
ACSWWebServiceServiceLocator locator = new ACSWWebServiceServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
ACSWWebService agrovoc_service = locator.getACSWWebService(url);
|
||||
result = agrovoc_service.getConceptByKeyword(ontologyName, searchString, format, searchMode, lang);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
protected String[] getDefinitions(int termcode, String lang) {
|
||||
String[] result = null;
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getDefinitions(termcode, lang);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getDefinitionsXML(int termcode, String lang, String format) {
|
||||
String result = null;
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getDefinitionsXML(termcode, lang, format);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected String getTermExpansion(String aQuery, String langugage) {
|
||||
String result = new String();
|
||||
AgrovocWSServiceLocator locator = new AgrovocWSServiceLocator();
|
||||
try {
|
||||
URL url = new URL(AgrovocWS_address);
|
||||
AgrovocWS agrovoc_service = locator.getAgrovocWS(url);
|
||||
result = agrovoc_service.getTermExpansion(aQuery, langugage);
|
||||
} catch (ServiceException e) {
|
||||
logger.error("service exception", e);
|
||||
e.printStackTrace();
|
||||
} catch (RemoteException e) {
|
||||
e.printStackTrace();
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected String getWsdl() {
|
||||
String result = new String();
|
||||
try {
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
URL rss = new URL(AgrovocWS_address + "?wsdl");
|
||||
URL rss = new URL(this.AgrovocWS_address + "?wsdl");
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(rss.openStream()));
|
||||
String inputLine;
|
||||
|
@ -490,4 +369,141 @@ public class AgrovocService implements ExternalConceptService {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public List<Concept> getConceptsByURIWithSparql(String uri) throws Exception {
|
||||
// deprecating this method...just return an empty list
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
protected String getAgrovocTermCode(String rdf) throws Exception {
|
||||
String termcode = new String();
|
||||
try {
|
||||
Document doc = XMLUtils.parse(rdf);
|
||||
NodeList nodes = doc.getElementsByTagName("hasCodeAgrovoc");
|
||||
if (nodes.item(0) != null) {
|
||||
Node node = nodes.item(0);
|
||||
termcode = node.getTextContent();
|
||||
}
|
||||
|
||||
} catch (SAXException e) {
|
||||
//e.printStackTrace();
|
||||
throw e;
|
||||
} catch (ParserConfigurationException e) {
|
||||
//e.printStackTrace();
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
//e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
return termcode;
|
||||
}
|
||||
|
||||
protected String getConceptURIFromRDF(String rdf) {
|
||||
String conceptUri = new String();
|
||||
try {
|
||||
Document doc = XMLUtils.parse(rdf);
|
||||
NodeList nodes = doc.getElementsByTagName("skos:Concept");
|
||||
Node node = nodes.item(0);
|
||||
|
||||
NamedNodeMap attrs = node.getAttributes();
|
||||
Attr idAttr = (Attr) attrs.getNamedItem("rdf:about");
|
||||
conceptUri = idAttr.getTextContent();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("rdf: "+rdf);
|
||||
} catch (SAXException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("rdf: "+rdf);
|
||||
} catch (ParserConfigurationException e) {
|
||||
e.printStackTrace();
|
||||
System.err.println("rdf: "+rdf);
|
||||
}
|
||||
return conceptUri;
|
||||
|
||||
}
|
||||
|
||||
protected String getDbpediaDescription(String uri) throws Exception{
|
||||
String descriptionSource = " (Source: DBpedia)";
|
||||
String description = new String();
|
||||
String qs = ""
|
||||
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
|
||||
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
|
||||
+ "PREFIX foaf: <http://xmlns.com/foaf/0.1/> \n"
|
||||
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
|
||||
+ "SELECT DISTINCT ?description WHERE { \n" + "<" + uri
|
||||
+ "> rdfs:comment ?description . \n"
|
||||
+ "FILTER (LANG(?description)='en' ) \n" + "}";
|
||||
//System.out.println(qs);
|
||||
List<HashMap> resultList = new ArrayList<HashMap>();
|
||||
QueryExecution qexec = null;
|
||||
try {
|
||||
|
||||
Query query = QueryFactory.create(qs);
|
||||
qexec = QueryExecutionFactory.sparqlService(this.dbpedia_endpoint,
|
||||
query);
|
||||
resultList = new ArrayList<HashMap>();
|
||||
ResultSet resultSet = qexec.execSelect();
|
||||
int resultSetSize = 0;
|
||||
while (resultSet.hasNext()) {
|
||||
resultSetSize++;
|
||||
QuerySolution solution = resultSet.nextSolution();
|
||||
Iterator varnames = solution.varNames();
|
||||
HashMap<String, String> hm = new HashMap<String, String>();
|
||||
while (varnames.hasNext()) {
|
||||
String name = (String) varnames.next();
|
||||
RDFNode rdfnode = solution.get(name);
|
||||
// logger.info("rdf node name, type: "+ name
|
||||
// +", "+getRDFNodeType(rdfnode));
|
||||
if (rdfnode.isLiteral()) {
|
||||
Literal literal = rdfnode.asLiteral();
|
||||
String nodeval = literal.getString();
|
||||
hm.put(name, nodeval);
|
||||
} else if (rdfnode.isResource()) {
|
||||
Resource resource = rdfnode.asResource();
|
||||
String nodeval = resource.toString();
|
||||
hm.put(name, nodeval);
|
||||
}
|
||||
}
|
||||
resultList.add(hm);
|
||||
}
|
||||
description = "";
|
||||
for (HashMap map : resultList) {
|
||||
if (map.containsKey("description")) {
|
||||
description = (String) map.get("description");
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw ex;
|
||||
}
|
||||
//Adding source so it is clear that this description comes from DBPedia
|
||||
return description + descriptionSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
protected String stripConceptId(String uri) {
|
||||
String conceptId = new String();
|
||||
int lastslash = uri.lastIndexOf('/');
|
||||
conceptId = uri.substring(lastslash + 1, uri.length());
|
||||
return conceptId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
protected String extractConceptId(String str) {
|
||||
try {
|
||||
return str.substring(1, str.length() - 1);
|
||||
} catch (Exception ex) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -4,38 +4,22 @@ package edu.cornell.mannlib.semservices.service.impl;
|
|||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.rmi.RemoteException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import javax.xml.rpc.ServiceException;
|
||||
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
import net.sf.json.JSONSerializer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.fao.gilw.aims.webservices.AgrovocWS;
|
||||
import org.fao.gilw.aims.webservices.AgrovocWSServiceLocator;
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import edu.cornell.mannlib.semservices.bo.Concept;
|
||||
import edu.cornell.mannlib.semservices.exceptions.ConceptsNotFoundException;
|
||||
import edu.cornell.mannlib.semservices.service.ExternalConceptService;
|
||||
import edu.cornell.mannlib.semservices.util.XMLUtils;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
|
||||
|
||||
public class GemetService implements ExternalConceptService {
|
||||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
|
@ -49,63 +33,112 @@ public class GemetService implements ExternalConceptService {
|
|||
private final String altLabelUri = "http://www.w3.org/2004/02/skos/core%23altLabel";
|
||||
private final String exampleUri = "http://www.w3.org/2004/02/skos/core%23example";
|
||||
private final String acronymLabelUri = "http://www.w3.org/2004/02/skos/core%23acronymLabel";
|
||||
private final String endpoint = "http://cr.eionet.europa.eu/sparql";
|
||||
private final String schemeURI = "http://www.eionet.europa.eu/gemet/gemetThesaurus";
|
||||
|
||||
|
||||
@Override
|
||||
public List<Concept> getConcepts(String term) throws Exception {
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
try {
|
||||
String results = getConceptsMatchingKeyword(term);
|
||||
//System.out.println(results);
|
||||
conceptList = processOutput(results);
|
||||
|
||||
} catch (Exception ex) {
|
||||
return new ArrayList<Concept>();
|
||||
//ex.printStackTrace();
|
||||
//throw ex;
|
||||
}
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
public List<Concept> processResults(String term) throws Exception {
|
||||
List<Concept> conceptList = processConceptsAndRelatedMatchingKeyword(term);
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
try {
|
||||
String results = getConceptsMatchingKeyword(term);
|
||||
conceptList = processOutput(results);
|
||||
} catch (Exception ex) {
|
||||
//ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
return conceptList;
|
||||
|
||||
}
|
||||
|
||||
public List<Concept> getConceptsByURIWithSparql(String uri)
|
||||
throws Exception {
|
||||
// deprecating this method...just return an empty list
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param results
|
||||
* @return
|
||||
* By default, concepts set with best match = true
|
||||
*/
|
||||
private List<Concept> processOutput(String results) {
|
||||
return processOutput(results, "true");
|
||||
private List<Concept> processOutput(String results) throws Exception {
|
||||
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
|
||||
}
|
||||
|
||||
private List<Concept> processOutput(String results, String bestMatch) {
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
try {
|
||||
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( results );
|
||||
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
Concept concept = new Concept();
|
||||
concept
|
||||
.setDefinedBy("http://www.eionet.europa.eu/gemet/gemetThesaurus");
|
||||
concept.setBestMatch(bestMatch);
|
||||
JSONObject json = jsonArray.getJSONObject(i);
|
||||
String uri = getJsonValue(json, "uri");
|
||||
if (jsonArray.size() == 0) {
|
||||
throw new ConceptsNotFoundException();
|
||||
}
|
||||
|
||||
concept.setUri(uri);
|
||||
concept.setConceptId(uri);
|
||||
if (json.has("preferredLabel")) {
|
||||
JSONObject preferredLabelObj = json
|
||||
.getJSONObject("preferredLabel");
|
||||
if (preferredLabelObj.has("string")) {
|
||||
concept.setLabel(getJsonValue(preferredLabelObj,
|
||||
"string"));
|
||||
}
|
||||
}
|
||||
if (json.has("definition")) {
|
||||
JSONObject definitionObj = json.getJSONObject("definition");
|
||||
if (definitionObj.has("string")) {
|
||||
concept.setDefinition(getJsonValue(definitionObj,
|
||||
"string"));
|
||||
}
|
||||
}
|
||||
conceptList.add(concept);
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
Concept concept = new Concept();
|
||||
concept.setDefinedBy(schemeURI);
|
||||
concept.setBestMatch("true");
|
||||
JSONObject json = jsonArray.getJSONObject(i);
|
||||
String uri = getJsonValue(json, "uri");
|
||||
|
||||
}
|
||||
|
||||
concept.setUri(uri);
|
||||
concept.setConceptId(stripConceptId(uri));
|
||||
concept.setSchemeURI(schemeURI);
|
||||
if (json.has("preferredLabel")) {
|
||||
JSONObject preferredLabelObj = json
|
||||
.getJSONObject("preferredLabel");
|
||||
if (preferredLabelObj.has("string")) {
|
||||
concept.setLabel(getJsonValue(preferredLabelObj,
|
||||
"string"));
|
||||
}
|
||||
}
|
||||
if (json.has("definition")) {
|
||||
JSONObject definitionObj = json.getJSONObject("definition");
|
||||
if (definitionObj.has("string")) {
|
||||
concept.setDefinition(getJsonValue(definitionObj,
|
||||
"string"));
|
||||
}
|
||||
}
|
||||
|
||||
String narrower = getRelatedConcepts(uri, "narrower");
|
||||
List<String> narrowerURIList = getRelatedUris(narrower);
|
||||
concept.setNarrowerURIList(narrowerURIList);
|
||||
|
||||
String broader = getRelatedConcepts(uri, "broader");
|
||||
List<String> broaderURIList = getRelatedUris(broader);
|
||||
concept.setBroaderURIList(broaderURIList);
|
||||
|
||||
/*String related = getRelatedConcepts(uri, "related");
|
||||
List<String> relatedURIList = getRelatedUris(related);
|
||||
for (String s: relatedURIList) {
|
||||
System.out.println("related uri: "+s);
|
||||
}*/
|
||||
//String altLabels = getAllTranslationsForConcept(uri, "nonPreferredLabels");
|
||||
|
||||
conceptList.add(concept);
|
||||
|
||||
}
|
||||
|
||||
} catch (Exception ex ) {
|
||||
ex.printStackTrace();
|
||||
//ex.printStackTrace();
|
||||
logger.error("Could not get concepts", ex);
|
||||
throw ex;
|
||||
}
|
||||
return conceptList;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -123,23 +156,50 @@ public class GemetService implements ExternalConceptService {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param concept_uri
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected String getAvailableLangs(String concept_uri) throws Exception {
|
||||
String result = new String();
|
||||
String serviceUrl = GemetWS_address + "getAvailableLanguages" +
|
||||
"?concept_uri=" + concept_uri;
|
||||
result = getGemetResults(serviceUrl);
|
||||
try {
|
||||
result = getGemetResults(serviceUrl);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param concept_uri
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected String getConcept(String concept_uri) throws Exception {
|
||||
String result = new String();
|
||||
String serviceUrl = GemetWS_address + "getConcept" +
|
||||
"?concept_uri=" + concept_uri +
|
||||
"&language=en";
|
||||
result = getGemetResults(serviceUrl);
|
||||
try {
|
||||
result = getGemetResults(serviceUrl);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
protected String getAllTranslationsForConcept(String concept_uri, String property) throws Exception {
|
||||
|
||||
/**
|
||||
* @param concept_uri
|
||||
* @param property
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected String getAllTranslationsForConcept(String concept_uri, String property) throws Exception {
|
||||
String result = new String();
|
||||
String property_uri = new String();
|
||||
if (property.equals("definition")) {
|
||||
|
@ -160,11 +220,24 @@ public class GemetService implements ExternalConceptService {
|
|||
"?concept_uri=" + concept_uri +
|
||||
"&property_uri=" + property_uri +
|
||||
"&language=en";
|
||||
result = getGemetResults(serviceUrl);
|
||||
try {
|
||||
result = getGemetResults(serviceUrl);
|
||||
List<String> props = getPropertyFromJson(result);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param concept_uri
|
||||
* @param relation
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected String getRelatedConcepts(String concept_uri, String relation) throws Exception {
|
||||
String result = new String();
|
||||
String relation_uri = new String();
|
||||
|
@ -179,41 +252,43 @@ public class GemetService implements ExternalConceptService {
|
|||
"?concept_uri=" + concept_uri +
|
||||
"&relation_uri=" + relation_uri +
|
||||
"&language=en";
|
||||
result = getGemetResults(serviceUrl);
|
||||
try {
|
||||
result = getGemetResults(serviceUrl);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param keyword
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
protected String getConceptsMatchingKeyword(String keyword) throws Exception {
|
||||
String result = new String();
|
||||
String serviceUrl = GemetWS_address + "getConceptsMatchingKeyword" +
|
||||
"?keyword=" + URLEncoder.encode(keyword) +
|
||||
"?keyword=" + keyword +
|
||||
"&search_mode=0" +
|
||||
"&thesaurus_uri=http://www.eionet.europa.eu/gemet/concept/" +
|
||||
"&language=en";
|
||||
|
||||
result = getGemetResults(serviceUrl);
|
||||
try {
|
||||
result = getGemetResults(serviceUrl);
|
||||
} catch (Exception ex) {
|
||||
throw ex;
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
//Get concepts matching keyword plus any related concepts
|
||||
protected List<Concept> processConceptsAndRelatedMatchingKeyword(String keyword) throws Exception {
|
||||
String result = getConceptsMatchingKeyword(keyword);
|
||||
//iterate through each of the concepts and add related concepts a well
|
||||
List<Concept> bestMatchConceptList = processOutput(result);
|
||||
List<Concept> relatedConceptList = new ArrayList<Concept>();
|
||||
for(Concept c: bestMatchConceptList) {
|
||||
String conceptUri = c.getUri();
|
||||
String resultsRelated = getRelatedConcepts(conceptUri, "related");
|
||||
relatedConceptList.addAll(processOutput(resultsRelated, "false"));
|
||||
}
|
||||
bestMatchConceptList.addAll(relatedConceptList);
|
||||
return bestMatchConceptList;
|
||||
}
|
||||
|
||||
protected String getGemetResults(String url) throws Exception {
|
||||
/**
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
protected String getGemetResults(String url) throws Exception {
|
||||
String results = new String();
|
||||
//System.out.println("url: "+url);
|
||||
try {
|
||||
|
@ -232,9 +307,50 @@ public class GemetService implements ExternalConceptService {
|
|||
|
||||
} catch (Exception ex) {
|
||||
logger.error("error occurred in servlet", ex);
|
||||
ex.printStackTrace();
|
||||
throw ex;
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
protected List<String> getRelatedUris(String json) {
|
||||
List<String> uriList = new ArrayList<String>();
|
||||
String uri = new String();
|
||||
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON( json );
|
||||
if (jsonArray.size() == 0) {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
JSONObject jsonObj = jsonArray.getJSONObject(i);
|
||||
uri = getJsonValue(jsonObj, "uri");
|
||||
uriList.add(uri);
|
||||
}
|
||||
|
||||
return uriList;
|
||||
|
||||
}
|
||||
|
||||
protected List<String> getPropertyFromJson(String json) {
|
||||
List<String> props = new ArrayList<String>();
|
||||
JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(json);
|
||||
if (jsonArray.size() == 0) {
|
||||
return new ArrayList<String>();
|
||||
}
|
||||
for (int i = 0; i < jsonArray.size(); i++) {
|
||||
JSONObject jsonObj = jsonArray.getJSONObject(i);
|
||||
System.out.println(jsonObj.toString());
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
protected String stripConceptId(String uri) {
|
||||
String conceptId = new String();
|
||||
int lastslash = uri.lastIndexOf('/');
|
||||
conceptId = uri.substring(lastslash + 1, uri.length());
|
||||
return conceptId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import java.io.BufferedReader;
|
|||
import java.io.InputStreamReader;
|
||||
import java.io.StringWriter;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -17,8 +18,8 @@ import org.apache.commons.logging.Log;
|
|||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import edu.cornell.mannlib.semservices.bo.Concept;
|
||||
import edu.cornell.mannlib.semservices.exceptions.ConceptsNotFoundException;
|
||||
import edu.cornell.mannlib.semservices.service.ExternalConceptService;
|
||||
import edu.cornell.mannlib.vitro.webapp.web.URLEncoder;
|
||||
|
||||
/**
|
||||
* @author jaf30
|
||||
|
@ -28,11 +29,48 @@ public class UMLSService implements ExternalConceptService {
|
|||
protected final Log logger = LogFactory.getLog(getClass());
|
||||
private static final String submissionUrl = "http://link.informatics.stonybrook.edu/MeaningLookup/MlServiceServlet?";
|
||||
private static final String baseUri = "http://link.informatics.stonybrook.edu/umls/CUI/";
|
||||
private static final String endpoint = "http://link.informatics.stonybrook.edu/sparql/";
|
||||
private static final String schemeURI = "http://link.informatics.stonybrook.edu/umls";
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public List<Concept> getConcepts(String term) throws Exception {
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
|
||||
public List<Concept> processResults(String term) throws Exception{
|
||||
String results = null;
|
||||
String dataUrl = submissionUrl + "textToProcess="
|
||||
+ URLEncoder.encode(term, "UTF-8")
|
||||
+ "&format=json";
|
||||
|
||||
try {
|
||||
|
||||
StringWriter sw = new StringWriter();
|
||||
URL rss = new URL(dataUrl);
|
||||
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(
|
||||
rss.openStream()));
|
||||
String inputLine;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
sw.write(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
results = sw.toString();
|
||||
//System.out.println("results before processing: "+results);
|
||||
conceptList = processOutput(results);
|
||||
return conceptList;
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("error occurred in servlet", ex);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Concept> processResults(String term) throws Exception {
|
||||
String results = null;
|
||||
String dataUrl = submissionUrl + "textToProcess="
|
||||
+ URLEncoder.encode(term) + "&format=json";
|
||||
+ URLEncoder.encode(term, "UTF-8") + "&format=json";
|
||||
|
||||
try {
|
||||
|
||||
|
@ -47,41 +85,58 @@ public class UMLSService implements ExternalConceptService {
|
|||
in.close();
|
||||
|
||||
results = sw.toString();
|
||||
//System.out.println("results before processing: "+results);
|
||||
List<Concept> conceptList = processOutput(results);
|
||||
return conceptList;
|
||||
|
||||
} catch (Exception ex) {
|
||||
logger.error("error occurred in servlet", ex);
|
||||
throw ex;
|
||||
return null;
|
||||
}
|
||||
List<Concept> conceptList = processOutput(results);
|
||||
return conceptList;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param uri
|
||||
* @return
|
||||
*/
|
||||
public List<Concept> getConceptsByURIWithSparql(String uri)
|
||||
throws Exception {
|
||||
// deprecating this method...just return an empty list
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
return conceptList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param results
|
||||
* @return
|
||||
*/
|
||||
private List<Concept> processOutput(String results) {
|
||||
private List<Concept> processOutput(String results) throws Exception {
|
||||
|
||||
List<Concept> conceptList = new ArrayList<Concept>();
|
||||
List<String> bestMatchIdList = new ArrayList<String>();
|
||||
String bestMatchId = new String();
|
||||
boolean bestMatchFound = false;
|
||||
boolean allFound = false;
|
||||
|
||||
try {
|
||||
JSONObject json = (JSONObject) JSONSerializer.toJSON( results );
|
||||
|
||||
//System.out.println(json.toString());
|
||||
if (json.has("Best Match")) {
|
||||
bestMatchFound = true;
|
||||
//System.out.println("Best Match");
|
||||
|
||||
JSONArray bestMatchArray = json.getJSONArray("Best Match");
|
||||
int len = bestMatchArray.size();
|
||||
if (len > 1) {
|
||||
System.out.println("Found this many best matches: "+ len);
|
||||
logger.debug("Found this many best matches: "+ len);
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
JSONObject o = bestMatchArray.getJSONObject(i);
|
||||
//System.out.println(o.toString());
|
||||
Concept concept = new Concept();
|
||||
concept.setDefinedBy("http://link.informatics.stonybrook.edu/umls");
|
||||
concept.setDefinedBy(schemeURI);
|
||||
concept.setBestMatch("true");
|
||||
String cui = getJsonValue(o, "CUI");
|
||||
bestMatchIdList.add(cui);
|
||||
|
@ -91,11 +146,12 @@ public class UMLSService implements ExternalConceptService {
|
|||
concept.setType(getJsonValue(o, "type"));
|
||||
concept.setDefinition(getJsonValue(o, "definition"));
|
||||
concept.setUri(baseUri + cui);
|
||||
concept.setSchemeURI(schemeURI);
|
||||
conceptList.add(concept);
|
||||
}
|
||||
}
|
||||
if (json.has("All")) {
|
||||
|
||||
allFound = true;
|
||||
JSONArray allArray = json.getJSONArray("All");
|
||||
int len = allArray.size();
|
||||
//System.out.println("size of best match array: "+ len);
|
||||
|
@ -104,7 +160,7 @@ public class UMLSService implements ExternalConceptService {
|
|||
JSONObject o = allArray.getJSONObject(i);
|
||||
//System.out.println(o.toString());
|
||||
Concept concept = new Concept();
|
||||
concept.setDefinedBy("http://link.informatics.stonybrook.edu/umls");
|
||||
concept.setDefinedBy(schemeURI);
|
||||
String cui = getJsonValue(o, "CUI");
|
||||
concept.setConceptId(cui);
|
||||
|
||||
|
@ -112,6 +168,7 @@ public class UMLSService implements ExternalConceptService {
|
|||
concept.setType(getJsonValue(o, "type"));
|
||||
concept.setDefinition(getJsonValue(o, "definition"));
|
||||
concept.setUri(baseUri + cui);
|
||||
concept.setSchemeURI(schemeURI);
|
||||
// prevent duplicate concepts in list
|
||||
if (! bestMatchIdList.contains(cui)) {
|
||||
concept.setBestMatch("false");
|
||||
|
@ -122,20 +179,18 @@ public class UMLSService implements ExternalConceptService {
|
|||
} catch (Exception ex ) {
|
||||
ex.printStackTrace();
|
||||
logger.error("Could not get concepts", ex);
|
||||
throw ex;
|
||||
}
|
||||
if (! bestMatchFound && !allFound) {
|
||||
// we did not get a bestMatch or All element
|
||||
throw new ConceptsNotFoundException();
|
||||
}
|
||||
|
||||
//
|
||||
return conceptList;
|
||||
|
||||
//
|
||||
// now serialize the list of Concepts to a JSON String
|
||||
//
|
||||
//JSONObject jsonObject = null;
|
||||
//jsonObject = BeanToJsonSerializer.serializeToJsonObject(conceptList);
|
||||
//System.out.println(jsonObject.toString());
|
||||
//return jsonObject.toString();
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a string from a json object or an empty string if there is no value for the given key
|
||||
* @param obj
|
||||
|
@ -150,4 +205,13 @@ public class UMLSService implements ExternalConceptService {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected String stripConceptId(String uri) {
|
||||
String conceptId = new String();
|
||||
int lastslash = uri.lastIndexOf('/');
|
||||
conceptId = uri.substring(lastslash + 1, uri.length());
|
||||
return conceptId;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/*
|
||||
|
||||
* $Id$
|
||||
* CONFIDENTIAL AND PROPRIETARY. ? 2007 Revolution Health Group LLC. All rights reserved.
|
||||
* This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/*
|
||||
|
||||
* $Id: DateConverter.java 50408 2007-03-28 19:14:46Z jdamick $
|
||||
*
|
||||
* Copyright 2006- Revolution Health Group. All rights reserved.
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/*
|
||||
|
||||
* $Id$
|
||||
* CONFIDENTIAL AND PROPRIETARY. © 2007 Revolution Health Group LLC. All rights reserved.
|
||||
* This source code may not be disclosed to others, used or reproduced without the written permission of Revolution Health Group.
|
||||
|
@ -264,5 +267,10 @@ public class ObjectUtils {
|
|||
}
|
||||
}
|
||||
|
||||
public static String nl2br(String text) {
|
||||
return text.replaceAll("\n\n", "<p>").replaceAll("\n", "<br>");
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/*
|
||||
|
||||
* $Id: XMLGregorianCalendarConverter.java 28642 2006-10-25 13:41:54Z jdamick $
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
package edu.cornell.mannlib.semservices.util;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
@ -81,7 +82,7 @@ public class XMLUtils {
|
|||
public synchronized static Document parse(String xmlString)
|
||||
throws IOException, SAXException, ParserConfigurationException {
|
||||
StringReader reader = new StringReader(xmlString);
|
||||
InputSource inputSource = new InputSource(reader);
|
||||
InputSource inputSource = new InputSource(reader);
|
||||
return getDocumentBuilder().parse(inputSource);
|
||||
}
|
||||
|
||||
|
@ -122,6 +123,15 @@ public class XMLUtils {
|
|||
serializer.serialize(doc);
|
||||
}
|
||||
|
||||
public static String serializeDoctoString(Document doc) throws IOException {
|
||||
XMLSerializer serializer = new XMLSerializer();
|
||||
ByteArrayOutputStream bout = new ByteArrayOutputStream();
|
||||
|
||||
serializer.setOutputByteStream(bout);
|
||||
serializer.serialize(doc);
|
||||
return bout.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param xml
|
||||
*/
|
||||
|
@ -152,6 +162,36 @@ public class XMLUtils {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param xml
|
||||
*/
|
||||
public static String prettyPrintToString(String xml) {
|
||||
Source xmlInput = new StreamSource(new StringReader(xml));
|
||||
StreamResult xmlOutput = new StreamResult(new StringWriter());
|
||||
Transformer transformer = null;
|
||||
try {
|
||||
transformer = TransformerFactory.newInstance().newTransformer();
|
||||
} catch (TransformerConfigurationException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (TransformerFactoryConfigurationError e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
//transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
|
||||
try {
|
||||
transformer.transform(xmlInput, xmlOutput);
|
||||
} catch (TransformerException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
String formattedxml=xmlOutput.getWriter().toString();
|
||||
return formattedxml;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param node
|
||||
*/
|
||||
|
|
|
@ -1,4 +1,18 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService;
|
||||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
/*
|
||||
* $Id: BeanToJsonSerializer.java 66350 2007-08-20 21:11:41Z xluan $
|
||||
*
|
||||
* Copyright 2006- Revolution Health Group. All rights reserved.
|
||||
*
|
||||
* This software is the confidential and proprietary information
|
||||
* of Revolution Health Group. (Confidential Information).
|
||||
* You shall not disclose such Confidential Information and shall
|
||||
* use it only in accordance with the terms of the license
|
||||
* agreement you entered into with Revolution Health Group.
|
||||
*
|
||||
*/
|
||||
package edu.cornell.mannlib.semservices.view;
|
||||
|
||||
import java.beans.PropertyDescriptor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
|
@ -16,8 +16,17 @@ import javax.servlet.http.HttpSession;
|
|||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.query.Query;
|
||||
import com.hp.hpl.jena.query.QueryExecution;
|
||||
import com.hp.hpl.jena.query.QueryExecutionFactory;
|
||||
import com.hp.hpl.jena.query.QueryFactory;
|
||||
import com.hp.hpl.jena.query.QuerySolution;
|
||||
import com.hp.hpl.jena.query.ResultSet;
|
||||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.vocabulary.OWL;
|
||||
import com.hp.hpl.jena.vocabulary.RDF;
|
||||
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||
import com.hp.hpl.jena.vocabulary.XSD;
|
||||
|
@ -27,11 +36,13 @@ import edu.cornell.mannlib.vitro.webapp.beans.ObjectProperty;
|
|||
import edu.cornell.mannlib.vitro.webapp.beans.ObjectPropertyStatement;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.UrlBuilder;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.ModelAccess;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.fields.FieldVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.AddAssociatedConceptsPreprocessor;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors.ConceptSemanticTypesPreprocessor;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.validators.AntiXssValidation;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.ConceptSearchServiceUtils;
|
||||
/**
|
||||
|
@ -45,9 +56,9 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
|
||||
private Log log = LogFactory.getLog(AddAssociatedConceptGenerator.class);
|
||||
private String template = "addAssociatedConcept.ftl";
|
||||
private static String SKOSConceptType = "http://www.w3.org/2004/02/skos/core#Concept";
|
||||
|
||||
|
||||
//TODO: Set this to a dynamic mechanism
|
||||
private static String VIVOCore = "http://vivoweb.org/ontology/core#";
|
||||
private static String SKOSConceptType = "http://www.w3.org/2004/02/skos/core#Concept";
|
||||
@Override
|
||||
public EditConfigurationVTwo getEditConfiguration(VitroRequest vreq, HttpSession session) {
|
||||
EditConfigurationVTwo editConfiguration = new EditConfigurationVTwo();
|
||||
|
@ -84,7 +95,9 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
setTemplate(editConfiguration, vreq);
|
||||
// No validators required here
|
||||
// Add preprocessors
|
||||
addPreprocessors(editConfiguration, vreq.getWebappDaoFactory());
|
||||
addPreprocessors(editConfiguration,
|
||||
ModelAccess.on(vreq).getJenaOntModel(),
|
||||
ModelAccess.on(vreq).getOntModelSelector().getTBoxModel());
|
||||
// Adding additional data, specifically edit mode
|
||||
addFormSpecificData(editConfiguration, vreq);
|
||||
// One override for basic functionality, changing url pattern
|
||||
|
@ -168,10 +181,15 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
return n3Required;
|
||||
}
|
||||
|
||||
//Don't think there's any n3 optional here
|
||||
//Optional N3, includes possibility of semantic type which may or may not be included
|
||||
//label and source are independent of semantic type
|
||||
//concept semantic type uri is a placeholder which is actually processed in the sparql update preprocessor
|
||||
private List<String> generateN3Optional() {
|
||||
return list("?conceptNode <" + RDFS.label.getURI() + "> ?conceptLabel .\n" +
|
||||
"?conceptNode <" + RDFS.isDefinedBy.getURI() + "> ?conceptSource ."
|
||||
"?conceptNode <" + RDFS.isDefinedBy.getURI() + "> ?conceptSource .",
|
||||
"?conceptNode <" + RDF.type + "> ?conceptSemanticTypeURI ." +
|
||||
"?conceptSemanticTypeURI <" + RDFS.label.getURI() + "> ?conceptSemanticTypeLabel ." +
|
||||
"?conceptSemanticTypeURI <" + RDFS.subClassOf + "> <" + SKOSConceptType + "> ."
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -185,6 +203,8 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
HashMap<String, String> newResources = new HashMap<String, String>();
|
||||
//There are no new resources here, the concept node uri doesn't
|
||||
//get created but already exists, and vocab uri should already exist as well
|
||||
//Adding concept semantic type uri just to test - note this isn't really on the form at all
|
||||
newResources.put("conceptSemanticTypeURI", null);
|
||||
return newResources;
|
||||
}
|
||||
|
||||
|
@ -233,9 +253,12 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
//The URI of the node that defines the concept
|
||||
urisOnForm.add("conceptNode");
|
||||
urisOnForm.add("conceptSource");
|
||||
urisOnForm.add("conceptSemanticTypeURI");
|
||||
editConfiguration.setUrisOnform(urisOnForm);
|
||||
//Also need to add the label of the concept
|
||||
literalsOnForm.add("conceptLabel");
|
||||
literalsOnForm.add("conceptSemanticTypeLabel");
|
||||
|
||||
editConfiguration.setLiteralsOnForm(literalsOnForm);
|
||||
}
|
||||
|
||||
|
@ -263,6 +286,8 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
setConceptNodeField(editConfiguration, vreq);
|
||||
setConceptLabelField(editConfiguration, vreq);
|
||||
setVocabURIField(editConfiguration, vreq);
|
||||
setConceptSemanticTypeURIField(editConfiguration,vreq);
|
||||
setConceptSemanticTypeLabelField(editConfiguration,vreq);
|
||||
}
|
||||
|
||||
//this field will be hidden and include the concept node URI
|
||||
|
@ -290,15 +315,36 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
setRangeDatatypeUri(XSD.xstring.toString())
|
||||
);
|
||||
}
|
||||
|
||||
//This will also be a URI
|
||||
private void setConceptSemanticTypeURIField(EditConfigurationVTwo editConfiguration,
|
||||
VitroRequest vreq) {
|
||||
editConfiguration.addField(new FieldVTwo().
|
||||
setName("conceptSemanticTypeURI")
|
||||
);
|
||||
}
|
||||
|
||||
private void setConceptSemanticTypeLabelField(EditConfigurationVTwo editConfiguration,
|
||||
VitroRequest vreq) {
|
||||
editConfiguration.addField(new FieldVTwo().
|
||||
setName("conceptSemanticTypeLabel").
|
||||
setRangeDatatypeUri(XSD.xstring.toString())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
//Add preprocessor
|
||||
|
||||
private void addPreprocessors(EditConfigurationVTwo editConfiguration, WebappDaoFactory wadf) {
|
||||
private void addPreprocessors(EditConfigurationVTwo editConfiguration, OntModel ontModel, OntModel modelChangeModel) {
|
||||
//An Edit submission preprocessor for enabling addition of multiple terms for a single search
|
||||
|
||||
//TODO: Check if this is the appropriate way of getting model
|
||||
|
||||
//Passing model to check for any URIs that are present
|
||||
|
||||
editConfiguration.addEditSubmissionPreprocessor(
|
||||
new AddAssociatedConceptsPreprocessor(editConfiguration));
|
||||
new AddAssociatedConceptsPreprocessor(editConfiguration, ontModel));
|
||||
editConfiguration.addModelChangePreprocessor(new ConceptSemanticTypesPreprocessor(
|
||||
modelChangeModel));
|
||||
|
||||
}
|
||||
|
||||
|
@ -350,54 +396,112 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
}
|
||||
|
||||
|
||||
//To determine whether or not a concept is a user generated or one from an external vocab source.
|
||||
//we cannot rely on whether or not it is a skos concept because incorporating UMLS semantic network classes as
|
||||
//SKOS concept subclasses means that even concepts from an external vocab source might be considered SKOS concepts
|
||||
//Instead, we will simply determine whether a concept is defined by an external vocabulary source and use that
|
||||
//as the primary indicator of whether a concept is from an external vocabulary source or a user generated concept
|
||||
private List<AssociatedConceptInfo> getAssociatedConceptInfo(
|
||||
List<Individual> concepts, VitroRequest vreq) {
|
||||
List<AssociatedConceptInfo> info = new ArrayList<AssociatedConceptInfo>();
|
||||
for ( Individual conceptIndividual : concepts ) {
|
||||
boolean isSKOSConcept = false;
|
||||
boolean userGenerated = true;
|
||||
//Note that this isn't technically
|
||||
String conceptUri = conceptIndividual.getURI();
|
||||
String conceptLabel = conceptIndividual.getName();
|
||||
//Check if SKOS Concept type
|
||||
List<ObjectPropertyStatement> osl = conceptIndividual.getObjectPropertyStatements(RDF.type.getURI());
|
||||
for(ObjectPropertyStatement os: osl) {
|
||||
if(os.getObjectURI().equals(SKOSConceptType)) {
|
||||
isSKOSConcept = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//Check if defined by an external vocabulary source
|
||||
List<ObjectPropertyStatement> vocabList = conceptIndividual.getObjectPropertyStatements(RDFS.isDefinedBy.getURI());
|
||||
String vocabSource = null;
|
||||
String vocabLabel = null;
|
||||
if(vocabList != null && vocabList.size() > 0) {
|
||||
userGenerated = false;
|
||||
vocabSource = vocabList.get(0).getObjectURI();
|
||||
Individual sourceIndividual = EditConfigurationUtils.getIndividual(vreq, vocabSource);
|
||||
//Assuming name will get label
|
||||
vocabLabel = sourceIndividual.getName();
|
||||
}
|
||||
|
||||
|
||||
if(isSKOSConcept) {
|
||||
info.add(new AssociatedConceptInfo(conceptLabel, conceptUri, null, null, SKOSConceptType));
|
||||
|
||||
if(userGenerated) {
|
||||
//if the concept in question is skos - which would imply a user generated concept
|
||||
info.add(new AssociatedConceptInfo(conceptLabel, conceptUri, null, null, SKOSConceptType, null, null));
|
||||
} else {
|
||||
//Get the vocab source and vocab label
|
||||
List<ObjectPropertyStatement> vocabList = conceptIndividual.getObjectPropertyStatements(RDFS.isDefinedBy.getURI());
|
||||
String vocabSource = null;
|
||||
String vocabLabel = null;
|
||||
if(vocabList != null && vocabList.size() > 0) {
|
||||
vocabSource = vocabList.get(0).getObjectURI();
|
||||
Individual sourceIndividual = EditConfigurationUtils.getIndividual(vreq, vocabSource);
|
||||
//Assuming name will get label
|
||||
vocabLabel = sourceIndividual.getName();
|
||||
String conceptSemanticTypeURI = null;
|
||||
String conceptSemanticTypeLabel = null;
|
||||
//Can a concept have multiple semantic types? Currently we are only returning the first one
|
||||
//TODO: Change this into a sparql query that returns all types for the concept that are subclasses of SKOS concepts
|
||||
HashMap<String, String> typeAndLabel = this.getConceptSemanticTypeQueryResults(conceptIndividual.getURI(), ModelAccess.on(vreq).getJenaOntModel());
|
||||
if(typeAndLabel.containsKey("semanticTypeURI")) {
|
||||
conceptSemanticTypeURI = typeAndLabel.get("semanticTypeURI");
|
||||
}
|
||||
info.add(new AssociatedConceptInfo(conceptLabel, conceptUri, vocabSource, vocabLabel, null));
|
||||
if(typeAndLabel.containsKey("semanticTypeLabel")) {
|
||||
conceptSemanticTypeLabel = typeAndLabel.get("semanticTypeLabel");
|
||||
}
|
||||
|
||||
//Assuming this is from an external vocabulary source
|
||||
info.add(new AssociatedConceptInfo(conceptLabel, conceptUri, vocabSource, vocabLabel, null, conceptSemanticTypeURI, conceptSemanticTypeLabel));
|
||||
|
||||
}
|
||||
}
|
||||
return info;
|
||||
}
|
||||
|
||||
private HashMap<String, String> getConceptSemanticTypeQueryResults(String conceptURI, OntModel ontModel) {
|
||||
HashMap<String, String> typeAndLabel = new HashMap<String, String>();
|
||||
String queryStr = "SELECT ?semanticTypeURI ?semanticTypeLabel WHERE { " +
|
||||
"<" + conceptURI + "> <" + RDF.type.getURI() + "> ?semanticTypeURI . " +
|
||||
"?semanticTypeURI <" + RDFS.subClassOf.getURI() + "> <" + this.SKOSConceptType + ">. " +
|
||||
"?semanticTypeURI <" + RDFS.label.getURI() + "> ?semanticTypeLabel ." +
|
||||
"}";
|
||||
QueryExecution qe = null;
|
||||
try{
|
||||
Query query = QueryFactory.create(queryStr);
|
||||
qe = QueryExecutionFactory.create(query, ontModel);
|
||||
ResultSet results = null;
|
||||
results = qe.execSelect();
|
||||
|
||||
while( results.hasNext()){
|
||||
QuerySolution qs = results.nextSolution();
|
||||
if(qs.get("semanticTypeURI") != null) {
|
||||
Resource semanticTypeURI = qs.getResource("semanticTypeURI");
|
||||
log.debug("Semantic Type URI returned " + semanticTypeURI.getURI());
|
||||
typeAndLabel.put("semanticTypeURI", semanticTypeURI.getURI());
|
||||
}
|
||||
if(qs.get("semanticTypeLabel") != null) {
|
||||
Literal semanticTypeLabel = qs.getLiteral("semanticTypeLabel");
|
||||
log.debug("Semantic Type label returned " + semanticTypeLabel.getString());
|
||||
typeAndLabel.put("semanticTypeLabel", semanticTypeLabel.getString());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}catch(Exception ex){
|
||||
throw new Error("Error in executing query string: \n" + queryStr + '\n' + ex.getMessage());
|
||||
}finally{
|
||||
if( qe != null)
|
||||
qe.close();
|
||||
}
|
||||
return typeAndLabel;
|
||||
}
|
||||
|
||||
public class AssociatedConceptInfo {
|
||||
private String conceptLabel;
|
||||
private String conceptURI;
|
||||
private String vocabURI;
|
||||
private String vocabLabel;
|
||||
private String type; //In case of SKOS concept, will have skos concept type
|
||||
public AssociatedConceptInfo(String inputLabel, String inputURI, String inputVocabURI, String inputVocabLabel, String inputType) {
|
||||
private String conceptSemanticTypeURI; //For some services, such as UMLS, we have a semantic type associated
|
||||
private String conceptSemanticTypeLabel;
|
||||
public AssociatedConceptInfo(String inputLabel, String inputURI, String inputVocabURI, String inputVocabLabel, String inputType, String inputConceptSemanticTypeURI, String inputConceptSemanticTypeLabel) {
|
||||
this.conceptLabel = inputLabel;
|
||||
this.conceptURI = inputURI;
|
||||
this.vocabURI = inputVocabURI;
|
||||
this.vocabLabel = inputVocabLabel;
|
||||
this.type = inputType;
|
||||
this.conceptSemanticTypeURI = inputConceptSemanticTypeURI;
|
||||
this.conceptSemanticTypeLabel = inputConceptSemanticTypeLabel;
|
||||
}
|
||||
|
||||
//Getters
|
||||
|
@ -421,6 +525,14 @@ public class AddAssociatedConceptGenerator extends VivoBaseGenerator implements
|
|||
return type;
|
||||
}
|
||||
|
||||
public String getConceptSemanticTypeURI(){
|
||||
return conceptSemanticTypeURI;
|
||||
}
|
||||
|
||||
public String getConceptSemanticTypeLabel(){
|
||||
return conceptSemanticTypeLabel;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class AssociatedConceptInfoComparator implements Comparator<AssociatedConceptInfo>{
|
||||
|
|
|
@ -3,16 +3,30 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.query.Query;
|
||||
import com.hp.hpl.jena.query.QueryExecution;
|
||||
import com.hp.hpl.jena.query.QueryExecutionFactory;
|
||||
import com.hp.hpl.jena.query.QueryFactory;
|
||||
import com.hp.hpl.jena.query.QuerySolution;
|
||||
import com.hp.hpl.jena.query.ResultSet;
|
||||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.vocabulary.OWL;
|
||||
import com.hp.hpl.jena.vocabulary.RDF;
|
||||
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||
import com.hp.hpl.jena.vocabulary.XSD;
|
||||
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.BaseEditSubmissionPreprocessorVTwo;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationUtils;
|
||||
import edu.cornell.mannlib.vitro.webapp.edit.n3editing.VTwo.EditConfigurationVTwo;
|
||||
|
@ -24,24 +38,36 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
|
||||
protected static final Log log = LogFactory
|
||||
.getLog(AddAssociatedConceptsPreprocessor.class.getName());
|
||||
|
||||
//TODO: Check if better way to do this?
|
||||
protected OntModel ontModel = null;
|
||||
// Field names/variables names for n3 - these will have numbers added as
|
||||
// suffix if more than one term
|
||||
private static String conceptNodeBase = "conceptNode";
|
||||
private static String sourceBase = "conceptSource";
|
||||
private static String labelBase = "conceptLabel";
|
||||
private static String conceptSemanticTypeLabelBase = "conceptSemanticTypeLabel";
|
||||
private static String conceptSemanticTypeURIBase = "conceptSemanticTypeURI";
|
||||
//keyed off label variable, specifies which uri variable should be used, useful if same label repeated twice
|
||||
private HashMap<String, String> labelVarToUriVarHash = null;
|
||||
private HashMap<String, List<String>> conceptSemanticTypeURIVarToValueMap = null;
|
||||
//Also storing submission values
|
||||
private static String conceptNodeValues = null;
|
||||
private static String conceptLabelValues = null;
|
||||
private static String conceptSourceValues = null;
|
||||
private static String conceptSemanticTypeLabelValues = null;
|
||||
private static String conceptSemanticTypeURIValues = null;
|
||||
private static MultiValueEditSubmission submission = null;
|
||||
|
||||
// String datatype
|
||||
|
||||
// Will be editing the edit configuration as well as edit submission here
|
||||
|
||||
public AddAssociatedConceptsPreprocessor(EditConfigurationVTwo editConfig) {
|
||||
public AddAssociatedConceptsPreprocessor(EditConfigurationVTwo editConfig, OntModel ontModel) {
|
||||
super(editConfig);
|
||||
this.ontModel = ontModel;
|
||||
this.labelVarToUriVarHash = new HashMap<String, String>();
|
||||
//Saves values of concept type uris
|
||||
this.conceptSemanticTypeURIVarToValueMap = new HashMap<String, List<String>>();
|
||||
}
|
||||
|
||||
public void preprocess(MultiValueEditSubmission inputSubmission) {
|
||||
|
@ -50,10 +76,16 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
// as vocab uri (which is based on thge
|
||||
// For query parameters, check whether CUI
|
||||
copySubmissionValues();
|
||||
|
||||
|
||||
|
||||
if (conceptNodeValues != null) {
|
||||
String[] conceptNodes = convertDelimitedStringToArray(conceptNodeValues);
|
||||
int numberConcepts = conceptNodes.length;
|
||||
//This will put the URI value in scope for the first semantic type label
|
||||
//and generate the rest if need be
|
||||
processConceptSemanticValues();
|
||||
//Also need to see if any broader or narrower uris for the concepts that already exist in the system
|
||||
//and set up the appropriate relationships between this concept and the broader/narrower uri
|
||||
if (numberConcepts > 1) {
|
||||
processConceptNodes(numberConcepts);
|
||||
}
|
||||
|
@ -71,10 +103,26 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
conceptLabelValues = getConceptLabelValues();
|
||||
conceptNodeValues = getConceptNodeValues();
|
||||
conceptSourceValues = getConceptSourceValues();
|
||||
log.debug("concept label values are " + conceptLabelValues);
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
private void processConceptSemanticValues() {
|
||||
conceptSemanticTypeLabelValues = getConceptSemanticTypeLabelValues();
|
||||
conceptSemanticTypeURIValues = getConceptSemanticTypeURIValues();
|
||||
|
||||
//We are first going to handle the single value case and then handle additional values
|
||||
//where the rest of the additional values are handled
|
||||
|
||||
}
|
||||
|
||||
|
||||
//This is for additional concept nodes (i.e. if user selects more than one concept)
|
||||
private void processConceptNodes(int numberConcepts) {
|
||||
//There are no "new" resources b/c the concept nodes are URIs from external vocabularies
|
||||
//New resources for concept semantic type uris
|
||||
addNewResources(numberConcepts);
|
||||
// Add N3Required
|
||||
addN3Required(numberConcepts);
|
||||
//Add N3 Optional as well
|
||||
|
@ -88,6 +136,42 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
|
||||
}
|
||||
|
||||
//This is specifically for concept semantic type URIs which may need to be generated
|
||||
private void addNewResources(int numberConcepts) {
|
||||
// TODO Auto-generated method stub
|
||||
addConceptSemanticTypeURIResources(numberConcepts);
|
||||
}
|
||||
|
||||
private void addConceptSemanticTypeURIResources(int numberConcepts) {
|
||||
//Iterate through the labels and get the corresponding uris
|
||||
HashSet<String> urisToAdd = new HashSet<String>();
|
||||
String[] conceptSemanticTypeLabels= convertDelimitedStringToArray(conceptSemanticTypeLabelValues);
|
||||
//the number of existing values may not match up, or at least existing populated ones
|
||||
//Now we can't determine whether all concepts will have semantic types - at some point what if
|
||||
//we ran a search across all external vocabularies? So we can't compare labels to number of concepts
|
||||
//but we can ensure that it isn't greater than then number of concepts
|
||||
if(conceptSemanticTypeLabels != null && conceptSemanticTypeLabels.length <= numberConcepts) {
|
||||
int i;
|
||||
for(i = 0; i < numberConcepts; i++) {
|
||||
int suffix = i + 1;
|
||||
String conceptSemanticTypeLabelVar = conceptSemanticTypeLabelBase + suffix;
|
||||
if(this.labelVarToUriVarHash.containsKey(conceptSemanticTypeLabelVar)) {
|
||||
String newResourceName = this.labelVarToUriVarHash.get(conceptSemanticTypeLabelVar);
|
||||
if(!urisToAdd.contains(newResourceName)) {
|
||||
urisToAdd.add(newResourceName);
|
||||
editConfiguration.addNewResource(newResourceName, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} else if(conceptSemanticTypeLabels != null && conceptSemanticTypeLabels.length > numberConcepts){
|
||||
log.error("Number of concept semantic type labels is greater than number of concepts");
|
||||
} else{
|
||||
log.error("Concept semantic type labels returned are null");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//This is where the actual values will be submitted as if they were separate input fields
|
||||
//Each field name will correspond to the names of the fileds/uris on form/literals on form
|
||||
//generated here
|
||||
|
@ -98,6 +182,10 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
addConceptNodeInputs(numberConcepts);
|
||||
addConceptSourceInputs(numberConcepts);
|
||||
addConceptLabelInputs(numberConcepts);
|
||||
//for concept semantic type labels and uris where they exist
|
||||
//TODO: Make into single method as URIs depend on labels
|
||||
addConceptSemanticTypeLabelAndURIInputs(numberConcepts);
|
||||
//addConceptSemanticTypeURIInputs(numberConcepts);
|
||||
|
||||
}
|
||||
|
||||
|
@ -167,22 +255,102 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
log.error("Concept labels returned were null");
|
||||
}
|
||||
}
|
||||
|
||||
private void addConceptSemanticTypeLabelAndURIInputs(int numberConcepts) {
|
||||
String[] labels = convertDelimitedStringToArray(conceptSemanticTypeLabelValues);
|
||||
HashSet<String> uniqueLabelValues = new HashSet<String>();
|
||||
if(labels != null && labels.length == numberConcepts) {
|
||||
int i;
|
||||
for(i = 0; i < numberConcepts; i++) {
|
||||
String thisLabel = labels[i];
|
||||
int suffix = i + 1;
|
||||
String labelInputName = conceptSemanticTypeLabelBase + suffix;
|
||||
String[] labelValues = new String[1];
|
||||
labelValues[0] = thisLabel;
|
||||
//TODO: Check if there are no funky typed information also stored
|
||||
//At this point the field should already have been added to edit configuration
|
||||
FieldVTwo labelField = editConfiguration.getField(labelInputName);
|
||||
//TODO: Also check to see whether the label is actually populate or will n3 editing take care of that?
|
||||
if(labelField != null) {
|
||||
submission.addLiteralToForm(editConfiguration, labelField, labelInputName, labelValues);
|
||||
//Associate URI
|
||||
if(!uniqueLabelValues.contains(thisLabel)) {
|
||||
uniqueLabelValues.add(thisLabel);
|
||||
this.addConceptSemanticTypeURIInputForLabel(labelInputName, suffix);
|
||||
}
|
||||
} else {
|
||||
log.error("Corresponding field for " + labelInputName + " was not added to edit configuration");
|
||||
}
|
||||
|
||||
}
|
||||
} else if(labels != null && labels.length != numberConcepts){
|
||||
log.error("Number of concept semantic type labels did not match the number of concepts to be added");
|
||||
} else{
|
||||
log.error("Concept labels returned were null");
|
||||
}
|
||||
}
|
||||
|
||||
private void addConceptSemanticTypeURIInputForLabel(String conceptSemanticTypeLabel, int suffix) {
|
||||
//String[] conceptSemanticTypeURIs= convertDelimitedStringToArray(conceptSemanticTypeURIValues);
|
||||
|
||||
//Get the semantic type URI variable name associated with this label
|
||||
String uriInputName = this.getConceptSemanticTypeURIFieldName(conceptSemanticTypeLabel, suffix);
|
||||
//List<>
|
||||
if(this.conceptSemanticTypeURIVarToValueMap.containsKey(uriInputName)) {
|
||||
List<String> uriVals = this.conceptSemanticTypeURIVarToValueMap.get(uriInputName);
|
||||
String[] uriValuesArray = uriVals.toArray(new String[uriVals.size()]);
|
||||
submission.addUriToForm(editConfiguration, uriInputName, uriValuesArray);
|
||||
}
|
||||
|
||||
|
||||
//the number of existing values may not match up, or at least existing populated ones
|
||||
/*
|
||||
if(conceptSemanticTypeURIs != null && conceptSemanticTypeURIs.length == numberConcepts) {
|
||||
int i;
|
||||
for(i = 0; i < numberConcepts; i++) {
|
||||
int suffix = i + 1;
|
||||
String conceptInputName = conceptSemanticTypeURIBase + suffix;
|
||||
String[] uriValues = new String[1];
|
||||
uriValues[0] = conceptSemanticTypeURIs[i];
|
||||
//Add value for uri to form
|
||||
//TODO: Check if value is empty in which case don't add to submission
|
||||
submission.addUriToForm(editConfiguration, conceptInputName, uriValues);
|
||||
}
|
||||
} else if(conceptSemanticTypeURIs != null && conceptSemanticTypeURIs.length != numberConcepts){
|
||||
log.error("Number of concept nodes did not match the number of concepts to be added");
|
||||
} else{
|
||||
log.error("Concept nodes returned were null");
|
||||
}
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
//Fields
|
||||
|
||||
private void addFields(int numberConcepts) {
|
||||
//Clear out all fields in edit configuration first
|
||||
editConfiguration.setFields(new HashMap<String, FieldVTwo>());
|
||||
int index;
|
||||
HashSet<String> conceptSemanticTypeUris = new HashSet<String>();
|
||||
// First one already included in generator so add additional ones here
|
||||
for (index = 1; index <= numberConcepts; index++) {
|
||||
int suffix = index;
|
||||
String conceptNode = conceptNodeBase + suffix;
|
||||
String label = labelBase + suffix;
|
||||
String source = sourceBase + suffix;
|
||||
String conceptSemanticTypeLabel = conceptSemanticTypeLabelBase + suffix;
|
||||
String conceptSemanticTypeURI = this.getConceptSemanticTypeURIFieldName(conceptSemanticTypeLabel, suffix);
|
||||
|
||||
addConceptNodeField(conceptNode);
|
||||
addLabelField(label);
|
||||
addSourceField(source);
|
||||
//Also add fields for concept semantic type label
|
||||
addConceptSemanticTypeLabelField(conceptSemanticTypeLabel);
|
||||
//and concept semantic type URI
|
||||
if(!conceptSemanticTypeUris.contains(conceptSemanticTypeURI)) {
|
||||
conceptSemanticTypeUris.add(conceptSemanticTypeURI);
|
||||
addConceptSemanticTypeURIField(conceptSemanticTypeURI);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -207,6 +375,25 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
setName(source));
|
||||
|
||||
}
|
||||
|
||||
//TODO: Do we need to check if label is empty string?
|
||||
private void addConceptSemanticTypeLabelField(String label) {
|
||||
if(label != null) {
|
||||
editConfiguration.addField(new FieldVTwo().
|
||||
setName(label).
|
||||
setRangeDatatypeUri(XSD.xstring.toString())
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void addConceptSemanticTypeURIField(String conceptSemanticTypeURI) {
|
||||
if(conceptSemanticTypeURI != null) {
|
||||
editConfiguration.addField(new FieldVTwo().
|
||||
setName(conceptSemanticTypeURI));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//original literals on form: label, uris on form: conceptNode and conceptSource
|
||||
//This will overwrite the original values in the edit configuration
|
||||
|
@ -215,16 +402,24 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
List<String> literalsOnForm = new ArrayList<String>();
|
||||
|
||||
int index;
|
||||
|
||||
HashSet<String> conceptSemanticTypeURIs = new HashSet<String>();
|
||||
// First one already included so add new ones here
|
||||
for (index = 1; index <= numberTerms; index++) {
|
||||
int suffix = index;
|
||||
String conceptNode = conceptNodeBase + suffix;
|
||||
String label = labelBase + suffix;
|
||||
String source = sourceBase + suffix;
|
||||
String conceptSemanticTypeLabel = conceptSemanticTypeLabelBase + suffix;
|
||||
//String conceptSemanticTypeURI = conceptSemanticTypeURIBase + suffix;
|
||||
String conceptSemanticTypeURI = this.getConceptSemanticTypeURIFieldName(conceptSemanticTypeLabel, suffix);
|
||||
urisOnForm.add(conceptNode);
|
||||
urisOnForm.add(source);
|
||||
urisOnForm.add(source);
|
||||
if(!conceptSemanticTypeURIs.contains(conceptSemanticTypeURI)) {
|
||||
conceptSemanticTypeURIs.add(conceptSemanticTypeURI);
|
||||
urisOnForm.add(conceptSemanticTypeURI);
|
||||
}
|
||||
literalsOnForm.add(label);
|
||||
literalsOnForm.add(conceptSemanticTypeLabel);
|
||||
}
|
||||
editConfiguration.setUrisOnform(urisOnForm);
|
||||
editConfiguration.setLiteralsOnForm(literalsOnForm);
|
||||
|
@ -240,8 +435,7 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
List<String> n3Required = new ArrayList<String>();
|
||||
int index;
|
||||
String nodeBase = "?" + conceptNodeBase;
|
||||
String labelVar = "?" + labelBase;
|
||||
String sourceVar = "?" + sourceBase;
|
||||
|
||||
String prefixStr = "@prefix core: <http://vivoweb.org/ontology/core#> .";
|
||||
// First one already included so add new ones here
|
||||
for (index = 1; index <= numberConcepts; index++) {
|
||||
|
@ -254,30 +448,60 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
editConfiguration.setN3Required(n3Required);
|
||||
}
|
||||
//Add n3 optional
|
||||
|
||||
//TODO: Rewrite optional N3
|
||||
private void addN3Optional(int numberConcepts) {
|
||||
List<String> n3Optional = new ArrayList<String>();
|
||||
int index;
|
||||
String nodeBase = "?" + conceptNodeBase;
|
||||
String labelVar = "?" + labelBase;
|
||||
String sourceVar = "?" + sourceBase;
|
||||
String conceptSemanticTypeLabelVar = "?" + conceptSemanticTypeLabelBase;
|
||||
String prefixStr = "@prefix core: <http://vivoweb.org/ontology/core#> .";
|
||||
// First one already included so add new ones here
|
||||
//We already have a label var to uri var setup
|
||||
for (index = 1; index <= numberConcepts; index++) {
|
||||
int suffix = index;
|
||||
String node = nodeBase + suffix;
|
||||
String label = labelVar + suffix;
|
||||
String source = sourceVar + suffix;
|
||||
String conceptSemanticTypeLabel = conceptSemanticTypeLabelVar + suffix;
|
||||
//get the URI appropriate for the concept semantic type label var
|
||||
String conceptSemanticTypeURI = getConceptSemanticTypeURIVar(conceptSemanticTypeLabelBase + suffix, suffix);
|
||||
//onceptSemanticTypeURIVar + suffix;
|
||||
String n3String = prefixStr;
|
||||
n3String += node + " <" + RDFS.label.getURI() + "> " + label + " .\n" +
|
||||
node + " <" + RDFS.isDefinedBy.getURI() + "> " + source + " .";
|
||||
node + " <" + RDFS.isDefinedBy.getURI() + "> " + source + " .";
|
||||
String n3ConceptTypeString = prefixStr;
|
||||
n3ConceptTypeString += node + " <" + RDF.type.getURI() + "> " + conceptSemanticTypeURI + " ." +
|
||||
conceptSemanticTypeURI + " <" + RDFS.label.getURI() + "> " + conceptSemanticTypeLabel + " .\n" +
|
||||
conceptSemanticTypeURI + " <" + RDFS.subClassOf.getURI() + "> <http://www.w3.org/2004/02/skos/core#Concept> .\n" ;
|
||||
|
||||
n3Optional.add(n3String);
|
||||
//adding separately so their resolution does not depend on each other
|
||||
n3Optional.add(n3ConceptTypeString);
|
||||
|
||||
}
|
||||
//Already have n3 required so need to add to that
|
||||
|
||||
editConfiguration.setN3Optional(n3Optional);
|
||||
}
|
||||
|
||||
//get the URI variable that is associated with this concept type URI, which might not be
|
||||
//the same suffix because the same label value might be repeated and we need to use the same URI
|
||||
//representing that concept semantic type
|
||||
private String getConceptSemanticTypeURIVar(String labelVar, int suffix) {
|
||||
// TODO Auto-generated method stub
|
||||
return "?" + this.getConceptSemanticTypeURIFieldName(labelVar, suffix);
|
||||
}
|
||||
|
||||
private String getConceptSemanticTypeURIFieldName(String labelVar, int suffix) {
|
||||
// TODO Auto-generated method stub
|
||||
if(this.labelVarToUriVarHash.containsKey(labelVar)) {
|
||||
return this.labelVarToUriVarHash.get(labelVar);
|
||||
}
|
||||
return this.conceptSemanticTypeURIBase + suffix;
|
||||
}
|
||||
|
||||
private String[] convertDelimitedStringToArray(String inputString) {
|
||||
String[] inputArray = new String[1];
|
||||
if (inputString.indexOf(",") != -1) {
|
||||
|
@ -290,6 +514,7 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
}
|
||||
|
||||
|
||||
//Get values from submission
|
||||
private String getConceptNodeValues() {
|
||||
Map<String, List<String>> urisFromForm = submission.getUrisFromForm();
|
||||
List<String> conceptNodes = urisFromForm.get("conceptNode");
|
||||
|
@ -308,6 +533,131 @@ public class AddAssociatedConceptsPreprocessor extends
|
|||
|
||||
}
|
||||
|
||||
private String getConceptSemanticTypeLabelValues() {
|
||||
Map<String, List<Literal>> literalsFromForm = submission.getLiteralsFromForm();
|
||||
Map<String, List<String>> transformed = EditConfigurationUtils.transformLiteralMap(literalsFromForm);
|
||||
return (String) getFirstElement(transformed.get("conceptSemanticTypeLabel"));
|
||||
}
|
||||
|
||||
//This will either generate or retrieve URIs for the concept semantic type labels if they exist
|
||||
//We will then update the submission to include this
|
||||
private String getConceptSemanticTypeURIValues() {
|
||||
String[] conceptSemanticTypeLabels = convertDelimitedStringToArray(conceptSemanticTypeLabelValues);
|
||||
//keep track of what label values already exist and to which label variables they map
|
||||
HashMap<String, List<Integer>> labelValueToVarSuffix = new HashMap<String, List<Integer>>();
|
||||
int numberLabels = conceptSemanticTypeLabels.length;
|
||||
String pseudoInputString = "";
|
||||
|
||||
//The rest of this code is really only relevant for multiple values, so we could break out the old code above
|
||||
//as we don't need to set up hashes etc. if there is only one concept node being added
|
||||
if(numberLabels == 1) {
|
||||
String label = conceptSemanticTypeLabels[0];
|
||||
String uri = getURIForSemanticTypeLabel(label);
|
||||
if(uri != "") {
|
||||
String[] urisToAdd = new String[1];
|
||||
urisToAdd[0] = uri;
|
||||
pseudoInputString = uri;
|
||||
log.debug("uris to add" + uri);
|
||||
submission.addUriToForm(this.editConfiguration, "conceptSemanticTypeURI", urisToAdd);
|
||||
}
|
||||
|
||||
}
|
||||
//if there is more than one concept node, we may have duplicate semantic types
|
||||
//which will need to be referred to by the same semantic type uri
|
||||
else if (numberLabels > 1){
|
||||
|
||||
for(int i = 0; i < numberLabels; i++) {
|
||||
int suffix = i + 1;
|
||||
String label = conceptSemanticTypeLabels[i];
|
||||
String labelVar = this.conceptSemanticTypeLabelBase + suffix;
|
||||
//if label has not already been encountered, create entry for label value
|
||||
//and list with the label variables that would refer to it
|
||||
//for unique values, the uri variable will be the same as label
|
||||
Integer thisSuffix = new Integer(suffix);
|
||||
if(!labelValueToVarSuffix.containsKey(label)) {
|
||||
labelValueToVarSuffix.put(label, new ArrayList<Integer>());
|
||||
//Add suffix to list if not already there
|
||||
labelValueToVarSuffix.get(label).add(thisSuffix);
|
||||
} else {
|
||||
//in this case, the label already exists, get the very first element in the list
|
||||
//and use that as the uri variable
|
||||
List<Integer> suffixList = labelValueToVarSuffix.get(label);
|
||||
if(suffixList != null && suffixList.size() > 0) {
|
||||
thisSuffix = suffixList.get(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Now add the uri var to the hash mapping label variable to uri variable
|
||||
String uriVar = this.conceptSemanticTypeURIBase + thisSuffix.intValue();
|
||||
this.labelVarToUriVarHash.put(labelVar, uriVar);
|
||||
|
||||
|
||||
//Make or retrieve URI for this label
|
||||
//TODO: Do we create this string with empty inputs ?
|
||||
String uri = getURIForSemanticTypeLabel(label);
|
||||
if(uri != "") {
|
||||
//uri var shouldn't be repeated?
|
||||
if(!this.conceptSemanticTypeURIVarToValueMap.containsKey(uriVar)) {
|
||||
this.conceptSemanticTypeURIVarToValueMap.put(uriVar, new ArrayList<String>());
|
||||
this.conceptSemanticTypeURIVarToValueMap.get(uriVar).add(uri);
|
||||
}
|
||||
}
|
||||
if(i != 0) {
|
||||
pseudoInputString += ",";
|
||||
}
|
||||
pseudoInputString += uri;
|
||||
|
||||
}
|
||||
|
||||
//Add this string to the uris for the form
|
||||
String[] urisToAdd = new String[1];
|
||||
urisToAdd[0] = pseudoInputString;
|
||||
log.debug("uris to add" + pseudoInputString);
|
||||
submission.addUriToForm(this.editConfiguration, "conceptSemanticTypeURI", urisToAdd);
|
||||
|
||||
}
|
||||
return pseudoInputString;
|
||||
}
|
||||
|
||||
private String getURIForSemanticTypeLabel(String label) {
|
||||
String existingURI = this.getExistingSemanticTypeURI(label);
|
||||
if(existingURI != null) {
|
||||
return existingURI;
|
||||
}
|
||||
//if we leave this as null, we should be able to generate a new resource
|
||||
//empty string because there may be more than one value returned for labels
|
||||
else return "";
|
||||
|
||||
}
|
||||
|
||||
private String getExistingSemanticTypeURI(String label) {
|
||||
String queryStr = "SELECT ?semanticType WHERE { ?semanticType <" + RDF.type.getURI() + "> <" + OWL.Class.getURI() + "> . " +
|
||||
"?semanticType <" + RDFS.label.getURI() + "> \"" + label + "\"^^<http://www.w3.org/2001/XMLSchema#string> . }";
|
||||
QueryExecution qe = null;
|
||||
try{
|
||||
Query query = QueryFactory.create(queryStr);
|
||||
qe = QueryExecutionFactory.create(query, this.ontModel);
|
||||
ResultSet results = null;
|
||||
results = qe.execSelect();
|
||||
|
||||
while( results.hasNext()){
|
||||
QuerySolution qs = results.nextSolution();
|
||||
if(qs.get("semanticType") != null) {
|
||||
Resource semanticTypeURI = qs.getResource("semanticType");
|
||||
log.debug("Semantic Type URI returned " + semanticTypeURI.getURI());
|
||||
return semanticTypeURI.getURI();
|
||||
}
|
||||
}
|
||||
}catch(Exception ex){
|
||||
throw new Error("Error in executing query string: \n" + queryStr + '\n' + ex.getMessage());
|
||||
}finally{
|
||||
if( qe != null)
|
||||
qe.close();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Object getFirstElement(List inputList) {
|
||||
if(inputList == null || inputList.size() == 0)
|
||||
return null;
|
||||
|
|
|
@ -0,0 +1,125 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.edit.n3editing.configuration.preprocessors;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
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.QueryExecutionFactory;
|
||||
import com.hp.hpl.jena.query.QueryFactory;
|
||||
import com.hp.hpl.jena.query.QuerySolutionMap;
|
||||
import com.hp.hpl.jena.query.Syntax;
|
||||
import com.hp.hpl.jena.rdf.model.Literal;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
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.Statement;
|
||||
import com.hp.hpl.jena.shared.Lock;
|
||||
import com.hp.hpl.jena.vocabulary.RDF;
|
||||
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.VitroVocabulary;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.DatasetWrapper;
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.DatasetWrapperFactory;
|
||||
|
||||
//We are representing semantic types from the UMLS Semantic Network as OWL Classes
|
||||
//and this preprocessor will add the appropriate class information to the TBox
|
||||
|
||||
public class ConceptSemanticTypesPreprocessor implements ModelChangePreprocessor {
|
||||
|
||||
private static String VIVOCore = "http://vivoweb.org/ontology/core#";
|
||||
private static String SKOSConceptType = "http://www.w3.org/2004/02/skos/core#Concept";
|
||||
private Log log = LogFactory.getLog(ConceptSemanticTypesPreprocessor.class);
|
||||
|
||||
private OntModel toUpdateModel = null;
|
||||
|
||||
//Custom constructor
|
||||
public ConceptSemanticTypesPreprocessor(OntModel updateModel) {
|
||||
this.toUpdateModel = updateModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preprocess(Model retractionsModel, Model additionsModel,
|
||||
HttpServletRequest request) {
|
||||
//Run a construct query against the additions model
|
||||
String prefixes = "PREFIX rdfs:<" + RDFS.getURI() + "> " +
|
||||
"PREFIX owl:<http://www.w3.org/2002/07/owl#> " +
|
||||
"PREFIX rdf:<" + RDF.getURI() + ">" +
|
||||
"PREFIX skos:<http://www.w3.org/2004/02/skos/core#>";
|
||||
String constructQuery = prefixes + " CONSTRUCT { " +
|
||||
"?semanticType rdf:type owl:Class. " +
|
||||
"?semanticType rdfs:subClassOf skos:Concept . " +
|
||||
"?semanticType rdfs:label ?label. " +
|
||||
"} WHERE { " +
|
||||
"?concept rdf:type ?semanticType. " +
|
||||
"?semanticType rdfs:label ?label . " +
|
||||
"?semanticType rdfs:subClassOf skos:Concept . " +
|
||||
"}";
|
||||
|
||||
//Execute construct query
|
||||
Model constructedModel = ModelFactory.createDefaultModel();
|
||||
|
||||
|
||||
log.debug("CONSTRUCT query string " + constructQuery);
|
||||
|
||||
Query query = null;
|
||||
try {
|
||||
query = QueryFactory.create(constructQuery, Syntax.syntaxARQ);
|
||||
} catch(Throwable th){
|
||||
log.error("Could not create CONSTRUCT SPARQL query for query " +
|
||||
"string. " + th.getMessage());
|
||||
log.error(constructQuery);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
additionsModel.getLock().enterCriticalSection(Lock.READ);
|
||||
QueryExecution qe = null;
|
||||
try {
|
||||
qe = QueryExecutionFactory.create(
|
||||
query, additionsModel);
|
||||
qe.execConstruct(constructedModel);
|
||||
} catch (Exception e) {
|
||||
log.error("Error getting constructed model for query string " + constructQuery);
|
||||
} finally {
|
||||
if (qe != null) {
|
||||
qe.close();
|
||||
}
|
||||
additionsModel.getLock().leaveCriticalSection();
|
||||
}
|
||||
|
||||
//Add constructed model to the designated update model
|
||||
toUpdateModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
toUpdateModel.add(constructedModel);
|
||||
} catch (Exception e) {
|
||||
log.error("Error adding statements to update model for " + constructQuery);
|
||||
} finally {
|
||||
toUpdateModel.leaveCriticalSection();
|
||||
}
|
||||
|
||||
//Take this constructed model and remove from the additions model
|
||||
additionsModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
additionsModel.remove(constructedModel.listStatements().toList());
|
||||
} catch (Exception e) {
|
||||
log.error("Error removing statements from additions model for " + constructQuery);
|
||||
} finally {
|
||||
additionsModel.leaveCriticalSection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
|
@ -20,9 +20,9 @@ import org.apache.commons.logging.LogFactory;
|
|||
import edu.cornell.mannlib.semservices.bo.Concept;
|
||||
import edu.cornell.mannlib.semservices.bo.ConceptInfo;
|
||||
import edu.cornell.mannlib.semservices.bo.SemanticServicesError;
|
||||
import edu.cornell.mannlib.semservices.view.BeanToJsonSerializer;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.BeanToJsonSerializer;
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.ConceptSearchService.ConceptSearchServiceUtils;
|
||||
|
||||
public class ConceptSearchServlet extends VitroHttpServlet {
|
||||
|
|
|
@ -39,7 +39,7 @@ public class ConceptSearchServiceUtils {
|
|||
HashMap<String, VocabSourceDescription> map = new HashMap<String, VocabSourceDescription>();
|
||||
map.put(UMLSVocabSource, new VocabSourceDescription("UMLS", UMLSVocabSource, "http://www.nlm.nih.gov/research/umls/", "Unified Medical Language System"));
|
||||
//Commenting out agrovoc for now until implementation is updated
|
||||
// map.put(AgrovocVocabSource, new VocabSourceDescription("AGROVOC", AgrovocVocabSource, "http://www.fao.org/agrovoc/", "Agricultural Vocabulary"));
|
||||
map.put(AgrovocVocabSource, new VocabSourceDescription("AGROVOC", AgrovocVocabSource, "http://www.fao.org/agrovoc/", "Agricultural Vocabulary"));
|
||||
map.put(GemetVocabSource, new VocabSourceDescription("GEMET", GemetVocabSource, "http://www.eionet.europa.eu/gemet", "GEneral Multilingual Environmental Thesaurus"));
|
||||
return map;
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ public class ConceptSearchServiceUtils {
|
|||
|
||||
//Get search
|
||||
String searchTerm = getSearchTerm(vreq);
|
||||
List<Concept> conceptResults = conceptServiceClass.processResults(searchTerm);
|
||||
List<Concept> conceptResults = conceptServiceClass.getConcepts(searchTerm);
|
||||
return conceptResults;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue