/**
 * Copyright 2008-2009 DRIVER PROJECT (Bielefeld University)
 * Original author: Marek Imialek <marek.imialek at uni-bielefeld.de>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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;

/**
 * @author <a href="mailto:marek.imialek at uni-bielefeld.de">Marek Imialek</a>
 *
 */
public class IndexSubscription extends AbstractSubscription{

	
	
	public IndexSubscription(ISSNService snService, String serviceProfId,
			String serviceHost) {
		super(snService, serviceProfId, serviceHost);
	}
	
	public IndexSubscription(){
		super();
	}

	public void initIndexNotificationHandler() {
		
		log.debug("Subscribing to Update IndexServiceResourceType topic" +
				" (changes in status part of index profile ");
		//String supportedIndexStatusTopic = NotificationConstants.PREFIX_TOPIC_UPDATE_INDEX_SERVICE +
		//	this.indexServiceProfId +
		//	NotificationConstants.SUFFIX_TOPIC_UPDATE_INDEX_SERVICE_STATUS;

		String topic = NotificationConstants.PREFIX_SUBSCR_TOPIC_UPDATE_INDEX_SERVICE +
			this.serviceProfId +
			NotificationConstants.SUFFIX_SUBSCR_TOPIC_UPDATE_INDEX_SERVICE_STATUS;
		
		try {
			//String serviceAddress = consumerReference.replace("?WSDL", "");
			W3CEndpointReference W3CEPR = EprUtils.buildW3CEPR(this.serviceHost,
					this.serviceHost+"?WSDL");

			String subscrIdIdxStatusUpd = snService.subscribe(W3CEPR, topic,
					NotificationConstants.TERMINATION_TIME_INFINITE);
			log.debug("subscribed to " + topic +
					" with subscrId:" + subscrIdIdxStatusUpd);
		} catch (ParserConfigurationException e){
			log.error("Problem during creating W3CEPR ", e);
		} catch(Exception e) {
//			exc may be thrown when using old IndexService schema lacking READ_ONLY status element
			log.error("couldn't subscribe with topic " + topic, e);
		}
	}
	
	public void initCollectionNotificationHandler() {
		
		log.debug("Subscribing to Create CollectionDSResourceType topic");
		
		String subscrIdCreate = SUBSCR_ID_UNSPECIFIED;
		String subscrIdDelete = SUBSCR_ID_UNSPECIFIED;
		String subscrIdUpdate = 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 {
				subscrIdCreate = snService.subscribe(W3CEPR, 
						NotificationConstants.SUBSCR_TOPIC_CREATE_COLLECTION, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to CollectionDSResourceType CREATE with subscrId:" + subscrIdCreate);
			} catch(Exception e) {
				log.error("couldn't subscribe to CollectionDSResourceType with CREATE topic type!",e);
			}
			try {
				subscrIdDelete = snService.subscribe(W3CEPR, 
						NotificationConstants.SUBSCR_TOPIC_DELETE_COLLECTION, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to CollectionDSResourceType DELETE with subscrId:" + subscrIdDelete);
			} catch(Exception e) {
				log.error("couldn't subscribe to CollectionDSResourceType with DELETE topic type!",e);
			}
			try {
				subscrIdUpdate = snService.subscribe(W3CEPR, 
						NotificationConstants.SUBSCR_TOPIC_UPDATE_COLLECTION, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to CollectionDSResourceType UPDATE with subscrId:" + subscrIdUpdate);
			} catch(Exception e) {
				log.error("couldn't subscribe to CollectionDSResourceType with UPDATE topic type!",e);
			}
		}	
	}
	
	public void initMDFormatNotificationHandler() {
		
		log.debug("Subscribing to Update/Delete MDFormatDSResourceType topic");
		
		String subscrIdDeleteMDF = SUBSCR_ID_UNSPECIFIED;
		String subscrIdUpdateMDF = 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 {
				subscrIdDeleteMDF = snService.subscribe(W3CEPR, 
						NotificationConstants.SUBSCR_TOPIC_DELETE_MD_FORMAT, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to MDFormatDSResourceType DELETE with subscrId:" 
						+ subscrIdDeleteMDF);
			} catch(Exception e) {
				log.error("couldn't subscribe to MDFormatDSResourceType" +
						" with DELETE topic type!",e);
			}
			try {
				subscrIdUpdateMDF = snService.subscribe(W3CEPR, 
						NotificationConstants.SUBSCR_TOPIC_UPDATE_MD_FORMAT, 
						NotificationConstants.TERMINATION_TIME_INFINITE);
				log.debug("subscribed to MDFormatDSResourceType UPDATE with subscrId:" 
						+ subscrIdUpdateMDF);
			} catch(Exception e) {
				log.error("couldn't subscribe to MDFormatDSResourceType" +
						" with UPDATE topic type!",e);
			}
		}	
	}

	public void initSubscriptions() {
		this.initIndexNotificationHandler();
		this.initCollectionNotificationHandler();
		this.initMDFormatNotificationHandler();		
	}
}
