package eu.dnetlib.common.ws.subscription;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.ws.wsaddressing.W3CEndpointReference;

import eu.dnetlib.common.utils.EprUtils;
import eu.dnetlib.enabling.is.sn.rmi.ISSNService;

public class TextEngineSubscription extends AbstractSubscription {


	public TextEngineSubscription(ISSNService snService, String serviceProfId,
			String serviceHost) {
		super(snService, serviceProfId, serviceHost);
	}
	
	public TextEngineSubscription(){
		super();
	}
	
	public void initSubscriptions() {
		log.debug("Subscribing to Update VocabularyDSResourceType Names of Language");
		this.initVocabularyNotificationHandler();
	}
	
	private void initVocabularyNotificationHandler() {
		String topic = NotificationConstants.SUBSCR_TOPIC_UPDATE_VOCABULARY_LANG;
		String subscrIdUpdateVocabulary = SUBSCR_ID_UNSPECIFIED;
		
		W3CEndpointReference W3CEPR = null;
		try {
			W3CEPR = EprUtils.buildW3CEPR(this.serviceHost,
					this.serviceHost+"?wsdl");
		} catch (ParserConfigurationException e1) {
			log.error("Problem during creating W3CEPR ", e1);
		}
		
		if(W3CEPR != null) {
			try {
				subscrIdUpdateVocabulary = snService.subscribe(W3CEPR, 
						topic, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to " + NotificationConstants.VOCABULARY_RESOURCE_TYPE + " UPDATE with subscrId:" 
						+ subscrIdUpdateVocabulary);
			} catch(Exception e) {
				log.error("couldn't subscribe to " + NotificationConstants.VOCABULARY_RESOURCE_TYPE + 
						" with UPDATE topic type!",e);
			}
		}	
	}


}
