From 589383f15bb2fe0011f7ffab687abbfe3c35edc0 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Fri, 27 Aug 2010 16:52:26 +0000 Subject: [PATCH] Handle search error message from Freemarker controller and template. --- .../FreemarkerPagedSearchController.java | 94 ++++++++----------- .../body/{ => search}/pagedSearchResults.ftl | 2 +- .../freemarker/body/search/searchError.ftl | 9 ++ 3 files changed, 50 insertions(+), 55 deletions(-) rename webapp/web/templates/freemarker/body/{ => search}/pagedSearchResults.ftl (96%) create mode 100644 webapp/web/templates/freemarker/body/search/searchError.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java index 83e9f4d4a..bfa0aa58e 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/controller/FreemarkerPagedSearchController.java @@ -119,8 +119,7 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple if( vreq.getWebappDaoFactory() == null || vreq.getWebappDaoFactory().getIndividualDao() == null ){ log.error("makeUsableBeans() could not get IndividualDao "); - //doSearchError(request, response, "Could not access Model", portalFlag); - //return; + return doSearchError("Could not access Model.", config); } IndividualDao iDao = vreq.getWebappDaoFactory().getIndividualDao(); VClassGroupDao grpDao = vreq.getWebappDaoFactory().getVClassGroupDao(); @@ -155,10 +154,9 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple Analyzer analyzer = getAnalyzer(getServletContext()); Query query = getQuery(vreq, portalFlag, analyzer, indexDir, qtxt); log.debug("query for '" + qtxt +"' is " + query.toString()); - + if (query == null ) { - //doNoQuery(vreq, response); - //return; + return doNoQuery(config, portal); } IndexSearcher searcherForRequest = getIndexSearcher(indexDir); @@ -175,23 +173,22 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple }catch (Exception ex){ log.error(ex); String msg = makeBadSearchMessage(qtxt,ex.getMessage()); - if(msg == null ) msg = "

The search request contained errors.

"; - //doFailedSearch(vreq, response, msg, qtxt); - //return; + if (msg == null) { + msg = "The search request contained errors."; + } + return doFailedSearch(msg, qtxt, config); } } if( topDocs == null || topDocs.scoreDocs == null){ log.error("topDocs for a search was null"); - String msg = "

The search request contained errors.

"; - //doFailedSearch(request, response, msg, qtxt); - //return; + String msg = "The search request contained errors."; + return doFailedSearch(msg, qtxt, config); } int hitsLength = topDocs.scoreDocs.length; if ( hitsLength < 1 ){ - //doFailedSearch(request, response, NORESULT_MSG, qtxt); - //return; + return doNoHits(qtxt, config); } log.debug("found "+hitsLength+" hits"); @@ -273,7 +270,7 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple if ( !StringUtils.isEmpty(classGroupParam) ) { VClassGroup grp = grpDao.getGroupByURI(classGroupParam); if( grp != null && grp.getPublicName() != null ) - body.put("classgroupName", grp.getPublicName()); + body.put("classGroupName", grp.getPublicName()); } if ( !StringUtils.isEmpty(typeParam) ) { @@ -285,9 +282,8 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple body.put("pagingLinks", getPagingLinks(startIndex, hitsPerPage, hitsLength, maxHitSize, vreq.getServletPath(), pagingLinkParams)); } catch (Throwable e) { - log.error(e, e); - //doSearchError(request, response, e.getMessage(), null); - //return; + log.error(e, e); + return doSearchError(e.getMessage(), config); } return mergeBodyToTemplate("pagedSearchResults.ftl", body, config); @@ -742,39 +738,37 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple log.debug("could not hightlight for entity " + ent.getURI(),th); } } + + private String doSearchError(String message, Configuration config) { + Map body = new HashMap(); + body.put("message", "Search failed: " + message); + return mergeBodyToTemplate("searchError.ftl", body, config); + } - private void doNoQuery(HttpServletRequest request, - HttpServletResponse response) - throws ServletException, IOException { - Portal portal = (new VitroRequest(request)).getPortal(); - request.setAttribute("title", "Search "+portal.getAppName()); - request.setAttribute("bodyJsp", Controllers.SEARCH_FORM_JSP); - - RequestDispatcher rd = request - .getRequestDispatcher(Controllers.BASIC_JSP); - rd.forward(request, response); + private String doNoQuery(Configuration config, Portal portal) { + Map body = new HashMap(); + body.put("title", "Search " + portal.getAppName()); + body.put("message", "No query entered."); + return mergeBodyToTemplate("searchError.ftl", body, config); + } + + private String doFailedSearch(String message, String querytext, Configuration config) { + Map body = new HashMap(); + body.put("title", "Search for '" + querytext + "'"); + if ( StringUtils.isEmpty(message) ) { + message = "Search failed."; + } + body.put("message", message); + return mergeBodyToTemplate("searchError.ftl", body, config); } - private void doFailedSearch(HttpServletRequest request, - HttpServletResponse response, String message, String querytext) - throws ServletException, IOException { - Portal portal = (new VitroRequest(request)).getPortal(); - if( querytext != null ){ - request.setAttribute("querytext", querytext); - request.setAttribute("title", querytext+" - "+portal.getAppName()+" Search" ); - }else{ - request.setAttribute("title", portal.getAppName()+" Search" ); - request.setAttribute("querytext", ""); - } - if( message != null && message.length() > 0) - request.setAttribute("message", message); - - request.setAttribute("bodyJsp", Controllers.SEARCH_FAILED_JSP); - RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP); - rd.forward(request, response); + private String doNoHits(String querytext, Configuration config) { + Map body = new HashMap(); + body.put("title", "Search for '" + querytext + "'"); + body.put("message", "No matching results."); + return mergeBodyToTemplate("searchError.ftl", body, config); } - /** * Makes a message to display to user for a bad search term. * @param query @@ -831,15 +825,7 @@ public class FreemarkerPagedSearchController extends FreemarkerHttpServlet imple return opBlacklist; } - private void doSearchError(HttpServletRequest request, - HttpServletResponse response, String message, Object object) - throws ServletException, IOException { - Portal portal = (new VitroRequest(request)).getPortal(); - - request.setAttribute("bodyJsp", Controllers.SEARCH_ERROR_JSP); - RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP); - rd.forward(request, response); - } + private final String defaultSearchField = "ALLTEXT"; public static final int MAX_QUERY_LENGTH = 500; diff --git a/webapp/web/templates/freemarker/body/pagedSearchResults.ftl b/webapp/web/templates/freemarker/body/search/pagedSearchResults.ftl similarity index 96% rename from webapp/web/templates/freemarker/body/pagedSearchResults.ftl rename to webapp/web/templates/freemarker/body/search/pagedSearchResults.ftl index d7946e3c2..8bee6dde8 100644 --- a/webapp/web/templates/freemarker/body/pagedSearchResults.ftl +++ b/webapp/web/templates/freemarker/body/search/pagedSearchResults.ftl @@ -4,7 +4,7 @@

Search Results for '${querytext}' - <#if classgroupName?has_content>limited to type '${classgroupName}' + <#if classgroupName?has_content>limited to type '${classGroupName}' <#if typeName?has_content>limited to type '${typeName}'

diff --git a/webapp/web/templates/freemarker/body/search/searchError.ftl b/webapp/web/templates/freemarker/body/search/searchError.ftl new file mode 100644 index 000000000..381aa382f --- /dev/null +++ b/webapp/web/templates/freemarker/body/search/searchError.ftl @@ -0,0 +1,9 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for displaying search error message --> + +<#if title??> +

${title}

+ + +

${message}

\ No newline at end of file