Esempio n. 1
0
        public void TestXSOutput(string filePath)
        {
            IList<IDataQuery> dataQueries;
            using (IReadableDataLocation dataLocation = new FileReadableDataLocation(filePath))
            {
                dataQueries = this._dataQueryParseManager.BuildDataQuery(dataLocation, this._retrievalManager);
            }

            Assert.IsNotEmpty(dataQueries);
            var dataQuery = dataQueries.First();

            var outputFileName = string.Format("{0}-out.xml", filePath);
            using (XmlWriter writer = XmlWriter.Create(outputFileName))
            {
                var dataWriter = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));

                this._dataRetrieval.GetData(dataQuery, dataWriter);
                writer.Flush();
            }

            var fileInfo = new FileInfo(outputFileName);
            Assert.IsTrue(fileInfo.Exists);
            var dsd = dataQuery.DataStructure as ICrossSectionalDataStructureObject;
            Assert.NotNull(dsd);
            using (var fileStream = fileInfo.OpenRead())
            using (var reader = XmlReader.Create(fileStream))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            {
                                var localName = reader.LocalName;
                                switch (localName)
                                {
                                    case "DataSet":
                                        AssertDimensionsAreMapped(dsd.GetCrossSectionalAttachDataSet(true, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dimension)), reader);
                                        break;
                                    case "Section":
                                        AssertDimensionsAreMapped(dsd.GetCrossSectionalAttachSection(true, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dimension)), reader);

                                        break;
                                    case "Group":
                                        AssertDimensionsAreMapped(dsd.GetCrossSectionalAttachGroup(true, SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dimension)), reader);

                                        break;
                                    case "OBS_VALUE":
                                        AssertDimensionsAreMapped(dsd.GetCrossSectionalAttachObservation(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.Dimension)), reader);
                                        break;
                                }
                            }

                            break;
                    }
                }
            }
        }
        public void TestCrossSectionalDataWriter(string dsd)
        {
            ISdmxObjects objects;
            var file = new FileInfo(dsd);
            IStructureParsingManager manager = new StructureParsingManager(SdmxSchemaEnumType.Null);
            using (var readable = new FileReadableDataLocation(file))
            {
                IStructureWorkspace structureWorkspace = manager.ParseStructures(readable);
                objects = structureWorkspace.GetStructureObjects(false);
            }

            foreach (var dataStructureObject in objects.DataStructures)
            {
                var crossDsd = dataStructureObject as ICrossSectionalDataStructureObject;
                Assert.IsNotNull(crossDsd);
                using (var writer = XmlWriter.Create("cross-" + file.Name))
                {
                    var crossWriter = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));
                    crossWriter.WriteHeader(new HeaderImpl("TEST", "TEST"));
                    crossWriter.StartDataset(null, crossDsd, null);
                    foreach (var component in crossDsd.GetCrossSectionalAttachDataSet(true))
                    {
                        switch (component.StructureType.EnumType)
                        {
                            case SdmxStructureEnumType.Dimension:
                                crossWriter.WriteDataSetKeyValue(component.ConceptRef.ChildReference.Id, "DS");
                                break;
                            case SdmxStructureEnumType.DataAttribute:
                                crossWriter.WriteAttributeValue(component.ConceptRef.ChildReference.Id, "DS");
                                break;
                        }
                    }

                    crossWriter.StartXSGroup();
                    var timeDimension = crossDsd.GetDimensions(SdmxStructureEnumType.TimeDimension).FirstOrDefault();
                    IDimension freq = crossDsd.GetDimensions().FirstOrDefault(dimension => dimension.FrequencyDimension);
                    IAttributeObject timeFormat = crossDsd.Attributes.FirstOrDefault(o => o.TimeFormat);
                    if (timeDimension != null)
                    {
                        crossWriter.WriteXSGroupKeyValue(timeDimension.ConceptRef.ChildReference.Id, "2000");
                        if (freq != null)
                        {
                            crossWriter.WriteXSGroupKeyValue(freq.ConceptRef.ChildReference.Id, "A");
                        }

                        if (timeFormat != null)
                        {
                            crossWriter.WriteAttributeValue(timeFormat.ConceptRef.ChildReference.Id, "DS");
                        }
                    }

                    foreach (var component in crossDsd.GetCrossSectionalAttachGroup(true))
                    {
                        switch (component.StructureType.EnumType)
                        {
                            case SdmxStructureEnumType.Dimension:
                                {
                                    if (!component.Equals(freq))
                                    {
                                        crossWriter.WriteXSGroupKeyValue(component.ConceptRef.ChildReference.Id, "DS");
                                    }
                                }

                                break;
                            case SdmxStructureEnumType.DataAttribute:
                                if (!component.Equals(timeFormat))
                                {
                                    crossWriter.WriteAttributeValue(component.ConceptRef.ChildReference.Id, "DS");
                                }

                                break;
                        }
                    }

                    crossWriter.StartSection();
                    foreach (var component in crossDsd.GetCrossSectionalAttachSection(true))
                    {
                        switch (component.StructureType.EnumType)
                        {
                            case SdmxStructureEnumType.Dimension:
                                crossWriter.WriteXSGroupKeyValue(component.ConceptRef.ChildReference.Id, "DS");
                                break;
                            case SdmxStructureEnumType.DataAttribute:
                                crossWriter.WriteAttributeValue(component.ConceptRef.ChildReference.Id, "DS");
                                break;
                        }
                    }

                    if (crossDsd.CrossSectionalMeasures.Count > 0)
                    {
                        for (int index = 0; index < crossDsd.CrossSectionalMeasures.Count; index++)
                        {
                            var crossSectionalMeasure = crossDsd.CrossSectionalMeasures[index];
                            crossWriter.StartXSObservation(crossSectionalMeasure.Code, "1.002");
                            foreach (var component in crossDsd.GetCrossSectionalAttachObservation())
                            {
                                switch (component.StructureType.EnumType)
                                {
                                    case SdmxStructureEnumType.Dimension:
                                        crossWriter.WriteXSGroupKeyValue(component.ConceptRef.ChildReference.Id, "XS" + index);
                                        break;
                                    case SdmxStructureEnumType.DataAttribute:
                                        crossWriter.WriteAttributeValue(component.ConceptRef.ChildReference.Id, "XSOBS");
                                        break;
                                }
                            }
                        }
                    }
                    else
                    {
                        crossWriter.StartXSObservation(crossDsd.PrimaryMeasure.ConceptRef.ChildReference.Id, "1.002");
                        foreach (var component in crossDsd.GetCrossSectionalAttachObservation())
                        {
                            switch (component.StructureType.EnumType)
                            {
                                case SdmxStructureEnumType.Dimension:
                                    crossWriter.WriteXSGroupKeyValue(component.ConceptRef.ChildReference.Id, "PM");
                                    break;
                                case SdmxStructureEnumType.DataAttribute:
                                    crossWriter.WriteAttributeValue(component.ConceptRef.ChildReference.Id, "PM");
                                    break;
                            }
                        }
                    }

                    crossWriter.Close();
                }
            }
        }
 /// <summary>
 /// Writes the cross data.
 /// </summary>
 /// <param name="outputFileName">
 /// Name of the output file.
 /// </param>
 /// <param name="crossSectionalDataStructureObject">
 /// The cross sectional data structure object.
 /// </param>
 private static void WriteCrossDataNoTimeMeasure(string outputFileName, ICrossSectionalDataStructureObject crossSectionalDataStructureObject)
 {
     using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings { Indent = true }))
     using (ICrossSectionalWriterEngine crossSectionalWriter = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo)))
     {
         crossSectionalWriter.WriteHeader(new HeaderImpl("TEST_ID", "TEST_SENDER"));
         crossSectionalWriter.StartDataset(null, crossSectionalDataStructureObject, null);
         crossSectionalWriter.StartXSGroup();
         crossSectionalWriter.WriteXSGroupKeyValue("FREQ", "Q");
         crossSectionalWriter.StartSection();
         crossSectionalWriter.WriteSectionKeyValue("ADJUSTMENT", "N");
         crossSectionalWriter.StartXSObservation("A", "1");
         crossSectionalWriter.WriteAttributeValue("DECIMALS", "3");
         crossSectionalWriter.StartXSObservation("B", "2");
         crossSectionalWriter.WriteAttributeValue("DECIMALS", "3");
     }
 }
        public void TestSDMXv20XS(string filePath, string name, int allowedObs)
        {
            var connectionString = ConfigurationManager.ConnectionStrings [name];
            IList<IDataQuery> dataQueries;
            var mappingStoreSdmxObjectRetrievalManager = new MappingStoreSdmxObjectRetrievalManager(connectionString);
            using (IReadableDataLocation dataLocation = new FileReadableDataLocation(filePath))
            {
                dataQueries = this._dataQueryParseManager.BuildDataQuery(dataLocation, mappingStoreSdmxObjectRetrievalManager);
            }

            Assert.IsNotEmpty(dataQueries);
            var dataQuery = dataQueries.First();

            var outputFileName = string.Format("{0}-soap-v20-xs-max-obs-out.xml", filePath);
            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings { Indent = true }))
            {
                ICrossSectionalWriterEngine dataWriter = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));

                ISdmxDataRetrievalWithCrossWriter advancedSdmxDataRetrievalWithWriter = new DataRetrieverCore(this._defaultHeader, connectionString, SdmxSchemaEnumType.VersionTwo, allowedObs);
                advancedSdmxDataRetrievalWithWriter.GetData(dataQuery, dataWriter);
                writer.Flush();
            }
        }
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        public static void Main(string[] args)
        {
            // 1. We need a IDataStructureObject. In this example we read it from a file. Alternative we could build it from a mutable object.
            IDataStructureObject dataStructure;
            using (IReadableDataLocation readable = new FileReadableDataLocation("ESTAT+DEMOGRAPHY+2.1.xml"))
            {
                IStructureWorkspace structureWorkspace = _parsingManager.ParseStructures(readable);

                ISdmxObjects structureBeans = structureWorkspace.GetStructureObjects(false);
                dataStructure = structureBeans.DataStructures.FirstOrDefault();
            }

            if (dataStructure == null)
            {
                throw new InvalidOperationException("Could not build dataStructure object");
            }

            using (XmlWriter writer = XmlWriter.Create("re-using-cross-writer.xml", new XmlWriterSettings { Indent = true }))
            {
                // initialize the data writing engine. It can be for SDMX versions 2.0 only.
                var dataWriterEngine = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));

                // write header
                dataWriterEngine.WriteHeader(new HeaderImpl("ZZ9", "ZZ9"));

                // start dataset
                dataWriterEngine.StartDataset(null, dataStructure, null);

                // write dataset attributes
                dataWriterEngine.WriteAttributeValue("REV_NUM", "1");
                dataWriterEngine.WriteAttributeValue("TAB_NUM", "RQEL11V1");

                // write 1 group entry
                dataWriterEngine.StartXSGroup();
                dataWriterEngine.WriteXSGroupKeyValue("FREQ", "A");
                dataWriterEngine.WriteXSGroupKeyValue("COUNTRY", "LU");
                dataWriterEngine.WriteXSGroupKeyValue("TIME", "1920");
                dataWriterEngine.WriteAttributeValue("TIME_FORMAT", "P1Y");

                // write a series entry
                dataWriterEngine.StartSection();
                dataWriterEngine.WriteAttributeValue("UNIT_MULT", "0");
                dataWriterEngine.WriteAttributeValue("DECI", "1");
                dataWriterEngine.WriteAttributeValue("UNIT", "UNITS");

                // write observations for the abose section
                dataWriterEngine.StartXSObservation("PJAN1T", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("LBIRTHST", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("DEATHST", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("ADJT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("LBIRTHOUT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("DEATHUN1", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("MAR", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("DIV", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("IMMIT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("EMIGT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("NETMT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("TFRNSI", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                dataWriterEngine.StartXSObservation("LEXPNSIT", "2030.1");
                dataWriterEngine.WriteXSObservationKeyValue("SEX", "F");
                dataWriterEngine.WriteAttributeValue("OBS_STATUS", "P");

                // close cross Writer
                dataWriterEngine.Close();
            }
        }
        public void TestGetData()
        {
            var dataQuery = this._dataQueryParseManager.ParseRestQuery("data/ESTAT,DEMOGRAPHY,2.3/ALL", this._retrievalManager);
            var outputFileName = string.Format("test-attachment-measures{0}.xml", this._name);
            using (XmlWriter writer = XmlWriter.Create(outputFileName, new XmlWriterSettings() {Indent = true}))
            using (ICrossSectionalWriterEngine xsWriter = new CrossSectionalWriterEngine(writer, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo)))
            {
                this._dataRetrieval.GetData(dataQuery, xsWriter);
            }

            var crossDsd = (ICrossSectionalDataStructureObject)dataQuery.DataStructure;
            var crossMeasures = crossDsd.CrossSectionalMeasures.ToDictionary(measure => measure.Id);

            var crossObservationAttributes = crossDsd.GetCrossSectionalAttachObservation(SdmxStructureType.GetFromEnum(SdmxStructureEnumType.DataAttribute)).ToDictionary(component => component.Id);
            using (XmlReader reader = XmlReader.Create(outputFileName))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                            case XmlNodeType.Element:
                            {
                                string localName = reader.LocalName;
                                ICrossSectionalMeasure crossMeasure;
                                if (crossMeasures.TryGetValue(localName, out crossMeasure))
                                {
                                    while (reader.MoveToNextAttribute())
                                    {
                                        string attributeName = reader.LocalName;
                                        IComponent attribute;
                                        if (crossObservationAttributes.TryGetValue(attributeName, out attribute))
                                        {
                                            var attributeCrossMeasures = crossDsd.GetAttachmentMeasures((IAttributeObject)attribute);
                                            CollectionAssert.Contains(attributeCrossMeasures, crossMeasure);
                                        }
                                    }
                                }
                            }
                            break;
                    }
                }
            }
        }