1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Google Pagination

Discussion in 'Java' started by naimish, Jul 3, 2009.

  1. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Google pagination used to find initial and final index for the search done using google3 for website application.

    GoogleAppSearch.java

    Code:
    import net.sf.gsaapi.GSAClient;
    import net.sf.gsaapi.GSAQuery;
    import net.sf.gsaapi.GSAResponse;
    import net.sf.gsaapi.GSAQuery.GSAQueryTerm;
    import net.sf.gsaapi.constants.Filter;
    import net.sf.gsaapi.constants.OutputFormat;
     
    
    /*
    public class GoogleAppSearch {
     
     // target GSA's hostname
     private static final String HOSTNAME = "google3";
     // The value for the frontend configured for the GSA
     private static final String SETTING_FRONTEND = "default_frontend";
     private static final String LANG1 = "lang_";
     private static GSAClient client = new GSAClient(HOSTNAME);
     private GSAQuery query = new GSAQuery();
     // TODO have to change from default to fido when google3 is ready for fido
     private String siteCollection[] = { "default_collection" };
     
     /**
      * default constructor
      *
      */
     public GoogleAppSearch() {
     }
     /**
      * This method calls the google3 search thru API and returns the Response Object
      * @param search
      * @param language
      * @return GSAResponse
      */
     public GSAResponse searchFromGoogleApps(String search, String language, int index) {
      String searchStr = getFormattedString(search);
      GSAResponse response = null;
      GSAQueryTerm term = new GSAQueryTerm(searchStr);
      query.setSiteCollections(siteCollection);
      query.setQueryTerm(term);
      query.setInputEncoding("UTF-8");
      query.setLanguage(LANG1 + language);// lang_en,lang_fr
      query.setFilter(Filter.DUPLICATE_SNIPPET_FILTER);
      query.setFrontend(SETTING_FRONTEND); // required!
      query.setOutputFormat(OutputFormat.XML_NO_DTD);
      System.out.println("google query :"+query.getValue());
      query.setScrollAhead(index);
      
      try {
       response = client.getGSAResponse(query);
      } catch (Exception e) {
       System.out.println(" Exception while in  google appliance search: "
         + e.getMessage());
      }
      return response;
     }
     /**
      * This method adds the escape characters to the search string for exact
      * match
      * 
      * @param search
      * @return string
      */
     private String getFormattedString(String search) {
      //search = search.trim();
      search = search!=null?search.trim():"";
      search = "\\" + search + "\\";
      return search;
     }
    }
    
    
    PaginationUtil.java

    Code:
     
    public class PaginationUtil
    {
     
     
     
     /**
      * This method populates  final and intila index for the pagination
      * @param pages
      * @param endIndex
      * @param currentPage
      * @return
      */
     public PaginationVO getSearchIndex(int pages,long endIndex,int currentPage)
     {
      PaginationVO paginatinVO=new PaginationVO();
      int noOfPages=pages;
      long endInd =endIndex;
      int currentInd=currentPage;
      System.out.println(" end index "+endInd);
      System.out.println(" current index "+currentInd);
      System.out.println(" noOfPages "+noOfPages);
      int initalIndex=0;
      int finalIndex=0;
      //assign initial page index as 1 and final page index as no. of pages
      if(noOfPages <=10)
      {
       initalIndex=1;
       finalIndex=noOfPages;
      }else {
       //endInd is the one we will get from google API by calling getEndIndex()
       if(endInd%10 !=0)
       {
        //System.out.println("got the end index "+endInd);
         finalIndex=((int)(endInd/10))+1;
         if (finalIndex>20){
          initalIndex=finalIndex-19;
         }
       }else{
        finalIndex=currentInd+9;
        if(finalIndex >noOfPages)
        {      
         finalIndex=noOfPages;
        }
        System.out.println("add the final index logic "+finalIndex);
       }
       if(currentInd<12 || finalIndex<20) {
       // System.out.println("sets initalIndex ==1");
        initalIndex=1;    
       } else {
         System.out.println("sets initalIndex ==1");
         initalIndex=currentInd-10;
       }
       
      }
      paginatinVO.setInitialIndex(initalIndex);
      paginatinVO .setFinallIndex(finalIndex);
      return paginatinVO;
     }
     /**
      * Used for Testing 
      * @param args
      */
     public static void main(String args[])
     {
      
      int noOfResults =Integer.parseInt(args[0]);
      int currentPage =Integer.parseInt(args[1]); 
      long endIndex =Long.parseLong(args[2]); 
      
      PaginationUtil util=new PaginationUtil();
      PaginationVO paginatinVO=new PaginationVO();
      paginatinVO= util.getSearchIndex(noOfResults,endIndex,currentPage);
      System.out.println("init index "+paginatinVO.getInitialIndex());
      System.out.println("final index "+paginatinVO.getFinalIndex());
      //for next
        int nextPage=currentPage+1;
       paginatinVO= util.getSearchIndex(noOfResults,endIndex,nextPage);
       System.out.println("init index "+paginatinVO.getInitialIndex());
       System.out.println("final index "+paginatinVO.getFinalIndex());
       //for prev
       int prevPage=currentPage-1;
       paginatinVO= util.getSearchIndex(noOfResults,endIndex,prevPage);
       System.out.println("init index "+paginatinVO.getInitialIndex());
          System.out.println("final index "+paginatinVO.getFinalIndex());
     
     }
    }
     
     
    public class PaginationVO
    {
     int initialIndex;
     int finalIndex;
     
     /**
      * Sets the intial index
      * @param initialIndex
      */
     public void  setInitialIndex(int initialIndex)
     {
      this.initialIndex=initialIndex;
     }
     /**
      * gives the final index
      * @return int
      */
     public int getInitialIndex()
     {
      return initialIndex;
     }
     
     /**
      * sets final index
      * @param finalIndex
      */
     public void  setFinallIndex(int finalIndex)
     {
      this.finalIndex=finalIndex;
     }
     
     /**
      * Gives final index
      * @return int
      */
     public int getFinalIndex()
     {
      return finalIndex;
     }
    }
    
    PaginationVO.java

    Code:
    package com.fido.portal.search;
    /**
     * @Description :This class is value object  for Pagination
     * 
     */
    public class PaginationVO
    {
     int initialIndex;
     int finalIndex;
     
     /**
      * Sets the intial index
      * @param initialIndex
      */
     public void  setInitialIndex(int initialIndex)
     {
      this.initialIndex=initialIndex;
     }
     /**
      * gives the final index
      * @return int
      */
     public int getInitialIndex()
     {
      return initialIndex;
     }
     
     /**
      * sets final index
      * @param finalIndex
      */
     public void  setFinallIndex(int finalIndex)
     {
      this.finalIndex=finalIndex;
     }
     
     /**
      * Gives final index
      * @return int
      */
     public int getFinalIndex()
     {
      return finalIndex;
     }
    }
    
    Search.java

    Code:
    public class Search
    {
      public void begin() {
      
      String initialIndexStr = null;
      String maxIndexStr = null;
      String currentIndexStr = null;
      String numPages = null;
      String resultSizeStr = null;
      
      searchText = "tcs";
      
      String language = "en";
      GoogleAppSearch appsearch = new GoogleAppSearch();
      GSAResponse resp = appsearch.searchFromGoogleApps(searchText, language, searchIndex);
      long endIndex = resp.getEndIndex();
      long resultSize = resp.getNumResults();
      
      
      pages = resultSize/10;
      if (resultSize%10 >0)
       pages = pages+1;
      if (endIndex%10 !=0){
       pages= currentIndex+1;
      } 
      PaginationUtil util=new PaginationUtil();
      paginatinVO= util.getSearchIndex((int)pages,(int)endIndex,currentIndex);
      
      
     }
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Could not understand much in this ?
     
  3. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    There are 4 different java files, I have posted all 4 code diff. I dnt knw hw they got combine.

    Below are 4 diff. Java files.

    GoogleAppSearch.java
    PaginationUtil.java
    PaginationVO.java
    Search.java
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    I messed it up when editing into Code block but I still dont get what It does
     
  5. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,046
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    our requirement was that, we will do internal search of our web applicaition and show the results. i uploaded pagniation logic code component, basically, the search also powered by google.

    The program which i uploaded will give the logic to get first , prev next values, which helps to show it in the search page.
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    So actually you posted the pagination logic? Anything special with the pagination logic?
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Still not getting it. You have a Google Mini Pack or something of that sort ?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83

Share This Page