package com.agcnetworks.flair.controllers; import javax.telephony.Call; import javax.telephony.Provider; import javax.telephony.Terminal; import javax.telephony.TerminalConnection; import javax.telephony.callcontrol.CallControlCall; import com.agcnetworks.flair.App; import com.agcnetworks.flair.connectors.SQLiteConnector; import com.agcnetworks.flair.interfaces.CallDirection; import com.agcnetworks.flair.interfaces.CallState; import com.agcnetworks.flair.interfaces.CallTypes; import com.agcnetworks.flair.interfaces.ResponseCodes; import com.agcnetworks.flair.models.CallResponse; import com.agcnetworks.flair.models.ConferenceRequest; import com.agcnetworks.flair.models.Session; import com.agcnetworks.flair.utils.Utils; import com.avaya.jtapi.tsapi.LucentConsultOptions; import com.avaya.jtapi.tsapi.LucentTerminalConnection; import com.avaya.jtapi.tsapi.LucentV10Call; import com.avaya.jtapi.tsapi.UserToUserInfo; import org.apache.log4j.Logger; public class DgftController { private static Logger logger = Logger.getLogger(CallController.class); public CallResponse performIvrTransfer(ConferenceRequest conferenceRequest) { CallResponse callResponse = new CallResponse(); logger.info("Request received. request=" + conferenceRequest.toString()); try { int ivrCallId = consultCallWithUui(conferenceRequest); if (ivrCallId != 0) { transferCallToIvr(conferenceRequest.getSessionId(), conferenceRequest.getPrimaryCallId(), ivrCallId); } else { callResponse.setResponseCode(ResponseCodes.JTAPI_ERROR); callResponse.setResponseMessage( "IVR call could not be made, please try again, or contact support if issue persists"); } } catch (Exception e) { logger.error("Exception: ", e); callResponse.setResponseCode(ResponseCodes.GENERAL_ERROR); callResponse.setResponseMessage(e.getMessage()); } return callResponse; } private int consultCallWithUui(ConferenceRequest conferenceRequest) { logger.info("Request received. request=" + conferenceRequest.toString()); Provider myProvider = App.getProvider(); CallResponse callResp = new CallResponse(); Session mySession = null; String newCallUcid = null; String deviceId = null; Integer newCallId = null; LucentV10Call primaryCall = null; // get required info from request String sessionId = conferenceRequest.getSessionId(); String dialedNumber = conferenceRequest.getDialedNumber(); Integer primaryCallId = conferenceRequest.getPrimaryCallId(); // get session for which the request belongs if (sessionId != null) { mySession = SessionController.getSessionFromSessionId(conferenceRequest.getSessionId()); } if (mySession != null) { deviceId = mySession.getDeviceId(); TerminalConnection[] termConns = mySession.getTerminal().getTerminalConnections(); LucentTerminalConnection myTermConn = (LucentTerminalConnection) termConns[0]; if (myTermConn == null) { logger.debug("Transfer Controller is null."); } else { try { if (primaryCallId != 0) { primaryCall = (LucentV10Call) mySession.getActiveCall(conferenceRequest.getPrimaryCallId()); if (primaryCall != null) { String uuiString = ""; UserToUserInfo existingUui = primaryCall.getUserToUserInfo(); if (existingUui != null) { uuiString = existingUui.getString(); } String ani = ""; LucentV10Call newCall = (LucentV10Call) myProvider.createCall(); try { ani = primaryCall.getCallingAddress().getName(); } catch (Exception e) { logger.error("Exception while repacking UUI: ", e); } // primaryCall.getUCID(); uuiString = uuiString + "|" + mySession.getAgentId() + "|" + ani; UserToUserInfo uuiWithAgentId = new UserToUserInfo(uuiString); newCall.setTransferEnable(true); newCall.consult(myTermConn, dialedNumber, false, uuiWithAgentId, LucentConsultOptions.TRANSFER_ONLY); newCallId = Utils.getCallId(newCall); callResp.setSuccessResponse(); mySession.addCallResponse(callResp); mySession.addActiveCall(newCallId, newCall); SQLiteConnector.dbInsertCall(newCallId, newCallUcid, deviceId, dialedNumber, CallTypes.CONF, CallDirection.OUTGOING, CallState.UNKNOWN); return newCallId; } } return 0; } catch (Exception e) { logger.error("Exception: ", e); return 0; } } } return 0; } private CallResponse transferCallToIvr(String sessionId, int primaryCallId, int ivrCallId) { Session mySession = SessionController.getSessionFromSessionId(sessionId); Call existingCall = mySession.getActiveCall(primaryCallId); Terminal myTerminal = mySession.getTerminal(); TerminalConnection myTermConn = myTerminal.getTerminalConnections()[0]; CallResponse resp = new CallResponse(); if (myTermConn == null) { logger.debug("Cannot Initiate Consult Call. myTermConn=null"); resp.setResponseCode(ResponseCodes.GENERAL_ERROR); resp.setResponseMessage( "Cannot Initiate Consult Call, please try again or contact admin if issue persists"); } else { try { CallControlCall newCall = (CallControlCall) mySession.getActiveCall(ivrCallId); ((CallControlCall) existingCall).setTransferController(myTermConn); ((CallControlCall) newCall).transfer(existingCall); resp.setResponseCode(ResponseCodes.SUCCESS); } catch (Exception e) { logger.error(e); resp.setResponseCode(ResponseCodes.GENERAL_ERROR); resp.setResponseMessage(e.getLocalizedMessage()); } } return resp; } }