public virtual IQueryable <T> Where <TModel>(IComponentContext container, string[] ordersBy = null, string[] includeProperties = null) { IQueryable <T> query = null; if (ordersBy != null) { for (int i = 0; i < ordersBy.Length; i++) { ordersBy[i] = string.Format("{0}", ordersBy[i]); } } string orderBy = (ordersBy == null || ordersBy.Length == 0 || ordersBy.Contains("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; var modelType = typeof(TModel); List <PropertyInfo> filterList = modelType.GetProperties().Where(p => p.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().DefaultIfEmpty().FirstOrDefault() != null && p.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().DefaultIfEmpty().FirstOrDefault().Internal == true).OrderBy(o => o.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().First().GroupOrder).ToList(); if (filterList.Count > 0) { var filters = new List <FilterWebapiAttribute>(); foreach (PropertyInfo prop in filterList) { foreach (Attribute att in prop.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().OrderBy(f => f.Order)) { FilterWebapiAttribute filterAtt = att as FilterWebapiAttribute; if (filterAtt == null) { continue; } filters.Add(filterAtt); } } whereClause = FilterFactory.GetWhereClause(modelType, filters); parameters = FilterFactory.GetParameters(modelType, filters); } 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(query); }
public static List <IFilter> GenerateFilters(Type modelType) { var filters = new List <IFilter>(); var dict = new List <FilterDictionary>(); IPrincipal user = null; if (System.Web.HttpContext.Current != null) { user = System.Web.HttpContext.Current.User; } // Order the columns of this table . List <PropertyInfo> filterList = modelType.GetProperties().Where(p => p.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().DefaultIfEmpty().FirstOrDefault() != null).OrderBy(o => o.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().First().GroupOrder).ToList(); if (filterList.Count == 0) { return(null); } foreach (PropertyInfo prop in filterList) { foreach (Attribute att in prop.GetCustomAttributes(true).OfType <FilterWebapiAttribute>().OrderBy(f => f.Order)) { FilterWebapiAttribute filterAtt = att as FilterWebapiAttribute; if (filterAtt == null) { continue; } if (user != null && !string.IsNullOrEmpty(filterAtt.Permission)) { string[] rolesRequired = filterAtt.Permission.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); RolePrincipal principal = (RolePrincipal)System.Web.HttpContext.Current.User; if (rolesRequired.Length > 0 && rolesRequired.Where(r => principal.IsInRole(r)).Count() == 0) { continue; } } IFilter f = Activator.CreateInstance(filterAtt.FilterType) as IFilter; f.Property = prop.Name; if (string.IsNullOrEmpty(filterAtt.DisplayName)) { f.DisplayName = f.Property; } var attLength = prop.GetCustomAttributes(true).OfType <StringLengthAttribute>().FirstOrDefault(); if (attLength != null) { f.Length = attLength.MaximumLength; } f.CopyPropertiesValue(filterAtt); if (f.Internal || (dict.Count(d => d.Property == f.Property && d.Type == f.GetType().Name&& d.DefaultOperator == f.DefaultOperator && d.DefaultValue.Equals(f.DefaultValue)) == 0)) { filters.Add(f); dict.Add(new FilterDictionary { Property = f.Property, Type = f.GetType().Name, DefaultOperator = f.DefaultOperator, DefaultValue = f.DefaultValue }); } } } return(filters); }