public ActionResult SetCostraint()
        {
            try
            {

                GetCodemapObject PostDataArrived = CS.GetPostData<GetCodemapObject>(this.Request);
                PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                SessionImplObject sdmxObj =
                    (Session[SESSION_KEY] != null) ? Session[SESSION_KEY] as SessionImplObject : new SessionImplObject();

                CodemapWidget codemapWidget = new CodemapWidget(PostDataArrived, sdmxObj);
                SessionQuery query = SessionQueryManager.GetSessionQuery(Session);
                query.Criteria = PostDataArrived.PreviusCostraint;

                //Se è il primo constraint è vuoto
                if (PostDataArrived.PreviusCostraint.Count > 0)
                { string ret = codemapWidget.ComponentSave(PostDataArrived.Codelist, PostDataArrived.PreviusCostraint[PostDataArrived.Codelist].ToArray(), query); }

                var count=0;

                return CS.ReturnForJQuery(count);
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(JSONConst.Error);
            }
        }
        public ActionResult GetSpecificCodemap()
        {
            try
            {
                GetCodemapObject PostDataArrived = CS.GetPostData<GetCodemapObject>(this.Request);
                PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                // Check if a SessionImplObject is store in session
                SessionImplObject sdmxObj =
                    (Session[SESSION_KEY] != null) ? Session[SESSION_KEY] as SessionImplObject : new SessionImplObject();

                //Clear query datasetmodel and store
                SessionQuery query = SessionQueryManager.GetSessionQuery(Session);
                query.ClearData();

                CodemapWidget codemapWidget = new CodemapWidget(PostDataArrived, sdmxObj);

                SessionImplObject ret = codemapWidget.GetSpecificCodemap(string.IsNullOrEmpty(PostDataArrived.Codelist) ? true : false, connectionStringSetting,query);

                // store current SessionImplObject in session
                if (Session[SESSION_KEY] == null) Session[SESSION_KEY] = ret;
                else ((SessionImplObject)Session[SESSION_KEY]).MergeObject(ret);

                return CS.ReturnForJQuery(ret.SavedCodemap);
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(JSONConst.Error);
            }
        }
        public ActionResult GetMetadata()
        {
            try
            {
                GetMetaDataObject PostDataArrived = CS.GetPostData<GetMetaDataObject>(this.Request);
                PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                SessionImplObject sdmxObj =
                    (Session[SESSION_KEY] != null) ? Session[SESSION_KEY] as SessionImplObject : new SessionImplObject();

                SessionQuery query = SessionQueryManager.GetSessionQuery(Session);

                if (PostDataArrived.ArtefactType == "*")
                {
                    #region Return summary DSD

                    CodemapWidget codemapWidget = new CodemapWidget(new GetCodemapObject()
                    {
                        Configuration = PostDataArrived.Configuration,
                        Dataflow = new MaintenableObj()
                        {
                            id = PostDataArrived.Artefact.id,
                            agency = PostDataArrived.Artefact.agency,
                            version = PostDataArrived.Artefact.version
                        }
                    }, sdmxObj);
                   /* ConnectionStringSettings connectionStringSetting = ConfigurationManager.ConnectionStrings["ISTATWebClientConnection"];
                    if (connectionStringSetting == null
                        || string.IsNullOrEmpty(connectionStringSetting.ConnectionString))
                        throw new Exception("ConnectionString not set");
                    */
                    //OLD
                    //var _ret = codemapWidget.GetCodemap(connectionStringSetting).SdmxObject;
                    //var _ret = codemapWidget.GetCodemap(query, connectionStringSetting).SdmxObject;
                    var conceptCodelist = codemapWidget.GetCodelistMap(query, false);
                    //sdmxObj.SdmxObject.Merge(_ret);

                    StructureResponceObject _dsd = new StructureResponceObject();
                    //var dsd = sdmxObj.SdmxObject.DataStructures.FirstOrDefault();
                    var dsd = query.Structure.DataStructures.FirstOrDefault();
                    _dsd.Dsd = new DsdObject()
                    {
                        Name = TextTypeHelper.GetText(dsd.Names, PostDataArrived.Configuration.Locale),
                        Desc = TextTypeHelper.GetText(dsd.Descriptions, PostDataArrived.Configuration.Locale),
                        Id = dsd.Id,
                        Agency = dsd.AgencyId,
                        Version = dsd.Version,
                    };
                    _dsd.Concept = new List<ConceptObject>();

                    //foreach (var concept in sdmxObj.SdmxObject.ConceptSchemes)
                    foreach (var concept in query.Structure.ConceptSchemes)
                    {
                        _dsd.Concept.Add(
                            new ConceptObject()
                        {
                            Name = TextTypeHelper.GetText(concept.Names, PostDataArrived.Configuration.Locale),
                            Desc = TextTypeHelper.GetText(concept.Descriptions, PostDataArrived.Configuration.Locale),
                            Id = concept.Id,
                            Agency = concept.AgencyId,
                            Version = concept.Version,
                        });
                    }
                    _dsd.Codelist = new List<CodelistObject>();
                    foreach (var dimension in dsd.DimensionList.Dimensions.Where(c => c.HasCodedRepresentation()))
                    {
                        var codelistObj = new CodelistObject()
                            {
                                Id = (dimension.Representation.Representation.MaintainableReference.HasMaintainableId()) ? dimension.Representation.Representation.MaintainableReference.MaintainableId : string.Empty,
                                Agency = (dimension.Representation.Representation.MaintainableReference.HasAgencyId()) ? dimension.Representation.Representation.MaintainableReference.AgencyId : string.Empty,
                                Version = (dimension.Representation.Representation.MaintainableReference.HasVersion()) ? dimension.Representation.Representation.MaintainableReference.Version : string.Empty,
                            };

                        var codelist =
                        (from c in sdmxObj.SdmxObject.Codelists
                         where c.Id.ToString() == codelistObj.Id
                         && c.AgencyId.ToString() == codelistObj.Agency
                         && c.Version.ToString() == codelistObj.Version
                         select c).FirstOrDefault();

                        if (codelist == null) continue;

                        codelistObj.Name = TextTypeHelper.GetText(codelist.Names, PostDataArrived.Configuration.Locale);
                        codelistObj.Desc = TextTypeHelper.GetText(codelist.Descriptions, PostDataArrived.Configuration.Locale);
                        codelistObj.Items = new List<ItemObject>();

                        _dsd.Codelist.Add(codelistObj);
                    }
                    if (dsd.AttributeList != null)
                        foreach (var attribute in dsd.AttributeList.Attributes.Where(c => c.HasCodedRepresentation()))
                        {
                            var codelistObj = new CodelistObject()
                                {
                                    Id = (attribute.Representation.Representation.MaintainableReference.HasMaintainableId()) ? attribute.Representation.Representation.MaintainableReference.MaintainableId : string.Empty,
                                    Agency = (attribute.Representation.Representation.MaintainableReference.HasAgencyId()) ? attribute.Representation.Representation.MaintainableReference.AgencyId : string.Empty,
                                    Version = (attribute.Representation.Representation.MaintainableReference.HasVersion()) ? attribute.Representation.Representation.MaintainableReference.Version : string.Empty,
                                };

                            var codelist =
                            (from c in sdmxObj.SdmxObject.Codelists
                             where c.Id.ToString() == codelistObj.Id
                             && c.AgencyId.ToString() == codelistObj.Agency
                             && c.Version.ToString() == codelistObj.Version
                             select c).FirstOrDefault();

                            if (codelist == null) continue;

                            codelistObj.Name = TextTypeHelper.GetText(codelist.Names, PostDataArrived.Configuration.Locale);
                            codelistObj.Desc = TextTypeHelper.GetText(codelist.Descriptions, PostDataArrived.Configuration.Locale);
                            codelistObj.Items = new List<ItemObject>();

                            _dsd.Codelist.Add(codelistObj);
                        }

                    return CS.ReturnForJQuery(_dsd);

                    #endregion
                }
                else if (PostDataArrived.ArtefactType == "DSD")
                {
                    #region Return dettail DSD
                    DsdResponceObject _dsd = new DsdResponceObject();
                    //var dsd = sdmxObj.SdmxObject.DataStructures.FirstOrDefault();
                    var dsd = query.Structure.DataStructures.FirstOrDefault();
                    _dsd.Dsd = new DsdObject()
                    {
                        Name = TextTypeHelper.GetText(dsd.Names, PostDataArrived.Configuration.Locale),
                        Desc = TextTypeHelper.GetText(dsd.Descriptions, PostDataArrived.Configuration.Locale),
                        Id = dsd.Id,
                        Agency = dsd.AgencyId,
                        Version = dsd.Version,
                    };

                    _dsd.Dimension = new List<DimensionObject>();
                    foreach (var dimension in dsd.DimensionList.Dimensions)
                    {

                        //var conceptScheme = (from c in sdmxObj.SdmxObject.ConceptSchemes
                        var conceptScheme = (from c in query.Structure.ConceptSchemes
                                             where c.Id == dimension.ConceptRef.MaintainableReference.MaintainableId
                                             && c.AgencyId == dimension.ConceptRef.MaintainableReference.AgencyId
                                             && c.Version == dimension.ConceptRef.MaintainableReference.Version
                                             select new
                                             {
                                                 c,
                                                 concept = (from d in c.Items
                                                            where d.Id == dimension.ConceptRef.FullId
                                                            select d).FirstOrDefault()
                                             }).FirstOrDefault();

                        _dsd.Dimension.Add(
                            new DimensionObject()
                            {
                                Concept_Id = (conceptScheme.concept!=null)?conceptScheme.concept.Id:string.Empty,
                                Concept_Name = TextTypeHelper.GetText(conceptScheme.concept.Names, PostDataArrived.Configuration.Locale),

                                ConceptScheme_Id = (conceptScheme.c != null) ? conceptScheme.c.Id : string.Empty,
                                ConceptScheme_Agency = (conceptScheme.c != null) ? conceptScheme.c.AgencyId : string.Empty,
                                ConceptScheme_Version = (conceptScheme.c != null) ? conceptScheme.c.Version : string.Empty,

                                Codelist_Id = (dimension.Representation != null && dimension.Representation.Representation != null) ? (dimension.Representation.Representation.MaintainableReference.HasMaintainableId()) ? dimension.Representation.Representation.MaintainableReference.MaintainableId : string.Empty : string.Empty,
                                Codelist_Agency = (dimension.Representation != null && dimension.Representation.Representation != null) ? (dimension.Representation.Representation.MaintainableReference.HasAgencyId()) ? dimension.Representation.Representation.MaintainableReference.AgencyId : string.Empty : string.Empty,
                                Codelist_Version = (dimension.Representation != null && dimension.Representation.Representation != null) ? (dimension.Representation.Representation.MaintainableReference.HasVersion()) ? dimension.Representation.Representation.MaintainableReference.Version : string.Empty : string.Empty,

                                DimensionType = (dimension.FrequencyDimension)?"Frequency":(dimension.TimeDimension)?"Time Period":(dimension.MeasureDimension)?"Measure":string.Empty,
                                TextFormat = (dimension.Representation != null && dimension.Representation.TextFormat != null && dimension.Representation.TextFormat.TextType != null) ? dimension.Representation.TextFormat.TextType.EnumType.ToString() : string.Empty,

                            });
                    }

                    if (dsd.AttributeList != null)
                    {

                        _dsd.Attribute = new List<AttributeObject>();
                        foreach (var attribute in dsd.AttributeList.Attributes)
                        {
                            //var conceptScheme = (from c in sdmxObj.SdmxObject.ConceptSchemes
                            var conceptScheme = (from c in query.Structure.ConceptSchemes
                                                 where c.Id == attribute.ConceptRef.MaintainableReference.MaintainableId
                                                 && c.AgencyId == attribute.ConceptRef.MaintainableReference.AgencyId
                                                 && c.Version == attribute.ConceptRef.MaintainableReference.Version
                                                 select new
                                                 {
                                                     c,
                                                     concept = (from d in c.Items
                                                                where d.Id == attribute.ConceptRef.FullId
                                                                select d).FirstOrDefault()
                                                 }).FirstOrDefault();
                            _dsd.Attribute.Add(
                                new AttributeObject()
                                {
                                    AttachmentLevel = attribute.AttachmentLevel.ToString(),
                                    AssignmentStatus = attribute.AssignmentStatus.ToString(),
                                    AttributeType = (attribute.Mandatory) ? "Mandatory" : string.Empty,

                                    Concept_Id = (conceptScheme.concept != null) ? conceptScheme.concept.Id : string.Empty,
                                    Concept_Name = TextTypeHelper.GetText(conceptScheme.concept.Names, PostDataArrived.Configuration.Locale),

                                    ConceptScheme_Id = (conceptScheme.c != null) ? conceptScheme.c.Id : string.Empty,
                                    ConceptScheme_Agency = (conceptScheme.c != null) ? conceptScheme.c.AgencyId : string.Empty,
                                    ConceptScheme_Version = (conceptScheme.c != null) ? conceptScheme.c.Version : string.Empty,

                                    Codelist_Id = (attribute.Representation != null && attribute.Representation.Representation != null) ? (attribute.Representation.Representation.MaintainableReference.HasMaintainableId()) ? attribute.Representation.Representation.MaintainableReference.MaintainableId : string.Empty : string.Empty,
                                    Codelist_Agency = (attribute.Representation != null && attribute.Representation.Representation != null) ? (attribute.Representation.Representation.MaintainableReference.HasAgencyId()) ? attribute.Representation.Representation.MaintainableReference.AgencyId : string.Empty : string.Empty,
                                    Codelist_Version = (attribute.Representation != null && attribute.Representation.Representation != null) ? (attribute.Representation.Representation.MaintainableReference.HasVersion()) ? attribute.Representation.Representation.MaintainableReference.Version : string.Empty : string.Empty,

                                    TextFormat = (attribute.Representation != null && attribute.Representation.TextFormat != null && attribute.Representation.TextFormat.TextType != null) ? attribute.Representation.TextFormat.TextType.EnumType.ToString() : string.Empty,

                                });
                        }
                    }
                        _dsd.Measure = new List<MeasureObject>();
                        //foreach (var measure in dsd.MeasureList)
                        var measure = dsd.MeasureList.PrimaryMeasure;
                        {
                            //var conceptScheme = (from c in sdmxObj.SdmxObject.ConceptSchemes
                            var conceptScheme = (from c in query.Structure.ConceptSchemes
                                                 where c.Id == measure.ConceptRef.MaintainableReference.MaintainableId
                                                 && c.AgencyId == measure.ConceptRef.MaintainableReference.AgencyId
                                                 && c.Version == measure.ConceptRef.MaintainableReference.Version
                                                 select new
                                                 {
                                                     c,
                                                     concept = (from d in c.Items
                                                                where d.Id == measure.ConceptRef.FullId
                                                                select d).FirstOrDefault()
                                                 }).FirstOrDefault();
                            _dsd.Measure.Add(
                                new MeasureObject()
                                {
                                    Code = "N/A",
                                    Type ="Primary",
                                    MeasureDimension = "N/A",

                                    Concept_Id = (conceptScheme.concept != null) ? conceptScheme.concept.Id : string.Empty,
                                    Concept_Name = TextTypeHelper.GetText(conceptScheme.concept.Names, PostDataArrived.Configuration.Locale),

                                    ConceptScheme_Id = (conceptScheme.c != null) ? conceptScheme.c.Id : string.Empty,
                                    ConceptScheme_Agency = (conceptScheme.c != null) ? conceptScheme.c.AgencyId : string.Empty,
                                    ConceptScheme_Version = (conceptScheme.c != null) ? conceptScheme.c.Version : string.Empty,

                                    Codelist_Id = (measure.Representation != null && measure.Representation.Representation != null) ? (measure.Representation.Representation.MaintainableReference.HasMaintainableId()) ? measure.Representation.Representation.MaintainableReference.MaintainableId : string.Empty : string.Empty,
                                    Codelist_Agency = (measure.Representation != null && measure.Representation.Representation != null) ? (measure.Representation.Representation.MaintainableReference.HasAgencyId()) ? measure.Representation.Representation.MaintainableReference.AgencyId : string.Empty : string.Empty,
                                    Codelist_Version = (measure.Representation != null && measure.Representation.Representation != null) ? (measure.Representation.Representation.MaintainableReference.HasVersion()) ? measure.Representation.Representation.MaintainableReference.Version : string.Empty : string.Empty,

                                    TextFormat = (measure.Representation != null && measure.Representation.TextFormat != null && measure.Representation.TextFormat.TextType != null) ? measure.Representation.TextFormat.TextType.EnumType.ToString() : string.Empty,

                                });
                        }

                    //}

                    return CS.ReturnForJQuery(_dsd);

                    #endregion
                }
                else if (PostDataArrived.ArtefactType == "CODELIST")
                {
                    #region Return dettail CODELIST

                    CodelistObject cod = new CodelistObject();

                    var codelist =
                        (from c in sdmxObj.SdmxObject.Codelists
                         where c.Id.ToString() == PostDataArrived.Artefact.id.ToString()
                         && c.AgencyId.ToString() == PostDataArrived.Artefact.agency.ToString()
                         && c.Version.ToString() == PostDataArrived.Artefact.version.ToString()
                         select c).FirstOrDefault();

                    cod.Id = codelist.Id;
                    cod.Version = codelist.Version;
                    cod.Agency = codelist.AgencyId;
                    cod.Name = TextTypeHelper.GetText(codelist.Names, PostDataArrived.Configuration.Locale);
                    cod.Desc = TextTypeHelper.GetText(codelist.Descriptions, PostDataArrived.Configuration.Locale);
                    cod.Items = new List<ItemObject>();
                    foreach (var code in codelist.Items)
                    {
                        cod.Items.Add(new ItemObject()
                        {
                            Name = TextTypeHelper.GetText(code.Names, PostDataArrived.Configuration.Locale),
                            Desc = TextTypeHelper.GetText(code.Descriptions, PostDataArrived.Configuration.Locale),
                            Code = code.Id,
                            Parent = code.ParentCode
                        });
                    }

                    return CS.ReturnForJQuery(cod);

                    #endregion
                }
                else if (PostDataArrived.ArtefactType == "CONCEPTSCHEME")
                {
                    #region Return dettail CONCEPTSCHEME

                    ConceptObject conc = new ConceptObject();

                    var conceptScheme=
                        //(from c in sdmxObj.SdmxObject.ConceptSchemes
                        (from c in query.Structure.ConceptSchemes
                        where c.Id.ToString()==PostDataArrived.Artefact.id.ToString()
                        && c.AgencyId.ToString()==PostDataArrived.Artefact.agency.ToString()
                        && c.Version.ToString()==PostDataArrived.Artefact.version.ToString()
                        select c).FirstOrDefault();

                    conc.Id = conceptScheme.Id;
                    conc.Version = conceptScheme.Version;
                    conc.Agency = conceptScheme.AgencyId;
                    conc.Name = TextTypeHelper.GetText(conceptScheme.Names, PostDataArrived.Configuration.Locale);
                    conc.Desc = TextTypeHelper.GetText(conceptScheme.Descriptions, PostDataArrived.Configuration.Locale);
                    conc.Items = new List<ItemObject>();
                    foreach (var concept in conceptScheme.Items) {
                        conc.Items.Add(new ItemObject() {
                            Name = TextTypeHelper.GetText(concept.Names, PostDataArrived.Configuration.Locale),
                            Desc = TextTypeHelper.GetText(concept.Descriptions, PostDataArrived.Configuration.Locale),
                            Code=concept.Id,
                        });
                    }

                    return CS.ReturnForJQuery(conc);

                    #endregion
                }
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(ex.Message);
            }
            return CS.ReturnForJQuery(JSONConst.Error);
        }
        public ActionResult GetData()
        {
            try
            {
                // Not remove this linee
                Utils.App_Data_Path = HttpContext.Server.MapPath("~/App_Data/");

                    GetDataObject PostDataArrived = CS.GetPostData<GetDataObject>(this.Request);
                    PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                    SessionImplObject sdmxObj =
                    (Session[SESSION_KEY] != null) ? Session[SESSION_KEY] as SessionImplObject : new SessionImplObject();

                    SessionQuery query = SessionQueryManager.GetSessionQuery(Session);
                    query._endpointSettings = PostDataArrived.Configuration;

                    CodemapWidget codemapWidget = new CodemapWidget(new GetCodemapObject()
                    {
                        Configuration = PostDataArrived.Configuration
                    }, sdmxObj);

                        CacheWidget cache = new CacheWidget(connectionStringSetting.ConnectionString);

                        string CodeForStreaming = Guid.NewGuid().ToString();
                        // +++ Caching +++
                        if (PostDataArrived.WidgetId > 0 && UseWidgetCache)
                        {
                            SavedWidget widget = cache.GetWidget(PostDataArrived.WidgetId, System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName);
                            if (widget != null && !String.IsNullOrEmpty(widget.widgetData))
                            {
                                Session[CodeForStreaming] = widget;
                                return CS.ReturnForJQuery("{\"code\":\"" + CodeForStreaming + "\"}");
                            }
                        }

                        var count = codemapWidget.GetCountObservation(query);
                        long maxResultObs = (long)ISTAT.WebClient.WidgetComplements.Model.Settings.WebClientSettings.Instance.MaxResultObs;

                        if (count <= maxResultObs)
                        {
                        DataWidget dataWidget = new DataWidget(
                            PostDataArrived, sdmxObj,
                            (ConfigurationManager.AppSettings["ParseSDMXAttributes"].ToString().ToLower() == "true"));

                        object DataStream = null;
                        SessionImplObject ret = dataWidget.GetData(out DataStream, query);

                        // store current SessionImplObject in session
                        //nuovo fabio
                        if (Session[SESSION_KEY] == null) Session[SESSION_KEY] = ret;
                        else ((SessionImplObject)Session[SESSION_KEY]).MergeObject(ret);
                        //fine nuovo fabio

                        DataObjectForStreaming dOFS = DataStream as DataObjectForStreaming;
                        if (dOFS != null && PostDataArrived.WidgetId > 0)
                            dOFS.WidgetID = PostDataArrived.WidgetId;

                        Session[CodeForStreaming] = DataStream;

                        return CS.ReturnForJQuery("{\"code\":\"" + CodeForStreaming + "\"}");
                    }
                    else
                        {
                            return CS.ReturnForJQuery(Messages.label_out_max_results + "(max:"+ maxResultObs + " - record:" + count + ")");
                        }
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(ex.Message);
            }
        }
        /*get CODELIST with mode "COSTRAINT_NO_LIMIT"*/
        public ActionResult GetCodemapCostraintNoLimit()
        {
            try
            {
                GetCodemapObject PostDataArrived = CS.GetPostData<GetCodemapObject>(this.Request);
                PostDataArrived.Configuration.Locale = System.Threading.Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;

                // Clear session in All Codelist Costraint mode criteria
                Session[SESSION_KEY] = null;

                CodemapWidget codemapWidget = new CodemapWidget(PostDataArrived, null);

                SessionQuery query = SessionQueryManager.GetSessionQuery(Session);
                SessionImplObject ret = codemapWidget.GetCodemap(query,connectionStringSetting);

                // store current SessionImplObject in session
                Session[SESSION_KEY] = ret;

                return CS.ReturnForJQuery(ret.SavedCodemap);
            }
            catch (Exception ex)
            {
                return CS.ReturnForJQuery(ex.Message);
            }
        }
        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);
                //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);

                #region Gestione last period
                if (this.DataObj.Criteria.ContainsKey(kf.TimeDimension.Id)
                 && this.DataObj.Criteria[kf.TimeDimension.Id].Count == 1
                 && !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._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;
            }
        }