package eu.dnetlib.data.textengine.resultset;

import java.util.List;

import javax.xml.ws.wsaddressing.W3CEndpointReference;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import eu.dnetlib.data.textengine.resultset.pull.PullResultset;
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;

public class ResultsetImpl implements ResultSetService{
	
	private static final Log log = LogFactory.getLog(ResultsetImpl.class);

	private String version;
	private PullResultset pullResultset;
	
	public void closeRS(String rsId) {
		try {
			pullResultset.closeRS(rsId);
		} catch (ResultsetException e) {
			log.error(e);
		}
	}

	public W3CEndpointReference createPullRS(String dataProviderServiceAddress,
			String bdId, int initialPageSize, int expiryTime,
			String styleSheet, Integer keepAliveTime, Integer total) {
		
		eu.dnetlib.resultset.impl.CreatePullRSType internalCreatePullRS_type = null;
		internalCreatePullRS_type = new eu.dnetlib.resultset.impl.CreatePullRSType();
		internalCreatePullRS_type.setKeepAliveTime(keepAliveTime);
		internalCreatePullRS_type.setStyleSheet(styleSheet);
		internalCreatePullRS_type.setTotal(total);
		
			
		try {
			return pullResultset.createPullRS(dataProviderServiceAddress, bdId, 
						initialPageSize, expiryTime, internalCreatePullRS_type);
		} catch (ResultsetException e) {
			log.error(e.getMessage());
			return null;
		}
	}

	public W3CEndpointReference createPullRSEPR(
			W3CEndpointReference dataProviderEPR, String bdId,
			int initialPageSize, int expiryTime, String styleSheet,
			Integer keepAliveTime, Integer total) {
		// TODO Auto-generated method stub
		return null;
	}

	public W3CEndpointReference createPushRS(int expiryTime, int keepAliveTime)
			throws ResultSetException {
		throw new ResultSetException("createPushRS is not implemented");
	}

	public int getNumberOfElements(String rsId) throws ResultSetException {
		try {
			return pullResultset.getNumberOfElements(rsId);
		} catch (ResultsetException e) {
			log.error(e.getMessage());
			throw new ResultSetException(e);
		}
	}

	public String getProperty(String rsId, String name)
			throws ResultSetException {
		try {
			return pullResultset.getProperty(rsId, name);
		} catch (ResultsetException e) {
			log.error(e.getMessage());
			throw new ResultSetException(e);
		}
	}

	public String getRSStatus(String rsId) throws ResultSetException {
		try {
			return pullResultset.getRSStatus(rsId);
		} catch (ResultsetException e) {
			log.error(e.getMessage());
			throw new ResultSetException(e);
		}	
	}

	public List<String> getResult(String rsId, int fromPosition,
			int toPosition, String requestMode) throws ResultSetException {
		
		List<String> localResult = null;

		try {
			localResult = pullResultset.getResult(
					rsId, fromPosition, toPosition, requestMode);
		} catch (ResultsetException e) {
			log.error(e);
			throw new ResultSetException(e);
		}
		if (localResult!=null && !localResult.isEmpty()) {
				
			return localResult;
		} else {
			return localResult;
		}
	}

	public String populateRS(String rsId, List<String> elements)
			throws ResultSetException {
		try {
			return pullResultset.populateRS(rsId, elements);
		} catch (ResultsetException e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage());
			throw new ResultSetException(e.getMessage());
		}
	}

	public String identify() {
		return this.getClass().getName() + " version-" + this.version;
	}

	public void notify(String subscriptionId, String topic, String isId,
			String message) {
		throw new RuntimeException("This method is unsupported in this ResultSet implementation!");
	}

	public void start() {
		// TODO Auto-generated method stub
		
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getVersion() {
		return version;
	}

	public void setPullResultset(PullResultset pullResultset) {
		this.pullResultset = pullResultset;
	}

	public PullResultset getPullResultset() {
		return pullResultset;
	}

}
