/**
 * Created on Jul 4, 2005
 * $id:  $
 */
package org.jvnet.lookout.core.adapter;

import java.util.List;

import org.jvnet.lookout.core.model.SearchQuery;
import org.jvnet.lookout.core.model.SearchResult;

abstract public class AbstractSearchAdapter implements SearchAdapter {
    
    /**
     * Populates the SearchExcutor property.
     */
    public AbstractSearchAdapter() {
    }
    
    public List<SearchResult> handleSearchRequest(SearchQuery query) throws CannotSearchException {
        
        //Perform search...
        List rawResults = createSearchExecutor().executeSearch(getSearcQueryString(query), query.getParameters());			
		
        //Adapt the search engine model to the generic search model..
        List<SearchResult> results = adaptSearchResults(rawResults);
        
        return results;
    }
    
    protected String getSearcQueryString(SearchQuery query) {

    	//Create a SearchQueryConverter
        SearchQueryConverter queryConverter = createSearchQueryConverter();
        
        //Obtain query string by converting the SearchQuery...
        return queryConverter.convert(query);    	
    }
    
    /**
     * Should adapt the provided <code>List</code> that contains the search results
     * in its proprietary form to a <code>List</code> of <code>SearchResult</code>.
     *  
     * @param rawResults
     * @return
     */
    abstract protected List<SearchResult> adaptSearchResults(List rawResults);
    
    /**
     * Should create and return a new instance of a type that implements the 
     * SearchExecutor interface. A new instance will be used each time a 
     * search needs to be executed. 
     * 
     * @return
     */
    abstract protected SearchExecutor createSearchExecutor();
    
    /**
     * Should create and return a new instance of a type that implements the 
     * SearchQueryConverter interface. A new instance will be used each time a 
     * query needs to be built. 
     * 
     * @return
     */
    abstract public SearchQueryConverter createSearchQueryConverter();

}
