/**
 * 
 */
package eu.dnetlib.data.textengine.ws;

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

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import eu.dnetlib.common.interfaces.ws.ServiceException;
import eu.dnetlib.common.nh.Notification;
import eu.dnetlib.common.nh.NotificationManagerImpl;
import eu.dnetlib.data.textengine.categorizer.LanguageCategorizerWrapper;
import eu.dnetlib.data.textengine.extractor.ExtractorException;
import eu.dnetlib.data.textengine.extractor.IExtractor;

/**
 * @author Jochen Schirrwagen (jochen.schirrwagen@uni-bielefeld.de)
 *
 */
public class ServiceNotificationManager extends NotificationManagerImpl {
	
	private static Log log = LogFactory.getLog(ServiceNotificationManager.class);

	private static final String topic_vocab = "UPDATE.VocabularyDSResourceType";
	private LanguageCategorizerWrapper languageCategorizer;
	private IExtractor extractor;

	/* (non-Javadoc)
	 * @see org.driver.ws.NotificationListener#processNotification(org.driver.ws.Notification)
	 */
	public boolean processNotification(Notification notification) {
		
		log.debug("topic: " + notification.getTopic());
		if (notification.getTopic().startsWith(topic_vocab)){
			log.debug("update Mapping");
			languageCategorizer.getVocabMapper().updateMapping(notification.getMessage());
			return true;
		}
		return false;
	}
	

	/* (non-Javadoc)
	 * @see org.driver.ws.NotificationListener#processRequest(org.driver.ws.Notification)
	 */
	public String processRequest(Notification notification) throws ServiceException{
		String text = "";
		if (languageCategorizer == null){
			log.error("langcat not constructed.");
		}
		if (notification.getParameters().containsKey(NotificationParameter.FILE)){
			try {
				InputStream inStream = (new URL(
											notification.getParameters().get(NotificationParameter.FILE)))
											.openStream();
				text = getExtractor().extract(inStream);
				log.debug(text);
			} catch (IOException e) {
				log.error(e);
				throw new ServiceException("io error occured when opening stream," + e.getMessage(), e);
			} catch (ExtractorException e) {
				log.error(e);
				throw new ServiceException("error occured when extracting text," + e.getMessage(), e);
			}

		}else if (notification.getParameters().containsKey(NotificationParameter.TEXT)){
			text = notification.getParameters().get(NotificationParameter.TEXT);			
		}
		return languageCategorizer.findLanguage(text);
	}

	/**
	 * @return the languageCategorizer
	 */
	public LanguageCategorizerWrapper getLanguageCategorizer() {
		return languageCategorizer;
	}

	/**
	 * @param languageCategorizer the languageCategorizer to set
	 */
	public void setLanguageCategorizer(LanguageCategorizerWrapper languageCategorizer) {
		this.languageCategorizer = languageCategorizer;
	}

	public void setExtractor(IExtractor extractor) {
		this.extractor = extractor;
	}

	public IExtractor getExtractor() {
		return extractor;
	}
	
	
}
