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 org.driver.www.wsdl.publisher_wsdl.PublisherStub;
import org.driver.www.wsdl.publisher_wsdl.PublisherStub.GetResourceById;

public class PublisherClient {

	protected Log log = LogFactory.getLog(PublisherClient.class);
	
	private PublisherStub publisher = null;

	public PublisherClient() {
	}

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

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

	public String getResourceById(String id) {
		return getResourceById(id, "DMF");
	}

	public String getResourceById(String id, String format) {
		GetResourceById req = new GetResourceById();
		req.setFormat(format);
		req.setId(id);

		try {
			return publisher.getResourceById(req).getReturn();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}
	
	/**
	 * subclasses may override this to insert record in cache
	 * 
	 * TODO: refactor it to a caching interface
	 * 
	 * @param id
	 * @param format
	 * @param record
	 */
	public void updateRecord(String id, String format, String record) {
	}

}
