package eu.dnetlib.common.nh;

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

import eu.dnetlib.common.interfaces.nh.IBlackboardMessage;
import eu.dnetlib.common.interfaces.nh.IMessageHandler;
import eu.dnetlib.common.interfaces.nh.MessageException;
import eu.dnetlib.common.profile.utils.Marshaller;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;


/**
 *  Copyright ©2008-2009 DRIVER-II Project All Rights Reserved
 *
 *  @author Jochen Schirrwagen (jochen.schirrwagen@uni-bielefeld.de) 
 */
/**
 * @author Jochen Schirrwagen (jochen.schirrwagen@uni-bielefeld.de)
 *
 */
public class MessageHandlerImpl implements IMessageHandler {

	private static Log log = LogFactory.getLog(MessageHandlerImpl.class);
	
	private ISRegistryService	 	registryService;

	public void replyBlackboardMessage(String profileId, IBlackboardMessage blackboardMessage) throws MessageException{
		try{
			registryService.replyBlackBoardMessage(profileId, Marshaller.generateMessagePartOfBlackBoard(blackboardMessage));			
		}catch(Exception e){
			log.error("Error occured while replying blackboard message.", e);
			throw new MessageException(e);
		}
	}
	
	public void sendBlackboardMessage(String profileId, IBlackboardMessage blackboardMessage) throws MessageException{
		try {
			registryService.addBlackBoardMessage(profileId, blackboardMessage.getId(), Marshaller.generateMessagePartOfBlackBoard(blackboardMessage));
		} catch (ISRegistryException e) {
			log.error("Error occured while sending blackboard message.", e);
			throw new MessageException(e);
		}
		
	}

	public void updateProfile(String profileId, String profile,
			String resourceType) throws MessageException {
		try {
			this.registryService.updateProfile(profileId, profile, resourceType);
		} catch (ISRegistryException e) {
			log.error(e);
			throw new MessageException(e);
		}
	}
	
	public void setRegistryService(ISRegistryService registryService) {
		this.registryService = registryService;
	}

	public ISRegistryService getRegistryService() {
		return registryService;
	}



}
