Exemplo n.º 1
0
        public override IQueryable <TEntity> ApplyWhere(IQueryable <TEntity> query, IFilterForm filteringForm)
        {
            var pageForm = filteringForm as IPageForm;

            if (pageForm == null)
            {
                throw new QuerySearchException("The IFilterForm must also implement IPageFrom in order to support FtsQuerySearchProvider.");
            }

            // keep track of the untouched query
            var untouchedQuery = query;

            // we only need to perform this 'special' filtering in case there is a term
            var rawTerm = filteringForm.GetTerm();
            var term    = GetSearchExpression(rawTerm);

            if (!string.IsNullOrWhiteSpace(term))
            {
                // rewrite the 'base query' to include a full text filter (will be placed in a sub query)
                string baseQuery = CreateBaseQuery(rawTerm);
                query = query.FromSql(baseQuery, term);

                // apply the default where clause onto the query (will be placed in an outer query)
                query = base.ApplyWhere(query, filteringForm);

                // get the sql representing the query
                var sql = query.ToSql();

                // create a reader for our sql, and a builder to build a new query
                var reader  = new StringReader(sql);
                var builder = new StringBuilder();
                builder.AppendLine(baseQuery);

                // variable to keep track of the alias used for the table in the outer query
                string usedAlias = string.Empty;

                // variable to keep track of whether or not the sql line we are iterating should be part of the new sql statement
                bool iteratingRelevantSql = false;

                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith(") AS "))
                    {
                        // this line marks the start of WHERE/ORDER BY/WHATEVER of the outer query
                        usedAlias            = line.Substring(5);
                        iteratingRelevantSql = true;
                    }
                    else if (iteratingRelevantSql)
                    {
                        // this writes the WHERE clause + other stuff
                        builder.AppendLine(line.Replace(usedAlias, TableAlias));
                    }
                }

                // Use the untouched query and append our own homebrew where clause
                return(untouchedQuery.FromSql(builder.ToString(), term));
            }
            else
            {
                // no special filtering to be performed
                return(base.ApplyWhere(query, filteringForm));
            }
        }
Exemplo n.º 2
0
        public void ShowForm(XtraForm MdiParent, Type FormType,
                             Type EntityType, DataPersistance dp,
                             Type FilterFormType, string DataFilter)
        {
            _Evaluator      = new Evaluator();
            Dp              = dp;
            this.DataFilter = DataFilter;
            comboBoxEdit1.Properties.Items.Add("(Layout Default)");

            if (EntityType != null)
            {
                _FormType   = FormType;
                _EntityType = EntityType;

                td = MetaData.GetTableDef(EntityType);
                Dp.ValidateTableDef(td);
                if (td.fldTransactionDate != null)
                {
                    dateEdit1.DateTime = DateTime.Today;
                    dateEdit2.DateTime = DateTime.Today;
                }
                else
                {
                    label3.Visible    = false;
                    label4.Visible    = false;
                    dateEdit1.Visible = false;
                    dateEdit2.Visible = false;
                }
                _ReportName = BaseUtility.SplitName(_EntityType.Name);
            }
            else
            {
                label3.Visible    = false;
                label4.Visible    = false;
                dateEdit1.Visible = false;
                dateEdit2.Visible = false;
                if (FilterFormType != null)
                {
                    _ReportName = FilterFormType.Name.Substring(0, 3).ToLower();
                    if (_ReportName == "frm" || _ReportName == "rpt")
                    {
                        _ReportName = BaseUtility.SplitName(
                            FilterFormType.Name.Substring(3));
                    }
                    else
                    {
                        _ReportName = BaseUtility.SplitName(FilterFormType.Name);
                    }
                }
                else
                {
                    _ReportName = "Bebas";
                }
            }
            Text = "Laporan " + _ReportName;

            comboBoxEdit1.Properties.Items.AddRange(
                DocBrowseLayout.GetListLayout(_ReportName));

            this.MdiParent = MdiParent;

            if (FilterFormType == null)
            {
                splitContainerControl1.PanelVisibility = SplitPanelVisibility.Panel2;
            }
            else
            {
                _FilterForm = BaseFactory.CreateInstance(FilterFormType) as IFilterForm;
                if (_FilterForm == null)
                {
                    XtraMessageBox.Show("Form Filter harus implement Interface IFilterForm !",
                                        "Error Filter", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Close();
                    return;
                }
                XtraForm Frm = _FilterForm as XtraForm;
                if (Frm != null)
                {
                    Frm.FormBorderStyle = FormBorderStyle.None;
                    Frm.TopLevel        = false;
                    Frm.Parent          = xtraScrollableControl1;
                    splitContainerControl1.SplitterPosition = Frm.Height + 5;
                    Frm.KeyPreview = true;
                    Frm.KeyDown   += new KeyEventHandler(frmGridReport_KeyDown);
                    xtraScrollableControl1.BackColor = Frm.BackColor;
                    Frm.Show();
                }
                else
                {
                    splitContainerControl1.PanelVisibility = SplitPanelVisibility.Panel2;
                }
            }
            barButtonItem8_ItemClick(null, null);

            DefaultLayout = new MemoryStream();
            pivotGridControl1.SaveLayoutToStream(DefaultLayout);

            string CurrBrowseLayoutId = string.Empty;
            bool   Tmp;

            DocDefault.GetDefaultLayout(_ReportName,
                                        out CurrBrowseLayoutId, out CurrPrintLayoutId, out Tmp);
            Show();
            comboBoxEdit1.SelectedItem = CurrBrowseLayoutId;
            if (comboBoxEdit1.SelectedIndex < 0)
            {
                comboBoxEdit1.SelectedIndex = 0;
            }
            textEdit1.Focus();
        }
        /// <summary>
        /// Applies filtering to the IQueryable provided.
        /// </summary>
        /// <param name="query">The IQueryable that is to be filtered.</param>
        /// <param name="filteringForm">The filtering form.</param>
        /// <returns>A filtered IQueryable.</returns>
        public virtual IQueryable <TEntity> ApplyWhere(IQueryable <TEntity> query, IFilterForm filteringForm)
        {
            var term  = filteringForm.GetTerm();
            var words = GetWords(term);

            if (_getApplyWhere != null && term != null)
            {
                var applyWhere = _getApplyWhere(term);
                query = applyWhere(query);
            }

            // create predicate1 (word expressions)
            Expression <Func <TEntity, bool> > predicate1 = null;

            if (_getFreeWordPredicate != null)
            {
                foreach (var word in words)
                {
                    predicate1 = AddExpression(predicate1, _getFreeWordPredicate(word), WordCombiner == WordSearchCombiner.And ? ExpressionType.AndAlso : ExpressionType.OrElse);
                }
            }

            // create predicate2 (sentence expression)
            Expression <Func <TEntity, bool> > predicate2 = null;

            if (_getFreeSentencePredicate != null && term != null)
            {
                predicate2 = _getFreeSentencePredicate(term);
            }

            // create predicate1 OR predicate2 as currentBody
            Expression <Func <TEntity, bool> > textPredicate = null;

            if (predicate1 != null && predicate2 != null)
            {
                textPredicate = predicate1.Or(predicate2);
            }
            else if (predicate1 != null)
            {
                textPredicate = predicate1;
            }
            else if (predicate2 != null)
            {
                textPredicate = predicate2;
            }

            var localizations = GetLocalizationDictionary();
            Expression <Func <TEntity, bool> > keywordPredicate = null;

            for (int i = 0; i < words.Length; i++)
            {
                for (int j = i; j < words.Length; j++)
                {
                    // from i to j
                    var word = string.Join(" ", words, i, j - i + 1);

                    Expression <Func <TEntity, bool> > predicate;
                    if (_predefinedPredicates.TryGetValue(word, out predicate))
                    {
                        keywordPredicate = AddExpression(keywordPredicate, predicate, ExpressionType.AndAlso);
                    }

                    Expression <Func <TEntity, bool> > localizedPredicate;
                    if (localizations.TryGetValue(word, out localizedPredicate))
                    {
                        keywordPredicate = AddExpression(keywordPredicate, localizedPredicate, ExpressionType.AndAlso);
                    }
                }
            }

            // create textPredicate OR keywordPredicate as currentBody
            Expression <Func <TEntity, bool> > currentBody = null;

            if (textPredicate != null && keywordPredicate != null)
            {
                currentBody = textPredicate.Or(keywordPredicate);
            }
            else if (textPredicate != null)
            {
                currentBody = textPredicate;
            }
            else if (keywordPredicate != null)
            {
                currentBody = keywordPredicate;
            }


            if (currentBody != null)
            {
                query = query.Where(currentBody);
            }


            var parameters = filteringForm.GetAdditionalFilters();

            if (parameters != null)
            {
                foreach (var propertyComparison in parameters)
                {
                    var comparisonType = propertyComparison.GetComparisonType();
                    var memberAccess   = propertyComparison.GetMemberAccess(_parameter);
                    var propertyValue  = propertyComparison.GetValue();
                    var memberAccessor = memberAccess.MemberAccessor;
                    if (memberAccessor != null)
                    {
                        var propertyType          = memberAccess.MemberType;
                        var unwrappedPropertyType = propertyType.GetTypeInfo().IsGenericType&& propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)
                     ? propertyType.GetTypeInfo().GetGenericArguments()[0]
                     : propertyType;


                        object convertedPropertyValue;
                        if (propertyValue is string && (memberAccessor.Type == typeof(Guid) || memberAccessor.Type == typeof(Guid? )))
                        {
                            convertedPropertyValue = Guid.Parse((string)propertyValue);
                        }
                        else
                        {
                            convertedPropertyValue = Convert.ChangeType(propertyValue, memberAccessor.Type);
                        }
                        var parameterizedPropertyValue = ExpressionHelper.WrappedConstant(memberAccessor.Type, convertedPropertyValue);

                        Expression left = null;
                        switch (comparisonType)
                        {
                        case ComparisonType.Equal:
                            left = Expression.Equal(memberAccessor, parameterizedPropertyValue);
                            break;

                        case ComparisonType.GreaterThan:
                            left = Expression.GreaterThan(memberAccessor, parameterizedPropertyValue);
                            break;

                        case ComparisonType.GreaterThanOrEqual:
                            left = Expression.GreaterThanOrEqual(memberAccessor, parameterizedPropertyValue);
                            break;

                        case ComparisonType.LessThan:
                            left = Expression.LessThan(memberAccessor, parameterizedPropertyValue);
                            break;

                        case ComparisonType.LessThanOrEqual:
                            left = Expression.LessThanOrEqual(memberAccessor, parameterizedPropertyValue);
                            break;

                        case ComparisonType.StartsWith:
                            left = Expression.Call(memberAccessor, StartsWith, parameterizedPropertyValue);
                            break;

                        case ComparisonType.Contains:
                            left = Expression.Call(memberAccessor, Contains, parameterizedPropertyValue);
                            break;

                        default:
                            throw new InvalidOperationException($"Invalid comparison type '{comparisonType}'.");
                        }

                        var expression = Expression.Lambda <Func <TEntity, bool> >(left, _parameter);

                        query = query.Where(expression);
                    }
                    else
                    {
                        query = ApplyManualFiltering(query, memberAccess.PropertyPath, propertyValue);
                    }
                }
            }

            return(query);
        }