Coverage Report - net.sourceforge.pebble.web.action.SearchAction
 
Classes in this File Line Coverage Branch Coverage Complexity
SearchAction
0%
0/34
0%
0/16
16
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, Simon Brown
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions are met:
 7  
  *
 8  
  *   - Redistributions of source code must retain the above copyright
 9  
  *     notice, this list of conditions and the following disclaimer.
 10  
  *
 11  
  *   - Redistributions in binary form must reproduce the above copyright
 12  
  *     notice, this list of conditions and the following disclaimer in
 13  
  *     the documentation and/or other materials provided with the
 14  
  *     distribution.
 15  
  *
 16  
  *   - Neither the name of Pebble nor the names of its contributors may
 17  
  *     be used to endorse or promote products derived from this software
 18  
  *     without specific prior written permission.
 19  
  *
 20  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 21  
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 22  
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 23  
  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 24  
  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 25  
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 26  
  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 27  
  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 28  
  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 29  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 30  
  * POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 package net.sourceforge.pebble.web.action;
 33  
 
 34  
 import net.sourceforge.pebble.Constants;
 35  
 import net.sourceforge.pebble.domain.Blog;
 36  
 import net.sourceforge.pebble.search.SearchException;
 37  
 import net.sourceforge.pebble.search.SearchHit;
 38  
 import net.sourceforge.pebble.search.SearchResults;
 39  
 import net.sourceforge.pebble.util.Pageable;
 40  
 import net.sourceforge.pebble.web.view.RedirectView;
 41  
 import net.sourceforge.pebble.web.view.View;
 42  
 import net.sourceforge.pebble.web.view.impl.AdvancedSearchView;
 43  
 import net.sourceforge.pebble.web.view.impl.SearchResultsView;
 44  
 import org.apache.commons.logging.Log;
 45  
 import org.apache.commons.logging.LogFactory;
 46  
 
 47  
 import javax.servlet.ServletException;
 48  
 import javax.servlet.http.HttpServletRequest;
 49  
 import javax.servlet.http.HttpServletResponse;
 50  
 import java.io.UnsupportedEncodingException;
 51  
 
 52  
 /**
 53  
  * Performs a search on the current blog.
 54  
  *
 55  
  * @author    Simon Brown
 56  
  */
 57  0
 public class SearchAction extends Action {
 58  
 
 59  
   /** the log used for this action */
 60  0
   private static final Log log = LogFactory.getLog(SearchAction.class);
 61  
 
 62  
   /** the number of results to show per page */
 63  
   static final int PAGE_SIZE = 20;
 64  
 
 65  
 
 66  
   /**
 67  
    * Peforms the processing associated with this action.
 68  
    *
 69  
    * @param request  the HttpServletRequest instance
 70  
    * @param response the HttpServletResponse instance
 71  
    * @return the name of the next view
 72  
    */
 73  
   public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
 74  
 
 75  0
     Blog blog = (Blog)getModel().get(Constants.BLOG_KEY);
 76  0
     String query = request.getParameter("query");
 77  
 
 78  0
     if (query == null || query.trim().length() == 0) {
 79  0
       if (blog instanceof Blog) {
 80  0
         return new AdvancedSearchView();
 81  
       }
 82  
     }
 83  
 
 84  0
     String pageAsString = request.getParameter("page");
 85  0
     int page = 1;
 86  0
     if (pageAsString == null || pageAsString.length() == 0) {
 87  0
       page = 1;
 88  
     } else {
 89  
       try {
 90  0
         page = Integer.parseInt(pageAsString);
 91  0
       } catch (NumberFormatException nfe) {
 92  0
       }
 93  
     }
 94  
 
 95  
     try {
 96  0
       SearchResults results = blog.getSearchIndex().search(query);
 97  
 
 98  0
       if (results.getNumberOfHits() == 1) {
 99  
         // if there is only one hit, redirect the user to it without the
 100  
         // search results page
 101  0
         SearchHit hit = (SearchHit)results.getHits().get(0);
 102  0
         return new RedirectView(hit.getPermalink());
 103  
       } else {
 104  
         // show all results on the search results page
 105  0
         String sort = request.getParameter("sort");
 106  0
         if (sort != null && sort.equalsIgnoreCase("date")) {
 107  0
           results.sortByDateDescending();
 108  
         } else {
 109  0
           results.sortByScoreDescending();
 110  
         }
 111  
 
 112  0
         Pageable pageable = new Pageable(results.getHits());
 113  0
         pageable.setPageSize(PAGE_SIZE);
 114  0
         pageable.setPage(page);
 115  
 
 116  
         try {
 117  0
           getModel().put("searchResults", results);
 118  0
           getModel().put("pageable", pageable);
 119  0
           getModel().put("query", java.net.URLEncoder.encode(query, blog.getCharacterEncoding()));
 120  0
         } catch (UnsupportedEncodingException uee) {
 121  0
           log.error(uee);
 122  0
         }
 123  
 
 124  0
         return new SearchResultsView();
 125  
       }
 126  0
     } catch (SearchException se) {
 127  0
       throw new ServletException(se);
 128  
     }
 129  
   }
 130  
 
 131  
 }