package org.codehaus.plexus.yahoosearch;

import java.util.Map;

import org.codehaus.plexus.httpclient.PlexusHttpClient;
import org.codehaus.plexus.httpclient.PlexusHttpClientException;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;


public abstract class AbstractYahooSearch extends AbstractLogEnabled implements
		PlexusYahooSearch, Initializable {
	/**
	 * @plexus.requirement role="org.codehaus.plexus.httpclient.PlexusHttpClient"
	 */
	private PlexusHttpClient httpclient;

	/**
	 * @plexus.configuration default-value="YahooDemo"
	 */
	private String appid;

	/**
	 * @plexus.configuration default-value="http://api.search.yahoo.com"
	 */
	private String requestUrl;

	private String yahooServiceName;

	private String yahooServiceVersion;

	private String yahooServiceQuery;

	private Map<String, Object> requestParameters;

	private String queryString;

	public byte[] query() throws PlexusYahooSearchException {
		byte[] result = null;

		requestParameters.put("appid", appid);

		try {
			result = httpclient.executePost(queryString, requestParameters);
		} catch (PlexusHttpClientException e) {
			throw new PlexusYahooSearchException("enable to request yahoo.", e);
		}
		return result;
	}

	public void initialize() throws InitializationException {
		StringBuffer buffer = new StringBuffer();

		buffer.append(requestUrl).append("/").append(yahooServiceName).append(
				"/").append(yahooServiceVersion).append("/").append(
				yahooServiceQuery);

		queryString = buffer.toString();
	}

	public Map<String, Object> getRequestParameters() {
		return requestParameters;
	}

	public void setRequestParameters(Map<String, Object> requestParameters) {
		this.requestParameters = requestParameters;
	}

}
