/// <summary>
 /// 建構式,將Validator的Validate方法參數傳入
 /// </summary>
 /// <param name="ValidatorPairs"></param>
 /// <param name="OutputFile"></param>
 /// <param name="OutputOptions"></param>
 public OutputBuilder(IEnumerable<ValidatePair> ValidatorPairs,
     string OutputFile,
     OutputOptions OutputOptions,
     ReportProgress ReportProgress)
 {
     //初始化從Validator傳來的參數
     mValidatorPairs = ValidatorPairs.ToList();
     mOutputFile = OutputFile;
     //預設讀取第一個Excel
     mSheet = new SheetHelper(mValidatorPairs[0].DataFile, mValidatorPairs[0].DataSheet);
     mCurrentPair = mValidatorPairs[0];
     mReportProgress = ReportProgress;
 }
        /// <summary>
        /// 開始進行資料驗證
        /// </summary>
        private void StartValidate()
        {
            //建立資料驗證組合
            ValidatePair Pair = new ValidatePair();
            Pair.DataFile = mImportOption.SelectedDataFile; //資料驗證來源檔案
            Pair.DataSheet = mImportOption.SelectedSheetName; //資料驗證來源檔案資料表
            Pair.ValidatorFile = mImportOption.SelectedValidateFile; //資料驗證描述檔

            Validator.Validator valStart = new Validator.Validator();

            if (mImportWizard.CustomValidate != null)
            {
                //  2012/3/7    自訂驗證規則需要使用者選取的鍵值:paul.wang
                mImportWizard.SelectedKeyFields = mImportOption.SelectedKeyFields;
                mImportWizard.SelectedFields = mImportOption.SelectedFields;
                valStart.CustomValidate = mImportWizard.CustomValidate;
            }

            //執行資料驗證方法
            worker.DoWork += (sender, e) => valStart.Validate(Pair, mResultFilename);

            //將驗證過程顯示在畫面上
            worker.ProgressChanged += (sender, e) =>
            {
                //取得資訊物件(數量/訊息/檔名/工作表名)
                ValidatingPair obj = (ValidatingPair)e.UserState;

                //指定介面進度
                pgValidProgress.Value = e.ProgressPercentage;

                lblProgress.Text = obj.Message;

                //如果錯誤大於0
                if (obj.ErrorCount + obj.WarningCount + obj.AutoCorrectCount > 0)
                {
                    lblErrorCount.Text = ""+obj.ErrorCount;
                    lblWarningCount.Text = ""+obj.WarningCount;
                    lblCorrectCount.Text = ""+obj.AutoCorrectCount;
                }
            };

            //資料驗證完成
            worker.RunWorkerCompleted += (sender, e) =>
            {
                if (e.Error != null)
                    throw e.Error;

                int ErrorText = int.Parse(lblErrorCount.Text);
                int WarningText = int.Parse(lblWarningCount.Text);
                int CorrectText = int.Parse(lblCorrectCount.Text);

                //若是錯誤數量為0才可進行到下一步
                if (lblErrorCount.Text.Equals("0"))
                    this.NextButtonEnabled = true;

                if (ErrorText >= 1) //錯誤大於1
                    pictureBox1.Image = EMBA.Import.Properties.Resources.filter_data_close_64;
                else if (WarningText >= 1) //警告大於1
                    pictureBox1.Image = EMBA.Import.Properties.Resources.filter_data_info_64;
                else //無錯誤亦無警告時
                    pictureBox1.Image = EMBA.Import.Properties.Resources.filter_data_ok_64;

                //將檢視驗證報告的按鈕啟用
                btnViewResult.Enabled = true;

                //將可暫停非同步作業的按鈕取消
                lnkCancelValid.Enabled = false;

                if (mValidatedInfo.ValidatedPairs[0].Exceptions.Count > 0)
                {
                    string ExceptionMessage = string.Empty;

                    foreach (Exception Exception in mValidatedInfo.ValidatedPairs[0].Exceptions)
                        ExceptionMessage += Exception.Message + System.Environment.NewLine;

                    if (!string.IsNullOrEmpty(ExceptionMessage))
                    {
                        ExceptionMessage = "驗證過程中發生錯誤,以下為詳細錯誤訊息:" + System.Environment.NewLine + ExceptionMessage;
                        MessageBox.Show(ExceptionMessage);
                    }

                    pictureBox1.Image = EMBA.Import.Properties.Resources.filter_data_close_64;

                    this.NextButtonEnabled = false;
                }
            };

            //接收資料驗證進度回報函式
            valStart.Progress  = (message, progress) => worker.ReportProgress(progress, message);
            //接收資料驗證完成函式
            valStart.Complete = (message) =>
               {
                   mValidatedInfo = message;
               };

            //支援非同步取消及進度回報
            worker.WorkerSupportsCancellation = true;
            worker.WorkerReportsProgress = true;

            //運用非同步執行資料驗證
            worker.RunWorkerAsync();
        }
 /// <summary>
 /// 資料驗證
 /// </summary>
 /// <param name="ValidatorFile">驗證規則路徑</param>
 /// <param name="DataFile">驗證檔案路徑</param>
 /// <param name="DataSheet">驗證的工作表</param>
 /// <param name="OutputFile">報告輸出路徑</param>
 public void Validate(string ValidatorFile, string DataFile, string DataSheet, string OutputFile)
 {
     ValidatePair validatorPair = new ValidatePair(ValidatorFile, DataFile, DataSheet);
     Validate(new ValidatePair[] { validatorPair }, OutputFile, OutputOptions.Full);
 }
        /// <summary>
        /// 回報驗證中的訊息
        /// </summary>
        /// <param name="Pair">驗證資料及規則組合</param>
        /// <param name="ErrorCount">錯誤數目</param>
        /// <param name="WarningCount">警告數目</param>
        /// <param name="AutoCorrectCount">自動修正數目</param>
        /// <param name="Message">進度回報訊息</param>
        /// <param name="CurrentProgress">目前進度</param>
        internal void ReportProgress(ValidatePair Pair,int ErrorCount,int WarningCount,int AutoCorrectCount,string Message,int CurrentProgress)
        {
            if (Progress != null)
            {
                ValidatingPair obj = new ValidatingPair();

                obj.ErrorCount = ErrorCount;
                obj.WarningCount = WarningCount;
                obj.AutoCorrectCount = AutoCorrectCount;

                obj.Message = Message;
                obj.DataFile = Pair != null ? Path.GetFileName(Pair.DataFile) : string.Empty;
                obj.DataSheet = Pair != null ? Pair.DataSheet : string.Empty;
                Progress(obj, CurrentProgress);
            }
        }
 /// <summary>
 /// 資料驗證
 /// </summary>
 /// <param name="ValidatePair">資料驗證相關資訊(檔案,工作表,規則)</param>
 /// <param name="OutputFile">報告輸出路徑</param>
 public void Validate(ValidatePair ValidatePair, string OutputFile)
 {
     Validate(new ValidatePair[] { ValidatePair }, OutputFile, OutputOptions.Full);
 }
        /// <summary>
        /// 根據ValidatePair來切換作用中的活頁簿
        /// 1.若是最佳化模式,則直接切換資料表。
        /// 2.若非最佳化模式,則開啟新的活頁簿。
        /// </summary>
        /// <param name="Pair"></param>
        public void Switch(ValidatePair Pair)
        {
            //若是最佳化模式或是與前次驗證的檔案路徑相同,則直接切換資料表即可
            //if (IsOptimizeMode || Pair.DataFile.Equals(mCurrentPair.DataFile))
                mSheet.SwitchSeet(Pair.DataSheet);
            //else
            //    mSheet = new SheetHelper(Pair.DataFile, Pair.DataSheet);

            mCurrentPair = Pair;
        }