/**
 * 
 */
package eu.dnetlib.data.utility.featureextraction.dao;

import java.util.List;
import java.util.UUID;

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

import eu.dnetlib.enabling.resultset.push.CacheTransientResultSetDaoImpl;

/**
 * @author jochen
 *
 */
public class CachedResultDaoWrapper implements IResultDao{

	private static final Log log = LogFactory.getLog(CachedResultDaoWrapper.class);
	private CacheTransientResultSetDaoImpl resultSetDao;
	
	private String rsId;

	public CachedResultDaoWrapper(){
		this.rsId = UUID.randomUUID().toString();
	}
	
	public void addResults(List<String> results){
		try{
			resultSetDao.addElements(rsId, results);			
		}catch(Exception e){
			log.error(e);
			throw new IllegalStateException(e);
		}
	}
	
	public List<String> getResults(int from, int to){
		try{
			if (to > getNumberOfElements()){
				to = getNumberOfElements();
			}
			List<String> results = resultSetDao.getElements(rsId, from, to);
			assert results != null;
			return results;
			
		}catch(Exception e){
			log.error(e);
			throw new IllegalStateException(e);
		}

	}
	
	public void close(){
	}
	
	public int getNumberOfElements(){
		try{
			return resultSetDao.getSize(rsId);			
		}catch(Exception e){
			log.error(e);
			throw new IllegalStateException(e);
		}
	}


	public CacheTransientResultSetDaoImpl getResultSetDao() {
		return resultSetDao;
	}


	public void setResultSetDao(CacheTransientResultSetDaoImpl resultSetDao) {
		this.resultSetDao = resultSetDao;
	}
}
