/**
 *  Copyright ©2008-2009 DRIVER-II Project All Rights Reserved
 *
 */
package eu.dnetlib.common.ws;

import java.util.Date;

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.INotificationListener;
import eu.dnetlib.common.interfaces.ws.IDriverService;
import eu.dnetlib.common.nh.Notification;
import eu.dnetlib.common.profile.BlackboardBody;
import eu.dnetlib.common.profile.Profile;
import eu.dnetlib.common.profile.blackboard.Message;
import eu.dnetlib.common.profile.utils.Unmarshaller;
import eu.dnetlib.enabling.tools.blackboard.NotificationHandler;

public abstract class DriverServiceImpl implements IDriverService {

	protected static final Log log = LogFactory.getLog(DriverServiceImpl.class);
	
	@Deprecated
	protected INotificationListener notifier;
	protected NotificationHandler notificationHandler;
	protected String serviceVersion;
	
	@Deprecated
	public void addListener(INotificationListener aListener){
		log.debug("register listener");
		this.notifier = aListener;
		log.debug(this.notifier.toString());
	}
	
	/* (non-Javadoc)
	 * @see org.driver.ws.DriverService#identify()
	 */
	public String identify() {
		log.debug("identify: " + serviceVersion);
		return serviceVersion;
	}

	/* (non-Javadoc)
	 * @see org.driver.ws.DriverService#notify(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
	 */
	public boolean notify(String subscrId, String topic, String isId,
			String message) {
		boolean returnValue = false;
		log.debug("---- service got notification ----");
		log.debug("subscrId: " + subscrId);
		log.debug("topic " + topic);
		log.debug("isId " + isId);
		log.debug("msg: " + message);
		log.debug("____ now processing the notification ____");
		Profile profile = Unmarshaller.unmarshallServiceResource(message);
		BlackboardBody bbBody = (BlackboardBody)profile.getBody();
		// TODO validate
		IBlackboardMessage bbMessage = (Message)bbBody.getBlackboard().getMessageForLastRequest();		
		
		Notification notification = new Notification();
		notification.setDate(new Date());
		notification.setIsId(isId);
		notification.setMessage(bbMessage);
		notification.setMessageList(bbBody.getBlackboard().getMessages());
		notification.setSubscriptionId(subscrId);
		notification.setTopic(topic);
		notification.setResourceIdentifier(profile.getHeader().getResourceIdentifier());
		notification.setResourceType(profile.getHeader().getResourceType());
		notification.setResourceUri(profile.getHeader().getResourceURI());
		if (this.notifier == null){
			log.debug("notifier is null");
		}else{
			log.debug("processing by notifier");
			returnValue = this.notifier.processNotification(notification);
		}
		
		if (notificationHandler != null){
			notificationHandler.notified(subscrId, topic, isId, message);
		}
		return returnValue;
	}
	
	/**
	 * @return the notifier
	 */
	@Deprecated
	public INotificationListener getNotifier() {
		return notifier;
	}

	/**
	 * @param notifier the notifier to set
	 */
	@Deprecated
	public void setNotifier(INotificationListener notifier) {
		this.notifier = notifier;
	}

	public String getServiceVersion() {
		return serviceVersion;
	}

	public void setServiceVersion(String serviceVersion) {
		this.serviceVersion = serviceVersion;
	}

	/**
	 * @param notificationHandler the notificationHandler to set
	 */
	public void setNotificationHandler(NotificationHandler notificationHandler) {
		this.notificationHandler = notificationHandler;
	}

	/**
	 * @return the notificationHandler
	 */
	public NotificationHandler getNotificationHandler() {
		return notificationHandler;
	}

}
