package it.cnr.isti.driver.utils;

import java.rmi.RemoteException;
import org.apache.axis2.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import gr.uoa.di.driver.ws.SearchServiceExceptionException0;
import gr.uoa.di.driver.ws.SearchServiceStub;

public class SearchClient {
	protected Log log = LogFactory.getLog(SearchClient.class);
	
	SearchServiceStub searchService = null;

	public SearchClient() {
	}

	public SearchClient(String address) {
		setAddress(address);
	}

	public void setAddress(String address) {
		log.debug("Setting Search address: " + address);
		try {
			searchService = new SearchServiceStub(address);
		} catch (AxisFault e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public ODL_EPR search(String query) {
		log.debug("Searching: " + query);
		
		SearchServiceStub.Search param = new SearchServiceStub.Search();
		param.setQuery(query);
		try {
			SearchServiceStub.SearchResponse res = searchService.search(param);
			return new ODL_EPR(res.getOut());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SearchServiceExceptionException0 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
