public DatasetJsonObj ParseAllData(IDataSetStore store, List <DataCriteria> Criterias, LayoutObj layObj, IDataStructureObject kf, ISet <ICodelistObject> codelists)
        {
            if (PageNumber > 0)
            {
                return(ParseAllDataPage(1));
            }

            string DominantFreq = CalculateDominantFrequency(Criterias, kf, codelists);


            CurrentPageIndex = 1;
            int            NObservationForPage = WebClientSettings.Instance.NObservationForPage;
            DatasetJsonObj dataset             = new DatasetJsonObj()
            {
                series = new Dictionary <string, Dictionary <string, string> >()
            };
            List <string> sort = new List <string>();

            sort.AddRange(layObj.axis_y);
            sort.AddRange(layObj.axis_x);
            store.SetSort(sort);
            store.SetCriteria(Criterias);
            IDataReader datareader = store.CreateDataReader(false);

            try
            {
                int ActualRecordRegistred = 0;
                PageNumber = 0;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = int.MaxValue;
                while (datareader.Read())
                {
                    List <string> Colonne = new List <string>();
                    layObj.axis_x.ForEach(axisX => Colonne.Add(GetFromReader(datareader, axisX, DominantFreq, kf.TimeDimension.Id)));

                    List <string> Righe = new List <string>();
                    layObj.axis_y.ForEach(axisY => Righe.Add(GetFromReader(datareader, axisY, DominantFreq, kf.TimeDimension.Id)));

                    string OBSVal = (string)datareader[kf.PrimaryMeasure.Id];

                    string serieString = string.Join("+", Righe);

                    if (!dataset.series.ContainsKey(serieString))
                    {
                        if (ActualRecordRegistred >= NObservationForPage)
                        {
                            //NormalizeDataset(dataset, layObj.axis_x, kf, codelists);
                            FileInfo fi = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                            File.WriteAllText(fi.FullName, ser.Serialize(dataset));

                            ActualRecordRegistred = 0;
                            PageNumber++;
                            dataset = null;
                            dataset = new DatasetJsonObj()
                            {
                                series = new Dictionary <string, Dictionary <string, string> >()
                            };
                        }

                        //dataset.series[serieString] = AllPossibleValues(layObj.axis_x, kf, codelists);
                        dataset.series[serieString] = new Dictionary <string, string>();
                    }
                    dataset.series[serieString][string.Join("+", Colonne)] = OBSVal;
                    ActualRecordRegistred++;
                }

                FileInfo fiLastPage = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                File.WriteAllText(fiLastPage.FullName, ser.Serialize(dataset));
                PageNumber++;
                if (PageNumber == 1)
                {
                    return(dataset);
                }

                return(ParseAllDataPage(1));
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Commit();
            }
        }
        public DatasetJsonObj ParseAllData(IDataSetStore store, List<DataCriteria> Criterias, LayoutObj layObj, IDataStructureObject kf, ISet<ICodelistObject> codelists)
        {
            if (PageNumber > 0)
                return ParseAllDataPage(1);

            string DominantFreq = CalculateDominantFrequency(Criterias, kf, codelists);

            CurrentPageIndex = 1;
            int NObservationForPage = WebClientSettings.Instance.NObservationForPage;
            DatasetJsonObj dataset = new DatasetJsonObj() { series = new Dictionary<string, Dictionary<string, string>>() };
            List<string> sort = new List<string>();
            sort.AddRange(layObj.axis_y);
            sort.AddRange(layObj.axis_x);
            store.SetSort(sort);
            store.SetCriteria(Criterias);
            IDataReader datareader = store.CreateDataReader(false);
            try
            {
                int ActualRecordRegistred = 0;
                PageNumber = 0;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = int.MaxValue;
                while (datareader.Read())
                {
                    List<string> Colonne = new List<string>();
                    layObj.axis_x.ForEach(axisX => Colonne.Add(GetFromReader(datareader, axisX, DominantFreq, kf.TimeDimension.Id)));

                    List<string> Righe = new List<string>();
                    layObj.axis_y.ForEach(axisY => Righe.Add(GetFromReader(datareader, axisY, DominantFreq, kf.TimeDimension.Id)));

                    string OBSVal = (string)datareader[kf.PrimaryMeasure.Id];

                    string serieString = string.Join("+", Righe);

                    if (!dataset.series.ContainsKey(serieString))
                    {
                        if (ActualRecordRegistred >= NObservationForPage)
                        {
                            //NormalizeDataset(dataset, layObj.axis_x, kf, codelists);
                            FileInfo fi = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                            File.WriteAllText(fi.FullName, ser.Serialize(dataset));

                            ActualRecordRegistred = 0;
                            PageNumber++;
                            dataset = null;
                            dataset = new DatasetJsonObj() { series = new Dictionary<string, Dictionary<string, string>>() };
                        }

                        //dataset.series[serieString] = AllPossibleValues(layObj.axis_x, kf, codelists);
                        dataset.series[serieString] = new Dictionary<string, string>();
                    }
                    dataset.series[serieString][string.Join("+", Colonne)] = OBSVal;
                    ActualRecordRegistred++;

                }

                FileInfo fiLastPage = new FileInfo(Path.Combine(PagingFileDirectory.FullName, string.Format(FilePageFormat, PagingFileDirectory.Name, PageNumber + 1)));
                File.WriteAllText(fiLastPage.FullName, ser.Serialize(dataset));
                PageNumber++;
                if (PageNumber == 1)
                    return dataset;

                return ParseAllDataPage(1);

            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                store.Commit();
            }
        }
        public SessionImplObject GetDataChart(SessionQuery sessionQuery)
        {
            try
            {
                // Init session objects
                if (this.SessionObj == null)
                {
                    this.SessionObj            = new SessionImplObject();
                    this.SessionObj.SdmxObject = new SdmxObjectsImpl();
                }

                JavaScriptSerializer ser = new JavaScriptSerializer();
                ser.MaxJsonLength = int.MaxValue;

                #region +++ Caching +++
                ConnectionStringSettings connectionStringSetting;
                CacheWidget cache          = null;
                bool        UseWidgetCache = (WebClientSettings.Instance != null) ? WebClientSettings.Instance.UseWidgetCache : false;
                if (UseWidgetCache)
                {
                    connectionStringSetting = ConfigurationManager.ConnectionStrings["ISTATWebClientConnection"];
                    cache = new CacheWidget(connectionStringSetting.ConnectionString);
                }
                if (ChartObj.WidgetId > 0 && UseWidgetCache)
                {
                    SavedWidget widget = cache.GetWidget(ChartObj.WidgetId, ChartObj.Configuration.Locale);
                    if (widget != null && !String.IsNullOrEmpty(widget.widgetData))
                    {
                        this.SessionObj.SavedChart = widget.widgetData;
                        return(this.SessionObj);
                    }
                }
                #endregion

                if (BDO == null || GetSDMXObject == null)
                {
                    throw new Exception(Messages.label_error_network);
                }

                codemapWidget = new CodemapWidget(
                    new GetCodemapObject()
                {
                    PreviusCostraint = this.ChartObj.Criteria,
                    Configuration    = this.ChartObj.Configuration,
                    Dataflow         = this.ChartObj.Dataflow
                },
                    this.SessionObj, sessionQuery);


                //ISdmxObjects structure = codemapWidget.GetDsd();
                //IDataflowObject df = structure.Dataflows.FirstOrDefault();
                //IDataStructureObject kf = structure.DataStructures.First();
                ISdmxObjects structure = sessionQuery.Structure;
                //IDataflowObject df = structure.Dataflows.First();
                IDataflowObject df = sessionQuery.Dataflow;
                //IDataStructureObject kf = structure.DataStructures.First();
                IDataStructureObject kf = sessionQuery.KeyFamily;

                if (kf == null)
                {
                    throw new InvalidOperationException("DSD is not set");
                }
                if (df == null)
                {
                    throw new InvalidOperationException("Dataflow is not set");
                }

                Dictionary <string, ICodelistObject> ConceptCodelists = codemapWidget.GetCodelistMap(df, kf, false);
                ComponentCodeDescriptionDictionary   codemap          = new ComponentCodeDescriptionDictionary();
                foreach (string ConceptId in ConceptCodelists.Keys)
                {
                    ICodelistObject             codelist = ConceptCodelists[ConceptId];
                    Dictionary <string, string> codes    = new Dictionary <string, string>();

                    foreach (ICode codeItem in codelist.Items)
                    {
                        codes.Add(codeItem.Id, TextTypeHelper.GetText(codeItem.Names, this.ChartObj.Configuration.Locale));
                    }
                    codemap.Add(ConceptId, codes);

                    //var useFix20 = (ConfigurationManager.AppSettings["UseFix20Criteria"].ToString().ToLower() == "true");
                    //fabio prova
                    var useFix20 = (ConfigurationManager.AppSettings["UseFix20Criteria"].ToString().ToLower() == "false");
                    if (useFix20)
                    {
                        if (!(codelist.Items.Count > 1))
                        {
                            this.ChartObj.Criteria.Remove(ConceptId);
                        }
                    }
                }

                this.SessionObj.MergeObject(codemapWidget.SessionObj);

                #region Gestione last period
                int num1;
                if (this.ChartObj.Criteria.ContainsKey(kf.TimeDimension.Id) &&
                    this.ChartObj.Criteria[kf.TimeDimension.Id].Count == 1 &&
                    int.TryParse(this.ChartObj.Criteria[kf.TimeDimension.Id].First(), out num1)
                    )
                {
                    int offsetTime = int.Parse(this.ChartObj.Criteria[kf.TimeDimension.Id].First());
                    var codMap     = codemap;
                    int lengthTime = codMap[kf.TimeDimension.Id].Count;

                    if ((lengthTime - offsetTime) >= 0)
                    {
                        var           codes         = codMap[kf.TimeDimension.Id].Reverse().Take(offsetTime);
                        List <string> _criteriaTime = (from c in codes select c.Key).ToList <string>();

                        this.ChartObj.Criteria[kf.TimeDimension.Id] = new List <string>();
                        this.ChartObj.Criteria[kf.TimeDimension.Id].Add(_criteriaTime.Last());
                        this.ChartObj.Criteria[kf.TimeDimension.Id].Add(_criteriaTime.First());
                    }
                    else
                    {
                        this.ChartObj.Criteria[kf.TimeDimension.Id] = new List <string>();
                        this.ChartObj.Criteria[kf.TimeDimension.Id].Add(codemap[kf.TimeDimension.Id].First().Key);
                        this.ChartObj.Criteria[kf.TimeDimension.Id].Add(codemap[kf.TimeDimension.Id].Last().Key);
                    }
                }

                #endregion

                List <DataCriteria> Criterias = BDO.InitCriteria(kf, this.ChartObj.Criteria);
                //List<DataCriteria> Criterias = sessionQuery.GetCriteria();
                Dictionary <string, List <DataChacheObject> > DataCache = SessionObj.DataCache;


                //aggiunta da fabio
                //IDataSetStore store = BDO.GetDataset(df, kf, Criterias, ref DataCache, false, sessionQuery);

                IDataSetStore store = sessionQuery._store;
                store.SetCriteria(Criterias);

                /*
                 * IDataSetStore store;
                 * if (sessionQuery._store != null)
                 * { store = sessionQuery._store; }
                 * else
                 * {
                 *  //store = BDO.GetDataset(df, kf, Criterias, ref DataCache, _useAttr);
                 *  store = BDO.GetDataset(df, kf, Criterias, ref DataCache, false, sessionQuery);
                 *  sessionQuery._store = store;
                 * }
                 */
                //fine nuovo



                //string DBFileName = null;
                //IDataSetStore store = BDO.FindDataCache(df, kf, Criterias, ref DataCache, false, out DBFileName);
                //if (store == null) store = BDO.GetDataset(df, kf, Criterias, ref DataCache);
                //if (store == null) store = BDO.GetDataset(df, kf, Criterias, ref DataCache, false);



                //da vedere se eliminare aggiunta fabio per svuotare datacache
                SessionObj.DataCache = null;


                SessionObj.DataCache = DataCache;

                DataObjectForStreaming DataStream = new DataObjectForStreaming()
                {
                    store     = store,
                    Criterias = Criterias,
                    structure = structure,
                    codemap   = codemap
                };

                ChartResponseObject ChartResponse = new ChartResponseObject();
                ChartResponse.series_title = TextTypeHelper.GetText(df.Names, this.ChartObj.Configuration.Locale);
                ChartResponse.series       = BuildChart(store, kf, ConceptCodelists);
                ChartResponse.primary_name =
                    (this.ChartObj.ObsValue[0] == "v") ? Messages.label_varValue :
                    (this.ChartObj.ObsValue[0] == "vt") ? Messages.label_varTrend :
                    (this.ChartObj.ObsValue[0] == "vc") ? Messages.label_varCyclical : string.Empty;
                ChartResponse.secondary_name =
                    (this.ChartObj.ObsValue.Count > 1) ?
                    (this.ChartObj.ObsValue[1] == "v") ? Messages.label_varValue :
                    (this.ChartObj.ObsValue[1] == "vt") ? Messages.label_varTrend :
                    (this.ChartObj.ObsValue[1] == "vc") ? Messages.label_varCyclical : string.Empty : string.Empty;
                ChartResponse.x_name = (!string.IsNullOrEmpty(ChartObj.DimensionAxe)) ? ChartObj.DimensionAxe : kf.TimeDimension.Id;;

                // 23/07/2015
                // calcolo massimo e minimo
                decimal?primary_max   = null;
                decimal?primary_min   = null;
                decimal?secondary_max = null;
                decimal?secondary_min = null;
                decimal costantemax   = 1.1m;
                decimal costantemin   = 0.9m;

                foreach (serieType serie in ChartResponse.series)
                {
                    if (serie.axisYType == "secondary")
                    {
                        //fabio 12/08/2015
                        //decimal max = (decimal)serie.dataPoints.Where(m => m.y != null).Max(d => d.y);
                        decimal max = Convert.ToDecimal(serie.dataPoints.Where(m => m.y != null).Max(d => d.y));
                        if (secondary_max == null || max > secondary_max)
                        {
                            secondary_max = max;
                        }

                        //fabio 12/08/2015
                        //decimal min = (decimal)serie.dataPoints.Where(m => m.y != null).Min(d => d.y);
                        decimal min = Convert.ToDecimal(serie.dataPoints.Where(m => m.y != null).Min(d => d.y));
                        if (secondary_min == null || min < secondary_min)
                        {
                            secondary_min = min;
                        }

                        //fabio 12/08/2015
                        if (secondary_min == secondary_max)
                        {
                            secondary_min = secondary_min * costantemin; secondary_max = secondary_max * costantemax;
                        }
                    }
                    else
                    {
                        //fabio 12/08/2015
                        //decimal max = (decimal)serie.dataPoints.Where(m => m.y != null).Max(d => d.y);
                        decimal max = Convert.ToDecimal(serie.dataPoints.Where(m => m.y != null).Max(d => d.y));
                        if (primary_max == null || max > primary_max)
                        {
                            primary_max = max;
                        }

                        //fabio 12/08/2015
                        //decimal min = (decimal)serie.dataPoints.Where(m => m.y != null).Min(d => d.y);
                        decimal min = Convert.ToDecimal(serie.dataPoints.Where(m => m.y != null).Min(d => d.y));
                        if (primary_min == null || min < primary_min)
                        {
                            primary_min = min;
                        }

                        //fabio 12/08/2015
                        if (primary_min == primary_max)
                        {
                            primary_min = primary_min * costantemin; primary_max = primary_max * costantemax;
                        }
                    }
                }
                if (primary_max != null && primary_min != null)
                {
                    //decimal delta = (decimal)primary_max - (decimal)primary_min;
                    //ChartResponse.primary_max = (decimal)primary_max;
                    //ChartResponse.primary_min = (decimal)primary_min;

                    if (primary_max > 0)
                    {
                        ChartResponse.primary_max = (decimal)(primary_max * 1.1m);
                    }
                    else if (primary_max == 0)
                    {
                        ChartResponse.primary_max = (decimal) - 1.1m;
                    }
                    else
                    {
                        ChartResponse.primary_max = (decimal)(primary_max * 0.9m);
                    }

                    if (primary_min > 0)
                    {
                        ChartResponse.primary_min = (decimal)(primary_min * 0.9m);
                    }
                    else if (primary_min == 0)
                    {
                        ChartResponse.primary_min = (decimal) - 1.1m;
                    }
                    else
                    {
                        ChartResponse.primary_min = (decimal)(primary_min * 1.1m);
                    }
                }
                if (secondary_max != null && secondary_min != null)
                {
                    //ChartResponse.secondary_max = (decimal)secondary_max;
                    //ChartResponse.secondary_min = (decimal)secondary_min;

                    if (secondary_max > 0)
                    {
                        ChartResponse.secondary_max = (decimal)(secondary_max * 1.1m);
                    }
                    else if (secondary_max == 0)
                    {
                        ChartResponse.secondary_max = (decimal) - 1.1m;
                    }
                    else
                    {
                        ChartResponse.secondary_max = (decimal)(secondary_max * 0.9m);
                    }

                    if (secondary_min > 0)
                    {
                        ChartResponse.secondary_min = (decimal)(secondary_min * 0.9m);
                    }
                    else if (secondary_min == 0)
                    {
                        ChartResponse.secondary_min = (decimal) - 1.1m;
                    }
                    else
                    {
                        ChartResponse.secondary_min = (decimal)(secondary_min * 1.1m);
                    }
                }

                this.SessionObj.SavedChart = ser.Serialize(ChartResponse);

                // +++ Caching +++
                if (ChartObj.WidgetId > 0 && UseWidgetCache)
                {
                    cache.InsertWidget(ChartObj.WidgetId, this.SessionObj.SavedChart, ChartObj.Configuration.Locale);
                }

                return(this.SessionObj);
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
                throw ex;
            }
        }