public static PaginatedList <TEntity> ToPaginatedList <TEntity>(this ICollection <TEntity> source, IComponentContext container, AInBox.Astove.Core.Options.Filter filter, KeyValue parentId, int pageIndex, int pageSize) where TEntity : IEntity, new() { FilterOptions options = null; FilterCondition[] conditions = null; if (filter != null && (filter.Options != null || filter.Conditions != null)) { options = new FilterOptions { Filters = filter.Options.Filters.Where(f => f.Internal == false).ToList() }; conditions = filter.Conditions.Where(c => c.Internal == false).ToArray(); } int totalCount = source.Count(); int totalPageCount = (int)Math.Ceiling(totalCount / (double)pageSize); if (pageIndex > totalPageCount) { pageIndex = totalPageCount; } var collection = source.Skip((pageIndex - 1) * pageSize).Take(pageSize); return(new PaginatedList <TEntity>(container, options, conditions, parentId, pageIndex, pageSize, totalCount, collection, source)); }
public async static Task <PaginatedList <T> > ToPaginatedListAsync <T>(this IQueryable <T> query, IComponentContext container, AInBox.Astove.Core.Options.Filter filter, KeyValue parentId, int pageIndex, int pageSize) where T : IEntity, new() { FilterOptions options = null; FilterCondition[] conditions = null; if (filter != null && (filter.Options != null || filter.Conditions != null)) { options = new FilterOptions { Filters = filter.Options.Filters.Where(f => f.Internal == false).ToList() }; conditions = filter.Conditions.Where(c => c.Internal == false).ToArray(); } int totalCount = await query.CountAsync(); int totalPageCount = (int)Math.Ceiling(totalCount / (double)pageSize); if ((pageIndex - 1) > totalPageCount) { pageIndex = totalPageCount; } var collection = query.Skip((pageIndex - 1) * pageSize).Take(pageSize); return(new PaginatedList <T>(container, options, conditions, parentId, pageIndex, pageSize, totalCount, collection, query)); }
public async virtual Task <PaginatedList <T> > PaginateAsync(IComponentContext container, AInBox.Astove.Core.Options.Filter filter, IRequestFilter cmd, Type modelType, int pageIndex, int pageSize, string[] ordersBy, string[] directions, string[] includeProperties) { IQueryable <T> query = null; if (ordersBy != null) { for (int i = 0; i < ordersBy.Length; i++) { ordersBy[i] = string.Format("{0} {1}", ordersBy[i], directions[i]); } } string orderBy = (ordersBy == null || ordersBy.Length == 0 || ordersBy.Contains("system.string[] system.string[]")) ? "Id" : string.Join(",", ordersBy); if (includeProperties != null) { query = AllIncluding(includeProperties, true).OrderBy(orderBy); } else { query = All.OrderBy(orderBy); } string whereClause = string.Empty; object[] parameters = null; if (cmd.Fields != null) { whereClause = FilterFactory.GetWhereClause(modelType, filter.Conditions); parameters = FilterFactory.GetParameters(modelType, filter.Conditions); } else if (filter.Conditions != null && filter.Conditions.Where(c => c.Internal == true).ToArray().Length > 0) { whereClause = FilterFactory.GetWhereClause(modelType, filter.Conditions.Where(c => c.Internal == true).ToArray()); parameters = FilterFactory.GetParameters(modelType, filter.Conditions.Where(c => c.Internal == true).ToArray()); } if (!string.IsNullOrEmpty(cmd.ParentValue) && !string.IsNullOrEmpty(cmd.ParentKey)) { if (string.IsNullOrEmpty(whereClause)) { whereClause = string.Empty; } whereClause = string.Concat(whereClause, (string.IsNullOrEmpty(whereClause)) ? string.Empty : " && ", string.Format("{0}==@{1}", cmd.ParentValue, (string.IsNullOrEmpty(whereClause)) ? 0 : Regex.Matches(whereClause, "&&").Count + 1)); var pars = new List <object>(); if (parameters != null) { pars.AddRange(parameters); } pars.Add(cmd.ParentKey); parameters = pars.ToArray(); } var attr = modelType.GetCustomAttributes(true).OfType <DataEntityAttribute>().FirstOrDefault(); if (attr != null && attr.WhereClauses != null) { if (string.IsNullOrEmpty(whereClause)) { whereClause = string.Empty; } whereClause = string.Concat(whereClause, (string.IsNullOrEmpty(whereClause)) ? string.Empty : " && ", string.Join(" && ", attr.WhereClauses)); } query = (string.IsNullOrEmpty(whereClause)) ? query : query.Where(whereClause, parameters); return(await query.ToPaginatedListAsync(container, filter, new KeyValue { Key = int.Parse(cmd.ParentKey), Value = cmd.ParentValue }, pageIndex, pageSize)); }
public async virtual Task <PaginatedList <T> > PaginateAsync(IComponentContext container, AInBox.Astove.Core.Options.Filter filter, IRequestFilter cmd, Type modelType, int pageIndex, int pageSize, string[] ordersBy, string[] directions) { return(await PaginateAsync(container, filter, cmd, modelType, pageIndex, pageSize, ordersBy, directions, null)); }
public async static Task <PaginatedMongoList <TModel, RModel> > ToPaginatedMongoListAsync <TModel, RModel>(this IFindFluent <TModel, TModel> source, IComponentContext container, AInBox.Astove.Core.Options.Filter filter, SortOptions sortOptions, KeyValue parentId, int pageIndex, int pageSize) where TModel : class, IMongoModel, new() where RModel : class, IMongoModel, IDto, new() { FilterOptions options = null; FilterCondition[] conditions = null; if (filter != null && (filter.Options != null || filter.Conditions != null)) { options = new FilterOptions { Filters = filter.Options.Filters.Where(f => f.Internal == false).ToList() }; conditions = filter.Conditions.Where(c => c.Internal == false).ToArray(); } int totalCount = (int)await source.CountAsync(); int totalPageCount = (int)Math.Ceiling(totalCount / (double)pageSize); if ((pageIndex - 1) > totalPageCount) { pageIndex = totalPageCount; } var paginatedData = await source.Skip((pageIndex - 1) *pageSize).Limit(pageSize).ToListAsync(); var returnData = new List <RModel>(); paginatedData.ForEach(p => returnData.Add(p.CreateInstanceOf <RModel>())); return(new PaginatedMongoList <TModel, RModel>(container, options, conditions, sortOptions, parentId, pageIndex, pageSize, totalCount, returnData, source)); }
public TModel ToModel <TModel, TInterfaceConstraint>(IComponentContext container, bool lazyLoading = false, int level = 0) where TModel : TInterfaceConstraint, new() { TModel model = new TModel(); Type t = typeof(TModel); Type c = this.GetType(); try { foreach (var propInfo in t.GetProperties()) { var attr = propInfo.GetCustomAttributes(true).OfType <ColumnDefinitionAttribute>().FirstOrDefault(); if (attr != null && attr.EnumType == null && !string.IsNullOrEmpty(attr.EntityProperty) && c.GetProperty(attr.EntityProperty) != null && (string.IsNullOrEmpty(attr.DisplayValue) || string.IsNullOrEmpty(attr.DisplayText)) && ((attr.Load && attr.Level >= level) || attr.Load || !lazyLoading)) { try { var entity = c.GetProperty(attr.EntityProperty).GetValue(this, null); if (entity != null && entity.GetType().GetInterfaces().Count(i => i.Name.Contains("ICollection")) > 0) { var parentKeyValue = new KeyValue { Key = this.Id, Value = string.Format("{0}Id", model.GetType().Name) }; AInBox.Astove.Core.Options.Filter filter = FilterFactory.GenerateFilter(container, null, propInfo.PropertyType.GetGenericArguments()[0], parentKeyValue); var miTPList = typeof(ICollectionExtensions).GetMethod("ToPaginatedList"); var miToPaginatedList = miTPList.MakeGenericMethod(new[] { entity.GetType().GetGenericArguments()[0] }); // Setting parentId var paginatedList = miToPaginatedList.Invoke(entity, new object[] { entity, container, filter, parentKeyValue, 1, DataEntityAttribute.DefaultPageSizes[0] }); var mi = typeof(PaginatedList <>).MakeGenericType(entity.GetType().GetGenericArguments()[0]).GetMethod("ToPaginatedModel"); var miToPaginatedModel = mi.MakeGenericMethod(new[] { propInfo.PropertyType.GetGenericArguments()[0] }); var paginatedModel = miToPaginatedModel.Invoke(paginatedList, new object[] { attr.WhereClause, attr.OrderBy }); propInfo.SetValue(model, paginatedModel, null); } else if (entity != null) { entity = (BaseEntity)entity; var interfaceConstraintType = typeof(IBindingModel); if (propInfo.PropertyType.IsAssignableTo <IModel>()) { interfaceConstraintType = typeof(IModel); } else if (propInfo.PropertyType.IsAssignableTo <IMongoModel>()) { interfaceConstraintType = typeof(IMongoModel); } var methodInfo = c.GetMethod("ToModel"); var toModelMethod = methodInfo.MakeGenericMethod(new[] { propInfo.PropertyType, interfaceConstraintType }); var obj = toModelMethod.Invoke(entity, new object[] { container, true, level + 1 }); propInfo.SetValue(model, obj, null); } } catch { throw new Exception(string.Format("Falha ao obter um objeto de navegação, propriedade {0} do tipo {1} do modelo {2}", propInfo.Name, propInfo.PropertyType.Name, typeof(TModel).Name)); } } else if (attr != null && attr.EnumType == null && !string.IsNullOrEmpty(attr.EntityProperty) && (c.GetProperty(attr.EntityProperty) == null || (!string.IsNullOrEmpty(attr.DisplayValue) && !string.IsNullOrEmpty(attr.DisplayText))) && ((attr.Load && attr.Level >= level) || attr.Load || !lazyLoading)) { try { if (attr.EntityProperty.Contains(".")) { this.SetValue(model, propInfo, attr.EntityProperty); continue; } var ts = typeof(IEntityService <>); var entityType = Type.GetType(string.Format(System.Web.Configuration.WebConfigurationManager.AppSettings["DataAssemblyFormat"], attr.EntityProperty)); var serviceType = ts.MakeGenericType(new[] { entityType }); var service = container.Resolve(serviceType); if (propInfo.PropertyType == typeof(DropDownOptions) && !string.IsNullOrEmpty(attr.DisplayValue) && !string.IsNullOrEmpty(attr.DisplayText)) { SetDropDownOptions <TModel, TInterfaceConstraint>(model, container, propInfo, this); } else if (propInfo.PropertyType == typeof(DropDownStringOptions)) { ((IEntityService <BaseEntity>)service).MongoService.SetDropDownOptions <TModel>(model); } else if (propInfo.PropertyType.GetInterfaces().Count(i => i.Name.Contains("ICollection")) > 0) { var entities = service.GetType().GetMethod("GetEntities", new Type[] { }).Invoke(service, null); var list = Activator.CreateInstance(typeof(HashSet <>).MakeGenericType(propInfo.PropertyType.GetGenericArguments()[0])); foreach (var obj in (IEnumerable)entities) { var methodInfo = obj.GetType().GetMethod("ToModel"); var toModelMethod = methodInfo.MakeGenericMethod(new[] { propInfo.PropertyType.GetGenericArguments()[0] }); var modelObj = toModelMethod.Invoke(obj, new object[] { container, true, level + 1 }); var miAdd = list.GetType().GetMethod("Add"); miAdd.Invoke(list, new object[] { modelObj }); } propInfo.SetValue(model, list, null); } } catch { throw new Exception(string.Format("Falha ao obter um objeto de navegação auxiliar, propriedade {0} do tipo {1} do modelo {2}", propInfo.Name, propInfo.PropertyType.Name, typeof(TModel).Name)); } } else if (attr != null && attr.EnumType != null && !string.IsNullOrEmpty(attr.Field)) { try { var enumText = AInBox.Astove.Core.Enums.EnumUtility.GetEnumText(attr.EnumType, Convert.ToInt32(c.GetProperty(propInfo.Name).GetValue(this, null))); t.GetProperty(attr.Field.ToPascalCase()).SetValue(model, enumText, null); propInfo.SetValue(model, c.GetProperty(propInfo.Name).GetValue(this, null), null); } catch { throw new Exception(string.Format("Falha ao obter um enum, propriedade {0} do tipo {1} do modelo {2}", propInfo.Name, propInfo.PropertyType.Name, typeof(TModel).Name)); } } else if (c.GetProperty(propInfo.Name) != null) { try { if (!propInfo.PropertyType.BaseType.Name.Equals("Array") && !propInfo.Name.Equals("Id")) { //propInfo.SetValue(model, c.GetProperty(propInfo.Name).GetValue(this, null), null); this.SetValue(model, propInfo); } else if (propInfo.Name.Equals("Id")) { var propParentId = t.GetProperty("ParentId"); if (propParentId != null) { propParentId.SetValue(model, Convert.ToString(c.GetProperty(propInfo.Name).GetValue(this, null)), null); } } } catch { throw new Exception(string.Format("Falha ao obter a propriedade {0} do tipo {1} do modelo {2}", propInfo.Name, propInfo.PropertyType.Name, typeof(TModel).Name)); } } } } catch (Exception ex) { throw ex; } return(model); }