/*
Copyright (c) 2004, Plumtree Software

All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions is met:

Neither the name of Plumtree Software nor the names of its contributors may be used
to endorse or promote products derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


package com.plumtree.remote.googlesample.search;

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchFault;
import com.google.soap.search.GoogleSearchResult;
import com.google.soap.search.GoogleSearchResultElement;
import com.plumtree.remote.ServiceException;
import com.plumtree.remote.googlesample.Constants;
import com.plumtree.remote.search.IRemoteSearch;
import com.plumtree.remote.search.ISearchContext;
import com.plumtree.remote.search.ISearchQuery;
import com.plumtree.remote.search.ISearchResult;
import com.plumtree.remote.search.ISearchUser;
import com.plumtree.remote.search.*;
import java.net.*;

/**
* basicSearch method for GoogleSearch
*/
public class PtGoogleSearch implements IRemoteSearch
{

	/**
	* Simple Google search- not using ISearchUser or ISearchContext
	* Queries Google , gets result array, and turns into search result array
	* @see com.plumtree.remote.search.IRemoteSearch#basicSearch(com.plumtree.remote.search.ISearchQuery, com.plumtree.remote.search.ISearchUser, com.plumtree.remote.search.ISearchContext)
	*/
	public ISearchResult basicSearch(
		ISearchQuery searchQuery,
		ISearchUser searchUser,
		ISearchContext searchContext)
		throws ServiceException
	{
		//get the key, query and image from getSearchInfo. The values were set in the Service Configuration Interface (SCI) page.
		//if not using SCI pages, hard-code the key and image values below.
		//getSearchInfo retrieves values set in the sci pages
		String key = searchQuery.getSearchInfo().get(Constants.KEY);
		String query = searchQuery.getSearchString();
		String image = searchQuery.getSearchInfo().get(Constants.IMAGE);

		//get the search result from the search query
		ISearchResult searchResult = searchQuery.getSearchResult();

		int startResult = searchQuery.getNumberToSkip();
		int totalResults = 0;
		try
		{
			//do the Google search
			//see the Google documentation for more info
			GoogleSearchResult result = null;
			GoogleSearchResultElement[] elements = null;

			GoogleSearch googleSearch =
				new com.google.soap.search.GoogleSearch();

			//encode the key as it may have + signs
			googleSearch.setKey(URLEncoder.encode(key));
			googleSearch.setQueryString(query);
			googleSearch.setStartResult(startResult);
			result = googleSearch.doSearch();
			elements = result.getResultElements();

			//Google search should have setDescriptionEncoded set to false
			//as html is sometimes passed in the title or description
			searchResult.setDescriptionEncoded(false);
			searchResult.setTotalNumberOfHits(
				result.getEstimatedTotalResultsCount());

		  //put into the format for IRemoteSearch.basicSearch
			int len = elements.length;
			ISearchRecord[] records = new ISearchRecord[len];
			for (int i = 0; i < len; i++)
			{

				RemoteSearchRecord record = new RemoteSearchRecord();
				GoogleSearchResultElement element = elements[i];
				record.setTitle(element.getTitle());
				record.setDescription(element.getSummary());
				record.setOpenDocumentURL(element.getURL());

				//Google does not provide an image- the image came from the sci page
				record.setImageURL(image);

			  records[i] = record;
			}

			searchResult.setSearchResultList(records);

		}
		//if there are exceptions, return the message to the user using TYPE_OTHER_ERROR
		//and the message string
		catch (GoogleSearchFault sf)
		{
			throw new SearchException(
				TypeSearchError.TYPE_OTHER_ERROR,
				sf.getMessage());
		}
		catch (Exception e)
		{
			throw new SearchException(
				TypeSearchError.TYPE_OTHER_ERROR,
				e.getMessage());
		}

		return searchResult;
	}

}