/**File Name :       GetSites.java
  *
  *Authors   :       Prashanth. K
  *                  Shireen Javali
  *
  *Date      :       20/05/2000
  *
  *Function  :       Connects to the sites and gets the search
  *                  results.
  */                        
                         
                                                                    
import java.io.*;
import java.net.*;
import Parser;


public class GetSites 
{

   Search connect;

   public GetSites(Search s)
   {
       this.connect = s;
   }


   /**This function establishes socket connection the sites
     *altavista.com and yahoo.com
     */
     

    public boolean getSearchResults(String searchEngine,String searchString,String outFileName,int from)
    {
        try 
        {
            boolean notFound = true;
            int nlist =   connect.gui.urlList.getItemCount();
            int j = 0;

            while(notFound && j< nlist)
            {
                if (connect.gui.customizeSearch.listOfSearchEngines.getItem(j).equals(searchEngine))
                     notFound = false;
               else
                 j++;
            }                                     

            Socket cSock=null;

            cSock = new Socket(connect.gui.urlList.getItem(j) , 80);
            return getPage(j,cSock,searchString,outFileName,from);
           
        }
        
        catch (UnknownHostException e)
        {
            System.err.println("UnknownHostException: " + e);
            return false;
        }
        catch (IOException e)  
        {
            System.err.println("IOException: " + e);
            return false;
        }
    }
      
    /**This method gets the search results once the connection
      *has been extablished
      */

    public  boolean getPage(int indexOfSearchEngine,Socket cSock,String searchString, String outFileName, int from)
    {
        byte responseLine[];
        responseLine = new byte[80];
        try  
        {
            DataOutputStream outbound = new DataOutputStream( cSock.getOutputStream() );
            DataInputStream inbound =  new DataInputStream( cSock.getInputStream() );
            RandomAccessFile outFile =  new RandomAccessFile(outFileName,"rw");

            String query = URLEncoder.encode(searchString);


            String theString;
            int whereIsQUERY = connect.gui.queryList.getItem(indexOfSearchEngine).indexOf("QUERY");
            System.out.println("Query is at :   " + whereIsQUERY);
            if (whereIsQUERY == -1)
            {
                System.err.println("Bad Search engine entry....");
                System.err.println("going on with Altavista");
                if (from > 10)
                {
                   from = ( from / 10 ) * 10;
                   theString = "GET http://www.altavista.com/cgi-bin/query?pg=q&sc=on&hl=on&q="
                            + query
                            + "&kl=en&stype=stext&stq="
                            + from
                            + "\r\n\r\n";
                }
                else
                {
                   theString = "GET http://www.altavista.com/cgi-bin/query?pg=q&sc=on&hl=on&q="
                            + query
                            + "&kl=en&stype=stext"
                            + "\r\n\r\n";
                }
            }
            else
            {

                int firstIndex = (new Integer(connect.gui.firstIndexList.getItem(indexOfSearchEngine))).intValue();
                int increment = (new Integer(connect.gui.incrementList.getItem(indexOfSearchEngine))).intValue();
                int whereIsFROM = connect.gui.queryList.getItem(indexOfSearchEngine).indexOf("FROM");
                System.out.println("From is at :   " + whereIsFROM);

                if (from >= firstIndex )
                {

                    whereIsQUERY = connect.gui.subsequentQueryList.getItem(indexOfSearchEngine).indexOf("QUERY");
                    whereIsFROM = connect.gui.subsequentQueryList.getItem(indexOfSearchEngine).indexOf("FROM");
                    from = firstIndex + ((from - firstIndex) / increment) * increment;

                    theString = "GET http://"
                                + connect.gui.urlList.getItem(indexOfSearchEngine)
                                + connect.gui.subsequentQueryList.getItem(indexOfSearchEngine).substring(0,whereIsQUERY)
                                + query
                                + connect.gui.subsequentQueryList.getItem(indexOfSearchEngine).substring(whereIsQUERY +5 ,whereIsFROM) 
                                + from
                                + connect.gui.subsequentQueryList.getItem(indexOfSearchEngine).substring(whereIsFROM +4)
                                + "\r\n\r\n";
                }
                else
                {
                    theString = "GET http://"
                                + connect.gui.urlList.getItem(indexOfSearchEngine)
                                + connect.gui.queryList.getItem(indexOfSearchEngine).substring(0,whereIsQUERY)
                                + query
                                + connect.gui.queryList.getItem(indexOfSearchEngine).substring(whereIsQUERY +5)
                                + "\r\n\r\n";
                }
            }
            
            System.out.println(theString);

            connect.gui.displayMessage("Connecting to "+  theString);
            outbound.writeBytes (theString);

            int howMany;
            System.out.println("connected   ");
            while ((howMany = inbound.read(responseLine,0,80)) != -1)
                outFile.write(responseLine,0,howMany);

            outbound.close();
            inbound.close();
            outFile.close();
            cSock.close();
            return true;
        }
        catch (IOException ioe)  
        {
            System.out.println("IOException: " + ioe);
            return false;
        }
    }


}

