From 830456ac9f99d6fd496e0c54ec5f5ad825ade6d7 Mon Sep 17 00:00:00 2001 From: runeliza Date: Wed, 15 Jun 2011 19:00:56 +0000 Subject: [PATCH] More cleanup. --- .../controller/JSONReconcileServlet.java | 85 +++++-------------- 1 file changed, 22 insertions(+), 63 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONReconcileServlet.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONReconcileServlet.java index c3ea6360e..fb863888b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONReconcileServlet.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/JSONReconcileServlet.java @@ -15,6 +15,7 @@ import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.lucene.analysis.Analyzer; @@ -53,7 +54,8 @@ import edu.cornell.mannlib.vitro.webapp.search.lucene.LuceneSetup; */ public class JSONReconcileServlet extends VitroHttpServlet { - private static String QUERY_PARAMETER_NAME = "term"; + private static final long serialVersionUID = 1L; + private static String QUERY_PARAMETER_NAME = "term"; public static final int MAX_QUERY_LENGTH = 500; private static final Log log = LogFactory.getLog(JSONReconcileServlet.class.getName()); @@ -70,14 +72,14 @@ public class JSONReconcileServlet extends VitroHttpServlet { super.doGet(req, resp); resp.setContentType("application/json"); VitroRequest vreq = new VitroRequest(req); -// System.out.println("vreq"); -// System.out.println(vreq.getWebappDaoFactory()); - + //log.debug("vreq"); + //log.debug(vreq.getWebappDaoFactory()); + try { if (vreq.getParameter("query") != null || vreq.getParameter("queries") != null) { JSONObject qJson = getResult(vreq, req, resp); -// System.out.println("result: " + qJson.toString()); + log.debug("result: " + qJson.toString()); String responseStr = (vreq.getParameter("callback") == null) ? qJson .toString() : vreq.getParameter("callback") + "(" + qJson.toString() + ")"; @@ -85,23 +87,14 @@ public class JSONReconcileServlet extends VitroHttpServlet { out.print(responseStr); } else { // metadata String defaultNamespace = null; - //String defaultTypeList = null; + String defaultTypeList = null; String serverName = null; int serverPort = req.getServerPort(); if (vreq.getWebappDaoFactory() != null) { defaultNamespace = vreq.getWebappDaoFactory().getDefaultNamespace(); } - String defaultTypeList = - ConfigurationProperties.getBean(getServletContext()).getProperty("Vitro.reconcile.defaultTypeList"); - - //defaultTypeList = - // "http://vivoweb.org/ontology/core#Course, Course; " + - // "http://vivoweb.org/ontology/core#Grant, Grant; " + - // "http://aims.fao.org/aos/geopolitical.owl, Location; " + - // "http://xmlns.com/foaf/0.1/Organization, Organization; " + - // "http://xmlns.com/foaf/0.1/Person, Person; " + - // "http://purl.org/ontology/bibo/Article, Publication"; + defaultTypeList = ConfigurationProperties.getBean(req).getProperty("Vitro.reconcile.defaultTypeList"); serverName = req.getServerName(); JSONObject metaJson = getMetadata(req, resp, defaultNamespace, defaultTypeList, serverName, serverPort); String callbackStr = (vreq.getParameter("callback") == null) ? "" @@ -133,8 +126,7 @@ public class JSONReconcileServlet extends VitroHttpServlet { // "q2":{"query":"Dina","type":"http://xmlns.com/foaf/0.1/Person","type_strict":"should"}} String qStr = (String) qObj; queries.add(qStr); -// System.out.println(); -// System.out.println("query: " + qStr + "\n"); + log.debug("\nquery: " + qStr + "\n"); } try { @@ -167,7 +159,7 @@ public class JSONReconcileServlet extends VitroHttpServlet { } } } catch (JSONException ex) { - System.err.println("JSONReconcileServlet JSONException: " + ex); + log.error("JSONException: " + ex); throw new ServletException("JSONReconcileServlet JSONException: " + ex); } @@ -302,7 +294,8 @@ public class JSONReconcileServlet extends VitroHttpServlet { Individual ind = iDao.getIndividualByURI(uri); if (ind != null) { String name = ind.getName(); - String modUri = uri.replace("#", "%23"); // encodes # to %23 + // encode # to %23 + String modUri = uri.replace("#", "%23"); resultJson.put("id", modUri); resultJson.put("name", name); } @@ -335,15 +328,15 @@ public class JSONReconcileServlet extends VitroHttpServlet { } } catch (JSONException ex) { - System.err.println("JSONReconcileServlet JSONException: " + ex); + log.error("JSONException: " + ex); throw new ServletException("JSONReconcileServlet JSONException: " + ex); } catch (SearchException ex) { - System.err.println("JSONReconcileServlet SearchException: " + ex); + log.error("SearchException: " + ex); throw new ServletException("JSONReconcileServlet SearchException: " + ex); } catch (IOException ex) { - System.err.println("JSONReconcileServlet IOException: " + ex); + log.error("IOException: " + ex); throw new ServletException("JSONReconcileServlet IOException: " + ex); } @@ -362,30 +355,12 @@ public class JSONReconcileServlet extends VitroHttpServlet { private Query makeReconcileNameQuery(String querystr, Analyzer analyzer, HttpServletRequest request) { - /* Original code - String tokenizeParam = (String) request.getParameter("tokenize"); - boolean tokenize = "true".equals(tokenizeParam); - - // Note: Stemming is only relevant if we are tokenizing: an untokenized name - // query will not be stemmed. So we don't look at the stem parameter until we get to - // makeTokenizedNameQuery(). - if (tokenize) { - return makeTokenizedNameQuery(querystr, analyzer, request); - } else { - return makeUntokenizedNameQuery(querystr); - } - */ - - // modified code for reconciliation service - request.setAttribute("stem", true); return makeTokenizedNameQuery(querystr, analyzer, request); } private Query makeTokenizedNameQuery(String querystr, Analyzer analyzer, HttpServletRequest request) { - - String stemParam = (String) request.getParameter("stem"); - boolean stem = "true".equals(stemParam); - String termName = stem ? VitroLuceneTermNames.NAME_STEMMED : VitroLuceneTermNames.NAME_UNSTEMMED; + + String termName = VitroLuceneTermNames.NAME_STEMMED; BooleanQuery boolQuery = new BooleanQuery(); @@ -415,30 +390,16 @@ public class JSONReconcileServlet extends VitroHttpServlet { } catch (ParseException e) { log.warn(e, e); } - - + return boolQuery; } - - private Query makeUntokenizedNameQuery(String querystr) { - - querystr = querystr.toLowerCase(); - String termName = VitroLuceneTermNames.NAME_LOWERCASE; - BooleanQuery query = new BooleanQuery(); - log.debug("Adding wildcard query on unanalyzed name"); - query.add( - new WildcardQuery(new Term(termName, querystr + "*")), - BooleanClause.Occur.MUST); - - return query; - } private QueryParser getQueryParser(String searchField, Analyzer analyzer){ // searchField indicates which field to search against when there is no term // indicated in the query string. // The analyzer is needed so that we use the same analyzer on the search queries as // was used on the text that was indexed. - QueryParser qp = new QueryParser(searchField,analyzer); + QueryParser qp = new QueryParser(Version.LUCENE_29, searchField,analyzer); //this sets the query parser to AND all of the query terms it finds. qp.setDefaultOperator(QueryParser.AND_OPERATOR); return qp; @@ -458,10 +419,8 @@ public class JSONReconcileServlet extends VitroHttpServlet { "query length is " + MAX_QUERY_LENGTH ); return null; } - - + query = makeReconcileNameQuery(querystr, analyzer, request); - // filter by type if (typeParam != null) { @@ -479,7 +438,7 @@ public class JSONReconcileServlet extends VitroHttpServlet { while (it.hasNext()) { String[] pvPair = it.next(); Query extraQuery = makeReconcileNameQuery(pvPair[1], analyzer, request); - if (!"".equals(pvPair[0]) && pvPair[0] != null) { + if ( ! StringUtils.isEmpty(pvPair[0]) ) { BooleanQuery boolQuery = new BooleanQuery(); boolQuery.add(new TermQuery(new Term( VitroLuceneTermNames.RDFTYPE, pvPair[0])),