| 1 |  |  | 
            
  
    | 2 |  |  | 
            
  
    | 3 |  |  | 
            
  
    | 4 |  |  | 
            
  
    | 5 |  |  | 
            
  
    | 6 |  |  | 
            
  
    | 7 |  |  | 
            
  
    | 8 |  |  | 
            
  
    | 9 |  |  | 
            
  
    | 10 |  |  | 
            
  
    | 11 |  |  | 
            
  
    | 12 |  |  | 
            
  
    | 13 |  |  | 
            
  
    | 14 |  |  | 
            
  
    | 15 |  |  | 
            
  
    | 16 |  |  | 
            
  
    | 17 |  |  | 
            
  
    | 18 |  |  | 
            
  
    | 19 |  |  | 
            
  
    | 20 |  |  | 
            
  
    | 21 |  |  | 
            
  
    | 22 |  |  | 
            
  
    | 23 |  |  | 
            
  
    | 24 |  |  | 
            
  
    | 25 |  |  | 
            
  
    | 26 |  |  | 
            
  
    | 27 |  |  | 
            
  
    | 28 |  |  | 
            
  
    | 29 |  |  | 
            
  
    | 30 |  |  | 
            
  
    | 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 |  |  | 
            
  
    | 54 |  |  | 
            
  
    | 55 |  | @author | 
            
  
    | 56 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 41 (41) | Complexity: 12 | Complexity Density: 0,4 |  | 
  
  
    | 57 |  | public class SearchAction extends Action { | 
            
  
    | 58 |  |  | 
            
  
    | 59 |  |  | 
            
  
    | 60 |  | private static final Log log = LogFactory.getLog(SearchAction.class); | 
            
  
    | 61 |  |  | 
            
  
    | 62 |  |  | 
            
  
    | 63 |  | static final int PAGE_SIZE = 20; | 
            
  
    | 64 |  |  | 
            
  
    | 65 |  |  | 
            
  
    | 66 |  |  | 
            
  
    | 67 |  |  | 
            
  
    | 68 |  |  | 
            
  
    | 69 |  | @param | 
            
  
    | 70 |  | @param | 
            
  
    | 71 |  | @return | 
            
  
    | 72 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 40 (40) | Complexity: 12 | Complexity Density: 0,4 |  | 
  
  
    | 73 | 0 |  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 | 0 | try { | 
            
  
    | 90 | 0 | page = Integer.parseInt(pageAsString); | 
            
  
    | 91 |  | } catch (NumberFormatException nfe) { | 
            
  
    | 92 |  | } | 
            
  
    | 93 |  | } | 
            
  
    | 94 |  |  | 
            
  
    | 95 | 0 | try { | 
            
  
    | 96 | 0 | SearchResults results = blog.getSearchIndex().search(query); | 
            
  
    | 97 |  |  | 
            
  
    | 98 | 0 | if (results.getNumberOfHits() == 1) { | 
            
  
    | 99 |  |  | 
            
  
    | 100 |  |  | 
            
  
    | 101 | 0 | SearchHit hit = (SearchHit)results.getHits().get(0); | 
            
  
    | 102 | 0 | return new RedirectView(hit.getPermalink()); | 
            
  
    | 103 |  | } else { | 
            
  
    | 104 |  |  | 
            
  
    | 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 | 0 | 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 |  | } catch (UnsupportedEncodingException uee) { | 
            
  
    | 121 | 0 | log.error(uee); | 
            
  
    | 122 |  | } | 
            
  
    | 123 |  |  | 
            
  
    | 124 | 0 | return new SearchResultsView(); | 
            
  
    | 125 |  | } | 
            
  
    | 126 |  | } catch (SearchException se) { | 
            
  
    | 127 | 0 | throw new ServletException(se); | 
            
  
    | 128 |  | } | 
            
  
    | 129 |  | } | 
            
  
    | 130 |  |  | 
            
  
    | 131 |  | } |