/// <summary> /// This method checks the given <see cref="AOIData"/> /// if it is already used in the database. /// </summary> /// <param name="shape">Ref. A <see cref="AOIData"/> with /// the area of interest to test.</param> /// <param name="splashDialog">A <see cref="System.ComponentModel.BackgroundWorker"/> with the /// background splash loading dialog worker which shows progress.</param> /// <returns><strong>True</strong>, if an AOI with same name and stimulus assignment /// already exists in the database, /// otherwise <strong>false</strong>.</returns> private static bool CheckForAOIDuplicate(AOIData shape, System.ComponentModel.BackgroundWorker splashDialog) { SQLiteOgamaDataSet.AOIsDataTable aoisTable = Document.ActiveDocument.DocDataSet.AOIsAdapter.GetDataByTrialIDAndShapeName(shape.TrialID, shape.ShapeName); if (aoisTable.Rows.Count > 0) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } string message = "The area of interest " + Environment.NewLine + shape.ShapeType.ToString() + ": " + shape.ShapeName + "already exists." + Environment.NewLine + "Would you like to overwrite it ?"; DialogResult duplicateQuestion = InformationDialog.Show("Overwrite AOI ?", message, true, MessageBoxIcon.Question); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } if (duplicateQuestion == DialogResult.No || duplicateQuestion == DialogResult.Cancel) { return false; } // User clicked yes so update the existing entry // Presuming the DataTable has a column named ShapeName. string expression = "ShapeName = '" + shape.ShapeName + "' AND TrialID = '" + shape.TrialID + "'"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = Document.ActiveDocument.DocDataSet.AOIs.Select(expression); if (foundRows.Length > 0) { foreach (DataRow row in foundRows) { row["ShapeType"] = shape.ShapeType; row["ShapeNumPts"] = shape.ShapeNumPts; row["ShapePts"] = shape.ShapePts; } } return false; } return true; }
/** * loc : Save Location * url : login URL * uid : User ID * pwd : User Pwd */ public JobResult getCVS(string loc , string uid, string pwd , List<string> items , ref int n , ref int max , ref System.ComponentModel.BackgroundWorker worker) { //string searchPage = @"http://www.unfranchise.com.tw/index.cfm?action=meetings.nmSvSe&mtgSrchType=b"; string searchPage = @"https://tw.unfranchise.com/index.cfm?action=meetings.unfMeetingSearchResults"; JobResult results = new JobResult(); // Login into Unfranchise System. bool LoginSuccess = false; //Login Unfranchise.com.tw for (int i = 1; i <= 3; i++) { LoginSuccess = login(uid, pwd); if (LoginSuccess == true) break; else Thread.Sleep(3000); } if (LoginSuccess == false) { log.Error("登入失敗,無法執行下載作業"); results.IsDownloadSuccess = false; results.ErrMsg.Add("登入失敗,無法執行下載作業"); return results; } try { //Get all Hidden input tag in Search Page string searchMainPage = @"https://tw.unfranchise.com/index.cfm?action=meetings.unfMeetingSearch"; string strSPageCnt = HTTPTool.DownloadContentsUTF8(Request.CookieContainer, searchMainPage, string.Empty, 2); HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); htmlDoc.LoadHtml(strSPageCnt); var inputs = htmlDoc.DocumentNode.Descendants("input"); foreach (var input in inputs) { if ((input.Attributes["type"].Value == "hidden") && (input.Attributes["name"] != null)) items.Add(input.Attributes["name"].Value + "=" + input.Attributes["value"].Value); } } catch (Exception ex9) { log.Fatal("Error at getCVS()-Step0- (Get All Hide Input Tag):" + ex9.Message); throw new Exception("Error at getCVS()-Step0(Get All Hide Input Tag):" + ex9.Message); } //Compose Post Data StringBuilder sbPostData = new StringBuilder(); for (int i = 0; i < items.Count; i++) { if (i == items.Count - 1) sbPostData.Append(items[i]); else sbPostData.Append(items[i]).Append("&"); } string strPostData = sbPostData.ToString(); //HttpWebRequest Request; byte[] data = new ASCIIEncoding().GetBytes(strPostData); // get each meeting data from all links StringBuilder meetings = new StringBuilder(); try { #region 保留程式碼 //Request = (HttpWebRequest)WebRequest.Create(searchPage); //Request.Method = "POST"; //Request.ContentType = "application/x-www-form-urlencoded"; //Request.ContentLength = data.Length; //Request.KeepAlive = true; //Request.CookieContainer = m_Cookies; ////send the request Data //Stream newStream = Request.GetRequestStream(); //newStream.Write(data, 0, data.Length); //newStream.Close(); ////Get Response //HttpWebResponse Response = (HttpWebResponse)Request.GetResponse(); //using (StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.UTF8)) //{ // string tmpStr = sr.ReadToEnd(); // List<string> MeetingLinks = new List<string>(); // string[] aryValue = GetAttribute(tmpStr, "a", "href"); // for (int k = 0; k < aryValue.Length; k++) // { // //只取出我要的Meeting Links // if (aryValue[k].IndexOf("index.cfm?action=meetings.nmMtgDtl&ID=") >= 0) // MeetingLinks.Add(aryValue[k].Trim()); // } //} #endregion DateTime t1 = DateTime.Now; string SearchResults = HTTPTool.DownloadContentsUTF8(Request.CookieContainer, searchPage, strPostData, 1); DateTime t2 = DateTime.Now; double dd = new TimeSpan(t2.Ticks - t1.Ticks).TotalSeconds; List<string> MeetingLinks = new List<string>(); //確認是否被導到登入頁 if (SearchResults.IndexOf("顯示活動的") < 0) log.Error("無法取得查詢結果-_-|||"); else results.IsDownloadSuccess = true; //string[] aryValue = GetAttribute(SearchResults, "a", "href"); //for (int k = 0; k < aryValue.Length; k++) //{ // //只取出我要的Meeting Links // if (aryValue[k].IndexOf("index.cfm?action=meetings.nmMtgDtl&ID=") >= 0) // MeetingLinks.Add(aryValue[k].Trim()); //} HtmlAgilityPack.HtmlDocument resultDoc = new HtmlAgilityPack.HtmlDocument(); resultDoc.LoadHtml(SearchResults); var links = resultDoc.DocumentNode.Descendants("a"); foreach (var link in links) { if ((link.Attributes["class"]!= null) && (link.Attributes["href"] != null)) if(link.Attributes["class"].Value == "event") MeetingLinks.Add(link.Attributes["href"].Value); } max = MeetingLinks.Count; worker.RunWorkerAsync(0); //Get all Meeting Links Detail Contents. for (int j = 0; j < MeetingLinks.Count; j++) { n = j; string url = @"https://tw.unfranchise.com/" + MeetingLinks[j]; string SingleInfo = HTTPTool.DownloadContentsUTF8(m_Cookies , url, "", 2); string strMeetingInfo = getMeetingInfo2(SingleInfo); if (strMeetingInfo != string.Empty) meetings.AppendLine(getMeetingInfo2(SingleInfo)); else { results.ERRORURL.Add(url); results.ErrMsg.Add("此筆資料查無其細節內容(" + url + "), 行事曆產生將跳過此筆資料。"); results.IsDownloadSuccess = false; } Application.DoEvents(); } } catch (Exception ex) { log.Fatal("Error at getCVS()-Step1:" + ex.Message); throw new Exception("Error at getCVS()-Step1:" + ex.Message); } //Compose to csvFile string header = @"""主旨"",""開始日期"",""開始時間"",""結束日期"",""結束時間"",""全天"",""提醒開啟/關閉"",""提醒日期"",""提醒時間"",""會議召集人"",""出席者"",""列席者"",""會議資源"",""地點"",""忙閒狀態"",""私人"",""津貼"",""帳目資訊"",""敏感度"",""描述"",""優先順序"",""類別"""; try { //清除不必要的字詞 meetings.Replace(" ", ""); //輸出成CSV檔 using (StreamWriter sw = new StreamWriter(loc , false,Encoding.UTF8)) { sw.WriteLine(header); sw.Write(meetings.ToString()); } } catch (Exception ex1) { log.Fatal(@"Error at getCVS()-Step 2:" + ex1.Message); //throw new Exception("Error at getCVS()-Step 2:" + ex1.Message); results.IsDownloadSuccess = false; results.ErrMsg.Add(@"Error at getCVS()-Step 2:" + ex1.Message); } //背景作業設為可取消 worker.WorkerSupportsCancellation = true; return results; }
/// <summary> /// This method checks the given <see cref="AOIData"/> shape for invalid characters /// and tests if it is already used in the database. /// It shows referring message boxes when the name is not valid /// and automatically removes invalid characters. /// </summary> /// <param name="shape">Ref. A <see cref="AOIData"/> with /// the area of interest to test.</param> /// <param name="splashDialog">A <see cref="System.ComponentModel.BackgroundWorker"/> with the /// background splash loading dialog worker which shows progress.</param> /// <returns><strong>True</strong>, if the AOI is valid, /// otherwise <strong>false</strong>.</returns> public static bool ValidateShape(ref AOIData shape, System.ComponentModel.BackgroundWorker splashDialog) { shape.ShapeName.Trim(); StringBuilder sb = new StringBuilder(); bool changed = false; for (int i = 0; i < shape.ShapeName.Length; i++) { if (char.IsLetterOrDigit(shape.ShapeName[i])) { sb.Append(shape.ShapeName[i]); } else { changed = true; } } shape.ShapeName = sb.ToString(); if (shape.ShapeName == string.Empty) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } InformationDialog.Show( "Empty shape name", "Please enter at least one character for the shape name", false, MessageBoxIcon.Information); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return false; } else if (IOHelpers.IsNumeric(shape.ShapeName[0])) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } InformationDialog.Show( "Invalid shape name", "Please do not use a number for the first character of the shape name.", false, MessageBoxIcon.Information); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return false; } else { if (changed) { if (splashDialog != null && splashDialog.IsBusy) { splashDialog.CancelAsync(); } string message = "Please note: Non letter and non digit characters are removed from the shape name." + Environment.NewLine + "Other characters are not allowed for the database entry."; ExceptionMethods.ProcessMessage("Shape name modified", message); // Show loading splash screen if it is not running if (splashDialog != null && !splashDialog.IsBusy) { splashDialog.RunWorkerAsync(); } return true; } if (!CheckForAOIDuplicate(shape, splashDialog)) { return false; } } return true; }