
/**
 * GoogleSearchBindingImpl.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis WSDL2Java emitter.
 */

package net.nutch.soap;

import net.nutch.searcher.*;

public class GoogleSearchBindingImpl
    implements net.nutch.soap.GoogleSearchPort {

    public byte[] doGetCachedPage(java.lang.String key, java.lang.String url)
	throws java.rmi.RemoteException {
	return null;
    }

    public java.lang.String doSpellingSuggestion(
						 java.lang.String key,
						 java.lang.String phrase)
	throws java.rmi.RemoteException {
	return null;
    }

    public net.nutch.soap.GoogleSearchResult doGoogleSearch(
							    java.lang.String key,
							    java.lang.String q,
							    int start,
							    int maxResults,
							    boolean filter,
							    java.lang.String restrict,
							    boolean safeSearch,
							    java.lang.String lr,
							    java.lang.String ie,
							    java.lang.String oe)
	throws java.rmi.RemoteException {

	String indexDir = "/scratch/local/eclipse/workspace/nutch";
	GoogleSearchResult result = new GoogleSearchResult();

	try {

	    // generate query
	    Query query = Query.parse(q);

	    // TODO: Using current directory is OK?
	    NutchBean nBean = new NutchBean();

	    // Run search
	    Hits hits = nBean.search(query, start + maxResults);
	    int totalResult = (int) hits.getTotal();
	    int end = Math.min(totalResult, start + maxResults);
	    int length = Math.max(0, end - start);
	    Hit[] show = hits.getHits(start, length);
	    HitDetails[] details = nBean.getDetails(show);
	    String[] summaries = nBean.getSummary(details, query);

	    // Results Elements
	    ResultElement resultElements[] = new ResultElement[show.length];

	    // Set Google SearchResult
	    result.setStartIndex(start);
	    result.setEndIndex(start + show.length);
	    result.setResultElements(resultElements);
	    result.setEstimatedTotalResultsCount(totalResult);
	    result.setEstimateIsExact(false);
	    result.setSearchQuery(q);

	    // Create nulldir and feed it for all elements
	    DirectoryCategory nullDir = new DirectoryCategory();
	    nullDir.setFullViewableName("");
	    nullDir.setSpecialEncoding("");

	    // Fill each Element
	    for (int i = 0; i < hits.getLength(); i++) {
		ResultElement re = resultElements[i] = new ResultElement();

		re.setCachedSize("0");
		re.setDirectoryCategory(nullDir);
		re.setDirectoryTitle("");
		re.setHostName("Hostname");
		re.setRelatedInformationPresent(false);
		re.setSnippet(summaries[i]);
		re.setSummary(summaries[i]);
		re.setTitle(details[i].getValue("title"));
		re.setURL(details[i].getValue("url"));
	    }
	} catch (Exception e) {
	    // FIXME: What can we do??
	    e.printStackTrace(System.err);

	}

	return result;
    }

    public static void main(String args[]) {
	GoogleSearchBindingImpl impl = new GoogleSearchBindingImpl();

	try {
	    GoogleSearchResult result =
		impl.doGoogleSearch(
				    null,
				    "yahoo",
				    0,
				    10,
				    false,
				    "",
				    false,
				    "",
				    "",
				    "");

	    ShowData.GoogleSearchResult(result);

	} catch (Exception e) {
	    e.printStackTrace(System.err);
	}

    }
}
