/**
 * @namespace com.thesis.ajax.DataAdapter
 * @author 钟军锐 August.R@263.net
 * imports com.thesis.base.Detect
 * imports com.thesis.util.Event
 * imports com.thesis.ajax.Connection
 * imports com.thesis.ajax.DataSet
 * imports com.thesis.ajax.DataLink
 */

/** @id DataAdapter */
function DataAdapter(oDataSet,oConnection){
	this.DataSet=oDataSet;
	this.Connection=oConnection;
	this.eConnection = undefined;
	this.DataLink=undefined;
	this.sEncose = undefined;
	this.sDecode = undefined;
	this.sPretreatmentData = undefined;
	this.bInitialized = false;
	if(typeof DataAdapter._initialized == "undefined"){
		DataAdapter._initialized = true;
		/** @id callbackFunction */
		DataAdapter.prototype.callbackFunction = function(oDS){
			uEvent.Listener.notify(this,"Loaded",oDS);
		};
		/** @id encode */
		DataAdapter.prototype.encode = function(sData){
			this.sEncose = sData;
			//do something about encode here
			return this.sEncose;
		};
		/** @id encodeConnection */
		DataAdapter.prototype.encodeConnection = function(){
			if(this.Connection && this.Connection.bEncode){
				delete this.eConnection;
				this.eConnection = new Connection(this.Connection.sURL);
				this.eConnection.bCompress=this.Connection.bCompress;
				this.eConnection.bDecode=this.Connection.bDecode;
				this.eConnection.bDecompress=this.Connection.bDecompress;
				this.eConnection.bEncode=this.Connection.bEncode;
				this.eConnection.sMethod=this.Connection.sMethod;
				for(var i=0;i<this.Connection.arrParam.length;i++){
					this.eConnection.addParam(this.Connection.arrParam[i].sParamName,this.encode(this.Connection.arrParam[i].sParamValue));
				}
				return this.eConnection;
			}
			else{
				return this.Connection;
			}
		};
		/** @id decode */
		DataAdapter.prototype.decode = function(sEncodeData){
			this.sDecode = sEncodeData;
			//do someting about decode here
			return this.sDecode;
		};
		/** @id dataPretreat */
		DataAdapter.prototype.dataPretreat = function(sData){
			if(this.Connection.bDecode){
				this.sPretreatmentData = this.decode(sData);
			}
			else{
				this.sPretreatmentData = sData;
			}
			//do something here
			return this.sPretreatmentData;
		};
		/** @id loadData */
		DataAdapter.prototype.loadData = function(sPretreatedData){
			switch(this.DataSet.sDataType.toLowerCase()){
				case "text/xml" :
					this.DataSet.XMLDocument.loadXML(sPretreatedData);
					break;
				case "text/code" :
					this.DataSet.iCode = parseInt(sPretreatedData);
					this.callbackFunction(this.DataSet);
					break;
				default :
					this.DataSet.sText = sPretreatedData;
					this.callbackFunction(this.DataSet);
			}
		};
		/** @id initialize */
		DataAdapter.prototype.initialize = function(oRoot){
			this.DataSet.clear();
			if(this.DataSet.sDataType.toLowerCase()=="text/xml"){
				this.DataSet.XMLDocument = new XmlDocument();
				this.DataSet.XMLDocument.onreadystatechange = function(){
					if(oRoot.DataSet.XMLDocument.readyState == 4){
						oRoot.callbackFunction(oRoot.DataSet);
					}
				};
			}
			if(this.bInitialized == false){
				this.bInitialized = true;
				this.DataLink = new DataLink();
				uEvent.Listener.add(this.DataLink,"Arrived",function(sData){
					uEvent.Listener.notify(oRoot,"Loading",sData);
					oRoot.loadData(oRoot.dataPretreat(sData));
				});
			}
			this.DataLink.Connection = this.encodeConnection();
		};
		/** @id fillData */
		DataAdapter.prototype.fillData = function(){
			this.initialize(this);
			this.DataLink.request();
		};
	}
}
