package eu.dnetlib.common.profile;

import java.util.LinkedList;
import java.util.List;

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

import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;

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

	private ISLookUpService lookupservice;
	
	public List<Resource> getResources(String xquery){
		List<Resource> list = new LinkedList<Resource>();
		try {
			List<String> profileList = lookupservice.quickSearchProfile(xquery);
			if (profileList != null){
				for (String profile: profileList){
					Resource resource = new Resource(profile);
					list.add(resource);
				}				
			}
		} catch (ISLookUpException e) {
			log.error(e);
		} catch (DocumentException e) {
			log.error(e);
		}
		return list;
	}
	
	public Resource getResource(String id) {
		Resource resource = null;
		String profile;
		try {
			profile = lookupservice.getResourceProfile(id);
			resource = new Resource(profile);
		} catch (ISLookUpDocumentNotFoundException e) {
			log.error(e);
		} catch (ISLookUpException e) {
			log.error(e);
		} catch (DocumentException e) {
			log.error(e);
		}
		return resource;
	}

	
	@Override
	public void removeResource(String id, Resource resource) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void updateResource(String id, Resource resource) {
		// TODO Auto-generated method stub
		
	}

	public void setLookupservice(ISLookUpService lookupservice) {
		this.lookupservice = lookupservice;
	}



}
