public static string BuildAggregationPiplineFrameWorkQuery(Report  report)
        {

            //BuildBson Query            

            BuildMatchQuery(report);

            return string.Empty;
        }
        private static void ProccessFileConfiguration(string config,Nodes myNode)
        {
           try
            {
                var report = new Report();
                var xConfig  =  XDocument.Load(config);

                var reportNode = xConfig.Descendants("report");
                //Load Report attribute
                var node = reportNode.Attributes().ToList();
                report.ReportName = node.First(p => p.Name.ToString() == "name").Value.ToString();
                report.ReporTheme = node.First(p => p.Name.ToString() == "theme").Value.ToString();
                report.CollectionName = node.First(p => p.Name.ToString() == "collection").Value.ToString();
                report.ServerPort =  node.First(p => p.Name.ToString() == "port").Value.ToString();
                report.ServerUri = node.First(p => p.Name.ToString() == "server").Value.ToString();
                report.DataBase = node.First(p => p.Name.ToString() == "database").Value.ToString();
                report.ExcelExportFileName = node.FirstOrDefault(p => p.Name.ToString() == "ExcelFileName") == null ? (report.ReportName +".xls") : node.FirstOrDefault(p => p.Name.ToString() == "ExcelFileName").Value.ToString();

                //Load Filter List

                var filterNode = reportNode.Descendants("filter").Descendants("item").ToList();
                if(filterNode != null && filterNode.Count > 0)
                {
                    report.FilterList = new List<Filter>();

                    foreach(var filter in filterNode)
                    {
                        var oneFilter = filter.Attributes().ToList();
                        var item = new Filter();
                        item.Name = oneFilter.First(p => p.Name == "name").Value.ToString();
                        item.DisplayName = oneFilter.First(p => p.Name == "displayName").Value.ToString();
                        item.Type = oneFilter.First(p => p.Name == "type").Value.ToString();
                        item.Source = oneFilter.FirstOrDefault(p => p.Name == "source") != null ? oneFilter.FirstOrDefault(p => p.Name == "source").Value.ToString() : null;

                        report.FilterList.Add(item);
                    }
                }

                //Load Query
                var test = reportNode.Descendants("query").ToList()[0].Value;

                //Load LambdaFilter

                report.Query = new List<string>();


                var piplineList = reportNode.Descendants("query").Descendants("pipline").ToList();
                foreach(var pipline in piplineList)
                {
                    report.Query.Add(pipline.Value);

                }


                report.OriginQuery = new List<string>();
                foreach(var item in report.Query)
                {
                    report.OriginQuery.Add(item);
                }
                                       

                report.LambdaQuery = AggregationQueryProcessing.LambdaFilterProcessing(reportNode.Descendants("lambdaFilter").ToList()[0]);

                //Load Grid Definition
                report.Grid = new GridDefinittion();
            
                var gridNode = reportNode.Descendants("grid");

                report.Grid.MaxInPage = int.Parse( gridNode.Attributes().ToList().First(p => p.Name == "maxInPage").Value.ToString());

                report.Grid.Column = new List<ColumnDefinition>();
                var columnsNode = gridNode.Descendants("column").ToList();

                if(columnsNode != null && columnsNode.Count()>0)
                {

                    foreach(var column in columnsNode)
                    {
                        var OneClomun = new ColumnDefinition();
                        var columnAttribute = column.Attributes().ToArray();

                        OneClomun.Header = columnAttribute.First(p => p.Name == "header").Value.ToString();
                        OneClomun.Name = columnAttribute.First(p => p.Name == "name").Value.ToString();

                        OneClomun.Binding = columnAttribute.First(p => p.Name == "binding").Value.ToString();

                        report.Grid.Column.Add(OneClomun);

                    }
                }

                //Process filter dictionnary

                report.FilterDataValue = new Dictionary<string, object>();

                if(report.FilterList != null)
                {
                    foreach(var filter in report.FilterList)
                    {
                        report.FilterDataValue.Add(filter.Name, null);
                    }
                }
               

                // Process Graph

                report.GraphList = new List<Graph>();
                var graphicsdNode = reportNode.Descendants("charts");
                if(graphicsdNode != null && graphicsdNode.Descendants("chart") != null && 
                    graphicsdNode.Descendants("chart").ToList() != null && 
                    graphicsdNode.Descendants("chart").ToList().Count() >0)
                {
                    var chartList = graphicsdNode.Descendants("chart").ToList();
                    foreach(var chart in chartList)
                    {
                        var chartAttribute = chart.Attributes().ToArray();
                        var tmp = new Graph();
                        tmp.width = chartAttribute.First(p => p.Name == "width").Value.ToString();
                        tmp.Height = chartAttribute.First(p => p.Name == "height").Value.ToString();
                        tmp.Title= chartAttribute.First(p => p.Name == "title").Value.ToString();
                        tmp.Xfield = chartAttribute.First(p => p.Name == "xfield").Value.ToString();
                        tmp.FieldType = chartAttribute.First(p => p.Name == "type").Value.ToString();
                        tmp.ApplyFunction = chartAttribute.FirstOrDefault(p => p.Name == "applyFunction") != null ? chartAttribute.First(p => p.Name == "applyFunction").Value.ToString() : string.Empty;
                        report.GraphList.Add(tmp);
                    }
                }


                report.ReportName = string.Format("{0}/{1}", myNode.NodeName, report.ReportName);

                myNode.Report.Add(report.ReportName);
               _configDictionnary.Add(report.ReportName, report);

               



            }
            catch(Exception ex)
            {
                APILogger.Write(ex.Message, LogLevel.Error);
                throw ex;
            }
        }
        public static Report BuildMatchQuery(Report report)
        {

         

            var bsonArray = new BsonArray();
            DateTime outDate = DateTime.MinValue;
            int filterCount = 0;

            foreach (var query in report.LambdaQuery)
            {

                if (report.FilterDataValue[query.Values] == null ||
                        string.IsNullOrEmpty(report.FilterDataValue[query.Values].ToString()))
                {
                    if ( query.AllowNull)
                    {
                        continue;
                    }
                    else
                    {

                        throw new Exception("champs obligatoire non renségné ");
                    }
                }

                BsonDocument element = null;
                if (query.Operator == LambdaOperator.Eq)
                {
                    if (DateTime.TryParse(report.FilterDataValue[query.Values].ToString(), out outDate))
                    {
                        element = new BsonDocument(query.Field, BsonDateTime.Create(outDate));
                        
                    }

                    else
                    {
                        element = new BsonDocument(query.Field, BsonString.Create(report.FilterDataValue[query.Values]));
                    }
                    filterCount++;
                    bsonArray.Add(element);
                }

                if (query.Operator == LambdaOperator.Gt)
                {
                 

                    if (DateTime.TryParse(report.FilterDataValue[query.Values].ToString(), out outDate))
                    {

                        element = new BsonDocument("$gt", BsonDateTime.Create(outDate));
                    }

                    else
                    {
                        element = new BsonDocument("$gt", BsonString.Create(report.FilterDataValue[query.Values]));
                    }

                    var surElement = new BsonDocument(query.Field, element);
                    bsonArray.Add(surElement);
                    filterCount++;
                }

              
                if (query.Operator == LambdaOperator.Lt)
                {
                    if ( DateTime.TryParse( report.FilterDataValue[query.Values].ToString(),out  outDate))
                    {
                        element = new BsonDocument("$lt", BsonDateTime.Create(outDate));
                    }

                    else
                    {
                        element = new BsonDocument("$lt", BsonString.Create(report.FilterDataValue[query.Values]));
                    }
                    var surElement = new BsonDocument(query.Field, element);
                    bsonArray.Add(surElement);
                    filterCount++;

                }

            }
            
            if(filterCount == 0)
            {
                throw new Exception("Au moins un filtre doit être renségné");
            }

            var bsonAnd = new BsonDocument("$and", bsonArray);
            var bsonMatch = new BsonDocument("$match", bsonAnd);
          

            for(int i=0;i< report.Query.Count; i++)
            {
                if(report.Query[i].Contains("$$lambdaFilter$$"))
                {
                    report.Query[i] = bsonMatch.ToString();
                }
            }

            var aggregationFrameWork  = new AggregationFrameWorkPipline(report.ServerUri,
                                                                            report.ServerPort,
                                                                            report.DataBase,
                                                                            report.CollectionName);


            var result =  aggregationFrameWork.Execute(report.Query,report.Grid.MaxInPage,
                                                                report.CurrentPage,
                                                                report.ResultCount
                                                                );

            report.ResultCount = result.Item2;
            report.Log = result.Item3;

            long rem = 0;

            report.PageCount = Math.DivRem(report.ResultCount, report.Grid.MaxInPage, out rem);

            if(rem >0)
            {
                report.PageCount = report.PageCount + 1;
            }

           BuildGridResult(report, result.Item1);

            return report;
        }
        private static void BuildGridResult(Report report,List<BsonDocument> resultSet)
        {
            var resultGrid = new DataSet();
            resultGrid.Tables.Add(new DataTable("ReseultSet"));
            //Adding columns

            foreach(var column in report.Grid.Column)
            {
                resultGrid.Tables[0].Columns.Add(new DataColumn(column.Name));
            }

         
            foreach (var record in resultSet)
            {
                var row = resultGrid.Tables[0].NewRow();

                foreach (var column in report.Grid.Column)
                {
                    var bindingPath = column.Binding.Split('.').ToList();
                    var value = GetValueForBinding(record, bindingPath);

                    row[column.Name] = value;

                }

                resultGrid.Tables[0].Rows.Add(row);
            }

            report.Grid.ResultSet = resultGrid;

        }
        private static void BuildChartsData(Report report)
        {
           
            

            //report.Grid.ExcelResultSet = resultGrid;

        }
        public static Report ExcelExport(Report report)
        {

            var aggregationFrameWork = new AggregationFrameWorkPipline(report.ServerUri,
                                                                            report.ServerPort,
                                                                            report.DataBase,
                                                                            report.CollectionName);

            var result = aggregationFrameWork.Execute(report.Query,-1,-1,-1);

            BuildExcelResult(report, result.Item1);
            report.Log = result.Item3;

            return report;
        }