Пример #1
0
        // Left Tab - search protocols according to word\s
        private void PhraseSearch(object sender, RoutedEventArgs e)
        {
            //reset\hide "No results" message
            noResultsMessagePhrase.Visibility = Visibility.Hidden;

            Mouse.OverrideCursor = Cursors.Wait; //set mouse cursor to "busy" status while searching & preparing results
            try
            {
                char[] nameListSplit = new char[] { ' ' };

                string          selectedExpression = cbPhraseList.Text;
                ParagraphReader paragraphReader    = new ParagraphReader();
                List <string>   searchWords        = paragraphReader.ReadWords(selectedExpression);

                if (searchWords.Count == 0)
                {
                    MessageBox.Show("חובה להכניס ביטוי לחיפוש");
                    return;
                }

                string firstWord = searchWords[0];
                IQueryable <ParagraphWord> selecetedExpression = context.ParagraphWords.Where(x => (x.word == firstWord));

                for (int j = 1; j < searchWords.Count; j++)
                {
                    // for technical reasons we need to save values used in the query expressions as simple variables
                    // and not access them through arrays, etc...
                    // we alsp need to copy values that might change until the query is executed.
                    string temp  = searchWords[j];
                    int    tempI = j;
                    selecetedExpression = selecetedExpression.Join(context.ParagraphWords,
                                                                   x => new { x.c_name, x.pr_number, x.pg_number, word_number = x.word_number + tempI, word = temp },
                                                                   y => new { y.c_name, y.pr_number, y.pg_number, y.word_number, y.word },
                                                                   (x, y) => x);
                }

                // fetch results
                var resultsRaw = selecetedExpression.Take(MAX_DISPLAY_RECORD).Include("paragraph.words").ToList();
                // highlight search phrase and create a result snippet for each result
                var results = resultsRaw.Select(x => new ParagraphMatch(x.paragraph, x, searchWords)).ToList();

                if (results.Count > 0)
                {
                    // display results
                    lstPhraseSearchResults.ItemsSource = results;
                }
                else
                {
                    // Display "NO results" message
                    lstPhraseSearchResults.ItemsSource = string.Empty;
                    noResultsMessagePhrase.Visibility  = Visibility.Visible;
                }
            }
            finally //set mouse cursor back to normal
            {
                Mouse.OverrideCursor = null;
            }
        }
Пример #2
0
        private void SaveGroup(object sender, RoutedEventArgs e)
        {
            // first do validations that do not reqire db access

            string grpName = groupNameTxt.Text;

            if (grpName.Length == 0)
            {
                MessageBox.Show("חובה להזין שם לקבוצה");
                groupNameTxt.Focus();
                return;
            }
            ParagraphReader  reader = new ParagraphReader(); // use paragraph read to remove all "non-word" chars and split into words
            HashSet <string> items  = new HashSet <string>(reader.ReadWords(wordsListTxt.Text));

            if (items.Count == 0)
            {
                MessageBox.Show("חובה להזין מילים לקבוצה");
                wordsListTxt.SelectAll();
                return;
            }
            else if (items.Count > WordsGroup.MaxItemsInGroup)
            {
                MessageBox.Show(string.Format("לא ניתן להזין יותר מ-{0} מילים בקבוצה", WordsGroup.MaxItemsInGroup));
                return;
            }
            try
            {
                using (KnessetContext context = new KnessetContext())
                {
                    // now do validations that do reqire db access
                    WordsGroup existing = context.WordsGroups.Find(grpName);
                    if (existing != null)
                    {
                        MessageBox.Show("כבר קיימת קבוצה עם שם זה");
                        return;
                    }

                    // input is OK, save the new group, if a word is not in the words relation add it
                    // (we might define groups before loading protocols)
                    WordsGroup group = new WordsGroup {
                        g_name = grpName
                    };
                    context.WordsGroups.Add(group);

                    foreach (var wordStr in items)
                    {
                        Word wordObj = context.Words.Find(wordStr);
                        if (wordObj == null)
                        {
                            wordObj = new Word {
                                word = wordStr
                            };
                            context.Words.Add(wordObj);
                        }
                        context.WordInGroups.Add(new WordInGroup {
                            wordsGroup = group, WordObj = wordObj
                        });
                    }
                    context.SaveChanges(); // commit all changes to DB
                }
                DialogResult = true;       // can be used by parent window - marks success
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }