package com.eactive.eai.common.jeus;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Properties;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicPublisher;
import javax.jms.TopicSession;

import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.jms.JmsServiceLocator;
import com.eactive.eai.common.util.Logger;

public class JeusTopicProducer {
	
	Logger logger = Logger.getLogger(Logger.LOGGER_DEFAULT);
	
    TopicConnectionFactory topicFactory = null;
    
    String connectionFactory = null;
    String destination       = null;
    String correlationID     = null;
	
	public JeusTopicProducer(String connectionUrl, String topicName, String correlationID) throws Exception {
		this.connectionFactory = connectionUrl;
		this.destination = topicName;
		this.correlationID = correlationID;
	}
	
	//JMSSender.sendToTopic(eaiMessage, topicConFactory, routeTopicName, null, traceKey, ttl);
	
	public void sendMessage(Serializable object, Properties prop, long ttl) throws Exception {
		TopicConnection tcon = null;
	    TopicSession tsession = null;
	    Topic topic = null;
	    TopicPublisher publisher = null;   
	    try {
        	if (logger.isDebug()){
        		logger.debug("sendToTopic] connectionFactory = "+connectionFactory);
        		logger.debug("sendToTopic] destination       = "+destination);
        		logger.debug("sendToTopic] correlationID     =["+correlationID+"]");
        	}
        	JmsServiceLocator locator = JmsServiceLocator.getInstance();
            topicFactory = locator.getTopicConnectionFactory(connectionFactory);
            topic = locator.getTopic(destination);

            tcon = topicFactory.createTopicConnection();
            tsession = tcon.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
            
            publisher = tsession.createPublisher(topic);
            
            // TTL Setting
            if(ttl > 0) {
            	publisher.setTimeToLive(ttl);
            }
            
            ObjectMessage msg = tsession.createObjectMessage();
            msg.setObject(object);
            if (correlationID != null)
                msg.setJMSCorrelationID(correlationID);
            
            // Properties setting setStringProperties();
            if(prop!=null) {
                Iterator it = prop.keySet().iterator();
                while(it.hasNext()) {
                    String key = (String)it.next();
                    String value = prop.getProperty(key);
                    msg.setStringProperty(key,value);   
                }   
            }            
            publisher.publish(msg);            
        } catch(Exception e) {
            throw new JMSException(ExceptionUtil.getErrorCode(e,"RECEAICUL002"));
        } finally {
            try {
                if(publisher!=null) publisher.close();    
            } catch(Exception e) {
            	// nothing to do
            }
            try {
                if(tsession!=null) tsession.close();    
            } catch(Exception e) {
            	// nothing to do
            }
            try {
                if(tcon!=null) tcon.close();    
            } catch(Exception e) {
            	// nothing to do
            }
        }      
	}	
}
