//Search Method private void Search(string query) { #region Variable Declaration int docCount = 1; bool truncate = false; Log.Info("Search()", String.Format(Resource.String.search_execution_begins + "")); searchFragmentActivity = new SearchFragmentActivity(); #endregion using (var conn = new SQLite.SQLiteConnection(dbPath)) { var cmd = new SQLite.SQLiteCommand(conn); var searchStr = new SQLite.SQLiteCommand(conn); bool proofs = true, answers = true, searchAll = false, viewDocs = false; CheckBox answerCheck = FindViewById <CheckBox>(Resource.Id.AnswerBox), proofCheck = FindViewById <CheckBox>(Resource.Id.proofBox), searchCheck = FindViewById <CheckBox>(Resource.Id.searchAllCheckBox); RadioButton viewRadio = FindViewById <RadioButton>(Resource.Id.viewAllRadio); Spinner spinner = FindViewById <Spinner>(Resource.Id.spinner1), spinner2 = FindViewById <Spinner>(Resource.Id.spinner2); spinner.ItemSelected += new EventHandler <AdapterView.ItemSelectedEventArgs>(Spinner1_ItemSelected); spinner2.ItemSelected += new EventHandler <AdapterView.ItemSelectedEventArgs>(Spinner2_ItemSelected); string fileString = "", accessString = ""; accessString = DataTableAccess(""); if (answerCheck.Checked) { answers = false; } else { answers = true; } if (searchCheck.Checked) { searchAll = true; } else { searchAll = false; accessString = DataTableAccess(string.Format(" and documenttitle.documentname = '{0}' ", fileName)); } //Data filters if (allOpen) { if (searchAll) { fileString = "select * from Documenttitle"; } else { fileString = String.Format("select * from Documenttitle where Documenttitle.DocumentName='{0}'", fileName); } } //Doctype=3 if (catechismOpen) { if (searchAll) { fileString = TableAccess(" documenttitle.DocumentTypeID=3"); docCount = 3; } else { fileString = TableAccess(String.Format(" documenttitle.DocumentTypeID=3 and DocumentName='{0}' ", fileName)); } } //DocType = 2 if (confessionOpen) { if (searchAll) { fileString = TableAccess(" documenttitle.DocumentTypeID=2' "); docCount = 2; } else { fileString = TableAccess(String.Format(" documenttitle.DocumentTypeID=2 and DocumentName='{0}' ", fileName)); } } //DocType=1 if (creedOpen) { if (searchAll) { fileString = TableAccess(" documenttitle.DocumentTypeID=1 "); docCount = 1; } else { fileString = TableAccess(string.Format(" documenttitle.DocumentTypeID=1 and DocumentName='{0}' ", fileName)); } } //Proofs enabled if (proofCheck.Checked) { proofs = false; } else { proofs = true; } //Read Document if (viewRadio.Checked) { viewDocs = true; } else { viewDocs = false; } cmd.CommandText = fileString; searchStr.CommandText = accessString; var r = cmd.ExecuteQuery <DocumentTitle>(); var searchFields = searchStr.ExecuteQuery <Document>(); documentList = new DocumentList(); //Add Entries to DocumentList for (int x = 0; x < searchFields.Count; x++) { DocumentTitle docTitle = new DocumentTitle(); docTitle.DocumentID = searchFields[x].DocumentID; for (int y = 0; y < r.Count; y++) { if (!r[y].DocumentID.Equals(docTitle.DocumentID)) { foreach (DocumentTitle doc in r) { if (doc.DocumentID == docTitle.DocumentID) { docTitle.Title = doc.Title; docTitle.DocumentTypeID = doc.DocumentTypeID; } else { continue; } } } else { docTitle.Title = r[y].CompareIDs(docTitle.DocumentID); } } if (docTitle.Title == fileName | searchAll == true & docTitle.DocumentTypeID == docCount | searchAll & allOpen) { searchFields[x].DocumentName = docTitle.Title; Document document = new Document(); document.ChName = searchFields[x].ChName; document.DocDetailID = searchFields[x].DocDetailID; document.DocumentText = Formatter(searchFields[x].DocumentText); document.DocumentName = searchFields[x].DocumentName; document.ChNumber = searchFields[x].ChNumber; document.ChProofs = Formatter(searchFields[x].ChProofs); document.Tags = searchFields[x].Tags; documentList.Add(document); } else { continue; } } if (FindViewById <CheckBox>(Resource.Id.truncateCheck).Checked) { truncate = true; } if (viewRadio.Checked != true && query != "" && FindViewById <RadioButton>(Resource.Id.topicRadio).Checked) { if (FindViewById <RadioButton>(Resource.Id.topicRadio).Checked) { stopwatch.Start(); FilterResults(documentList, truncate, answers, proofs, searchAll, query); documentList.Reverse(); stopwatch.Stop(); } } else if (FindViewById <RadioButton>(Resource.Id.chapterRadio).Checked & query != "") { int searchInt = Int32.Parse(query); FilterResults(this.documentList, truncate, answers, proofs, searchAll, searchInt); } else if (viewDocs) { if (!FindViewById <CheckBox>(Resource.Id.searchAllCheckBox).Checked) { query = "Results for All"; } else { query = "View All"; } } if (documentList.Count > 1) { SetContentView(Resource.Layout.search_results); ViewPager viewPager = FindViewById <ViewPager>(Resource.Id.viewpager); SearchAdapter adapter = new SearchAdapter(SupportFragmentManager, documentList, query, truncate); searchFragmentActivity.DisplayResults(documentList, viewPager, adapter, query, 0, truncate); } else { stopwatch.Stop(); if (this.documentList.Count == 0) { #region Error Logging Log.Info("Search()", String.Format("No Results were found for {0}", query)); Toast.MakeText(this, String.Format("No results were found for {0}", query), ToastLength.Long).Show(); #endregion #region Variable Declaration and Assignment SetContentView(Resource.Layout.errorLayout); TextView errorMsg = FindViewById <TextView>(Resource.Id.errorTV); errorMsg.Text = String.Format("No Search Results were found for {0}\r\n\r\n" + "Go back to home page to search for another topic", query); #endregion #region Dialog Box Android.App.AlertDialog.Builder alert = new Android.App.AlertDialog.Builder(this); alert.SetTitle("No Results Found"); alert.SetMessage(String.Format("No Results were found for {0}.\r\n\r\n" + "Do you want to go back and search for another topic?", query)); alert.SetPositiveButton("Yes", (senderAlert, args) => { intent = new Intent(this, Class); searchFragmentActivity = null; this.OnStop(); this.Finish(); StartActivity(intent); }); alert.SetNegativeButton("No", (senderAlert, args) => { alert.Dispose(); }); Dialog dialog = alert.Create(); dialog.Show(); #endregion } else { //SetTitle(); Document document = this.documentList[this.documentList.Count - 1]; SetContentView(Resource.Layout.confession_results); TextView chapterBox = FindViewById <TextView>(Resource.Id.chapterText); TextView proofBox = FindViewById <TextView>(Resource.Id.proofText); TextView chNumbBox = FindViewById <TextView>(Resource.Id.confessionChLabel); TextView docTitleBox = FindViewById <TextView>(Resource.Id.documentTitleLabel); proofBox.Text = document.ChProofs; docTitleBox.Text = document.DocumentName; chapterBox.Text = document.DocumentText; if (chapterBox.Text.Contains("Question")) { header = "Question "; chNumbBox.Text = string.Format("{0} {1}: {2}", header, document.ChNumber.ToString(), document.ChName); } else if (chapterBox.Text.Contains("I.")) { header = "Chapter"; chNumbBox.Text = string.Format("{0} {1}: {2}", header, document.ChNumber.ToString(), document.ChName); } else { chNumbBox.Text = string.Format("{0}", document.DocumentName); } TextView proofView = FindViewById <TextView>(Resource.Id.proofLabel); ChangeColor(true, Android.Graphics.Color.Black, chapterBox, proofBox, chNumbBox, docTitleBox); ChangeColor(proofView, false, Android.Graphics.Color.Black); shareList = docTitleBox.Text + newLine + chNumbBox.Text + newLine + chapterBox.Text + newLine + "Proofs" + newLine + proofBox.Text; FloatingActionButton fab = FindViewById <FloatingActionButton>(Resource.Id.shareActionButton); ChangeColor(fab, Android.Graphics.Color.Black); fab.Click += ShareContent; } } } //If there is only 1 search result Toast.MakeText(this, String.Format("Search Completed for {0}" + "\r\n" + "{1} ms Passed", query, stopwatch.ElapsedMilliseconds.ToString()), ToastLength.Long).Show(); }
//Search by topic public void FilterResults(DocumentList documentList, bool truncate, bool answers, bool proofs, bool allDocs, string searchTerm) { DocumentList resultList = new DocumentList(); Log.Info("Filter", "Filtering Confession Results"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Reset(); stopwatch.Start(); Log.Debug("Timer", "Timer Started"); Log.Info("Counter", String.Format("{0} elements", searchTerm.Split(' ').Length.ToString())); //Allows for searching in documents for phrases Regex regex = new Regex(searchTerm, RegexOptions.IgnoreCase); Log.Info("Filter Results", "Filtering has begun"); foreach (Document document in documentList) { int nothere = 0; List <string> searchEntries = new List <string>(); searchEntries.Add(document.ChName); searchEntries.Add(document.DocumentText); searchEntries.Add(document.ChProofs); searchEntries.Add(document.Tags); foreach (string word in searchEntries) { //DEBUGGED Search Errors. Allows for phrases to be searched now if (searchTerm.Split(' ').Length > 1) { if (regex.IsMatch(word)) { document.Matches++; } } // Single word searches else { string[] pieces = word.Split(' '); foreach (string chunks in pieces) { if (regex.IsMatch(chunks)) { document.Matches++; } } } //Filtering for the list } if (document.Matches > 0) { resultList.Add(document); if (!answers) { if (document.DocumentText.Contains("Question")) { document.DocumentText = GetBetween(document.DocumentText, "Question:", "Answer:"); } else { continue; } } else if (!proofs) { document.ChProofs = "No Proofs available!"; } else if (truncate) { document.DocumentText = GetBetween(document.DocumentText, document.DocumentText.Substring(0), searchTerm); document.DocumentText = GetBetween(document.ChProofs, document.ChProofs.Substring(0), searchTerm); } } stopwatch.Stop(); Log.Debug("Timer", String.Format("{0}ms Passed", stopwatch.ElapsedMilliseconds.ToString())); stopwatch.Reset(); } resultList.Sort(Document.CompareMatches); this.documentList = resultList; }