/**
 * Created on Jul 3, 2005
 * $id:  $
 */
package org.jvnet.lookout.google.web.adapter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jvnet.lookout.core.adapter.AbstractSearchAdapter;
import org.jvnet.lookout.core.adapter.SearchExecutor;
import org.jvnet.lookout.core.adapter.SearchQueryConverter;
import org.jvnet.lookout.core.model.SearchResult;
import org.jvnet.lookout.core.model.SearchResultLocation;
import org.jvnet.lookout.core.model.SearchResultProperty;
import org.jvnet.lookout.core.model.SearchResultSource;
import org.jvnet.lookout.core.model.SearchResultType;
import org.jvnet.lookout.core.util.Utilities;

import com.google.soap.search.GoogleSearchResultElement;

/**
 * <code>WebSearchAdapter</code> is in charge of adapting the search results obtained by triggering the undelying 
 * search engine to the generic search model.
 * 
 * <p>
 * @author Cleo Barretto
 *
 */
public class WebSearchAdapter extends AbstractSearchAdapter {
    
    private static final SearchResultSource GOOGLE_WEB_SEARCH_SOURCE = 
    	new SearchResultSource("GOOGLE WEB SEARCH SOURCE");

    @Override
    protected List<SearchResult> adaptSearchResults(List rawResults) {

        List<SearchResult> adaptedResults = new ArrayList<SearchResult>(rawResults.size());        
        
        for (Iterator iter = rawResults.iterator(); iter.hasNext();) {
            
            GoogleSearchResultElement resultElement = (GoogleSearchResultElement) iter.next();
            
            String titleValue = resultElement.getTitle();
            String urlValue = resultElement.getURL();
            
            //These values are required...
            if(!Utilities.isValidValue(titleValue) || !Utilities.isValidValue(urlValue)) {
                continue;    
            }
            
            SearchResult searchResult = new SearchResult();
            
            //Source
            searchResult.setSource(getSearchResultSource());

            //Name
            searchResult.setProperty(SearchResultProperty.NAME, Utilities.stripHTML(titleValue));
            
            //Type
            searchResult.setType(Utilities.getTypeForURL(resultElement.getURL(), SearchResultType.HTML));

            //URL
            searchResult.setProperty(SearchResultProperty.URL, Utilities.stripHTML(urlValue));

            //Snippet
            String snippetValue = resultElement.getSnippet();
            
            if(Utilities.isValidValue(snippetValue)) {
                searchResult.setProperty(SearchResultProperty.SNIPPET, Utilities.stripHTML(snippetValue));
            }

            //Location
            searchResult.setProperty(SearchResultProperty.LOCATION, SearchResultLocation.REMOTE);
            
            /*
            //Possible additions...
            resultElement.getDirectoryCategory();           //NO
            resultElement.getCachedSize();                  //MAYBE
            resultElement.getDirectoryTitle();              //NO
            resultElement.getHostName();                    //NO
            resultElement.getRelatedInformationPresent();   //NO
            resultElement.getSummary();                     //NO
            */
            
            //Save it
            adaptedResults.add(searchResult);        
        }
        
        return adaptedResults;
    }
    
    protected SearchExecutor createSearchExecutor() {     
        return new WebSearchExecutor();
    }
    
    public SearchQueryConverter createSearchQueryConverter() {
        return new WebQueryConverter();
    }
    
    public SearchResultSource getSearchResultSource() {
        return WebSearchAdapter.GOOGLE_WEB_SEARCH_SOURCE;
    }
}
