package eu.dnetlib.functionality.community.app;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import eu.dnetlib.api.functionality.CommunityService;
import eu.dnetlib.api.functionality.CommunityServiceException;
import eu.dnetlib.domain.ServiceIdentity;
import eu.dnetlib.domain.enabling.Notification;
import eu.dnetlib.domain.functionality.Community;
import eu.dnetlib.domain.functionality.CommunitySearchCriteria;
import eu.dnetlib.functionality.community.dao.CommunityDAO;
import gr.uoa.di.driver.app.DriverServiceImpl;
import gr.uoa.di.driver.dao.DAOException;

/**
 * 
 * @author thanos@di.uoa.gr
 *
 */
public class CommunityServiceImpl extends DriverServiceImpl implements CommunityService {
	private CommunityDAO dao = null;
	
	/**
	 * Set the DAO.
	 * @param dao the new community dao to use
	 */
	public void setDao(CommunityDAO dao) {
		this.dao = dao;
	}
	
	@Override
	public void init() {
		super.init();
	}
	
	@Override
	public void deleteCommunity(Community community)
			throws CommunityServiceException {
		try {
			dao.delete(community);
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
		
	}

	@Override
	public void deleteCommunityById(String id) throws CommunityServiceException {
		deleteCommunity(getCommunityById(id));
	}

	@Override
	public Community getCommunityById(String id)
			throws CommunityServiceException {
		try {
			return dao.getById(id); 
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
	}

	@Override
	public Community saveCommunity(Community community)
			throws CommunityServiceException {
		try {
			
			Logger.getLogger(getClass()).debug("<<-- saving community with authoritative documents = " + community.getAuthoritativeDocuments() + "-->>");
			
			return dao.save(community);
		} catch (DAOException e) {
			throw new CommunityServiceException(e); 
		}
	}

	@Override
	public ServiceIdentity identify() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Community> searchCommunities(CommunitySearchCriteria criteria)
			throws CommunityServiceException {
		try {
			return dao.search(criteria);
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
	}

	@Override
	public List<String> searchCommunityIds(CommunitySearchCriteria criteria)
			throws CommunityServiceException {
		List<String> result = new ArrayList<String>();
		try {
			for (Community community: dao.search(criteria))
				result.add(community.getResourceId());
			return result;
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
	}

	@Override
	public boolean isManager(String communityId, String userId)
			throws CommunityServiceException {
		try {
			return dao.getById(communityId).getManagers().contains(userId);
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
	}

	@Override
	public boolean isOwner(String communityId, String userId)
			throws CommunityServiceException {
		try {
			return dao.getById(communityId).getOwner().equals(userId);
		} catch (DAOException e) {
			throw new CommunityServiceException(e);
		}
	}

	@Override
	public void notify(Notification notification) {
		// TODO Auto-generated method stub
	}
}
