Exemple #1
0
        /// <summary>
        /// Write a DataMessage in XmlWriter calling WriteTimeSeriesData or WriteFlatData or WriteCrossSectional
        /// </summary>
        /// <param name="writer">Stream Destination</param>
        public override void WriteDataMessage(IFlyWriter writer)
        {
            try
            {
                IDataWriterEngine dataWriterEngine = null;
                switch (MessageType)
                {
                case MessageTypeEnum.GenericData_21:
                    dataWriterEngine = new GenericDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));
                    break;

                case MessageTypeEnum.GenericTimeSeries_21:
                    dataWriterEngine = new GenericDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));
                    break;

                case MessageTypeEnum.StructureSpecific_21:
                    dataWriterEngine = new CompactDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));
                    break;

                case MessageTypeEnum.StructureSpecificTimeSeries_21:
                    dataWriterEngine = new CompactDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwoPointOne));
                    break;
                }

                isFlat = false;
                if (!string.IsNullOrEmpty(this.DimensionAtObservation) && this.DimensionAtObservation == "AllDimensions")
                {
                    isFlat = true;
                }


                //Da capire!!!! perche in teoria le crossSectional dovrebbero essere GenericData_21 e StructureSpecific_21 mentre le atre 2 sono TimeSeries
                //Ma le CommonApi non supportano le TimeSeries e quelle che in teoria sono Cross in realtà sono TimeSeries

                //WriteCrossSectional(ref writer);
                //return;

                if (dataWriterEngine == null)
                {
                    throw new SdmxException(this, FlyExceptionObject.FlyExceptionTypeEnum.InternalError, new Exception("No Type Message recognized"));
                }


                // write header
                IHeader header = FlyConfiguration.HeaderSettings.GetHeader();
                dataWriterEngine.WriteHeader(header);

                // start dataset
                DataStructureObjectImpl dsd = _retrievalManager._dsd;
                dataWriterEngine.StartDataset(null, dsd.Immutated, null);

                // Aggiungo dataset Title
                if (FlyConfiguration.DatasetTitle && !string.IsNullOrEmpty(this._retrievalManager.DataFlowTitle))
                {
                    dataWriterEngine.WriteAttributeValue("Title", this._retrievalManager.DataFlowTitle);
                }

                //GROUPS
                if (_retrievalManager.Groups != null && _retrievalManager.Groups.Count > 0)
                {
                    foreach (DataGroupObject group in _retrievalManager.Groups)
                    {
                        dataWriterEngine.StartGroup(group.GroupId);
                        foreach (GroupReferenceObject GroupKey in group.DimensionReferences)
                        {
                            dataWriterEngine.WriteGroupKeyValue(GroupKey.ConceptCode, GroupKey.ConceptValue.ToString());
                        }

                        foreach (GroupReferenceObject GroupAttribute in group.AttributeReferences)
                        {
                            dataWriterEngine.WriteAttributeValue(GroupAttribute.ConceptCode, GroupAttribute.ConceptValue.ToString());
                        }
                    }
                }

                this.LastSeriesKey = null;
                if (isFlat)
                {
                    WriteFlatData(dataWriterEngine);
                }
                else
                {
                    WriteTimeSeriesData(dataWriterEngine);
                }
                dataWriterEngine.Close();
                writer.__SdmxXml.Flush();
            }
            catch (SdmxException) { throw; }
            catch (Exception ex)
            {
                throw new SdmxException(this, FlyExceptionObject.FlyExceptionTypeEnum.ErrorWriteDataMessage, ex);
            }
        }
        /// <summary>
        /// Write a DataMessage in XmlWriter
        /// </summary>
        /// <param name="writer">Stream Destination</param>
        public override void WriteDataMessage(IFlyWriter writer)
        {
            try
            {
                IDataWriterEngine dataWriterEngine = null;
                switch (MessageType)
                {
                case MessageTypeEnum.Compact_20:
                    dataWriterEngine = new CompactDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));
                    break;

                case MessageTypeEnum.Generic_20:
                    dataWriterEngine = new GenericDataWriterEngine(writer.__SdmxXml, SdmxSchema.GetFromEnum(SdmxSchemaEnumType.VersionTwo));
                    break;

                case MessageTypeEnum.CrossSectional_20:
                    break;
                }

                if (dataWriterEngine == null)
                {
                    throw new SdmxException(this, FlyExceptionObject.FlyExceptionTypeEnum.InternalError, new Exception("No Type Message recognized"));
                }


                // write header
                dataWriterEngine.WriteHeader(FlyConfiguration.HeaderSettings.GetHeader());

                // start dataset
                DataStructureObjectImpl dsd = _retrievalManager._dsd;
                dataWriterEngine.StartDataset(null, dsd.Immutated, null);

                // Aggiungo dataset Title
                if (FlyConfiguration.DatasetTitle && !string.IsNullOrEmpty(this._retrievalManager.DataFlowTitle))
                {
                    dataWriterEngine.WriteAttributeValue("Title", this._retrievalManager.DataFlowTitle);
                }



                this.LastSeriesKey = null;
                List <DataMessageObject> dmo = _tableResponse.GetNext();

                if (dmo != null)
                {
                    //Aggiungo gli Attributi di Dataset
                    foreach (DataMessageObject seriesAttribute in dmo.FindAll(att => OnlyDatasetAttribute(att)))
                    {
                        dataWriterEngine.WriteAttributeValue(seriesAttribute.ColImpl.ConceptObjectCode, seriesAttribute.Val.ToString());
                    }


                    //GROUPS
                    if (_retrievalManager.Groups != null && _retrievalManager.Groups.Count > 0)
                    {
                        foreach (DataGroupObject group in _retrievalManager.Groups)
                        {
                            dataWriterEngine.StartGroup(group.GroupId);
                            foreach (GroupReferenceObject GroupKey in group.DimensionReferences)
                            {
                                dataWriterEngine.WriteGroupKeyValue(GroupKey.ConceptCode, GroupKey.ConceptValue.ToString());
                            }

                            foreach (GroupReferenceObject GroupAttribute in group.AttributeReferences)
                            {
                                dataWriterEngine.WriteAttributeValue(GroupAttribute.ConceptCode, GroupAttribute.ConceptValue.ToString());
                            }
                        }
                    }

                    do
                    {
                        if (dmo == null)
                        {
                            break;
                        }

                        //SERIE
                        if (CheckChanged(dmo))
                        {
                            dataWriterEngine.StartSeries();

                            foreach (DataMessageObject seriesKey in dmo.FindAll(dim => OnlySeriesKey(dim)))
                            {
                                dataWriterEngine.WriteSeriesKeyValue(seriesKey.ColImpl.ConceptObjectCode, seriesKey.Val.ToString());
                            }

                            foreach (DataMessageObject seriesAttribute in dmo.FindAll(att => OnlySeriesAttribute(att)))
                            {
                                dataWriterEngine.WriteAttributeValue(seriesAttribute.ColImpl.ConceptObjectCode, seriesAttribute.Val.ToString());
                            }
                        }

                        //OBSERVATION
                        DataMessageObject val  = dmo.Find(dimV => dimV.ColImpl.ConceptType == ConceptTypeEnum.Attribute && ((IAttributeConcept)dimV.ColImpl).IsValueAttribute);
                        DataMessageObject time = dmo.Find(dimT => dimT.ColImpl.ConceptType == ConceptTypeEnum.Dimension && ((IDimensionConcept)dimT.ColImpl).DimensionType == DimensionTypeEnum.Time);
                        dataWriterEngine.WriteObservation(time.Val.ToString(), val.Val.ToString());

                        foreach (DataMessageObject seriesAttribute in dmo.FindAll(att => OnlyObservationAttribute(att)))
                        {
                            if (seriesAttribute.Val.ToString() != String.Empty)
                            {
                                dataWriterEngine.WriteAttributeValue(seriesAttribute.ColImpl.ConceptObjectCode, seriesAttribute.Val.ToString());
                            }
                        }

                        dmo = _tableResponse.GetNext();
                    } while (dmo != null);
                }

                dataWriterEngine.Close();
                writer.__SdmxXml.Flush();
            }
            catch (SdmxException) { throw; }
            catch (Exception ex)
            {
                throw new SdmxException(this, FlyExceptionObject.FlyExceptionTypeEnum.ErrorWriteDataMessage, ex);
            }
        }