/**
 * @namespace com.thesis.ajax.DataAccess
 * @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
 * imports com.thesis.ajax.DataAdapter
 */

/** @id DataAccess */
function DataAccess(selectCommand,updateCommand){
	this.DataClass = undefined;
	this.arrDataObjectList = new Array();
	this.selectCommand = selectCommand;
	this.selectDataAdapter = new DataAdapter(new DataSet("text/xml"));
	this.updateCommand = updateCommand;
	this.updateDataAdapter = new DataAdapter(new DataSet("text/code"));
	
	if(typeof DataAccess._initialized == "undefined"){
		DataAccess._initialized = true;
		
		/** @id selectedCallback */
		DataAccess.prototype.selectedCallback = function(DataObjectList){
			uEvent.Listener.notify(this,"Selected",DataObjectList);
		};
		/** @id selector */
		DataAccess.prototype.selector = function(xmlDomNode,Class,Instance,Property){
			var bDescendant = false;
			for(var i in Class[Property]){
				bDescendant = true;
				break;
			}
			if(typeof Class[Property] == "string"){
				bDescendant = false;
			}
			if(bDescendant && typeof Class[Property].root=="undefined"){
				Instance[Property] = {};
				for(var i in Class[Property]){
					if(typeof Class[Property][i] !="function"){
						Instance[Property][i]=undefined;
						this.selector(xmlDomNode,Class[Property],Instance[Property],i);
					}
				}
			}
			else if(bDescendant){
				Instance[Property] = new Array();
				var xmlNodes = xmlDomNode.selectNodes(Class[Property].root);
				for(var i=0;i<xmlNodes.length;i++){
					var oChild = {root: undefined};
					oChild.root = xmlNodes[i];
					for(var j in Class[Property]){
						if(j != "root" && typeof Class[Property][j] != "function"){
							this.selector(oChild.root,Class[Property],oChild,j);
						}
					}
					Instance[Property].push(oChild);
				}
			}
			else{
				var xmlNodes = xmlDomNode.selectNodes(Class[Property]);
				if(xmlNodes.length>1){
					Instance[Property] = new Array();
					for(var i=0;i<xmlNodes.length;i++){
						Instance[Property].push(xmlNodes[i].nodeValue);
					}
				}
				else if(xmlNodes.length==1){
					Instance[Property] = xmlNodes[0].nodeValue;
				}
				else{
					Instance[Property] = undefined;
				}
			}
		};
		/** @id conversion */
		DataAccess.prototype.conversion = function(oDataSet,Class){
			Class();
			//delete this.arrDataObjectList;
			this.arrDataObjectList = new Array();
			if(Class.prototype.sType == "Message"){
				var msg = new Class(oDataSet.sText);
				this.arrDataObjectList.push(msg);
			}
			else{
				var xmlNodes = oDataSet.xPath(Class.prototype.root);
				if(xmlNodes == null) return this.arrDataObjectList;
				for(var i=0;i<xmlNodes.length;i++){
					var Instance = new Class();
					Instance.root = xmlNodes[i];
					for(var j in Class.prototype){
						if(j!="root" && typeof Class.prototype[j]!="function"){
							this.selector(Instance.root,Class.prototype,Instance,j);
						}
					}
					this.arrDataObjectList.push(Instance);
				}
			}
			//this.selectedCallback(this.arrDataObjectList);
			return this.arrDataObjectList;
		};
		/** @id verify */
		DataAccess.prototype.verify = function(iCode){
			if(iCode && iCode==1){
				uEvent.Listener.notify(this,"Updated",this.updateCommand.ParamValueSource);
			}
			else{
				uEvent.Listener.notify(this,"UpdateFailed",this.updateCommand.ParamValueSource);
			}
		};
		/** @id update */
		DataAccess.prototype.update = function(oData){
			this.updateCommand.ParamValueSource = oData;
			this.updateDataAdapter.Connection = this.updateCommand.toConnection();
			uEvent.Listener.notify(this.updateDataAdapter,"Updating");
			this.updateDataAdapter.fillData();
		};
		/** @id select */
		DataAccess.prototype.select = function(Class,ParamSource){
			this.DataClass = Class;
			Class();
			if(Class.prototype.sType == "Message"){
				this.selectDataAdapter.DataSet.sDataType = "text";
			}
			this.selectCommand.ParamValueSource = ParamSource ;
			this.selectDataAdapter.Connection = this.selectCommand.toConnection();
			this.selectDataAdapter.fillData();
		};
		/** @id recognise */
		DataAccess.prototype.recognise = function(oDs,Class){
			var Result = this.conversion(oDs,ServiceException);
			if(Result.length>0){
				uEvent.Listener.notify(this,"Exception",Result[0]);
				return;
			}
			Result = this.conversion(oDs,SystemMessage);
			if(Result.length>0){
				uEvent.Listener.notify(this,"Message",Result[0]);
				return;
			}
			Result = this.conversion(oDs,Class);
			this.selectedCallback(Result);
		};
		/** @id initialize */
		DataAccess.prototype.initialize = function(oRoot){
			uEvent.Listener.add(this.selectDataAdapter,"Loaded",function(oDS){
				uEvent.Listener.notify(oRoot,"Selecting");
				oRoot.recognise(oDS,oRoot.DataClass);
				//oRoot.conversion(oDS,oRoot.DataClass);
			});
			uEvent.Listener.add(this.updateDataAdapter,"Loaded",function(oDS){
				oRoot.verify(oDS.iCode);
			});
		};
	}
	this.initialize(this);
}
