/// <summary>
        /// Builds the complex data query implementation.
        /// </summary>
        /// <param name="freqCriteria">The frequency criteria.</param>
        /// <returns>
        /// The <see cref="ComplexDataQueryImpl"/>
        /// </returns>
        private ComplexDataQueryImpl BuildComplexDataQueryImpl(IComplexDataQuerySelection freqCriteria)
        {
            ISet<IComplexDataQuerySelection> sections = new HashSet<IComplexDataQuerySelection>();
            sections.Add(freqCriteria);

            ICollection<IComplexDataQuerySelectionGroup> collection = new[]
                                                                          {
                                                                              new ComplexDataQuerySelectionGroupImpl(
                                                                                  sections,
                                                                                  null,
                                                                                  OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal),
                                                                                  null,
                                                                                  OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal),
                                                                                  null)
                                                                          };

            var complexDataQueryImpl = new ComplexDataQueryImpl(
                null,
                null,
                null,
                this._dataStructureObject,
                this._dataflowObject,
                null,
                null,
                0,
                null,
                false,
                null,
                DimensionAtObservation.GetFromEnum(DimensionAtObservationEnumType.Time).Value,
                false,
                DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full),
                collection);
            return complexDataQueryImpl;
        }
        public void TestBuild()
        {
            IStructureParsingManager manager = new StructureParsingManager();
            IDataQueryParseManager queryParseManager = new DataQueryParseManager(SdmxSchemaEnumType.VersionTwoPointOne);
            IComplexDataQueryBuilderManager dataQueryBuilderManager = new ComplexDataQueryBuilderManager(new ComplexDataQueryFactoryV21());
            
            IDataflowObject dataFlow;
            IDataStructureObject dsd;
            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+SSTSCONS_PROD_M+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dataFlow = structureWorkspace.GetStructureObjects(false).Dataflows.First();
            }

            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+STS+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dsd = structureWorkspace.GetStructureObjects(false).DataStructures.First();
            }

            ISet<IComplexDataQuerySelection> sections = new HashSet<IComplexDataQuerySelection>();
            var freqCriteria = new ComplexDataQuerySelectionImpl("FREQ", new IComplexComponentValue[] { new ComplexComponentValueImpl("M", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("A", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("B", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension) });
            sections.Add(freqCriteria);
            var adjustmentCriteria = new ComplexDataQuerySelectionImpl("ADJUSTMENT", new IComplexComponentValue[] { new ComplexComponentValueImpl("01", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension) });
            sections.Add(adjustmentCriteria);
            var titleCriteria = new ComplexDataQuerySelectionImpl(
                "TITLE", 
                new IComplexComponentValue[] { new ComplexComponentValueImpl("PAOKARA", TextSearch.GetFromEnum(TextSearchEnumType.Contains), SdmxStructureEnumType.DataAttribute),  new ComplexComponentValueImpl("ARIS", TextSearch.GetFromEnum(TextSearchEnumType.DoesNotContain), SdmxStructureEnumType.DataAttribute), });
            sections.Add(titleCriteria);
            OrderedOperator equalOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal);
            var dateFrom = new SdmxDateCore("2000-01");
            var dateFromOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThanOrEqual);
            var primaryMeasureValue = new ComplexComponentValueImpl("200.20", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThan), SdmxStructureEnumType.PrimaryMeasure);
            ICollection<IComplexDataQuerySelectionGroup> collection = new[] { new ComplexDataQuerySelectionGroupImpl(sections, dateFrom, dateFromOperator, null, equalOperator, new HashSet<IComplexComponentValue> { primaryMeasureValue }) };

            var complexDataQueryImpl = new ComplexDataQueryImpl(
                null, 
                null, 
                null, 
                dsd, 
                dataFlow, 
                null, 
                null, 
                0, 
                null, 
                false, 
                null, 
                DimensionAtObservation.GetFromEnum(DimensionAtObservationEnumType.Time).Value, 
                false, 
                DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full), 
                collection);

            var dataQuery = this._builder.Build(complexDataQueryImpl);
            Assert.NotNull(dataQuery);
        }
        /// <summary>
        /// Build complex data queries from the specified <paramref name="dataQueryType"/>
        /// </summary>
        /// <param name="dataQueryType">
        /// The data query type.
        /// </param>
        /// <param name="structureRetrievalManager">
        /// The structure retrieval manager.
        /// </param>
        /// <returns>
        /// The list of complex data query from the specified <paramref name="dataQueryType"/>
        /// </returns>
        public IList<IComplexDataQuery> BuildComplexDataQuery(DataQueryType dataQueryType, ISdmxObjectRetrievalManager structureRetrievalManager)
        {
            if (structureRetrievalManager == null)
            {
                throw new SystemException("ComplexDataQueryBuilder expectes a ISdmxObjectRetrievalManager");
            }

            IList<IComplexDataQuery> returnList = new List<IComplexDataQuery>();
            DataReturnDetailsType returnDetails = dataQueryType.ReturnDetails;
            DataParametersAndType dataWhere = dataQueryType.DataWhere;

            /**process the DataReturnDetailsType*/
            DataQueryDetail queryDetail = GetReturnDetailsDetail(returnDetails);
            int? firstNObs = GetReturnDetailsFirstNobs(returnDetails);
            int? lastNObs = GetReturnDetailsLastNobs(returnDetails);
            int defaultLimit = GetReturnDetailsDefaultLimit(returnDetails);
            ObservationAction obsAction = GetReturnDetailsObsAction(returnDetails);
            IList<object> structureReferenceDetails = GetStructureRefDetails(returnDetails);
            string dimensionAtObservation = (structureReferenceDetails != null) ? (string)structureReferenceDetails[0] : null;
            bool hasExplicitMeasures = (structureReferenceDetails != null && structureReferenceDetails.Count > 1) ? (bool)structureReferenceDetails[1] : false;
            /**process the DataParametersAndType*/
            //Get the Data Providers
            ISet<IDataProvider> dataProviders = GetDataWhereDataProviders(dataWhere, structureRetrievalManager);
            //Get the DatasetId
            string[] datasetArray = GetDataWhereDatasetId(dataWhere);
            string datasetId = (datasetArray != null) ? datasetArray[0] : null;
            TextSearch datasetIdOper = (datasetArray != null) ? TextSearch.ParseString(datasetArray[1]) : null;
            //Get the DataFlow
            IDataflowObject dataFlow = GetDataWhereDataFlow(dataWhere, structureRetrievalManager);
            //Get the Data Structure Definition
            IDataStructureObject dataStructure = GetDataWhereDataStrucuture(dataWhere, structureRetrievalManager, dataFlow);
            //Get the Provision Agreement
            IProvisionAgreementObject provisionAgreement = GetProvisionAgreement(dataWhere, structureRetrievalManager);
            //Get the Updated dates
            IList<ITimeRange> updatedDates = GetDataWhereUpdatedDates(dataWhere);
            //Get the ComplexDataQueryGoups
            ISet<IComplexDataQuerySelectionGroup> complexDataQuerySelectionGroups = BuildComplexDataQueryGroups(dataWhere, structureRetrievalManager, dataProviders);
            //Build the complex Query
            IComplexDataQuery complexQuery = new ComplexDataQueryImpl(datasetId, datasetIdOper, dataProviders, dataStructure, dataFlow, provisionAgreement, updatedDates, firstNObs, lastNObs, defaultLimit, obsAction, dimensionAtObservation, hasExplicitMeasures, queryDetail, complexDataQuerySelectionGroups);
            returnList.Add(complexQuery);
            return returnList;
        }
        public void TestBuildComplexDataQuery()
        {
            IStructureParsingManager manager = new StructureParsingManager();
            IDataQueryParseManager queryParseManager = new DataQueryParseManager(SdmxSchemaEnumType.VersionTwoPointOne);
            IComplexDataQueryBuilderManager dataQueryBuilderManager = new ComplexDataQueryBuilderManager(new ComplexDataQueryFactoryV21());
            
            IDataflowObject dataFlow;
            IDataStructureObject dsd;
            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+SSTSCONS_PROD_M+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dataFlow = structureWorkspace.GetStructureObjects(false).Dataflows.First();
            }

            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+STS+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dsd = structureWorkspace.GetStructureObjects(false).DataStructures.First();
            }

            ISet<IComplexDataQuerySelection> sections = new HashSet<IComplexDataQuerySelection>();
            var freqCriteria = new ComplexDataQuerySelectionImpl("FREQ", new IComplexComponentValue[] { new ComplexComponentValueImpl("M", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("A", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("B", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension) });
            sections.Add(freqCriteria);
            var adjustmentCriteria = new ComplexDataQuerySelectionImpl("ADJUSTMENT", new IComplexComponentValue[] { new ComplexComponentValueImpl("01", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension),  new ComplexComponentValueImpl("S2", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension) });
            sections.Add(adjustmentCriteria);
            var titleCriteria = new ComplexDataQuerySelectionImpl(
                "TITLE", 
                new IComplexComponentValue[] { new ComplexComponentValueImpl("PAOKARA", TextSearch.GetFromEnum(TextSearchEnumType.Contains), SdmxStructureEnumType.DataAttribute),  new ComplexComponentValueImpl("ARIS", TextSearch.GetFromEnum(TextSearchEnumType.DoesNotContain), SdmxStructureEnumType.DataAttribute) });
            sections.Add(titleCriteria);
            OrderedOperator equalOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal);
            var dateFrom = new SdmxDateCore("2000-01");
            var dateFromOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThanOrEqual);
            var primaryMeasureValue = new ComplexComponentValueImpl("200.20", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThan), SdmxStructureEnumType.PrimaryMeasure);
            ICollection<IComplexDataQuerySelectionGroup> collection = new[] { new ComplexDataQuerySelectionGroupImpl(sections, dateFrom, dateFromOperator, null, equalOperator, new HashSet<IComplexComponentValue> { primaryMeasureValue }) };

            var complexDataQueryImpl = new ComplexDataQueryImpl(
                null, 
                null, 
                null, 
                dsd, 
                dataFlow, 
                null, 
                null, 
                0, 
                null, 
                false, 
                null, 
                DimensionAtObservation.GetFromEnum(DimensionAtObservationEnumType.Time).Value, 
                false, 
                DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full), 
                collection);

            var buildComplexDataQuery = dataQueryBuilderManager.BuildComplexDataQuery(
                complexDataQueryImpl, new StructSpecificDataFormatV21());

            var fileName = string.Format("test-TestBuildComplexDataQuery.xml");
            buildComplexDataQuery.Save(fileName);

            ISdmxObjects objects = new SdmxObjectsImpl();
            objects.AddDataStructure(dsd);
            objects.AddDataflow(dataFlow);
            ISdmxObjectRetrievalManager beanRetrievalManager = new InMemoryRetrievalManager(objects);

            IComplexDataQuery query;
            using (var readable = new FileReadableDataLocation(fileName))
            {
                XMLParser.ValidateXml(readable, SdmxSchemaEnumType.VersionTwoPointOne);

                query = queryParseManager.BuildComplexDataQuery(readable, beanRetrievalManager).First();
            }

            Assert.AreEqual(1, query.SelectionGroups.Count);
            var selectionGroup = query.SelectionGroups.First();
            Assert.AreEqual(dateFrom, selectionGroup.DateFrom);
            Assert.AreEqual(dateFromOperator, selectionGroup.DateFromOperator);
            Assert.IsNull(selectionGroup.DateTo);
            Assert.AreEqual(1, selectionGroup.PrimaryMeasureValue.Count);
            var primaryValue = selectionGroup.PrimaryMeasureValue.First();
            Assert.AreEqual(primaryMeasureValue.Value, primaryValue.Value);
            Assert.AreEqual(primaryMeasureValue.OrderedOperator, primaryValue.OrderedOperator);
            Assert.AreEqual(3, selectionGroup.Selections.Count);

            var gotFreqCriteria = selectionGroup.GetSelectionsForConcept(freqCriteria.ComponentId);
            Assert.AreEqual(gotFreqCriteria, freqCriteria, "FREQ diff");
            
            var gotAdjustmentCriteria = selectionGroup.GetSelectionsForConcept(adjustmentCriteria.ComponentId);
            Assert.AreEqual(gotAdjustmentCriteria, adjustmentCriteria, "ADJ diff");
            Assert.IsTrue(gotAdjustmentCriteria.Values.All(value => value.OrderedOperator.EnumType == OrderedOperatorEnumType.NotEqual));

            var gotTitleCriteria = selectionGroup.GetSelectionsForConcept(titleCriteria.ComponentId);
            Assert.AreEqual(gotTitleCriteria, titleCriteria, "TITLE diff");
        }
        public void TestBuildComplexDataQueryRaw()
        {
            IStructureParsingManager manager = new StructureParsingManager();
            IDataQueryParseManager queryParseManager = new DataQueryParseManager(SdmxSchemaEnumType.VersionTwoPointOne);
            IComplexDataQueryBuilderManager dataQueryBuilderManager = new ComplexDataQueryBuilderManager(new ComplexDataQueryFactoryV21());

            IDataflowObject dataFlow;
            IDataStructureObject dsd;
            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+SSTSCONS_PROD_M+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dataFlow = structureWorkspace.GetStructureObjects(false).Dataflows.First();
            }

            using (var readable = new FileReadableDataLocation("tests/V21/Structure/test-sdmxv2.1-ESTAT+STS+2.0.xml"))
            {
                var structureWorkspace = manager.ParseStructures(readable);
                dsd = structureWorkspace.GetStructureObjects(false).DataStructures.First();
            }

            ISet<IComplexDataQuerySelection> sections = new HashSet<IComplexDataQuerySelection>();
            var freqCriteria = new ComplexDataQuerySelectionImpl("FREQ", new IComplexComponentValue [] { new ComplexComponentValueImpl("M", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("A", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("B", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal), SdmxStructureEnumType.Dimension) });
            sections.Add(freqCriteria);
            var adjustmentCriteria = new ComplexDataQuerySelectionImpl("ADJUSTMENT", new IComplexComponentValue [] { new ComplexComponentValueImpl("01", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension), new ComplexComponentValueImpl("S2", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.NotEqual), SdmxStructureEnumType.Dimension) });
            sections.Add(adjustmentCriteria);
            var titleCriteria = new ComplexDataQuerySelectionImpl(
                "TITLE",
                new IComplexComponentValue [] { new ComplexComponentValueImpl("PAOKARA", TextSearch.GetFromEnum(TextSearchEnumType.Contains), SdmxStructureEnumType.DataAttribute), new ComplexComponentValueImpl("ARIS", TextSearch.GetFromEnum(TextSearchEnumType.DoesNotContain), SdmxStructureEnumType.DataAttribute) });
            sections.Add(titleCriteria);
            OrderedOperator equalOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.Equal);
            var dateFrom = new SdmxDateCore("2000-01");
            var dateFromOperator = OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThanOrEqual);
            var primaryMeasureValue = new ComplexComponentValueImpl("200.20", OrderedOperator.GetFromEnum(OrderedOperatorEnumType.GreaterThan), SdmxStructureEnumType.PrimaryMeasure);
            ICollection<IComplexDataQuerySelectionGroup> collection = new [] { new ComplexDataQuerySelectionGroupImpl(sections, dateFrom, dateFromOperator, null, equalOperator, new HashSet<IComplexComponentValue> { primaryMeasureValue }) };

            var complexDataQueryImpl = new ComplexDataQueryImpl(
                null,
                null,
                null,
                dsd,
                dataFlow,
                null,
                null,
                0,
                null,
                false,
                null,
                DimensionAtObservation.GetFromEnum(DimensionAtObservationEnumType.Time).Value,
                false,
                DataQueryDetail.GetFromEnum(DataQueryDetailEnumType.Full),
                collection);

            var buildComplexDataQuery = dataQueryBuilderManager.BuildComplexDataQuery(
                complexDataQueryImpl, new StructSpecificDataFormatV21());

            var fileName = string.Format("test-TestBuildComplexDataQuery.xml");
            buildComplexDataQuery.Save(fileName);

            bool insideOr = false;
            string localName;
            string lastDimension = null;
            string value = null;

            using (var reader = XmlReader.Create(fileName))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            localName = reader.LocalName;
                            switch (localName)
                            {
                                case "Or":
                                    insideOr = true;  
                                    lastDimension = null;
                                    value = null;
                                    break;

                            }
                            break;
                        case XmlNodeType.Text:
                            value = reader.Value;
                            break;
                        case XmlNodeType.EndElement:
                            if ("ID".Equals(reader.LocalName))
                            {
                                if (insideOr)
                                {
                                    Assert.IsNotNullOrEmpty(value);
                                    if (lastDimension == null)
                                    {
                                        lastDimension = value;
                                    }

                                    Assert.AreEqual(lastDimension, value);
                                }
                            }
                            else if ("Or".Equals(reader.LocalName))
                            {
                                insideOr = false;
                            }
                            break;

                    }
                }
            }
        }