///////////////////////////////////////////////////////////////////////////////////////////////////
        ////////////BUILD FROM MUTABLE OBJECTS             //////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////////////
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CrossSectionalMeasureCore"/> class.
        /// </summary>
        /// <param name="itemMutableObject">
        /// The itemMutableObject. 
        /// </param>
        /// <param name="parent">
        /// The parent. 
        /// </param>
        /// <exception cref="SdmxSemmanticException">
        /// Throws Validate exception.
        /// </exception>
        public CrossSectionalMeasureCore(
            ICrossSectionalMeasureMutableObject itemMutableObject, ICrossSectionalDataStructureObject parent)
            : base(itemMutableObject, parent)
        {
            this.measureDimension = itemMutableObject.MeasureDimension;
            this.code = itemMutableObject.Code;
            try
            {
                this.Validate();
            }
            catch (SdmxSemmanticException ex)
            {
                throw new SdmxSemmanticException(ex, ExceptionCode.ObjectStructureConstructionError, this.Urn);
            }
            catch (Exception th)
            {
                throw new SdmxException(th, ExceptionCode.ObjectStructureConstructionError, this.Urn);
            }
        }
        /// <summary>
        ///     Determines the current data structure.
        /// </summary>
        /// <exception cref="SdmxNoResultsException">
        ///     Could not read dataset, the data set references dataflow '+errorString+' which could not be resolved
        ///     or
        ///     Could not read dataset, the data set references provision '+errorString+' which could not be resolved
        ///     or
        ///     Could not read dataset, the data set references dataflow '+errorString+' which could not be resolved
        ///     or
        ///     Can not read dataset, the data set references the DSD ' + errorString + ' which could not be resolved
        ///     or
        ///     Can not read dataset, the data set does no reference any data structures, and there was no default data structure
        ///     definition provided
        /// </exception>
        /// <exception cref="SdmxNotImplementedException">Can not write dataset for structure of type:  + sRef</exception>
        private void DetermineCurrentDataStructure()
        {
            // 1. Set the current DSD to null before trying to resolve it
            this._currentDsd = null;
            this._currentDataflow = null;

            IDatasetStructureReference datasetStructureReference = null;
            if (this._datasetHeader != null)
            {
                datasetStructureReference = this._datasetHeader.DataStructureReference;
                if (datasetStructureReference == null && this._headerObject != null && this._headerObject.Structures.Count == 1)
                {
                    datasetStructureReference = this._headerObject.Structures[0];
                }
            }

            if (datasetStructureReference != null)
            {
                var structureReference = datasetStructureReference.StructureReference;

                string errorString = structureReference.MaintainableReference.ToString();

                // Provision, Flow, DSD or MSD
                switch (structureReference.TargetReference.EnumType)
                {
                    case SdmxStructureEnumType.Dsd:
                        if (this._defaultDsd != null && structureReference.IsMatch(this._defaultDsd))
                        {
                            this.SetCurrentDsd(this._defaultDsd);
                        }
                        else if (this._objectRetrieval != null)
                        {
                            this.SetCurrentDsd(structureReference.MaintainableReference);
                        }

                        break;
                    case SdmxStructureEnumType.Dataflow:
                        if (this._objectRetrieval != null)
                        {
                            this._currentDataflow = this._objectRetrieval.GetMaintainableObject<IDataflowObject>(structureReference.MaintainableReference);
                            if (this._currentDataflow == null)
                            {
                                throw new SdmxNoResultsException("Could not read dataset, the data set references dataflow '" + errorString + "' which could not be resolved");
                            }

                            this.SetCurrentDsd(this._currentDataflow.DataStructureRef.MaintainableReference);
                        }
                        else
                        {
                            // Use the "header" values 
                            this.SetCurrentDsd(this._defaultDsd);
                            this._currentDataflow = this._dataflow;
                        }

                        break;
                    default:
                        throw new SdmxNotImplementedException("Can not write dataset for structure of type: " + structureReference);
                }

                if (this._currentDsd == null)
                {
                    throw new SdmxNoResultsException("Can not read dataset, the data set references the DSD '" + errorString + "' which could not be resolved");
                }
            }
            else if (this._defaultDsd == null)
            {
                throw new SdmxNoResultsException("Can not read dataset, the data set does no reference any data structures, and there was no default data structure definition provided");
            }
            else
            {
                if (this._dataflow != null)
                {
                    this.SetCurrentDataflow(this._dataflow);
                }
                else
                {
                    this.SetCurrentDsd(this._defaultDsd);
                }
            }
        }
        /// <summary>
        /// Sets the current DSD.
        /// </summary>
        /// <param name="currentDsd">
        /// The current DSD.
        /// </param>
        protected void SetCurrentDsd(ICrossSectionalDataStructureObject currentDsd)
        {
            this._currentDsd = currentDsd;
            foreach (var component in currentDsd.Components)
            {
                this._conceptToComponentId[component.ConceptRef.FullId] = component.Id;
            }

            this._availableDimensions.AddRange(currentDsd.GetDimensions(SdmxStructureEnumType.Dimension).Select(dimension => dimension.Id));
            this._availableMeasureDimensions.AddRange(currentDsd.GetDimensions(SdmxStructureEnumType.MeasureDimension).Select(dimension => dimension.Id));
            this._availableCrossSectionalMeasures.AddRange(currentDsd.CrossSectionalMeasures.Select(measure => measure.Id));
            this._primaryMeasure = currentDsd.PrimaryMeasure.Id;
            if (currentDsd.TimeDimension != null)
            {
                this._timeDimensionId = currentDsd.TimeDimension.Id;
            }

            // If the current dataset header does not reference a DSD then amend it
            var datasetHeader = this._datasetHeader;
            if (datasetHeader != null && datasetHeader.DataStructureReference == null)
            {
                IDatasetStructureReference datasetStructureReference = new DatasetStructureReferenceCore(currentDsd.AsReference);
                this._datasetHeader = this._datasetHeader.ModifyDataStructureReference(datasetStructureReference);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="CrossSectionalDataReaderEngine"/> class.
        /// </summary>
        /// <param name="dataLocation">
        /// The data location.
        /// </param>
        /// <param name="objectRetrieval">
        /// The object retrieval.
        /// </param>
        /// <param name="defaultDsd">
        /// The DSD. giving the ability to retrieve DSDs for the datasets this reader engine is reading.  This
        ///     can be null if there is only one relevant DSD - in which case the default DSD should be provided
        /// </param>
        /// <param name="dataflow">
        /// The dataflow.
        /// </param>
        public CrossSectionalDataReaderEngine(IReadableDataLocation dataLocation, ISdmxObjectRetrievalManager objectRetrieval, ICrossSectionalDataStructureObject defaultDsd, IDataflowObject dataflow)
        {
            this._xmlBuilder = new XmlReaderBuilder();
            if (objectRetrieval == null && defaultDsd == null)
            {
                throw new ArgumentException("AbstractDataReaderEngine expects either a ISdmxObjectRetrievalManager or a IDataStructureObject to be able to interpret the structures");
            }

            this._objectRetrieval = objectRetrieval;
            this._dataLocation = dataLocation;
            this._defaultDsd = defaultDsd;
            this._dataflow = dataflow;

            this.Reset();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="CrossSectionalDataReaderEngine"/> class.
 /// </summary>
 /// <param name="dataLocation">
 /// The data location.
 /// </param>
 /// <param name="dsd">
 /// The DSD. giving the ability to retrieve DSDs for the datasets this reader engine is reading.  This
 ///     can be null if there is only one relevant DSD - in which case the default DSD should be provided
 /// </param>
 /// <param name="dataflow">
 /// The dataflow.
 /// </param>
 public CrossSectionalDataReaderEngine(IReadableDataLocation dataLocation, ICrossSectionalDataStructureObject dsd, IDataflowObject dataflow)
     : this(dataLocation, null, dsd, dataflow)
 {
 }
        /// <summary>
        /// Starts a dataset with the data conforming to the <paramref name="dsd"/>
        /// </summary>
        /// <param name="dataflow">
        /// The <see cref="IDataflowObject"/>
        /// </param>
        /// <param name="dsd">
        /// The <see cref="ICrossSectionalDataStructureObject"/>
        /// </param>
        /// <param name="header">
        /// The Dataset attributes
        /// </param>
        /// <exception cref="System.ArgumentNullException">
        /// if the <paramref name="dsd"/> is null
        /// </exception>
        public void StartDataset(IDataflowObject dataflow, ICrossSectionalDataStructureObject dsd, IDatasetHeader header)
        {
            if (dsd == null)
            {
                throw new ArgumentNullException("dsd");
            }

            base.StartDataset(null, dsd, header);
        }
 /// <summary>
 /// Starts a dataset with the data conforming to the <paramref name="dsd" />
 /// </summary>
 /// <param name="dataflow">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IDataflowObject" /></param>
 /// <param name="dsd">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.ICrossSectionalDataStructureObject" /></param>
 /// <param name="header">The Dataset attributes</param>
 /// <exception cref="T:System.ArgumentNullException">if the <paramref name="dsd" /> is null</exception>
 public void StartDataset(IDataflowObject dataflow, ICrossSectionalDataStructureObject dsd, IDatasetHeader header)
 {
     switch (this._delayBehavior)
     {
         case DataWriterDelayBehavior.UntilFirstStartDataSet:
             this.RunQueue();
             this._dataWriterEngine.StartDataset(dataflow, dsd, header);
             break;
         default:
             this._actions.Enqueue(() => this._dataWriterEngine.StartDataset(dataflow, dsd, header));
             break;
     }
 }
 /// <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");
     }
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="CrossSectionalDataStructureMutableCore"/> class.
        /// </summary>
        /// <param name="objTarget">
        /// The obj target. 
        /// </param>
        public CrossSectionalDataStructureMutableCore(ICrossSectionalDataStructureObject objTarget)
            : base(objTarget)
        {
            this._crossSectionalMeasures = new List<ICrossSectionalMeasureMutableObject>();
            this._crossSectionalAttachDataSet = new List<string>();
            this._crossSectionalAttachGroup = new List<string>();
            this._crossSectionalAttachSection = new List<string>();
            this._crossSectionalAttachObservation = new List<string>();
            this._attributeToMeasureMap = new DictionaryOfLists<string, string>(StringComparer.Ordinal);
            PopulateList(this._crossSectionalAttachDataSet, objTarget.GetCrossSectionalAttachDataSet(false));
            PopulateList(this._crossSectionalAttachGroup, objTarget.GetCrossSectionalAttachGroup(false));
            PopulateList(this._crossSectionalAttachObservation, objTarget.GetCrossSectionalAttachObservation());
            PopulateList(this._crossSectionalAttachSection, objTarget.GetCrossSectionalAttachSection(false));

            foreach (IAttributeObject attribute in objTarget.Attributes)
            {
                IList<string> measureIds = new List<string>();

                foreach (ICrossSectionalMeasure xsMeasure in objTarget.GetAttachmentMeasures(attribute))
                {
                    measureIds.Add(xsMeasure.Id);
                }

                this._attributeToMeasureMap.Add(attribute.Id, measureIds);
            }

            foreach (ICrossSectionalMeasure measure in objTarget.CrossSectionalMeasures)
            {
                this._crossSectionalMeasures.Add(new CrossSectionalMeasureMutableCore(measure));
            }

            foreach (var dimension in objTarget.GetDimensions(SdmxStructureEnumType.MeasureDimension))
            {
                var codelistForMeasureDimension = objTarget.GetCodelistForMeasureDimension(dimension.Id);
                if (codelistForMeasureDimension != null)
                {
                    this._measureCodelistMapping[dimension.Id] = codelistForMeasureDimension.CreateMutableInstance();
                }
                
            }
        }
 /// <summary>
 /// Starts a dataset with the data conforming to the <paramref name="dsd" />
 /// </summary>
 /// <param name="dataflow">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IDataflowObject" /></param>
 /// <param name="dsd">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.ICrossSectionalDataStructureObject" /></param>
 /// <param name="header">The Dataset attributes</param>
 /// <exception cref="T:System.ArgumentNullException">if the <paramref name="dsd" /> is null</exception>
 public void StartDataset(IDataflowObject dataflow, ICrossSectionalDataStructureObject dsd, IDatasetHeader header)
 {
     this._actions.Enqueue(() => this._dataWriterEngine.StartDataset(dataflow, dsd, header));
 }
Example #11
0
 /// <summary>
 /// Starts a dataset with the data conforming to the <paramref name="dsd" />
 /// </summary>
 /// <param name="dataflow">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.IDataflowObject" /></param>
 /// <param name="dsd">The <see cref="T:Org.Sdmxsource.Sdmx.Api.Model.Objects.DataStructure.ICrossSectionalDataStructureObject" /></param>
 /// <param name="header">The Dataset attributes</param>
 /// <exception cref="T:System.ArgumentNullException">if the <paramref name="dsd" /> is null</exception>
 public void StartDataset(IDataflowObject dataflow, ICrossSectionalDataStructureObject dsd, IDatasetHeader header)
 {
     this._actions.Enqueue(() => this._dataWriterEngine.StartDataset(dataflow, dsd, header));
 }