Ejemplo n.º 1
0
        /// <summary>
        /// Executes a query for the specified objects
        /// </summary>
        public IEnumerable <AuditData> Query(Expression <Func <AuditData, bool> > query, int offset, int?count, out int totalCount, IPrincipal overrideAuthContext = null, params ModelSort <AuditData>[] orderBy)
        {
            var preEvtData = new QueryRequestEventArgs <AuditData>(query, offset: offset, count: count, queryId: null, principal: overrideAuthContext);

            this.Querying?.Invoke(this, preEvtData);
            if (preEvtData.Cancel)
            {
                this.m_traceSource.TraceWarning("Pre-event handler for query indicates cancel : {0}", query);
                totalCount = 0;
                return(null);
            }

            try
            {
                using (var context = this.m_configuration.Provider.GetReadonlyConnection())
                {
                    context.Open();

                    var sql = this.m_builder.CreateQuery(query).Build();

                    if (orderBy != null && orderBy.Length > 0)
                    {
                        foreach (var ob in orderBy)
                        {
                            sql = sql.OrderBy <DbAuditData>(this.m_mapper.MapModelExpression <AuditData, DbAuditData, dynamic>(ob.SortProperty), ob.SortOrder);
                        }
                    }
                    else
                    {
                        sql = sql.OrderBy <DbAuditData>(o => o.Timestamp, SortOrderType.OrderByDescending);
                    }

                    // Total results
                    totalCount = context.Count(sql);

                    // Query control
                    if (count.GetValueOrDefault() == 0)
                    {
                        sql.Offset(offset).Limit(100);
                    }
                    else
                    {
                        sql.Offset(offset).Limit(count.Value);
                    }
                    sql = sql.Build();
                    var itm = context.Query <CompositeResult <DbAuditData, DbAuditCode> >(sql).ToList();
                    AuditUtil.AuditAuditLogUsed(ActionType.Read, OutcomeIndicator.Success, sql.ToString(), itm.Select(o => o.Object1.Key).ToArray());
                    var results = itm.Select(o => this.ToModelInstance(context, o)).ToList().AsQueryable();

                    // Event args
                    var postEvtArgs = new QueryResultEventArgs <AuditData>(query, results, offset, count, totalCount, null, overrideAuthContext);
                    this.Queried?.Invoke(this, postEvtArgs);
                    return(postEvtArgs.Results);
                }
            }
            catch (Exception e)
            {
                AuditUtil.AuditAuditLogUsed(ActionType.Read, OutcomeIndicator.EpicFail, query.ToString());
                this.m_traceSource.TraceError("Could not query audit {0}: {1}", query, e);
                throw;
            }
        }