Esempio n. 1
0
        public static FilterCondition Create(string colName, FilterModel model, TextSearchOption poorMansGridOptions)
        {
            if (model.FieldType == "text")
            {
                return(new TextFilter(colName, model, poorMansGridOptions));
            }

            if (model.FieldType == "number")
            {
                return(new NumberFilter(colName, model));
            }

            return(new DateFilter(colName, model));
        }
Esempio n. 2
0
        //
        // Summary:
        //     /// Adds PoorMan's Grid FilterService
        //     to the /// Microsoft.Extensions.DependencyInjection.IServiceCollection. ///
        //
        // Parameters:
        //   services:
        //     The Microsoft.Extensions.DependencyInjection.IServiceCollection to add services
        //     to.
        //
        // Returns:
        //     The Microsoft.Extensions.DependencyInjection.IServiceCollection so that additional
        //     calls can be chained.
        public static IServiceCollection AddPoorMansGridFilterService(this IServiceCollection services, TextSearchOption options = TextSearchOption.Default)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            OptionsServiceCollectionExtensions.AddOptions(services);
            ServiceCollectionDescriptorExtensions.TryAdd(services, ServiceDescriptor.Scoped <IFilterService>(s => new FilterService(options)));

            return(services);
        }
Esempio n. 3
0
 public FilterService(TextSearchOption options = TextSearchOption.Default)
 {
     Options = options;
 }
Esempio n. 4
0
 public TextFilter(string columnName, FilterModel model, TextSearchOption options)
 {
     GenerateTextCondition(model, columnName, options);
 }
Esempio n. 5
0
        /// <summary>
        /// Builds a lambda expression that can be used in a where clause,
        /// that searches for the given phrase (considering the search
        /// options) in the given property name(s)
        ///
        /// Eg to search for the exact phrase "This is a test phrase" in the
        /// field/property "TestField" of some object, you could call
        /// var res = someCollection.Where(SearchText("This is a test phrase", TextSearchOptions.SearchExact, o=>o.TestField));
        ///
        /// It is important to note that the properties are OR'd together,
        /// ie the returned expression will return true if the searchPhrase
        /// is found in ANY of the properties.
        /// </summary>
        /// <param name="searchPhrase"></param>
        /// <param name="opt"></param>
        /// <param name="fieldsToSearch"></param>
        /// <returns></returns>
        public static Expression <Func <MPDrawing, bool> > SearchText(string searchPhrase, TextSearchOption opt,
                                                                      params Expression <Func <MPDrawing, string> >[]
                                                                      fieldsToSearch)
        {
            int numFields = fieldsToSearch.Count();
            var propNames = new string[numFields];
            Expression <Func <MPDrawing, bool> > whereClause;

            //fetch the property names
            for (int i = 0; i < numFields; i++)
            {
                propNames[i] = GPN(fieldsToSearch[i]);
            }

            //deal with the SearchExact case
            if (opt == TextSearchOption.SearchExact)
            {
                whereClause = WhereLike(propNames[0], searchPhrase);
                for (int j = 1; j < numFields; j++)
                {
                    whereClause = whereClause.OrElse(WhereLike(propNames[j], searchPhrase));
                }
                return(whereClause);
            }
            //tokenise the search phrase
            string[] tokens = searchPhrase.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            //if there aren't any tokens (eg a string of spaces), just search the exact phrase instead
            if (!tokens.Any())
            {
                return(SearchText(searchPhrase, TextSearchOption.SearchExact, fieldsToSearch));
            }

            //create the clause for the first property
            whereClause = WhereLike(propNames[0], tokens[0]);
            for (int i = 1; i < tokens.Length; i++)
            {
                whereClause = opt == TextSearchOption.SearchAllWords
                                  ? whereClause.AndAlso(WhereLike(propNames[0], tokens[i]))
                                  : whereClause.OrElse(WhereLike(propNames[0], tokens[i]));
            }
            //if there are more fields, or them all up
            for (int j = 1; j < numFields; j++)
            {
                Expression <Func <MPDrawing, bool> > whereSubClause = WhereLike(propNames[j], tokens[0]);
                for (int i = 1; i < tokens.Length; i++)
                {
                    whereSubClause = opt == TextSearchOption.SearchAllWords
                                         ? whereSubClause.AndAlso(WhereLike(propNames[j], tokens[i]))
                                         : whereSubClause.OrElse(WhereLike(propNames[j], tokens[i]));
                }
                whereClause = whereClause.OrElse(whereSubClause);
            }
            return(whereClause);
        }
Esempio n. 6
0
        /// <summary>
        /// Метод, осуществляющий поиск файла в отдельном потоке
        /// </summary>
        /// <param name="dirPath">Стартовая папка для поиска</param>
        /// <param name="filePattern">Шаблон поиска файла</param>
        /// <param name="searchingText">Искомый текст</param>
        /// <param name="searchOption">Режим поиска</param>
        /// <param name="cToken">Токен отмены</param>
        /// <param name="processedFile">Передача из потока имени обрабатываемого файла</param>
        /// <param name="searchedFile">Передача из потока имени найденного файла</param>
        /// <param name="end">Передача из потока информации о том, что поиск завёршен</param>
        /// <returns></returns>
        Task SearchFilesAync(string dirPath,
                             string filePattern,
                             string searchingText,
                             TextSearchOption searchOption,
                             CancellationToken cToken,
                             IProgress <string> processedFile,
                             IProgress <string> searchedFile,
                             IProgress <bool> end)
        {
            return(Task.Run(() =>
            {
                dirPath += "\\";
                IEnumerable <string> fileNames = Directory.EnumerateFiles(dirPath,
                                                                          filePattern,
                                                                          SearchOption.AllDirectories)
                                                 .Select(fileName => fileName.Replace(dirPath, ""));
                Regex regex;
                switch (searchOption)
                {
                case TextSearchOption.AnyWord:
                    {
                        searchingText = "(" + searchingText.Replace(" ", "|") + ")";
                        regex = new Regex(searchingText, RegexOptions.IgnoreCase);
                        break;
                    }

                case TextSearchOption.AllWordsInAnyOrder:
                    {
                        searchingText = "(" + searchingText.Replace(" ", "|") + ")";
                        regex = new Regex(searchingText);
                        MatchCollection matchCollection = regex.Matches(searchingText);;
                        for (int i = 0; i < matchCollection.Count - 1; i++)
                        {
                            searchingText += ".+" + searchingText;
                        }
                        regex = new Regex(searchingText, RegexOptions.IgnoreCase);
                        break;
                    }

                default:
                    {
                        regex = new Regex(searchingText, RegexOptions.IgnoreCase);
                        break;
                    }
                }

                foreach (var fileName in fileNames)
                {
                    mREvent.Wait();
                    if (cToken.IsCancellationRequested)
                    {
                        return;
                    }

                    processedFile.Report(dirPath + fileName);
                    Thread.Sleep(2);

                    if (File.Exists(dirPath + fileName))
                    {
                        string fileText = "";
                        try
                        {
                            fileText = File.ReadAllText(dirPath + fileName, Encoding.Default);
                        }
                        finally { }
                        if (fileText != "")
                        {
                            if (regex.IsMatch(fileText))
                            {
                                searchedFile.Report(fileName);
                                Thread.Sleep(2);
                            }
                        }
                    }
                }
                end.Report(true);
            }, cToken));
        }