/// <summary> /// Validates the includes. /// </summary> /// <param name="item">The item.</param> /// <param name="includes">The includes.</param> /// <param name="typeWildcard">The type wildcard.</param> /// <param name="parentTable">The parent table.</param> /// <author>Mauricio Suárez Robelto</author> public static void ValidateIncludes(object item, List <string> includes, string typeWildcard, string parentTable) { if (item != null) { PropertyInfo[] properties = item.GetType().GetProperties(); foreach (PropertyInfo property in properties) { if (property.GetValue(item, null) != null) { if (ConditionalQuery.IsGenericType(property.PropertyType)) { if (property.PropertyType.ToString() == typeWildcard) { bool?valueProperty = (bool?)property.GetValue(item, null); if (valueProperty.Value) { includes.Add(parentTable + "." + property.Name); } } } else { if (property.PropertyType.IsGenericType) { IList listNewObject = (IList)property.GetValue(item, null); if (listNewObject.Count > 0) { ConditionalQuery.ValidateIncludes(listNewObject[0], includes, typeWildcard, listNewObject[0].GetType().Name); } } else { ConditionalQuery.ValidateIncludes(property.GetValue(item, null), includes, typeWildcard, property.GetValue(item, null).GetType().Name); } } } } } }
/// <summary> /// Generates the includes. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="listDtoObject">The list dto object.</param> /// <returns></returns> /// <author>Mauricio Suárez Robelto</author> public static string[] GenerateIncludes <T>(List <T> listDtoObject) where T : BaseDto { string referenceWildcard = "ReferenceTable"; int lenghtReferenceWildcard = referenceWildcard.Length; string typeWildcard = "System.Nullable`1[System.Boolean]"; List <string> includes = new List <string>(); if (listDtoObject.Count() > 0) { PropertyInfo[] properties = listDtoObject[0].GetType().GetProperties(); object item = listDtoObject[0]; ConditionalQuery.ValidateIncludes(item, includes, typeWildcard, ""); } for (int i = 0; i < includes.Count; i++) { if (includes[i].Length > lenghtReferenceWildcard) { if (referenceWildcard == includes[i].Substring(includes[i].IndexOf('.') + 1, lenghtReferenceWildcard)) { includes[i] = includes[i].Replace(referenceWildcard, "").Replace("Dto", ""); includes[i] = (includes[i].StartsWith(".") ? includes[i].Replace(".", "") : includes[i]); } else { includes[i] = null; } } else { includes[i] = null; } } includes.RemoveAll(remove => remove == null); //return ConditionalQuery.ConcatIncludes(includes).ToArray(); return(ConditionalQuery.GenerateNewInclude(includes).ToArray()); }
/// <summary> /// Generates the parameters conditional query. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="listDtoObject">The list dto object.</param> /// <returns></returns> /// <author>Mauricio Suárez Robelto</author> public static ObjectParameter[] GenerateParametersConditionalQuery <T>(List <T> listDtoObject) where T : BaseDto { List <ObjectParameter> parameters = new List <ObjectParameter>(); if (listDtoObject.Count() > 0) { PropertyInfo[] properties = listDtoObject[0].GetType().GetProperties(); int CurrentObject = 0; foreach (object item in listDtoObject) { CurrentObject++; foreach (PropertyInfo property in properties) { if (ConditionalQuery.IsGenericType(property.PropertyType)) { if (property.GetValue(item, null) != null) { string validate = property.PropertyType.ToString(); if (validate == "System.Nullable`1[System.DateTime]") { DateTime filterDate = (DateTime)property.GetValue(item, null); parameters.Add(new ObjectParameter(property.Name + "Ini" + CurrentObject.ToString(), new DateTime(filterDate.Year, filterDate.Month, filterDate.Day, 0, 0, 0))); parameters.Add(new ObjectParameter(property.Name + "End" + CurrentObject.ToString(), new DateTime(filterDate.Year, filterDate.Month, filterDate.Day, 23, 59, 59))); } else { parameters.Add(new ObjectParameter(property.Name + CurrentObject.ToString(), property.GetValue(item, null))); } } } } } } return(parameters.ToArray()); }
/// <summary> /// Removes the matches. /// </summary> /// <param name="listUnionString">The list union string.</param> /// <param name="listInclude">The list include.</param> /// <returns></returns> /// <author>Mauricio Suárez Robelto</author> public static List <string> RemoveMatches(List <string> listUnionString, List <string> listInclude) { List <string> verifiUnionstring = new List <string>(); for (int ij = 0; ij < listUnionString.Count; ij++) { string[] valueIni = listUnionString[ij].Split('.'); for (int j = 0; j < listUnionString.Count; j++) { string[] valueEnd = listUnionString[j].Split('.'); if (valueEnd.Count() == 3) { if (valueIni[1] + "." + valueIni[2] == valueEnd[0] + "." + valueEnd[1]) { listUnionString[ij] = valueIni[0] + "." + valueIni[1] + "." + valueIni[2] + "." + valueEnd[2]; listUnionString[j] = ""; } } } } listUnionString.RemoveAll(filter => filter == ""); List <int> listRemoveIndex = new List <int>(); int i = 0; foreach (string include in listInclude) { foreach (string unionString in listUnionString) { string PatronBusqueda = include; Regex regex = new Regex(PatronBusqueda, RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); if (regex.IsMatch(unionString)) { if (ConditionalQuery.ValidateStringExist(PatronBusqueda.Split('.').ToList(), unionString.Split('.').ToList()) && listRemoveIndex.Where(filter => filter == i).Count() == 0) { listRemoveIndex.Add(i); } } } i++; } listRemoveIndex.ForEach(data => { listInclude[data] = "Delete"; }); listInclude.RemoveAll(filter => filter == "Delete"); listUnionString.AddRange(listInclude); //listRemoveIndex.Clear(); List <string> listnew = new List <string>(); listUnionString.ForEach(data => { string result = data; string[] element = data.Split('.'); Regex regex = new Regex(element[0], RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (var item in listUnionString.Where(filters => filters != data)) { if (regex.IsMatch(item)) { int index = item.IndexOf(element[0]); if (ConditionalQuery.ValidateStringExist(new List <string> { element[0] }, item.Split('.').ToList()) && index > 0) { result = item.Substring(0, index) + data; break; } } } listnew.Add(result); }); return(listnew); //return listUnionString; }
/// <summary> /// Generates the new include. /// </summary> /// <param name="listInclude">The list include.</param> /// <returns></returns> /// <author>Mauricio Suárez Robelto</author> public static List <string> GenerateNewInclude(List <string> listInclude) { return(ConditionalQuery.GenerateTree(ConditionalQuery.RemoveMatches(ConditionalQuery.UnionString(listInclude), listInclude))); }
/// <summary> /// Generates the conditional query. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="listDtoObject">The list dto object.</param> /// <returns></returns> /// <author>Mauricio Suárez Robelto</author> public static string GenerateConditionalQuery <T>(List <T> listDtoObject) where T : BaseDto { if (listDtoObject.Count() > 0) { string referenceWildcard = "ReferenceTable"; int lenghtReferenceWildcard = referenceWildcard.Length; string typeWildcard = "System.Nullable`1[System.Boolean]"; StringBuilder sbResult = new StringBuilder(); StringBuilder sb = new StringBuilder(); PropertyInfo[] properties = listDtoObject[0].GetType().GetProperties(); int CurrentObject = 0; foreach (object item in listDtoObject) { CurrentObject++; int i = 1; foreach (PropertyInfo property in properties) { if (ConditionalQuery.IsGenericType(property.PropertyType)) { if (property.GetValue(item, null) != null) { bool resultIsFilter = true; if (property.PropertyType.ToString() == typeWildcard) { string validateFilter = property.Name; if ((validateFilter.Substring(0, validateFilter.Length < lenghtReferenceWildcard ? validateFilter.Length : lenghtReferenceWildcard)) == referenceWildcard) { resultIsFilter = false; } } if (resultIsFilter) { string validate = property.PropertyType.ToString(); if (validate == "System.Nullable`1[System.DateTime]") { sb.Append((i != 1 ? " AND (" : "")); sb.Append("it." + property.Name).Append(" >= ").Append(("@" + property.Name + "Ini" + CurrentObject.ToString())); sb.Append(" AND " + "it." + property.Name).Append(" <= ").Append(("@" + property.Name + "End" + CurrentObject.ToString())); sb.Append((i != 1 ? ")" : "")); i++; } else { sb.Append((i != 1 ? " AND " : "")); sb.Append("it." + property.Name).Append(" = ").Append(("@" + property.Name + CurrentObject.ToString())); i++; } } } } } if (sb.Length > 0) { sb.Insert(0, "("); sb.Insert(sb.Length, ")"); } if (sbResult.Length > 0 && sb.Length > 0) { sbResult.Append(" OR "); } sbResult.Append(sb); sb.Clear(); } if (sbResult.Length == 0) { sbResult.Append("1 = 1"); } return(sbResult.ToString()); } else { return("1 = 1"); } }