public IDataQuerySelection AddDimensionToQuery(QueryComponent queryComponent, IComponent component)
        {
            ISet<string> valuern = new HashSet<string>();
            foreach (ICode c in queryComponent.RetrieveCodes())
            {
                if (!string.IsNullOrEmpty(c.Id))
                {
                    valuern.Add((c.Id));
                }
            }
            IDataQuerySelection selection = new DataQueryDimensionSelectionImpl(component.Id, valuern);

            return selection;
        }
 public IDataQuery Build(IDataStructureObject dsd, IDataflowObject dataflowObject)
 {
     IDataQuerySelection dataQuerySelection = new DataQueryDimensionSelectionImpl("FREQ", "M", "A");
     IDataQuerySelectionGroup dataQuerySelectionGroup = new DataQuerySelectionGroupImpl(new HashSet<IDataQuerySelection> { dataQuerySelection }, new SdmxDateCore("2000-01"), new SdmxDateCore("2005-03"));
     return new DataQueryImpl(
         dataStructure: dsd,
         lastUpdated: null,
         dataQueryDetail: DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full),
         firstNObs: null,
         lastNObs: null,
         dataProviders: null,
         dataflow: dataflowObject,
         dimensionAtObservation: "TIME_PERIOD",
         selectionGroup: new[] { dataQuerySelectionGroup });
 }
        /// <summary>
        /// Build from a REST query and a bean retrival manager
        /// </summary>
        /// <param name="dataQuery"></param>
        /// <param name="retrievalManager"></param>
        /// <exception cref="SdmxSemmanticException"></exception>
        public DataQueryImpl(IRestDataQuery dataQuery, ISdmxObjectRetrievalManager retrievalManager)
        {
            this._lastUpdated = dataQuery.UpdatedAfter;
            this._dataQueryDetail = dataQuery.QueryDetail ?? DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full);

            base.FirstNObservations = dataQuery.FirstNObservations;
            base.LastNObservations = dataQuery.LastNObsertations;
            if (ObjectUtil.ValidString(dataQuery.DimensionAtObservation))
            {
                this.DimensionAtObservation = dataQuery.DimensionAtObservation;
            }

            base.Dataflow = retrievalManager.GetMaintainableObject<IDataflowObject>(dataQuery.FlowRef.MaintainableReference);
            if (base.Dataflow == null)
            {
                throw new SdmxNoResultsException("Data Flow could not be found for query : " + dataQuery.FlowRef);
            }

            base.DataStructure =
                retrievalManager.GetMaintainableObject<IDataStructureObject>(base.Dataflow.DataStructureRef.MaintainableReference);
            if (base.DataStructure == null)
            {
                throw new SdmxNoResultsException("DSD could not be found for query : " + base.Dataflow.DataStructureRef);
            }

            ISet<IDataProvider> dataProviders = new HashSet<IDataProvider>();
            if (dataQuery.ProviderRef != null)
            {
                ISet<IDataProviderScheme> dataProviderSchemes =
                    retrievalManager.GetMaintainableObjects<IDataProviderScheme>(dataQuery.ProviderRef.MaintainableReference);
                foreach (IDataProviderScheme currentDpScheme in dataProviderSchemes)
                {
                    foreach (IDataProvider dataProvider in currentDpScheme.Items)
                    {
                        if (dataProvider.Id.Equals(dataQuery.ProviderRef.ChildReference.Id))
                        {
                            dataProviders.Add(dataProvider);
                        }
                    }
                }
            }
            ISet<IDataQuerySelection> selections = new HashSet<IDataQuerySelection>();
            if (dataQuery.QueryList.Count > 0)
            {
                int i = 0;
                foreach (IDimension dimension in base.DataStructure.GetDimensions(SdmxStructureEnumType.Dimension, SdmxStructureEnumType.MeasureDimension).OrderBy(dimension => dimension.Position))
                {
                    if (dataQuery.QueryList.Count <= i)
                    {
                        throw new SdmxSemmanticException(
                            "Not enough key values in query, expecting "
                            + base.DataStructure.GetDimensions(SdmxStructureEnumType.Dimension, SdmxStructureEnumType.MeasureDimension).Count + " got "
                            + dataQuery.QueryList.Count);
                    }
                    ISet<string> queriesForDimension = dataQuery.QueryList[i];
                    if (queriesForDimension != null && queriesForDimension.Count > 0)
                    {
                        IDataQuerySelection selectionsForDimension =
                            new DataQueryDimensionSelectionImpl(
                                dimension.Id,
                                new HashSet<string>(queriesForDimension));
                        selections.Add(selectionsForDimension);
                    }
                    i++;
                }
            }

            if (ObjectUtil.ValidCollection(selections) || dataQuery.StartPeriod != null || dataQuery.EndPeriod != null)
            {
                _dataQuerySelectionGroups.Add(
                    new DataQuerySelectionGroupImpl(selections, dataQuery.StartPeriod, dataQuery.EndPeriod));
            }
            ValidateQuery();
        }
            /// <summary>
            /// Process the <paramref name="dataWhereType"/>.
            /// </summary>
            /// <param name="dataWhereType">
            /// The data where type.
            /// </param>
            private void ProcessDataWhere(DataWhereType dataWhereType)
            {
                ISet<IDataQuerySelection> selections = new HashSet<IDataQuerySelection>();
                ISdmxDate dateFrom = null;
                ISdmxDate dateTo = null;

                this._dataflowId = dataWhereType.Dataflow;
                this._keyFamilyId = dataWhereType.KeyFamily;
                this._version = dataWhereType.Version;

                if (dataWhereType.Time != null)
                {
                    var t = new Time(dataWhereType.Time);
                    dateFrom = t.DateFrom;
                    dateTo = t.DateTo;
                }

                if (dataWhereType.Dimension != null)
                {
                    IDataQuerySelection newSelection = new DataQueryDimensionSelectionImpl(
                        dataWhereType.Dimension.id, dataWhereType.Dimension.TypedValue);

                    selections.Add(newSelection);
                }

                if (dataWhereType.Attribute != null)
                {
                    IDataQuerySelection newSelection0 = new DataQueryDimensionSelectionImpl(
                        dataWhereType.Attribute.id, dataWhereType.Attribute.TypedValue);

                    selections.Add(newSelection0);
                }

                this.ProcessOr(dataWhereType.Or, selections);
                this.AddGroupIfSelectionsExist(selections, dateFrom, dateTo);
            }
            /// <summary>
            /// Adds a selection value, either into an existing DataQuerySelection with the given concept, or a new DataQuerySelection if none exist with the given concept.
            /// </summary>
            /// <param name="selections">
            /// The selections
            /// </param>
            /// <param name="conceptId">
            /// The concept id
            /// </param>
            /// <param name="valueren">
            /// The value
            /// </param>
            private static void AddComponentSelection(
                ISet<IDataQuerySelection> selections, string conceptId, string valueren)
            {
                /* foreach */
                foreach (IDataQuerySelection selection in selections)
                {
                    if (selection.ComponentId.Equals(conceptId))
                    {
                        ((DataQueryDimensionSelectionImpl)selection).AddValue(valueren);
                        return;
                    }
                }

                IDataQuerySelection newSelection = new DataQueryDimensionSelectionImpl(conceptId, valueren);
                selections.Add(newSelection);
            }
        /*
        internal List<DataCriteria> InitCriteria(IDataStructureObject kf, Dictionary<string, List<string>> Criteria)
        {
            List<DataCriteria> criterias = new List<DataCriteria>();
            foreach (IComponent comp in kf.Components.Where(c => Criteria.ContainsKey(c.Id)))
            {
                criterias.Add(new DataCriteria() { component = comp.Id, values = Criteria[comp.Id] });
            }

            foreach (IComponent comp in kf.DimensionList.Dimensions.Where(c => !Criteria.ContainsKey(c.Id)))
            {
                criterias.Add(new DataCriteria() { component = comp.Id, values = Criteria[comp.Id] });
            }
            return criterias;
        }
        */
        public IDataQuery CreateQueryBean(IDataflowObject df, IDataStructureObject kf, List<DataCriteria> Criterias)
        {
            ISet<IDataQuerySelection> selections = new HashSet<IDataQuerySelection>();
            string startTime = String.Empty;
            string endTime = String.Empty;

            // Under the DataWhere only one child MUST reside.
            foreach (var queryComponent in Criterias)
            {
                if (queryComponent != null)
                {

                    if (!string.IsNullOrEmpty(queryComponent.component) && queryComponent.component != kf.TimeDimension.Id)
                    {
                        //if (queryComponent.values.Count > 0) baco 25/11/2015
                        if (queryComponent.values.Count > 0 && !string.IsNullOrEmpty(queryComponent.values[0]))
                        {
                            ISet<string> valuern = new HashSet<string>();
                            foreach (string c in queryComponent.values)
                            {
                                if (!string.IsNullOrEmpty(c))
                                    valuern.Add((c));
                            }
                            IDataQuerySelection selection = new DataQueryDimensionSelectionImpl(queryComponent.component, valuern);
                            selections.Add(selection);
                        }
                    }
                    else if (!string.IsNullOrEmpty(queryComponent.component) && queryComponent.component == kf.TimeDimension.Id)
                    {
                        if (queryComponent.values.Count > 0 && !string.IsNullOrEmpty(queryComponent.values[0]))
                        {
                            startTime = queryComponent.values[0];
                            //if (queryComponent.values.Count > 1 && !string.IsNullOrEmpty(queryComponent.values[1]))
                            if (queryComponent.values.Count > 1 && !string.IsNullOrEmpty(queryComponent.values[queryComponent.values.Count-1]))
                                endTime = queryComponent.values[queryComponent.values.Count - 1];
                        }
                    }
                }
            }
            IDataQuerySelectionGroup sel = new DataQuerySelectionGroupImpl(selections, null, null);
            if ((string.IsNullOrEmpty(startTime)) && (!string.IsNullOrEmpty(endTime)))
            {
                sel = new DataQuerySelectionGroupImpl(selections, new SdmxDateCore(endTime), new SdmxDateCore(endTime));
            }
            else if ((!string.IsNullOrEmpty(startTime)) && (string.IsNullOrEmpty(endTime)))
            {
                sel = new DataQuerySelectionGroupImpl(selections, new SdmxDateCore(startTime), new SdmxDateCore(startTime));
            }
            else if ((!string.IsNullOrEmpty(startTime)) && (!string.IsNullOrEmpty(endTime)))
            {
                sel = new DataQuerySelectionGroupImpl(selections, new SdmxDateCore(startTime), new SdmxDateCore(endTime));
            }
            IList<IDataQuerySelectionGroup> selGroup = new List<IDataQuerySelectionGroup>();
            selGroup.Add(sel);
            IDataQuery query;
            if (DataObjConfiguration._TypeEndpoint == EndpointType.REST)
            {
                query = new DataQueryFluentBuilder().Initialize(kf, df).
                      WithDataQuerySelectionGroup(selGroup).Build();
            }
            else
            {
                query = new DataQueryFluentBuilder().Initialize(kf, df).
                       WithOrderAsc(true).
                       WithMaxObservations(MaximumObservations).
                       WithDataQuerySelectionGroup(selGroup).Build();
            }
            return query;
        }