public SessionImplObject GetData(out object DataStream, SessionQuery query)
        {
            try
            {
                // Init session objects
                if (this.SessionObj == null)
                {
                    this.SessionObj            = new SessionImplObject();
                    this.SessionObj.SdmxObject = new SdmxObjectsImpl();
                }


                if (BDO == null || GetSDMXObject == null)
                {
                    throw new Exception(Messages.label_error_network + " " + DataObj.Configuration.Title);
                }


                codemapWidget = new CodemapWidget(new GetCodemapObject()
                {
                    Configuration    = this.DataObj.Configuration,
                    Dataflow         = this.DataObj.Dataflow,
                    PreviusCostraint = this.DataObj.Criteria
                },
                                                  this.SessionObj, query);
                //ISdmxObjects structure = codemapWidget.GetDsd();
                ISdmxObjects         structure = query.Structure;
                IDataflowObject      df        = structure.Dataflows.FirstOrDefault();
                IDataStructureObject kf        = structure.DataStructures.First();
                if (df == null)
                {
                    throw new InvalidOperationException("Dataflow is not set");
                }

                /****************/
                // Get all codelist
                /****************/
                //Dictionary<string, ICodelistObject> ConceptCodelists = codemapWidget.GetCodelistMap(df, kf, true);
                Dictionary <string, ICodelistObject> ConceptCodelists = codemapWidget.GetCodelistMap(query, 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.DataObj.Configuration.Locale));
                    }
                    codemap.Add(ConceptId, codes);
                }
                /****************/
                //codemapWidget.GetCodeListCostraint(df,kf,component)

                this.SessionObj.MergeObject(codemapWidget.SessionObj);
                int num1;
                #region Gestione last period
                if (this.DataObj.Criteria.ContainsKey(kf.TimeDimension.Id) &&
                    this.DataObj.Criteria[kf.TimeDimension.Id].Count == 1 &&
                    int.TryParse(this.DataObj.Criteria[kf.TimeDimension.Id].First(), out num1) &&
                    !this.DataObj.Layout.axis_z.Contains(kf.TimeDimension.Id))
                {
                    int offsetTime = int.Parse(this.DataObj.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.DataObj.Criteria[kf.TimeDimension.Id] = new List <string>();
                        this.DataObj.Criteria[kf.TimeDimension.Id].Add(_criteriaTime.Last());
                        this.DataObj.Criteria[kf.TimeDimension.Id].Add(_criteriaTime.First());
                    }
                    else
                    {
                        this.DataObj.Criteria[kf.TimeDimension.Id] = new List <string>();
                        this.DataObj.Criteria[kf.TimeDimension.Id].Add(codemap[kf.TimeDimension.Id].First().Key);
                        this.DataObj.Criteria[kf.TimeDimension.Id].Add(codemap[kf.TimeDimension.Id].Last().Key);
                    }
                }
                #endregion

                LayoutObj           layObj    = InitLayout(df, kf);
                List <DataCriteria> Criterias = BDO.InitCriteria(kf, this.DataObj.Criteria);
                //query.GetCriteria();

                Dictionary <string, List <DataChacheObject> > DataCache = SessionObj.DataCache;
                List <string>       ret            = null;
                List <DataCriteria> QueryCriterias = new List <DataCriteria>();

                if (query.Criteria != null)   //criteri nulli se proviene da un template
                {
                    if (query._store != null) //.Count == 1)
                    {
                        query.SetCriteriaTime(this.DataObj.Criteria[kf.TimeDimension.Id]);
                    }

                    if (query.Criteria.TryGetValue(kf.TimeDimension.Id, out ret))
                    {
                        if (ret.Count == 1)
                        {
                            query.SetCriteriaTime(this.DataObj.Criteria[kf.TimeDimension.Id]);
                        }
                    }


                    QueryCriterias = query.GetCriteria();

                    /*if (query._store == null) //.Count == 1)
                     * { QueryCriterias = Criterias; }
                     * else
                     * { QueryCriterias = query.GetCriteria(); }*/
                }
                else
                {
                    QueryCriterias = Criterias;
                }


                //aggiunta da fabio
                IDataSetStore store;
                if (query._store != null)
                {
                    store = query._store;
                }
                else
                {
                    //store = BDO.GetDataset(df, kf, Criterias, ref DataCache, _useAttr);
                    store        = BDO.GetDataset(df, kf, QueryCriterias, ref DataCache, _useAttr, query);
                    query._store = store;
                }
                //fine nuovo

                SessionObj.DataCache = DataCache;

                DataStream = new DataObjectForStreaming()
                {
                    Configuration = this.DataObj.Configuration,
                    store         = store,
                    layObj        = layObj,
                    Criterias     = Criterias,
                    structure     = structure,
                    codemap       = codemap
                };


                return(this.SessionObj);
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
                throw ex;
            }
        }
        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;
            }
        }