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);
        }
        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");
        }
        /// <summary>
        /// Parses the value types of the incoming component value type, and returns the complex component value to construct in the selection.
        /// </summary>
        /// <param name="compValue">
        /// The component value.
        /// </param>
        /// <param name="compType">
        /// The component type : either a dimension, time dimension, attribute or primary measure.
        /// </param>
        /// <returns>
        /// The set with the ComplexCompoentValue objects.
        /// </returns>
        private IList<IComplexComponentValue> GetComplexComponentValues(DataStructureComponentValueQueryType compValue, SdmxStructureEnumType compType)
        {
            IList<IComplexComponentValue> comValues = new List<IComplexComponentValue>();
            IComplexComponentValue comValue = null;

            //Numeric Values
            if (compValue.NumericValue != null)
            {
                foreach (NumericValue numerValue in compValue.NumericValue)
                {
                    OrderedOperator orderedOperator = OrderedOperator.ParseString([email protected]());
                    comValue = new ComplexComponentValueImpl(numerValue.TypedValue.ToString(CultureInfo.InvariantCulture), orderedOperator, compType);
                    comValues.Add(comValue);
                }
            }

            //Time Value
            if (compValue.TimeValue != null)
            {
                foreach (TimeValue timeValue in compValue.TimeValue)
                {
                    OrderedOperator orderedOperator = OrderedOperator.ParseString([email protected]());
                    comValue = new ComplexComponentValueImpl(timeValue.TypedValue.ToString(), orderedOperator, compType);
                    comValues.Add(comValue);
                }
            }

            //Text Value applicable only for attribute and primary measure
            if (compType.Equals(SdmxStructureEnumType.PrimaryMeasure) || compType.Equals(SdmxStructureEnumType.DataAttribute))
            {
                //It's a generic type but the type will always be one
                if (compValue.TextValue != null)
                {
                    foreach (TextValue textValue in compValue.TextValue)
                    {
                        TextSearch textOperator = TextSearch.ParseString([email protected]());
                        comValue = new ComplexComponentValueImpl(textValue.ToString(), textOperator, compType);
                        comValues.Add(comValue);
                    }
                }
            }

            //Value
            if (compValue.Value != null)
            {
                OrderedOperator orderedOperator = OrderedOperator.ParseString([email protected]());
                comValue = new ComplexComponentValueImpl(compValue.Value.ToString(), orderedOperator, compType);
                comValues.Add(comValue);
            }

            return comValues;
        }
        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;

                    }
                }
            }
        }