Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/articles/java-tutorials/)
-   -   Google Pagination (http://www.go4expert.com/articles/google-pagination-t18329/)

naimish 3Jul2009 15:37

Google Pagination
 
Google pagination used to find initial and final index for the search done using google3 for website application.

GoogleAppSearch.java

Code: Java

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: Java

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: Java

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: Java

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);
 
 
 }
}


shabbir 3Jul2009 15:49

Re: Google Pagination
 
Could not understand much in this ?

naimish 3Jul2009 15:51

Re: Google Pagination
 
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

shabbir 3Jul2009 15:58

Re: Google Pagination
 
I messed it up when editing into Code block but I still dont get what It does

naimish 3Jul2009 16:09

Re: Google Pagination
 
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.

pradeep 3Jul2009 16:31

Re: Google Pagination
 
So actually you posted the pagination logic? Anything special with the pagination logic?

shabbir 3Jul2009 16:36

Re: Google Pagination
 
Quote:

Originally Posted by naimish (Post 51247)
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.

Still not getting it. You have a Google Mini Pack or something of that sort ?

shabbir 3Aug2009 14:34

Re: Google Pagination
 
Nominate this article for Article of the month - Jul 2009


All times are GMT +5.5. The time now is 16:08.