protected IDictionary<long, UserDto> GetUserMap(IUserContext userContext, AuditQueryInfo info)
 {
     EntitiesModel context = userContext.Context as EntitiesModel;
     IDictionary<long, UserDto> result = new Dictionary<long, UserDto>();
     if ((info.UserIds != null) && (info.UserIds.Count > 0))
     {
         IList<WebUser> users;
         if (info.UserIds.Count == 1)
         {
             users = context.GetAll<WebUser>().Where(u => u.UserId == info.UserIds[0]).ToList();
         }
         else
         {
             users = context.GetAll<WebUser>().Where(u => info.UserIds.Contains(u.UserId)).ToList();
         }
         foreach (WebUser user in users)
         {
             UserDto userDto = ObjectProxyFactory.MapTo<UserDto>(user);
             result.Add(userDto.UserId.Value, userDto);
         }
     }
     return result;
 }
 protected IList<AuditTransacAction> GetEntityTransacActions(IUserContext userContext, AuditQueryInfo info)
 {
     EntitiesModel context = userContext.Context as EntitiesModel;
     IQueryable<AuditTransacAction> query = context.GetAll<AuditTransacAction>().Where(a => a.AuditTransac.EndDttm != null);
     if ((info.UserIds != null) && (info.UserIds.Count > 0))
     {
         if (info.UserIds.Count == 1)
         {
             query = query.Where(a => a.AuditTransac.UserId == info.UserIds[0]);
         }
         else
         {
             query = query.Where(a => info.UserIds.Contains(a.AuditTransac.UserId));
         }
     }
     if (info.MinTransacId.HasValue)
     {
         query = query.Where(a => a.AuditTransac.TransacId >= info.MinTransacId.Value);
     }
     if (info.MaxTransacId.HasValue)
     {
         query = query.Where(a => a.AuditTransac.TransacId <= info.MinTransacId.Value);
     }
     if (info.MinStartDate.HasValue)
     {
         query = query.Where(a => a.AuditTransac.StartDttm >= info.MinStartDate.Value);
     }
     if (info.MaxStartDate.HasValue)
     {
         query = query.Where(a => a.AuditTransac.StartDttm <= info.MaxStartDate.Value);
     }
     if (info.MinEndDate.HasValue)
     {
         query = query.Where(a => a.AuditTransac.EndDttm >= info.MinEndDate.Value);
     }
     if (info.MaxEndDate.HasValue)
     {
         query = query.Where(a => a.AuditTransac.EndDttm <= info.MaxEndDate.Value);
     }
     if ((info.EntIds != null) && (info.EntIds.Count > 0))
     {
         if (info.EntIds.Count == 1)
         {
             query = query.Where(a => a.EntId == info.EntIds[0]);
         }
         else
         {
             query = query.Where(a => info.EntIds.Contains(a.EntId));
         }
     }
     if (!string.IsNullOrEmpty(info.PrimaryKey))
     {
         query = query.Where(a => a.PrimaryKey == info.PrimaryKey);
     }
     if ((info.OperationTypes != null) && (info.OperationTypes.Count > 0))
     {
         if (info.OperationTypes.Count == 1)
         {
             char ch = AuditOperationTypeToChar(info.OperationTypes[0]);
             query = query.Where(a => a.ActionType == ch);
         }
         else
         {
             char[] chs = new char[info.OperationTypes.Count];
             for (int i = 0; i < info.OperationTypes.Count; i++)
             {
                 chs[i] = AuditOperationTypeToChar(info.OperationTypes[i]);
             }
             query = query.Where(a => chs.Contains(a.ActionType));
         }
     }
     return query.ToList();
 }
        protected IList<AuditTransac> GetEntityTransactions(IUserContext userContext, AuditQueryInfo info)
        {
            EntitiesModel context = userContext.Context as EntitiesModel;

            //1. Get transactions.
            IQueryable<AuditTransac> query = context.GetAll<AuditTransac>().Where(t => t.EndDttm != null);
            // 1.1. Filter by users
            if ((info.UserIds != null) && (info.UserIds.Count > 0))
            {
                if (info.UserIds.Count == 1)
                {
                    query = query.Where(t => t.UserId == info.UserIds[0]);
                }
                else
                {
                    query = query.Where(t => info.UserIds.Contains(t.UserId));
                }
            }
            // 1.2. Filter by minimum transac id
            if (info.MinTransacId.HasValue)
            {
                query = query.Where(t => t.TransacId >= info.MinTransacId.Value);
            }
            // 1.3. Filter by maximum transac id
            if (info.MaxTransacId.HasValue)
            {
                query = query.Where(t => t.TransacId <= info.MinTransacId.Value);
            }
            // 1.4. Filter by minimum start date
            if (info.MinStartDate.HasValue)
            {
                query = query.Where(t => t.StartDttm >= info.MinStartDate.Value);
            }
            // 1.5. Filter by maximum start date
            if (info.MaxStartDate.HasValue)
            {
                query = query.Where(t => t.StartDttm <= info.MaxStartDate.Value);
            }
            // 1.6. Filter by minimum end date
            if (info.MinEndDate.HasValue)
            {
                query = query.Where(t => t.EndDttm >= info.MinEndDate.Value);
            }
            // 1.7. Filter by maximum end date
            if (info.MaxEndDate.HasValue)
            {
                query = query.Where(t => t.EndDttm <= info.MaxEndDate.Value);
            }
            // 1.8. Transac order by
            if (!string.IsNullOrEmpty(info.OrderBy))
            {
                string[] tokens = info.OrderBy.Split(',');
                foreach (string token in tokens)
                {
                    string trimtoken = token.Trim().ToLower();
                    if (!string.IsNullOrEmpty(trimtoken))
                    {
                        if (trimtoken[0] == '-')
                        {
                            if (trimtoken.Equals("transacid"))
                            {
                                query = query.OrderByDescending(t => t.TransacId);
                            }
                        }
                        else
                        {
                            if (trimtoken.Equals("transacd"))
                            {
                                query = query.OrderBy(t => t.TransacId);
                            }
                        }
                    }
                }
            }
            return query.ToList();
        }
 public override IList<TransacDto> GetTransactions(IUserContext userContext, AuditQueryInfo info)
 {
     IList<AuditTransac> allTransacEntities = this.GetEntityTransactions(userContext, info);
     IDictionary<long, UserDto> userMap = this.GetUserMap(userContext, info);
     IList<TransacDto> result = new List<TransacDto>();
     Dictionary<long, TransacDto> transacMap = new Dictionary<long, TransacDto>();
     foreach (AuditTransac transacEntity in allTransacEntities)
     {
         TransacDto transac = new TransacDto();
         transac.Id = transacEntity.TransacId;
         transac.UserId = transacEntity.UserId;
         transac.UserName = userMap[transacEntity.UserId].Name;
         transac.StartDateTime = transacEntity.StartDttm;
         transac.EndDateTime = transacEntity.EndDttm.Value;
         result.Add(transac);
         transacMap.Add(transac.Id, transac);
     }
     IList<AuditTransacAction> allTransacActionEntities = this.GetEntityTransacActions(userContext, info);
     foreach (AuditTransacAction actionEntity in allTransacActionEntities)
     {
         if (transacMap.ContainsKey(actionEntity.TransacId))
         {
             MetaEntity meta = this.metaEntitiesByKey[actionEntity.EntId];
             TransacDto transac = transacMap[actionEntity.TransacId];
             TransacActionDto action = new TransacActionDto();
             action.TransacId = transac.Id;
             action.ActionIx = actionEntity.TransacActionIx;
             action.EntId = actionEntity.EntId;
             action.Operation = CharToAuditOperationType(actionEntity.ActionType);
             action.Change = actionEntity.ObjJson;
             action.EntityName = meta.EntityName;
             transac.Actions.Add(action);
         }
     }
     return result;
 }