def __init__(self):
     from cc_vector_manager.routing_algorithms import UCD_MIA_Alogrithm
     from cc_vector_webservices.services.gateway import AgentGateway, CallGateway
     self.algo = UCD_MIA_Alogrithm()
     #create gateway instances     
     self.agentGateway = AgentGateway()
     self.callGateway = CallGateway()
class CallCenterVectorManager(object):
    """ Handles selection of agents based on different routing algorithms.
        Currently implementing UCD-MIA.
    """
    
    
    def __init__(self):
        from cc_vector_manager.routing_algorithms import UCD_MIA_Alogrithm
        from cc_vector_webservices.services.gateway import AgentGateway, CallGateway
        self.algo = UCD_MIA_Alogrithm()
        #create gateway instances     
        self.agentGateway = AgentGateway()
        self.callGateway = CallGateway()
        
    def getNextAgent(self,msg):
        from urllib import urlencode
        from urllib2 import urlopen, Request
        from config import IVRSettings as SETTINGS
        #get next available agent 
        log.info("in CallCenterVectorManager.getNextAgent()")
        #get message contents
        loadedMessage = simplejson.loads(msg.body)
        ccxmlSessionID = loadedMessage['session']
        ccxmlUniqueID = loadedMessage['id']              
        
        #get available agents
        #returns <Agent Instance>
        agents = self.agentGateway.getAvailableAgentsByIdleTime()
        
        #use our selected UCD_MIA algorithm to select the next agent
        agentWithMostIdleTime = self.algo.getNextAgent(agents)
        
        if agentWithMostIdleTime:            
            #create call with agent
            call = self.callGateway.addCall(agent=agentWithMostIdleTime)
            #update agent phone status
            agentWithMostIdleTime=self.agentGateway.updateAgentPhoneStatus(agentWithMostIdleTime,1)
            #inject event into ccxml session
            data = urlencode({SETTINGS.IVR_SESSION_KEY:ccxmlSessionID, SETTINGS.IVR_EVENT_KEY:SETTINGS.IVR_EVENT_VALUE, \
                              SETTINGS.IVR_UNIQUE_ID_KEY:ccxmlUniqueID, SETTINGS.IVR_AGENT_ANI_KEY:agentWithMostIdleTime.ani, \
                              SETTINGS.IVR_DESTINATION_TYPE_KEY: agentWithMostIdleTime.determineANIType(), \
                              SETTINGS.IVR_CALL_ID_KEY: call.call_id})
            url = SETTINGS.IVR_URL
            request = Request(url, data)
            log.info("ccxml url: " + request.get_full_url() + request.get_data())
            response = urlopen(request)
            if response:
                log.info("Agent assigned to call sucessfully")
            else:
                log.info("Agent assigned to call unsucessfully")
        else:
            #No agent was found
            #send call back to queue
            publisher_args = {MQConfig.DEFAULT_ROUTING_KEY_KEY: MQConfig.DEFAULT_ROUTING_KEY_VALUE,
                          MQConfig.DEFAULT_DELIVERY_MODE_KEY: MQConfig.DEFAULT_DELIVERY_MODE,
                          MQConfig.DEFAULT_EXCHANGE_VALUE_KEY: MQConfig.DEFAULT_EXCHANGE_VALUE
                         }        
            publisher = CallCenterVectorPublisher(**publisher_args)
        
            #setup message
            msg = simplejson.dumps({'session': ccxmlSessionID , 'id': ccxmlUniqueID})
            log.info("message dumped as json")
            publisher.publish_to_queue(msg)
            log.info("message pushed to queue")
            publisher.close()