Hey guys,
I've been writing in Blogger for almost 10 years this is a time to move on.
I'm happy to announce my new blog at Medium:
Ohad.
public string BuildFilterQuery(string Xmlquery) { var query = string.Empty; var doc = new XmlDocument(); var values = string.Empty; doc.LoadXml(Xmlquery); try { string fieldID; string conditionOperator; string conditionValues = string.Empty; XmlNodeList filters = doc.SelectNodes("//Root//Filter"); for (int i = 0; (i <= (filters.Count - 1)); i++) { XmlNodeList root = filters[i].SelectNodes(".//FilterItem"); query = (query + Convert.ToString("(")); foreach (XmlNode item in root) { fieldID = item.SelectSingleNode("FieldID").InnerText; conditionOperator = item.SelectSingleNode("Operator").InnerText.ToLower(); conditionValue = item.SelectSingleNode("Operator").InnerText; switch (conditionOperator) { case "contains": query = string.Format("{0} [{1}] {2} \'%{3}%\' AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; case "after": query = string.Format("{0} Convert([{1}], \'System.DateTime\') {2} \"#{3}#\" AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; case "greater than": query = string.Format("{0} Convert([{1}], \'System.Int64\') {2} {3} AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; default: query = string.Format("{0} [{1}] {2} \'{3}\' AND", query, DataManipulate.ConvertFromSafe(fieldID), dOperators[conditionOperator], conditionValue); break; } } if (query.EndsWith(" AND")) { query = (query.Substring(0, query.LastIndexOf(" AND")) + ")"); } query = (query + " OR "); } } catch (Exception ex) { throw ex; } return query; }
[XmlRoot(ElementName = "Filter")] public class Filter { [XmlElement(ElementName = "FilterName")] public string FilterName { get; set; } [XmlElement(ElementName = "FilterItem")] public List<FilterItem> FilterItem { get; set; } }
public interface IQueryStringType {string ConcatQueryString(QueryStringTypeModel queryStringTypeModel); }
public class QueryStringTypeModel { public string FilterName { get; set; } public string Operator { get; set; } public List<string> ValueList { get; set; } public Dictionary<string, string> TranslateOperators { get; set; } public QueryStringTypeSettingsModel QueryStringTypeSettingsModel { get; set; } }
public class ContainsQueryStringType : IQueryStringType { public string ConcatQueryString(QueryStringTypeModel queryStringTypeModel) { var convertedFromSafe = $"[{DataManipulate.ConvertFromSafe(queryStringTypeModel.FilterName)}]"; var concatOperator = $"{queryStringTypeModel.TranslateOperators[queryStringTypeModel.Operator]}"; var value = $"'%{queryStringTypeModel.ValueList[0]}%'"; var response = $"{convertedFromSafe} {concatOperator} {value}"; return response; } }
public class QueryStringTypeSettingsModelRepository { private readonly Dictionary<string, QueryStringTypeSettingsModel> _queryStringTypeSettingsModelDic = new Dictionary<string, QueryStringTypeSettingsModel>(); public QueryStringTypeSettingsModelRepository() { var containsSettingsModel = new QueryStringTypeSettingsModel() { QueryStringType = new ContainsQueryStringType(), };
this._queryStringTypeSettingsModelDic.Add("Contains", containsSettingsModel); this._queryStringTypeSettingsModelDic.Add("Not contains", containsSettingsModel); } public QueryStringTypeSettingsModel GetModel(string @operator) { if (!this._queryStringTypeSettingsModelDic.ContainsKey(@operator)) return null; var queryStringTypeSettingsModel = this._queryStringTypeSettingsModelDic[@operator]; return queryStringTypeSettingsModel; } }
public class QueryStringCalculator : IQueryStringCalculator { public string GetQueryString(QueryStringTypeModel queryStringTypeModel) { var response = string.Empty; if (queryStringTypeModel.QueryStringTypeSettingsModel == null || string.IsNullOrEmpty(queryStringTypeModel.FilterName) || string.IsNullOrEmpty(queryStringTypeModel.Operator)) return response; var queryStringType = queryStringTypeModel.QueryStringTypeSettingsModel.QueryStringType; response = queryStringType.ConcatQueryString(queryStringTypeModel); return response; } }
public class FilterItemToQueryStringConvertor : IFilterItemToQueryStringConvertor { private readonly IQueryStringCalculator _queryStringCalculator; private readonly IQueryStringTypeModelGenerator _queryStringTypeModelGenerator; public FilterItemToQueryStringConvertor(IQueryStringCalculator queryStringCalculator, IQueryStringTypeModelGenerator queryStringTypeModelGenerator) { _queryStringCalculator = queryStringCalculator; _queryStringTypeModelGenerator = queryStringTypeModelGenerator; } public string GetQueryString(List<FilterItem> filterItems, Dictionary<string,string> dOperators) { var response = string.Empty; if (filterItems.Count == 0) return response; var queryStringCollection = new List<string>(); foreach (var filterItem in filterItems) { var queryStringTypeGeneratorModel = new QueryStringTypeGeneratorModel { FilterItem = filterItem, TranslateOperators = dOperators }; var queryStringTypeModel = _queryStringTypeModelGenerator.GetQueryStringTypeModel(queryStringTypeGeneratorModel); var queryString = _queryStringCalculator.GetQueryString(queryStringTypeModel); queryStringCollection.Add(queryString); } response = queryStringCollection.Aggregate((current, next) => current + " AND " + next); return response; } }
public class FilterCollectionToQueryStringConvertor : IFilterCollectionToQueryStringConvertor
{
private readonly IFilterItemToQueryStringConvertor _filterItemToQueryStringConvertor;
public FilterCollectionToQueryStringConvertor(IFilterItemToQueryStringConvertor filterItemToQueryStringConvertor)
{
this._filterItemToQueryStringConvertor = filterItemToQueryStringConvertor;
}
public string GetQueryString(List<Filter> filters,
Dictionary<string, string> dOperators)
{
var response = string.Empty;
if (filters.Count == 0)
return response;
var queryStringCollection = new List<string>();
foreach (var filter in filters)
{
var queryString = _filterItemToQueryStringConvertor.GetQueryString(filter.FilterItem, dOperators);
queryStringCollection.Add(queryString);
}
response = queryStringCollection.Aggregate((current, next) => current + " OR " + next);
return response;
}
}
Hey guys, I've been writing in Blogger for almost 10 years this is a time to move on. I'm happy to announce my new blog at Med...