/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package EMIDA.comm;
import EMIDA.CATMainMidlet;
import EMIDA.Constants;
import EMIDA.Utils;
import EMIDA.xml.XmlResponseParse;
/**
*
* @author nmartinez
*/
public class ConnectorBox extends Connection implements Runnable{
private int retrys=0;
private int tryTime=0;
private CATMainMidlet mainMidlet;
private boolean comesFromUpdateCarriersSplash=false;
private boolean comesFromUpdateCarriersOtherPhone=false;
private boolean comesFromSignOn=false;
private boolean comesFromHistory=false;
private boolean comesFromLocator=false;
private boolean comesFromTopUp=false;
private boolean comesFromUpgrade=false;
private boolean comesFromSupport=false;
private boolean comesFromACHListAccounts=false;
private StringBuffer xmlResponse = null;
private StringBuffer sb_xmlResponse;
private String pin;
private String standalone_operation;
private boolean ifDebug=true;
/*
* urlData = This is data that includes user number, version, siteuser, lang etc (NOT sms_req operation)
* sms = This is the operation command "bal pin"
* mobileNumber = consumers login number
* pinNumber = passsword
* smssend =&sendSMS=
* operation = The sms_req="+Constants.sms_req
*/
public ConnectorBox(CATMainMidlet mainMidlet, String urlData, String sms, String mobileNumber, String pinNumber, String smssend,
String operation)
{
this.pin = pinNumber;
this.mobilenumber = mobileNumber;
this.mainMidlet = mainMidlet;
String urlDataServer="";
String immediateValue="true";
//we must log in first and get the consumers data and then at a later point get the carrier data
if ( sms.startsWith(Constants.smsActionSignOn)){
comesFromSignOn = true;
//sms = Utils.replace(sms,Constants.smsActionSignOn,Constants.smsActionCarriers);
sms = Utils.replace(sms,Constants.smsActionSignOn,Constants.smsActionSignOn);
}
else if (sms.startsWith(Constants.smsActionAutomaticTaskSplash) ) {
comesFromUpdateCarriersSplash = true;
sms = Utils.replace(sms,Constants.smsActionAutomaticTaskSplash,Constants.smsActionCarriers);
}
else if (sms.startsWith(Constants.smsActionAutomaticTaskOtherPhone)) {
comesFromUpdateCarriersOtherPhone = true;
sms = Utils.replace(sms,Constants.smsActionAutomaticTaskOtherPhone,Constants.smsActionCarriers);
}
else if (sms.startsWith(Constants.smsActionHistory)) {
comesFromHistory = true;
}
else if (sms.startsWith(Constants.smsActionTopUp1)) {
comesFromTopUp = true;
sms = Utils.replace(sms,Constants.smsActionTopUp1,Constants.smsActionTopUp);
immediateValue = "false";
}
else if ( sms.startsWith(Constants.smsActionTopUp2) ){
comesFromTopUp = true;
sms = Utils.replace(sms,Constants.smsActionTopUp2,Constants.smsActionTopUp);
immediateValue = "false";
}
else if (sms.startsWith(Constants.smsActionLocations)) {
comesFromLocator = true;
urlDataServer = urlData;
}
else if (sms.startsWith(Constants.smsActionUpgrade)) {
comesFromUpgrade = true;
urlDataServer = urlData;
}
else if (sms.startsWith(Constants.smsActionSupport)) {
comesFromSupport = true;
urlDataServer = urlData;
}
else if (sms.startsWith(Constants.smsActionListAccounts) ) {
comesFromACHListAccounts = true;
urlDataServer = urlData;
}
if (ifDebug) {
System.out.println("sms VAR before encode "+sms );
}
sms = CATMainMidlet.urlEncode(sms);
//urlDataServer = Utils.replace(urlData,Constants.sms_req,sms);
this.standalone_operation = "sms_req="+sms;
urlDataServer = urlData;
this.serverUrl = urlDataServer + "&immediate="+immediateValue + smssend + "&" + this.standalone_operation;
this.serverUrlToCheckTransaction = urlData;
if (ifDebug) {
System.out.println("this.standalone_operation "+this.standalone_operation );
System.out.println("urlDataServer "+urlDataServer );
System.out.println("this.serverUrl "+this.serverUrl );
System.out.println("this.serverUrlToCheckTransaction "+this.serverUrlToCheckTransaction );
}
}
public void run()
{
int retriesTemp = 0;
boolean waitingResponse = true;
setXmlResponse(null);
if ( comesFromTopUp )
{
configureValuesForAsyncProcess();
String recID = "" + this.getUniqueID();
String tmpAsyncRequest = this.serverUrl + "&smsRecId=" + recID;
if (ifDebug)
System.out.println("tmpAsyncRequest "+tmpAsyncRequest );
StringBuffer xmlTemp = this.sendRequest(tmpAsyncRequest, this.mainMidlet,this.standalone_operation);
String status = xmlTemp.toString().toLowerCase();
if ( status.indexOf("success") > 0 )
{
setSb_xmlResponse(new StringBuffer(this.mainMidlet.setLang("AsyncMsg4")));
System.out.println("Waiting... ["+xmlTemp+"]");
String sms = CATMainMidlet.urlEncode(Constants.smsActionChckTrx+pin);
//tmpAsyncRequest = Utils.replace(this.serverUrlToCheckTransaction,Constants.sms_req,sms);
tmpAsyncRequest = this.serverUrlToCheckTransaction;
tmpAsyncRequest = tmpAsyncRequest + "&smsRecId=" + recID +"&immediate=true";
while( retriesTemp < retrys && waitingResponse )
{
try
{
Thread.sleep(this.tryTime);
}
catch (Exception ex)
{
ex.printStackTrace();
}
xmlTemp = this.sendRequest(tmpAsyncRequest, this.mainMidlet,this.standalone_operation);
System.out.println("AsyncRequest... ["+xmlTemp+"]");
status = xmlTemp.toString().toLowerCase();
if ( status.indexOf("success") > 0 || status.indexOf("failed") > 0 )
{
waitingResponse = false;
}
else if (status.indexOf("pending") > 0 )
{
setSb_xmlResponse(new StringBuffer(this.mainMidlet.setLang("AsyncMsg3")));
}
retriesTemp++;
}
if ( waitingResponse )
{
xmlTemp = new StringBuffer();
xmlTemp.append("<emida><Result>failed</Result><msg>").append(this.mainMidlet.setLang("AsyncMsg5"));
xmlTemp.append("</msg></emida>");
setSb_xmlResponse(xmlTemp);
}
}
setXmlResponse(xmlTemp);
}
else
{
setSb_xmlResponse(new StringBuffer(this.mainMidlet.setLang("AsyncMsg3")));
setXmlResponse(this.sendRequest(this.serverUrl, this.mainMidlet,this.standalone_operation));
}
if ( this.getXmlResponse() != null )
{
XmlResponseParse xmlResponseParse = new XmlResponseParse(mainMidlet,
comesFromUpdateCarriersSplash,
comesFromUpdateCarriersOtherPhone,
comesFromSignOn,
comesFromLocator,
comesFromHistory,
comesFromTopUp,
comesFromUpgrade,
comesFromSupport,
comesFromACHListAccounts);
xmlResponseParse.parseResponseServer(this.getXmlResponse());
}
}
private void configureValuesForAsyncProcess(){
try{
this.retrys = Integer.parseInt(this.mainMidlet.getAppProperty("EMIDA_APP_RETRY_VALUE"));
if ( this.retrys > 10){
this.retrys = 10;
}
}
catch(Exception e)
{
this.retrys = 3;
}
try{
this.tryTime = Integer.parseInt(this.mainMidlet.getAppProperty("EMIDA_APP_TIMEOUT_VALUE"));
if ( this.tryTime > 20 || this.tryTime < 5 ){
this.tryTime = 20;
}
}
catch(Exception e)
{
this.tryTime = 5;
}
finally{
this.tryTime = this.tryTime * 1000;
}
}
/**
* @return the xmlResponse
*/
public StringBuffer getXmlResponse() {
return xmlResponse;
}
/**
* @param xmlResponse the xmlResponse to set
*/
public void setXmlResponse(StringBuffer xmlResponse) {
this.xmlResponse = xmlResponse;
}
/**
* @return the sb_xmlResponse
*/
public StringBuffer getSb_xmlResponse() {
return sb_xmlResponse;
}
/**
* @param sb_xmlResponse the sb_xmlResponse to set
*/
public void setSb_xmlResponse(StringBuffer sb_xmlResponse) {
this.sb_xmlResponse = sb_xmlResponse;
}
}