package com.eactive.eai.common.util;

import com.eactive.eai.common.activemq.AmqQueueProducer;
import com.eactive.eai.common.exception.ExceptionUtil;
import com.eactive.eai.common.routing.JMSQueueSender;

public class JMSSender
{
    private static int getMqType(String connectionUri) {
    	int type = 1; // rabbitmq
    	
    	if(connectionUri == null) return 0;
    	
    	if(connectionUri.startsWith("amqp:")) {
    		type = 1;
    	}
    	else if(connectionUri.startsWith("tcp:")) {
    		type = 2;
    	}
    	else if(connectionUri.startsWith("vm:")) {
    		type = 2;
    	}
    	else if(connectionUri.startsWith("jms/")) {
    		type = 3;
    	}
    	else {
    		type = 0;
    	}
    	
    	return type;
    }
    public static void sendToQueue(Object data, String connectionUri, String destination ) 
            throws Exception {
    	try {
    		switch(getMqType(connectionUri)) {
	    		case 1 :
//	    			QueueProducer producer = new QueueProducer(connectionUri, destination);    		
//	        		producer.sendMessage((java.io.Serializable)data);
//	        		break;
	    			throw new Exception("not implemented connection type - " +connectionUri);
	    		case 2: 	
	        		AmqQueueProducer aproducer = new AmqQueueProducer(connectionUri, destination);    		
	        		aproducer.sendMessage((java.io.Serializable)data);
	        		break;
	    		case 3 :
	    			JMSQueueSender sender = new JMSQueueSender();
    				sender.sendObject(connectionUri, destination, (java.io.Serializable) data, null);
    				break;
	        	default :
	        		throw new Exception("Unknown connectionUri - " +connectionUri + " / type - " + getMqType(connectionUri) );
    		}
        } catch(Exception e) {
            throw new Exception(ExceptionUtil.getErrorCode(e,"RECEAICUL001"));
        } finally {
            
        }
    }
}
