cvs commit: spice/sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents SearchServiceFailedServletException.java SearchComponentsController.java

Mike Hogan <[email protected]>
Newsgroups gmane.comp.java.spice.cvs
Message-ID <[email protected]>
hogie       03/10/28 11:36:06

  Modified:    sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents
                        SearchComponentsController.java
  Added:       sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents
                        SearchServiceFailedServletException.java
  Log:
  Some test cases for search use case.  Incomplete.
  
  Revision  Changes    Path
  1.6       +43 -33    spice/sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents/SearchComponentsController.java
  
  Index: SearchComponentsController.java
  ===================================================================
  RCS file: /cvsroot/spice/spice/sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents/SearchComponentsController.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- SearchComponentsController.java	16 Oct 2003 19:00:43 -0000	1.5
  +++ SearchComponentsController.java	28 Oct 2003 19:36:06 -0000	1.6
  @@ -2,6 +2,7 @@
   
   import org.componenthaus.repository.api.ComponentRepository;
   import org.componenthaus.search.SearchService;
  +import org.componenthaus.usecases.common.MissingRequestParameterServletException;
   import org.springframework.validation.BindException;
   import org.springframework.web.servlet.ModelAndView;
   import org.springframework.web.servlet.mvc.SimpleFormController;
  @@ -18,7 +19,15 @@
   import java.util.Map;
   
   public class SearchComponentsController extends SimpleFormController {
  -    private static final int HITS_PER_PAGE = 10;
  +    static final String QUERY_PARAMETER_NAME = "query";
  +    static final String BEGIN_INDEX_PARAMETER_NAME = "beginIndex";
  +    static final String END_INDEX_PARAMETER_NAME = "endIndex";
  +    static final String RESULTS_BEAN_NAME = "results";
  +    static final String TOTAL_MATCHES_BEAN_NAME = "totalMatches";
  +    static final String PAGES_BEAN_NAME = "pages";
  +    static final String CURRENT_PAGE_BEAN_NAME = "currentPage";
  +    static final int HITS_PER_PAGE = 10;
  +
       private final SearchService searchService;
       private final ComponentRepository repository;
   
  @@ -36,36 +45,37 @@
   	}
   
       private boolean requestHasSearchParameters(HttpServletRequest request) {
  -        return  !isEmpty(request.getParameter("query")) &&
  -                !isEmpty(request.getParameter("beginIndex")) &&
  -                !isEmpty(request.getParameter("endIndex"));
  +        return  !isEmpty(request.getParameter(QUERY_PARAMETER_NAME)) &&
  +                !isEmpty(request.getParameter(BEGIN_INDEX_PARAMETER_NAME)) &&
  +                !isEmpty(request.getParameter(END_INDEX_PARAMETER_NAME));
       }
   
       protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object o, BindException e) throws ServletException, IOException {
  -        final String query = request.getParameter("query");
  -        final String begin = request.getParameter("beginIndex");
  -        final String end = request.getParameter("endIndex");
  +        final String query = request.getParameter(QUERY_PARAMETER_NAME);
  +        if ( isEmpty(query)) {
  +            throw new MissingRequestParameterServletException(QUERY_PARAMETER_NAME);
  +        }
  +        final String begin = request.getParameter(BEGIN_INDEX_PARAMETER_NAME);
  +        final String end = request.getParameter(END_INDEX_PARAMETER_NAME);
           final List componentIds = new ArrayList();
  -        final int beginIndex = !isEmpty(begin) ? Integer.parseInt(begin) - 1 : 0;
  -        final int endIndex = !isEmpty(end) ? Integer.parseInt(end) - 1 : HITS_PER_PAGE - 1;
  +        final int beginIndex = !isEmpty(begin) ? Integer.parseInt(begin) : 1;
  +        final int endIndex = !isEmpty(end) ? Integer.parseInt(end) : HITS_PER_PAGE;
           int totalMatches = 0;
           try {
  -            totalMatches = searchService.search(query, beginIndex, endIndex, componentIds);
  +            totalMatches = searchService.search(query, beginIndex - 1, endIndex - 1, componentIds);
           } catch (SearchService.Exception sse) {
  -            throw new ServletException("Exception performing search",sse);
  +            throw new SearchServiceFailedServletException("Exception performing search",sse);
           }
           final Collection components = getComponent(componentIds);
           final Map model = new Hashtable();
  -        model.put("results",components);
  -        model.put("query",query);
  -        model.put("beginIndex",new Integer(beginIndex + 1));
  -        model.put("endIndex",new Integer(Math.min(totalMatches, endIndex + 1)));
  -        model.put("totalMatches",new Integer(totalMatches));
  -        model.put("pages",computePages(totalMatches,HITS_PER_PAGE));
  +        model.put(RESULTS_BEAN_NAME,components);
  +        model.put(QUERY_PARAMETER_NAME,query);
  +        model.put(BEGIN_INDEX_PARAMETER_NAME,new Integer(beginIndex));
  +        model.put(END_INDEX_PARAMETER_NAME,new Integer(Math.min(totalMatches, endIndex)));
  +        model.put(TOTAL_MATCHES_BEAN_NAME,new Integer(totalMatches));
  +        model.put(PAGES_BEAN_NAME,computePages(totalMatches,HITS_PER_PAGE));
           final int currentPage = ((beginIndex+1) / HITS_PER_PAGE) + 1;
  -        model.put("currentPage",new Page(currentPage, beginIndex+1,endIndex+1));
  -        model.put("hitsPerPage",new Integer(HITS_PER_PAGE));
  -
  +        model.put(CURRENT_PAGE_BEAN_NAME,new Page(currentPage, beginIndex,endIndex));
           return new ModelAndView("searchResultsView",model);
       }
   
  @@ -76,7 +86,7 @@
           }
           final Collection result = new ArrayList();
           for(int i=0;i<numPages;i++) {
  -            result.add(new Page(i+1,(i*resultsPerPage)+1,((i+1)*resultsPerPage)+1));
  +            result.add(new Page(i+1,(i*resultsPerPage),((i+1)*resultsPerPage)));
           }
           return result;
       }
  @@ -95,26 +105,26 @@
       }
   
       public static final class Page {
  -        private int id;
  -        private int beginIndex;
  -        private int endIndex;
  +        private int pageIndex;
  +        private int hitBeginIndex;
  +        private int hitEndIndex;
   
           public Page(int id, int beginIndex, int endIndex) {
  -            this.id = id;
  -            this.beginIndex = beginIndex;
  -            this.endIndex = endIndex;
  +            this.pageIndex = id;
  +            this.hitBeginIndex = beginIndex;
  +            this.hitEndIndex = endIndex;
           }
   
  -        public int getId() {
  -            return id;
  +        public int getPageIndex() {
  +            return pageIndex;
           }
   
  -        public int getBeginIndex() {
  -            return beginIndex;
  +        public int getHitBeginIndex() {
  +            return hitBeginIndex;
           }
   
  -        public int getEndIndex() {
  -            return endIndex;
  +        public int getHitEndIndex() {
  +            return hitEndIndex;
           }
       }
   
  
  
  
  1.1                  spice/sandbox/repository/componenthaus/src/java/org/componenthaus/usecases/searchcomponents/SearchServiceFailedServletException.java
  
  Index: SearchServiceFailedServletException.java
  ===================================================================
  package org.componenthaus.usecases.searchcomponents;
  
  import javax.servlet.ServletException;
  
  public class SearchServiceFailedServletException extends ServletException {
      public SearchServiceFailedServletException(String message, Throwable cause) {
          super(message,cause);
      }
  }
  
  
  


-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.