updates to include error handling in external concept services

This commit is contained in:
hjkhjk54 2011-12-02 22:03:52 +00:00
parent 2fb20f1efe
commit f9d402bff6
11 changed files with 184 additions and 32 deletions

View file

@ -142,16 +142,17 @@ var addConceptForm = {
} }
var vocabSourceValue = checkedVocabSource.val(); var vocabSourceValue = checkedVocabSource.val();
var dataServiceUrl = addConceptForm.dataServiceUrl + "?searchTerm=" + encodeURIComponent(searchValue) + "&source=" + encodeURIComponent(vocabSourceValue); var dataServiceUrl = addConceptForm.dataServiceUrl + "?searchTerm=" + encodeURIComponent(searchValue) + "&source=" + encodeURIComponent(vocabSourceValue);
//This should return an object including the concept list or any errors if there are any
$.getJSON(dataServiceUrl, function(results) { $.getJSON(dataServiceUrl, function(results) {
var htmlAdd = ""; var htmlAdd = "";
var vocabUnavailable = "<p>The vocabulary service is unavailable. Please try again later.</p>";
if ( results== null || results.array == null || results.array.length == 0 ) { if ( results== null || results.semanticServicesError != null || results.conceptList == null) {
htmlAdd = "<p>No search results found.</p>"; htmlAdd = vocabUnavailable;
} else { }
else {
//array is an array of objects representing concept information //array is an array of objects representing concept information
//loop through and find all the best matches //loop through and find all the best matches
var bestMatchResults = addConceptForm.parseResults(results.conceptList);
var bestMatchResults = addConceptForm.parseResults(results.array);
var numberMatches = bestMatchResults.length; var numberMatches = bestMatchResults.length;
var i; var i;
//For each result, display //For each result, display
@ -170,7 +171,7 @@ var addConceptForm = {
} }
htmlAdd+= "</ul>"; htmlAdd+= "</ul>";
} else { } else {
htmlAdd+= "<p>No search results found.</p>"; htmlAdd+= "<p>No search results were found.</p>";
} }
} }

View file

@ -231,7 +231,8 @@ var customForm = {
dataType: 'json', dataType: 'json',
data: { data: {
term: request.term, term: request.term,
type: customForm.acType type: customForm.acType,
multipleTypes:(customForm.acMultipleTypes == undefined || customForm.acMultipleTypes == null)? null: customForm.acMultipleTypes
}, },
complete: function(xhr, status) { complete: function(xhr, status) {
// Not sure why, but we need an explicit json parse here. // Not sure why, but we need an explicit json parse here.

View file

@ -0,0 +1,34 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.semservices.bo;
import java.util.List;
public class ConceptInfo extends SemanticServicesInfoBase {
private List<Concept> conceptList;
/**
*
*/
public ConceptInfo() {
super();
}
/**
* @return the vivoDepartmentList
*/
public List<Concept> getConceptList() {
return conceptList;
}
/**
* @param vivoDepartmentList the vivoDepartmentList to set
*/
public void setConceptList(List<Concept> inputConceptList) {
this.conceptList = inputConceptList;
}
}

View file

@ -0,0 +1,75 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.semservices.bo;
public class SemanticServicesError {
private String message;
private String exception;
private String severity;
/**
*
*/
public SemanticServicesError() {
super();
}
/**
* @param exception
* @param message
* @param severity
*/
public SemanticServicesError(String exception, String message, String severity) {
super();
this.exception = exception;
this.message = message;
this.severity = severity;
}
/**
* @return the message
*/
public String getMessage() {
return message;
}
/**
* @param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* @return the exception
*/
public String getException() {
return exception;
}
/**
* @param exception the exception to set
*/
public void setException(String exception) {
this.exception = exception;
}
/**
* @return the severity
*/
public String getSeverity() {
return severity;
}
/**
* @param severity the severity to set
*/
public void setSeverity(String severity) {
this.severity = severity;
}
}

View file

@ -0,0 +1,29 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.semservices.bo;
public class SemanticServicesInfoBase {
private SemanticServicesError semanticServicesError;
/**
*
*/
public SemanticServicesInfoBase() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the semanticServicesError
*/
public SemanticServicesError getSemanticServicesError() {
return semanticServicesError;
}
/**
* @param semanticServicesError the semanticServicesError to set
*/
public void setSemanticServicesError(SemanticServicesError semanticServicesError) {
this.semanticServicesError = semanticServicesError;
}
}

View file

@ -8,6 +8,6 @@ import edu.cornell.mannlib.semservices.bo.Concept;
public interface ExternalConceptService { public interface ExternalConceptService {
// this is the only method that needs to be exposed // this is the only method that needs to be exposed
List<Concept> processResults(String term); List<Concept> processResults(String term) throws Exception;
} }

View file

@ -35,7 +35,7 @@ public class AgrovocService implements ExternalConceptService {
protected final Log logger = LogFactory.getLog(getClass()); 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://www.fao.org/webservices/AgrovocWS";
public List<Concept> processResults(String term) { public List<Concept> processResults(String term) throws Exception {
List<Concept> conceptList = new ArrayList<Concept>(); List<Concept> conceptList = new ArrayList<Concept>();
String termcode; String termcode;
@ -43,7 +43,7 @@ public class AgrovocService implements ExternalConceptService {
termcode = getTermcodeByTerm(term); termcode = getTermcodeByTerm(term);
} catch (Exception e1) { } catch (Exception e1) {
logger.error("Could not get termcode from service", e1); logger.error("Could not get termcode from service", e1);
return null; throw e1;
} }
String format = "SKOS"; String format = "SKOS";
@ -89,9 +89,6 @@ public class AgrovocService implements ExternalConceptService {
e.printStackTrace(); e.printStackTrace();
} }
return conceptList; return conceptList;
//JSONObject jsonObject = null;
//jsonObject = BeanToJsonSerializer.serializeToJsonObject(conceptList);
//return jsonObject.toString();
} }

View file

@ -49,7 +49,7 @@ public class GemetService implements ExternalConceptService {
private final String exampleUri = "http://www.w3.org/2004/02/skos/core%23example"; 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 acronymLabelUri = "http://www.w3.org/2004/02/skos/core%23acronymLabel";
public List<Concept> processResults(String term) { public List<Concept> processResults(String term) throws Exception {
List<Concept> conceptList = new ArrayList<Concept>(); List<Concept> conceptList = new ArrayList<Concept>();
String results = getConceptsMatchingKeyword(term); String results = getConceptsMatchingKeyword(term);
conceptList = processOutput(results); conceptList = processOutput(results);
@ -121,7 +121,7 @@ public class GemetService implements ExternalConceptService {
} }
protected String getAvailableLangs(String concept_uri) { protected String getAvailableLangs(String concept_uri) throws Exception {
String result = new String(); String result = new String();
String serviceUrl = GemetWS_address + "getAvailableLanguages" + String serviceUrl = GemetWS_address + "getAvailableLanguages" +
"?concept_uri=" + concept_uri; "?concept_uri=" + concept_uri;
@ -129,7 +129,7 @@ public class GemetService implements ExternalConceptService {
return result; return result;
} }
protected String getConcept(String concept_uri) { protected String getConcept(String concept_uri) throws Exception {
String result = new String(); String result = new String();
String serviceUrl = GemetWS_address + "getConcept" + String serviceUrl = GemetWS_address + "getConcept" +
"?concept_uri=" + concept_uri + "?concept_uri=" + concept_uri +
@ -137,7 +137,7 @@ public class GemetService implements ExternalConceptService {
result = getGemetResults(serviceUrl); result = getGemetResults(serviceUrl);
return result; return result;
} }
protected String getAllTranslationsForConcept(String concept_uri, String property) { protected String getAllTranslationsForConcept(String concept_uri, String property) throws Exception {
String result = new String(); String result = new String();
String property_uri = new String(); String property_uri = new String();
if (property.equals("definition")) { if (property.equals("definition")) {
@ -163,7 +163,7 @@ public class GemetService implements ExternalConceptService {
} }
protected String getRelatedConcepts(String concept_uri, String relation) { protected String getRelatedConcepts(String concept_uri, String relation) throws Exception {
String result = new String(); String result = new String();
String relation_uri = new String(); String relation_uri = new String();
if (relation.equals("broader")) { if (relation.equals("broader")) {
@ -183,7 +183,7 @@ public class GemetService implements ExternalConceptService {
protected String getConceptsMatchingKeyword(String keyword) { protected String getConceptsMatchingKeyword(String keyword) throws Exception {
String result = new String(); String result = new String();
String serviceUrl = GemetWS_address + "getConceptsMatchingKeyword" + String serviceUrl = GemetWS_address + "getConceptsMatchingKeyword" +
"?keyword=" + keyword + "?keyword=" + keyword +
@ -196,7 +196,7 @@ public class GemetService implements ExternalConceptService {
} }
protected String getGemetResults(String url) { protected String getGemetResults(String url) throws Exception {
String results = new String(); String results = new String();
//System.out.println("url: "+url); //System.out.println("url: "+url);
try { try {
@ -215,7 +215,7 @@ public class GemetService implements ExternalConceptService {
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error occurred in servlet", ex); logger.error("error occurred in servlet", ex);
return null; throw ex;
} }
return results; return results;
} }

View file

@ -29,7 +29,7 @@ public class UMLSService implements ExternalConceptService {
private static final String submissionUrl = "http://link.informatics.stonybrook.edu/MeaningLookup/MlServiceServlet?"; 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 baseUri = "http://link.informatics.stonybrook.edu/umls/CUI/";
public List<Concept> processResults(String term) { public List<Concept> processResults(String term) throws Exception{
String results = null; String results = null;
String dataUrl = submissionUrl + "textToProcess=" String dataUrl = submissionUrl + "textToProcess="
+ URLEncoder.encode(term) + "&format=json"; + URLEncoder.encode(term) + "&format=json";
@ -50,9 +50,8 @@ public class UMLSService implements ExternalConceptService {
} catch (Exception ex) { } catch (Exception ex) {
logger.error("error occurred in servlet", ex); logger.error("error occurred in servlet", ex);
return null; throw ex;
} }
//System.out.println("results before processing: "+results);
List<Concept> conceptList = processOutput(results); List<Concept> conceptList = processOutput(results);
return conceptList; return conceptList;
} }

View file

@ -18,6 +18,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.semservices.bo.Concept; 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.vitro.webapp.controller.VitroHttpServlet; import edu.cornell.mannlib.vitro.webapp.controller.VitroHttpServlet;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; 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.BeanToJsonSerializer;
@ -40,9 +42,23 @@ public class ConceptSearchServlet extends VitroHttpServlet {
try{ try{
ServletContext ctx = vreq.getSession().getServletContext(); ServletContext ctx = vreq.getSession().getServletContext();
//Captures both concept list and any errors if they exist
ConceptInfo conceptInfo = new ConceptInfo();
conceptInfo.setSemanticServicesError(null);
//Json output should be written out //Json output should be written out
List<Concept> results = ConceptSearchServiceUtils.getSearchResults(ctx, vreq); List<Concept> results = null;
String json = renderJson(results); try {
results = ConceptSearchServiceUtils.getSearchResults(ctx, vreq);
}
catch (Exception ex) {
SemanticServicesError semanticServicesError = new SemanticServicesError(
"Exception encountered ", ex.getMessage(), "fatal");
log.error("An error occurred retrieving search results");
conceptInfo.setSemanticServicesError(semanticServicesError);
}
conceptInfo.setConceptList(results);
String json = renderJson(conceptInfo);
json = StringUtils.replaceChars(json, "\r\t\n", ""); json = StringUtils.replaceChars(json, "\r\t\n", "");
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
resp.setContentType("application/json"); resp.setContentType("application/json");
@ -54,10 +70,10 @@ public class ConceptSearchServlet extends VitroHttpServlet {
} }
} }
protected String renderJson(List<Concept> conceptList) { protected String renderJson(ConceptInfo conceptInfo) {
JSONObject jsonObject = null; JSONObject jsonObject = null;
jsonObject = BeanToJsonSerializer.serializeToJsonObject(conceptList); jsonObject = BeanToJsonSerializer.serializeToJsonObject(conceptInfo);
log.debug(jsonObject.toString()); log.debug(jsonObject.toString());
return jsonObject.toString(); return jsonObject.toString();
} }

View file

@ -39,7 +39,7 @@ public class ConceptSearchServiceUtils {
HashMap<String, VocabSourceDescription> map = new HashMap<String, VocabSourceDescription>(); 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")); 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 //Commenting out agrovoc for now until implementation is updated
// map.put(AgrovocVocabSource, new VocabSourceDescription("AGROVOC", AgrovocVocabSource, "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")); map.put(GemetVocabSource, new VocabSourceDescription("GEMET", GemetVocabSource, "http://www.eionet.europa.eu/gemet", "GEneral Multilingual Environmental Thesaurus"));
return map; return map;
} }
@ -56,7 +56,7 @@ public class ConceptSearchServiceUtils {
return map; return map;
} }
public static List<Concept> getSearchResults(ServletContext context, VitroRequest vreq) { public static List<Concept> getSearchResults(ServletContext context, VitroRequest vreq) throws Exception {
String searchServiceName = getSearchServiceUri(vreq); String searchServiceName = getSearchServiceUri(vreq);
String searchServiceClassName = getConceptSearchServiceClassName(searchServiceName); String searchServiceClassName = getConceptSearchServiceClassName(searchServiceName);