//===========================================================================
// Smarclient FPMS Demo Project a sample project for SmartClient technology.
// Smarclient FPMS Demo is designed and implemented bases Microsoft Application Block tech.
// Author: SFPMS Team, Date 2004-11
// Vision 1.0
// Revison History
//=============================================================================
using System;
using System.Data;
using Microsoft.ApplicationBlocks.SmartClient.Offline;
using JSW.FPMS.Tool;
using JSW.FPMS.Xiang.ShiJian;


namespace JSW.FPMS.Xiang.YeWu
{
	/// <summary>
	/// clsYeWuDaBiao:ServiceAgent的子类。通过该类，拼装数据请求，插入该请求到队列和处理返回信息。
	/// </summary>
	public class clsYeWuDaiBiao : ServiceAgent
	{
		#region 成员变量区域
		private string              chengxuZuJianMingCheng  = "FPMS_Xiang";
		private string              wangluoDaiBiaoLeiMing	= "JSW.FPMS.Xiang.YeWu.clsWangLuoDaiLi";
		private OfflineBlockBuilder lixianKongZhi = null;


		/// <summary>
		/// 当发送请求过程中出现错误的时候，业务代表发出该事件
		/// </summary>
		public event ReportErrorEventHandler ReportErrorEvent;
		
		public event XuQiuChaXunFanHuiShiJianHandler XuQiuChaXunFanHuiShiJian;

		public event HuoDeJiBenShuJuFanHuiShiJianHandler HuoDeJiBenShuJuFanHuiShiJian;

		public event HuoDePeiZhiXinXiFanHuiShiJianHandler HuoDePeiZhiXinXiFanHuiShiJian;

		public event XuQiuTiJiaoFanHuiShiJianHandler XuQiuTiJiaoFanHuiShiJian;


		#endregion

		#region 私有方法区域
		/// <summary>
		/// 发送请求，该请求返回数据
		/// </summary>
		/// <param name="FangFaMing">要调用的网络代表方法名称</param> 		
		/// <param name="XiangYingHanShu">响应请求的方法名称</param> 
		/// <param name="ShuJuJianZhi">返回数据的键值</param> 
		/// <param name="GuoQiShiJian">过期时间</param>
		private void FaSongQingQiuHuoDeShuJu(string FangFaMing,string XiangYingHanShu,
															int GuoQiShiJian,string ShuJuJianZhi) 
		{
			//程序组件名称
			string onlineProxyAssemblyName = chengxuZuJianMingCheng;
			//网络代表类绝对名称
			string onlineProxyClassName = wangluoDaiBiaoLeiMing;
			//网络代表方法名称
			string onlineProxyMethodName =FangFaMing;
			//业务代表响应方法
			string specificServiceAgentMethodTobeInvoked = XiangYingHanShu;
			//过期时间
			int absoluteExpirationTime = GuoQiShiJian;
			//数据缓存键值
			string cacheKey = ShuJuJianZhi;
			
			OnlineProxyContext onlineProxyMethodContext = new OnlineProxyContext(onlineProxyAssemblyName,
				onlineProxyClassName,
				onlineProxyMethodName);

			ServiceAgentContext specificServiceAgentContext = new ServiceAgentContext(
				specificServiceAgentMethodTobeInvoked);
			
			ReferenceDataDefinition refDataDefination = new ReferenceDataDefinition(cacheKey,
				absoluteExpirationTime, 
				onlineProxyMethodContext);
			//发送请求
			lixianKongZhi.DataLoaderManager.LoadData(this.Guid, 
				specificServiceAgentContext,
				refDataDefination);
		}

		/// <summary>
		/// 发送请求，该请求不返回数据
		/// </summary>
		/// <param name="FangFaMing">要调用的网络代表方法名称</param> 		
		/// <param name="XiangYingHanShu">响应请求的方法名称</param> 
		/// <param name="GengXinShuJu">要更新的数据</param> 
		private void FaSongQingQiuGengXinShuJu(string FangFaMing,string XiangYingHanShu,DataSet GengXinShuJu) 
		{
			//程序组件名称
			string onlineProxyAssemblyName = chengxuZuJianMingCheng;
			//网络代表类绝对名称
			string onlineProxyClassName = wangluoDaiBiaoLeiMing;
			//网络代表方法名称
			string onlineProxyMethodName =FangFaMing;
			//业务代表响应方法
			string specificServiceAgentMethodTobeInvoked = XiangYingHanShu;					
			
			OnlineProxyContext onlineProxyMethodContext = new OnlineProxyContext(onlineProxyAssemblyName,
				onlineProxyClassName,
				onlineProxyMethodName);
			ServiceAgentContext specificServiceAgentContext = new ServiceAgentContext(specificServiceAgentMethodTobeInvoked);
				
			Payload messageToEnqueue = new Payload(onlineProxyMethodContext,this.Guid,
				specificServiceAgentContext, GengXinShuJu);
			//数据请求进队列
			lixianKongZhi.PayloadConsumer.Enqueue(messageToEnqueue);
		}

		#region 响应方法区域
		/// <summary>
		/// 需求录入返回方法
		/// 生成事件
		/// </summary>
		private void XuQiuTiJiaoFanHui(Payload QingQiu) 
		{	
			// Raise an event to inform controller that data has been updated.
			if(XuQiuTiJiaoFanHuiShiJian != null)
			{
				XuQiuTiJiaoFanHuiShiJianArgs args = new XuQiuTiJiaoFanHuiShiJianArgs();
				XuQiuTiJiaoFanHuiShiJian(this,args);				
			}
		}

		/// <summary>
		/// 需求查询返回方法
		/// </summary>
		/// <returns>需求集合</returns> 
		private void XuQiuChaXunFanHui(Payload QingQiu)  
		{		
			//生成需求查询完成事件
			ReferenceCacheDataPayload huanCunQingQiu = QingQiu as ReferenceCacheDataPayload;
			if(this.XuQiuChaXunFanHuiShiJian  != null)
			{
				XuQiuChaXunFanHuiShiJianArgs args = 
					new XuQiuChaXunFanHuiShiJianArgs(huanCunQingQiu.Results);
				XuQiuChaXunFanHuiShiJian(this,args);				
			}
		}

		/// <summary>
		/// 获得基本数据返回方法
		/// </summary>
		private void HuoDeJiBenShuJuFanHui(Payload QingQiu)  
		{
			//生成获得基本数据完成事件
			ReferenceCacheDataPayload huanCunQingQiu = QingQiu as ReferenceCacheDataPayload;
			if(this.HuoDeJiBenShuJuFanHuiShiJian  != null)
			{
				HuoDeJiBenShuJuFanHuiShiJianArgs args = 
					new HuoDeJiBenShuJuFanHuiShiJianArgs(huanCunQingQiu.Results);
				HuoDeJiBenShuJuFanHuiShiJian(this,args);				
			}
			
		}
		
		/// <summary>
		/// 获得配置信息返回方法
		/// </summary>
		private void HuoDePeiZhiFanHui(Payload QingQiu)  
		{
			//生成获得基本数据完成事件
			ReferenceCacheDataPayload huanCunQingQiu = QingQiu as ReferenceCacheDataPayload;
			if(this.HuoDePeiZhiXinXiFanHuiShiJian  != null)
			{
				HuoDePeiZhiXinXiFanHuiShiJianArgs args = 
					new HuoDePeiZhiXinXiFanHuiShiJianArgs(huanCunQingQiu.Results);
				HuoDePeiZhiXinXiFanHuiShiJian(this,args);				
			}			
		}
		#endregion

		#endregion

		#region 公有方法区域
		/// <summary>
		/// 构造器
		/// </summary>
		public clsYeWuDaiBiao()
		{
			lixianKongZhi = OfflineBlockBuilder.Instance; 
		}

		/// <summary>
		/// 回调函数，当业务代表调用的请求发生了错误的时候，这个函数会被调用。
		/// </summary>
		/// <param name="refPayload">发生错误的请求</param>
		override public void ReportError(Payload refPayload) 
		{
			string cuowuXinXi = "";
			//生成错误信息
			if(refPayload.FailureReason.GetType() == typeof(System.Web.Services.Protocols.SoapException))
			{
				
				System.Text.RegularExpressions.MatchCollection mc = System.Text.RegularExpressions.Regex.Matches(
					refPayload.FailureReason.Message, 
					"---> ([^:]+):");
			
				if (mc.Count >= 1 && mc[0].Groups[1].Value == "System.Data.DBConcurrencyException")
				{
					cuowuXinXi += "参考数据同步错误 \n \n";
				}
			}

			cuowuXinXi += refPayload.FailureReason.Message;
			
			//清楚错误请求中的所有错误信息，如果你需要重新请求。
			refPayload.ClearResults();
			
			//发出错误报告消息
			if(ReportErrorEvent != null)
			{
				ReportErrorEventArgs args = new ReportErrorEventArgs();
				args.FailureReason = cuowuXinXi;
				ReportErrorEvent(this, args);
			}
			
		}
		
		/// <summary>
		/// 需求录入
		/// </summary>
		public void XuQiuTiJiao(DataSet XuQiu) 
		{
			this.FaSongQingQiuGengXinShuJu("XuQiuTiJiao","XuQiuTiJiaoFanHui",XuQiu);
		}

		/// <summary>
		/// 需求查询
		/// </summary>
		/// <returns>需求集合</returns> 
		public void XuQiuChaXun() 
		{		
			this.FaSongQingQiuHuoDeShuJu("XuQiuChaXun","XuQiuChaXunFanHui",600,clsChangLiang.SHUJU_XUQIU); 
		}

		/// <summary>
		/// 获得基本数据
		/// </summary>
		public void HuoDeJiBenShuJu() 
		{
			this.FaSongQingQiuHuoDeShuJu("HuoDeJiBenShuJu","HuoDeJiBenShuJuFanHui",600,clsChangLiang.SHUJU_JIBENSHUJU); 
		}
		
		/// <summary>
		/// 获得配置信息
		/// </summary>
		public void HuoDePeiZhi() 
		{
			this.FaSongQingQiuHuoDeShuJu("HuoDePeiZhi","HuoDePeiZhiFanHui",600,clsChangLiang.SHUJU_PEIZHIXINXI); 
		}

		#endregion
        
	}
}
