public static object[] GetParameters(Type modelType, FilterCondition[] filters) { if (filters == null) { return(null); } List <object> parameters = new List <object>(); for (int i = 0; i < filters.Length; i++) { IFilter filter = GetFilter(filters[i].FilterType); if (!filters[i].Internal) { filter.CopyPropertiesValue(filters[i]); } object parameter = GetParameter(modelType, filters[i]); if (parameter != null) { parameters.Add(parameter); } } return(parameters.ToArray()); }
private static string JoinWhereClause(Type modelType, FilterCondition[] conditions) { var attr = modelType.GetCustomAttributes(true).OfType <DataEntityAttribute>().FirstOrDefault(); string whereOperator = (attr != null && !string.IsNullOrEmpty(attr.WhereOperator)) ? attr.WhereOperator : " && "; StringBuilder whereClause = new StringBuilder(); int parameterIndex = 0; for (int i = 0; i < conditions.Length; i++) { if (!conditions[i].Internal) { IFilter filter = GetFilter(conditions[i].FilterType); filter.CopyPropertiesValue(conditions[i]); whereClause.AppendFormat("{0}{1}", filter.GetWhereClause(conditions[i], parameterIndex), whereOperator); } else { IFilter filter = GetFilter(conditions[i].FilterType); whereClause.AppendFormat("{0} ", filter.GetWhereClauseInternal(conditions[i], parameterIndex)); } if (GetParameter(modelType, conditions[i]) != null) { parameterIndex++; } } string where = whereClause.ToString(); if (where.EndsWith(whereOperator)) { where = where.Substring(0, where.Length - whereOperator.Length); } return(where); }
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); }