public static string BuildGremlinQuery(string query, AggregateFilter filters, ContentTypeDistribution distribution) { IEnumerable <string> distribFieldNames = Enumerable.Empty <string>(); if (distribution != null && distribution.Quotas != null && distribution.Quotas.Count > 0) { distribFieldNames = distribution.GetFieldNames(); } List <string> queryParts = new List <string>(); int distribCount = distribFieldNames.Count(); int cursor = 0; foreach (GremlinFormatKey formatKey in Enum.GetValues(typeof(GremlinFormatKey))) { string formatKeyStr = "{" + formatKey.ToString() + "}"; int formatKeyPos = query.IndexOf(formatKeyStr, cursor); if (formatKeyPos >= 0) { queryParts.Add(query.Substring(cursor, formatKeyPos - cursor)); switch (formatKey) { case GremlinFormatKey.ContentTypeFields: if (distribCount > 0) { string columnsName = "," + string.Join(", ", distribFieldNames.Select(f => "'" + f + "'")); queryParts.Add(columnsName); Log.Debug("BuildGremlinQuery", "Name of the columns added in the CypherResult", columnsName); } break; case GremlinFormatKey.Filter: if (filters != null && filters.Filters.Count > 0) { string gremlinCondition = filters.ToGremlinConditions(); queryParts.Add(gremlinCondition); Log.Debug("BuildGremlinQuery", "The if statement created for filtering the results", gremlinCondition); } break; case GremlinFormatKey.FilterEnd: if (filters != null && filters.Filters.Count > 0) { queryParts.Add("\n}\n"); } break; case GremlinFormatKey.ContentTypeFilling: if (distribCount > 0) { string fillingValues = string.Join("\n", distribFieldNames.Select(f => "d" + f + " = endNode.getProperty('" + f + "', null);")); queryParts.Add(fillingValues); Log.Debug("BuildGremlinQuery", "The lines used to gather node data used by distribution", fillingValues); } break; case GremlinFormatKey.ContentTypeKey: if (distribCount > 0) { string mapKey = "+';'+" + string.Join("+';'+", distribFieldNames.Select(f => "d" + f)); queryParts.Add(mapKey); Log.Debug("BuildGremlinQuery", "The key added to the HashMap to return all the values needed", mapKey); } break; } cursor = formatKeyPos + formatKeyStr.Length; } } queryParts.Add(query.Substring(cursor)); return(string.Concat(queryParts)); }
private static string BuildCypherRecommendationQuery(string query, AggregateFilter filters, ContentTypeDistribution distribution) { string where = string.Empty; if (filters != null) { where = filters.ToConditions(); } Log.Debug("BuildRecommendationQuery", "where", where); var filteredQueryText = query.Replace("\r\n", " ").Replace("\n", " "); if (!string.IsNullOrEmpty(where) && filteredQueryText.Contains(AggregateFilter.Where)) { Log.Debug("BuildRecommendationQuery", "Query before where", filteredQueryText); int wherePos = filteredQueryText.LastIndexOf(AggregateFilter.Where); string left = filteredQueryText.Substring(0, wherePos); Log.Debug("BuildRecommendationQuery", "left", left); string right = filteredQueryText.Substring(wherePos + 6); Log.Debug("BuildRecommendationQuery", "right", right); filteredQueryText = left + AggregateFilter.Where + where + " " + AggregateFilter.And + right; Log.Debug("BuildRecommendationQuery", "Query with where clause", filteredQueryText); } // Added the distribution configured field in the queries if (distribution != null && distribution.Quotas != null && distribution.Quotas.Count > 0) { Log.Debug("BuildRecommendationQuery", "Query before replace for quota", filteredQueryText); filteredQueryText = filteredQueryText.Replace("RETURN", string.Format("RETURN {0},", string.Join(",", distribution.GetFieldNames().Select(f => string.Format("c.{0}? as {0}", f))))); } Log.Debug("BuildRecommendationQuery", "Query generated", filteredQueryText); return(filteredQueryText); }