/// <summary>
        /// Retrieve data from a DDB and write it to the specified <paramref name="writer"/> This is the main public method of the DataRetriever class. It is called with a populated QueryBean (containing essentially an SDMX-ML Query) and a database Connection to a "Mapping Store" database. This method is responsible for: 
        /// <list type="bullet">
        /// <item>
        /// Retrieving the <see cref="MappingSetEntity"/> (the class containing the performed mappings), according to the provided Dataflow ID, from the "Mapping Store". Mapping Sets are defined on a Dataflow basis. Thus, this method checks the input QueryBean for the Dataflow that data are requested and fetches the appropriate
        /// <see cref="MappingSetEntity"/>. If no <see cref="MappingSetEntity"/> exists, an exception (<see cref="DataRetrieverException"/>) is thrown.
        /// </item>
        /// <item>
        /// Calling the method generating the appropriate SQL for the dissemination database.
        /// </item>
        /// <item>
        /// Calling the method that executes the generated SQL and uses the
        ///  <paramref name="writer"/>
        ///  to write the output.
        /// </item>
        /// </list>
        /// <note type="note">
        /// The "Data Retriever" expects exactly one Dataflow clause under the DataWhere clause, exactly one
        ///        DataFlowBean within the DataWhereBean (which in turn resides inside the incoming QueryBean).
        /// </note>
        /// </summary>
        /// <exception cref="DataRetrieverException">
        /// See the
        ///   <see cref="ErrorTypes"/>
        ///   for more details
        /// </exception>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="query"/>
        ///   is null
        ///   -or-
        ///   <paramref name="writer"/>
        ///   is null
        /// </exception>
        /// <param name="query">
        /// The query bean for which data will be requested 
        /// </param>
        /// <param name="writer">
        /// The Cross Sectional writer 
        /// </param>
        /// <example>
        /// An example using this method in C# with <see cref="CrossSectionalWriter"/> 
        ///  <code source="ReUsingExamples\DataRetriever\ReUsingDataRetrieverCrossSectional.cs" lang="cs">
        /// </code>
        /// </example>
        public void GetData(IDataQuery dataQuery, ICrossSectionalWriterEngine dataWriter)
        {
            if (dataQuery == null)
            {
                throw new ArgumentNullException("query");
            }

            if (dataWriter == null)
            {
                throw new ArgumentNullException("writer");
            }

            try
            {
                Logger.Info(Resources.InfoDataRetrieverBBInvoked);
                Logger.Info(Resources.InfoOutput + dataWriter.GetType().Name);

                // validate input and initialize the mappingset entitiy
                MappingSetEntity mappingSet = this.Initialize(dataQuery);

                var info = new DataRetrievalInfoXS(mappingSet, dataQuery, this._connectionStringSettings, dataWriter)
                {
                    DefaultHeader = this._defaultHeader
                };
                ValidateMappingSet(info);
                this.WriteHeader(dataWriter, info);
                ICrossSectionalDataStructureObject crossDsd = dataQuery.DataStructure as ICrossSectionalDataStructureObject;

                //(SRA-345) 
                //DR the info from I*DataQuery. DimensionAtObservation to IDataWriterEngine.StartDataSet  
                IDatasetStructureReference dsr = new DatasetStructureReferenceCore("", dataQuery.DataStructure.AsReference, null, null, dataQuery.DimensionAtObservation);
                IDatasetHeader header = new DatasetHeaderCore(this._defaultHeader.DatasetId, this._defaultHeader.Action, dsr);

                dataWriter.StartDataset(dataQuery.Dataflow, crossDsd, header);

                this.GenerateSql(info, _sqlXsBuilder);

                // execute sql query.
                this.ExecuteSql(info, CrossSectionalQueryEngineManager.Instance.GetQueryEngine(info));

                // close output
                dataWriter.Close();
                Logger.Info(Resources.InfoEndDataRetrieverBBInvoked);
            }
            catch (DataRetrieverException)
            {
                throw;
            }
            catch (SdmxException)
            {
                throw;
            }
            catch (DbException ex)
            {
                Logger.Error(ex.ToString());
                throw new DataRetrieverException(ex,
                    SdmxErrorCode.GetFromEnum(SdmxErrorCodeEnumType.InternalServerError),
                    Resources.DataRetriever_ExecuteSqlQuery_Error_executing_generated_SQL_and_populating_SDMX_model);
                //ErrorTypes.DDB_CONNECTION_ERROR,
                //Resources.DataRetriever_ExecuteSqlQuery_Error_executing_generated_SQL_and_populating_SDMX_model,
                //ex);
            }
            catch (OutOfMemoryException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.Error(ex.ToString());
                throw new DataRetrieverException(ex,
                    SdmxErrorCode.GetFromEnum(SdmxErrorCodeEnumType.InternalServerError),
                    Resources.DataRetriever_ExecuteSqlQuery_Error_during_writing_responce);
                //ErrorTypes.WRITING_OUTPUT_ERROR,
                //Resources.DataRetriever_ExecuteSqlQuery_Error_during_writing_responce,
                //ex);
            }
        }
        /// <summary>
        /// Gets the data.
        /// </summary>
        /// <param name="dataQuery">The data query.</param>
        /// <param name="dataWriter">The data writer.</param>
        /// <exception cref="Org.Sdmxsource.Sdmx.Api.Exception.SdmxSemmanticException">Cross Sectional data requested for non-cross sectional dataflow  + dataQuery.Dataflow.Id</exception>
        public void GetData(IDataQuery dataQuery, ICrossSectionalWriterEngine dataWriter)
        {
            var crossDsd = dataQuery.DataStructure as ICrossSectionalDataStructureObject;
            if (crossDsd == null)
            {
                throw new SdmxSemmanticException("Cross Sectional data requested for non-cross sectional dataflow " + dataQuery.Dataflow.Id);
            }
            dataWriter.StartDataset(null, crossDsd, null);
            dataWriter.WriteAttributeValue("TAB_NUM", "AL");
            dataWriter.WriteAttributeValue("REV_NUM", "1");
            dataWriter.StartXSGroup();
            dataWriter.WriteXSGroupKeyValue("TIME_PERIOD", "2001"); // must use the component id which is fixed for TimeDimension to TIME_PERIOD
            dataWriter.WriteXSGroupKeyValue("FREQ", "A");
            dataWriter.WriteXSGroupKeyValue("COUNTRY", "A");
            dataWriter.WriteAttributeValue("TIME_FORMAT", "P1Y");
            dataWriter.StartSection();
            dataWriter.WriteAttributeValue("UNIT_MULT", "9");
            dataWriter.WriteAttributeValue("DECI", "0");
            dataWriter.WriteAttributeValue("UNIT", "AUD");

            // write observations
            dataWriter.StartXSObservation("ADJT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("DEATHST", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("DEATHUN1", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("DIV", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("EMIGT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("IMMIT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("LBIRTHOUT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("LBIRTHST", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("LEXPNSIT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("MAR", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("NETMT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("PJAN1T", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("PJANT", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
            dataWriter.StartXSObservation("TFRNSI", "1");
            dataWriter.WriteXSObservationKeyValue("SEX", "T");
            dataWriter.WriteAttributeValue("OBS_STATUS", "P");
        }