/// <summary>
        /// Configurations the filter from filter and configuration filter.
        /// </summary>
        /// <param name="upmFilter">The upm filter.</param>
        /// <param name="configFilter">The configuration filter.</param>
        /// <returns>Config</returns>
        public static UPConfigFilter ConfigFilterFromFilterAndConfigFilter(UPMFilter upmFilter, UPConfigFilter configFilter)
        {
            if (upmFilter == null)
            {
                throw new ArgumentNullException(nameof(upmFilter));
            }

            if (configFilter == null)
            {
                throw new ArgumentNullException(nameof(configFilter));
            }

            var parameters = configFilter.Parameters();

            if (upmFilter.GetType() == typeof(UPMSelectCatalogFilter))
            {
                return(QueryForCatalogFilter(configFilter, parameters, upmFilter));
            }

            switch (upmFilter.FilterType)
            {
            case UPMFilterType.Distance:
                return(QueryForDistance(configFilter, parameters, upmFilter));

            case UPMFilterType.Date:
                return(QueryForDate(configFilter, parameters, upmFilter));

            case UPMFilterType.Catalog:
            case UPMFilterType.DependentCatalog:
                return(QueryForCatalog(configFilter, parameters, upmFilter));

            default:
                return(QueryForDefault(configFilter, parameters, upmFilter));
            }
        }
        private static UPMFilter GetFilter(string filtername, UPConfigFilterParameters parameters)
        {
            UPMFilter filter     = null;
            var       parameter  = parameters.FirstParameter();
            var       fieldInfo  = parameter.CrmFieldInfo;
            var       type       = fieldInfo.FieldType;
            var       identifier = FieldIdentifier.IdentifierWithInfoAreaIdRecordIdFieldId(fieldInfo.InfoAreaId, filtername, fieldInfo.FieldId.ToString());

            if (type == CatalogFilterK || type == CatalogFilterX)
            {
                filter = GetCatalogFilter(parameter, identifier, fieldInfo);
            }
            else if (type == DateFilter)
            {
                filter = GetDateFilter(parameters, identifier, fieldInfo);
            }
            else if (parameters.AllParameterNames != null &&
                     (parameters.AllParameterNames.Contains("$parGPSXmin") || parameters.AllParameterNames.Contains("$parGPSXmax") ||
                      parameters.AllParameterNames.Contains("$parGPSYmin") || parameters.AllParameterNames.Contains("$parGPSYmax")))
            {
                filter = GetUPMDistanceFilter(identifier, fieldInfo);
            }
            else if (!string.IsNullOrEmpty(fieldInfo.RepType))
            {
                filter = GetRepFilter(identifier, fieldInfo);
            }
            else if (type == EditFieldF || type == EditFieldS || type == EditFieldL || type == EditFieldC || type == EditFieldB)
            {
                filter = GetEditFieldFilter(parameters, identifier, fieldInfo, type);
            }

            if (filter != null)
            {
                var fieldValueArray = parameter.Condition.FieldValues;
                if (fieldValueArray.Count > 1 && (string)fieldValueArray[0] == "$parValue")
                {
                    var defaultValueArray = new List <object>(fieldValueArray);
                    defaultValueArray.RemoveAt(0);
                    filter.SetDefaultRawValues(defaultValueArray);
                }
            }

            return(filter);
        }
        /// <summary>
        /// Filters for name.
        /// </summary>
        /// <param name="filtername">The filtername.</param>
        /// <param name="filterParameters">The filter parameters.</param>
        /// <returns>Filter</returns>
        public static UPMFilter FilterForName(string filtername, Dictionary <string, object> filterParameters = null)
        {
            var       configStore  = ConfigurationUnitStore.DefaultStore;
            var       configFilter = configStore.FilterByName(filtername);
            UPMFilter filter       = null;

            if (configFilter != null)
            {
                var selectCondition = configFilter.ConditionWith("Parameter:Select", true);
                if (selectCondition != null)
                {
                    return(FilterForConfigFilter(configFilter, selectCondition.ValueAtIndex(0), filterParameters, false));
                }

                selectCondition = configFilter.ConditionWith("Parameter:SingleSelect");
                if (selectCondition != null)
                {
                    return(FilterForConfigFilter(configFilter, selectCondition.ValueAtIndex(0), filterParameters, true));
                }

                var replacements = UPConditionValueReplacement.ReplacementsFromValueParameterDictionary(filterParameters, true);
                configFilter = configFilter.FilterByApplyingReplacements(replacements);
                var parameters = configFilter?.Parameters();
                if (parameters?.NumberOfParameters > 0)
                {
                    filter = GetFilter(filtername, parameters);
                }
                else
                {
                    var identifier = FieldIdentifier.IdentifierWithInfoAreaIdRecordIdFieldId(configFilter.InfoAreaId, filtername, string.Empty);
                    filter = new UPMNoParamFilter(identifier);
                }

                if (filter != null)
                {
                    filter.Name        = filtername;
                    filter.DisplayName = !string.IsNullOrEmpty(configFilter?.DisplayName) ? configFilter.DisplayName : filtername;
                }
            }

            return(filter);
        }
        private static UPConfigFilter QueryForDefault(UPConfigFilter configFilter, UPConfigFilterParameters parameters, UPMFilter upmFilter)
        {
            if (!(parameters?.NumberOfParameters > 0))
            {
                return(configFilter);
            }

            if (parameters.NumberOfParameters > 1 && parameters.NumberOfParameters == parameters.NumberOfUnnamedParameters)
            {
                var unnmaedParameters = parameters.UnnamedParameters;
                var rawValues         = upmFilter.RawValues;
                var valueCount        = rawValues.Count;
                if (valueCount == unnmaedParameters.Count)
                {
                    for (var i = 0; i < valueCount; i++)
                    {
                        parameters.SetUnnamedValueForIndex(rawValues[i], i);
                    }

                    return((UPConfigFilter)configFilter.QueryByApplyingFilterParameters(parameters));
                }

                return(configFilter);
            }
            else
            {
                var rawValues = upmFilter.RawValues;
                if (rawValues?.Count == 0)
                {
                    return(configFilter);
                }

                var firstParameter = parameters.FirstParameter();
                firstParameter.Values = rawValues;
                return((UPConfigFilter)configFilter.QueryByApplyingFilterParameter(firstParameter));
            }
        }
        private static UPConfigFilter QueryForCatalog(UPConfigFilter configFilter, UPConfigFilterParameters parameters, UPMFilter upmFilter)
        {
            if (!(parameters?.NumberOfParameters > 0))
            {
                return(configFilter);
            }

            var firstParameter = parameters.FirstParameter();
            var rawValues      = upmFilter.RawValues;

            firstParameter.Values = rawValues;
            if (firstParameter.ValueIndex == 1 && upmFilter.FilterType == UPMFilterType.Catalog)
            {
                firstParameter.RemoveFirstValuesFromValueSet = true;
                return((UPConfigFilter)configFilter.QueryByApplyingFilterParameter(firstParameter));
            }

            if (upmFilter.FilterType == UPMFilterType.DependentCatalog && rawValues.Count > 1)
            {
                return((UPConfigFilter)configFilter.QueryByApplyingFilterParameter(firstParameter));
            }

            return((UPConfigFilter)configFilter.QueryByApplyingFilterParameter(firstParameter));
        }
        private static UPConfigFilter QueryForDate(UPConfigFilter configFilter, UPConfigFilterParameters parameters, UPMFilter upmFilter)
        {
            if (!(parameters?.NumberOfParameters > 0))
            {
                return(configFilter);
            }

            if (parameters.NumberOfParameters == parameters.NumberOfUnnamedParameters)
            {
                var unnmaedParameters = parameters.UnnamedParameters;
                var rawValues         = upmFilter.RawValues;
                var valueCount        = rawValues.Count;
                for (var i = 0; i < parameters.NumberOfUnnamedParameters; i++)
                {
                    var param = unnmaedParameters[i];
                    if (valueCount > i)
                    {
                        param.Value = rawValues[i];
                    }
                }

                return((UPConfigFilter)configFilter.QueryByApplyingFilterParameters(parameters));
            }

            var firstParameter = parameters.FirstParameter();

            firstParameter.Values = upmFilter.RawValues;
            return((UPConfigFilter)configFilter.QueryByApplyingFilterParameter(firstParameter));
        }
        private static UPConfigFilter QueryForDistance(UPConfigFilter configFilter, UPConfigFilterParameters parameters, UPMFilter upmFilter)
        {
            var distanceFilter = (UPMDistanceFilter)upmFilter;

            parameters.SetValuesName(new List <string> {
                distanceFilter.GetGPSXMin.ToString()
            }, "$parGPSXmin");
            parameters.SetValuesName(new List <string> {
                distanceFilter.GetGPSXMax.ToString()
            }, "$parGPSXmax");
            parameters.SetValuesName(new List <string> {
                distanceFilter.GetGPSYMin.ToString()
            }, "$parGPSYmin");
            parameters.SetValuesName(new List <string> {
                distanceFilter.GetGPSYMax.ToString()
            }, "$parGPSYmax");
            return((UPConfigFilter)configFilter.QueryByApplyingFilterParameters(parameters));
        }
        private static UPConfigFilter QueryForCatalogFilter(UPConfigFilter configFilter, UPConfigFilterParameters parameters, UPMFilter upmFilter)
        {
            var selectCatalogFilter       = (UPMSelectCatalogFilter)upmFilter;
            var rawValues                 = upmFilter.RawValues;
            var selectedValueDictionaries = new List <Dictionary <string, object> >(rawValues.Count);

            foreach (var recordIdentification in rawValues)
            {
                var row = selectCatalogFilter.ResultRowDictionary[recordIdentification];
                var rowFunctionNames = row.ValuesWithFunctions();
                if (rowFunctionNames != null)
                {
                    selectedValueDictionaries.Add(rowFunctionNames);
                }
            }

            return(configFilter
                   .FilterByApplyingArrayOfValueDictionariesParameters(
                       selectedValueDictionaries,
                       selectCatalogFilter.FilterParameters));
        }