Ejemplo n.º 1
0
        /// <summary>
        /// Creates the appropriate Time Dimension Mapping object <see cref="ITimeDimension"/>  based on the mapping from the mapping store
        /// e.g. it will create a different object if TimeDimension is mapped to one column, different if it is mapped two column and different if the column is Date type
        /// </summary>
        /// <param name="mapping">
        /// The mapping entity<see cref="MappingEntity"/> of the Time Dimension component
        /// </param>
        /// <param name="frequencyComponentMapping">
        /// The frequency component mapping.
        /// </param>
        /// <param name="databaseType">
        /// The dissemination database vendor. Is needed to generate the correct SQL query where conditions
        /// </param>
        /// <returns>
        /// An Time Dimension Transcoding object<see cref="ITimeDimension"/>
        /// </returns>
        public static ITimeDimension Create(MappingEntity mapping, IComponentMapping frequencyComponentMapping, string databaseType)
        {
            ITimeDimension timeDimensionTranscoding = null;

            if (mapping.Transcoding == null)
            {
                timeDimensionTranscoding = new TimeDimensionSingleFrequency(CreateTimeDimensionMapping(mapping, databaseType));
            }
            else if (mapping.Transcoding.TimeTranscodingCollection.Count == 1)
            {
                timeDimensionTranscoding = new TimeDimensionSingleFrequency(CreateTranscodedTimeDimensionMapping(mapping, databaseType, mapping.Transcoding.TimeTranscodingCollection.First()));
            }
            else if (mapping.Transcoding.TimeTranscodingCollection.Count > 1)
            {
                var timeDimensionMappings = new Dictionary <string, ITimeDimensionMapping>(StringComparer.Ordinal);
                foreach (var transcodingEntity in mapping.Transcoding.TimeTranscodingCollection)
                {
                    var timeDimensionSubTranscoding = CreateTranscodedTimeDimensionMapping(mapping, databaseType, transcodingEntity);
                    timeDimensionMappings[transcodingEntity.FrequencyValue] = timeDimensionSubTranscoding;
                }

                timeDimensionTranscoding = new TimeDimensionMultiFrequency(timeDimensionMappings, frequencyComponentMapping);
            }

            if (timeDimensionTranscoding != null)
            {
                timeDimensionTranscoding.Component = mapping.Components[0];
                timeDimensionTranscoding.Mapping   = mapping;
            }

            return(timeDimensionTranscoding);
        }
        /// <summary>
        /// Builds the component to Mapping dictionary. Before building it the methods make sure that the dictionary is empty
        /// </summary>
        private void BuildMappings()
        {
            // do some safety work
            bool measureDimensionMapped = false;
            IComponentMapping frequencyMapping = null;

            // fill the dictionaries and the fields
            foreach (MappingEntity mapping in this._mappingSet.Mappings)
            {
                if (this.IsTimeDimensionMapping(mapping))
                {
                    // find the mapping that contains the timedimension
                    this._timeMapping = mapping;
                    this._componentMapping.Add(mapping.Components[0], mapping);
                }
                else
                {
                    foreach (ComponentEntity component in mapping.Components)
                    {
                        this._componentMapping.Add(component, mapping);

                        IComponentMapping componentMapping = Sri.MappingStoreRetrieval.Engine.Mapping.ComponentMapping.CreateComponentMapping(component, mapping);

                        //// TODO REMOVE this._componentMappingType.Add(component, componentMapping);
                        this._componentIdMap.Add(component.Id, componentMapping);

                        switch (component.ComponentType)
                        {
                            case SdmxComponentType.Dimension:
                                if (component.MeasureDimension)
                                {
                                    measureDimensionMapped = true;
                                }

                                if (component.FrequencyDimension)
                                {
                                    frequencyMapping = componentMapping;
                                }

                                if (component.Id.Equals(this._dimensionAtObservation))
                                {
                                    this._dimensionAtObservationMapping = componentMapping;
                                }

                                break;
                            case SdmxComponentType.CrossSectionalMeasure:
                                this._xsMeasureMappings.Add(component.CrossSectionalMeasureCode, mapping);
                                break;
                        }
                    }
                }
            }

            this._componentMappingList = new IComponentMapping[this._componentIdMap.Count];
            this._componentIdMap.Values.CopyTo(this._componentMappingList, 0);
            if (this._timeMapping != null)
            {
                    this._timeTranscoder = TimeDimensionMapping.Create(this._timeMapping, frequencyMapping, this._mappingSet.DataSet.Connection.DBType);
            }

            if (!measureDimensionMapped)
            {
                foreach (ComponentEntity component in this._mappingSet.Dataflow.Dsd.Dimensions)
                {
                    if (component.MeasureDimension)
                    {
                        this.MeasureComponent = component;
                        return;
                    }
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Set the time dimension
        /// </summary>
        /// <param name="timeDimension">
        /// The Time Dimension mapping 
        /// </param>
        /// <returns>
        /// The time dimension <see cref="ComponentValue"/> 
        /// </returns>
        protected ComponentValue SetTimeDimensionComponent(ITimeDimension timeDimension)
        {
            if (timeDimension == null)
            {
                return null;
            }

            this.TimeDimensionValue = new ComponentValue(timeDimension.Component);
            return this.TimeDimensionValue;
        }
        /// <summary>
        /// Initialize component mappings for coded components and time dimension used in mappings in the dataflow
        /// </summary>
        /// <param name="dataflowRef">
        /// The dataflow Ref.
        /// </param>
        /// <remarks>
        /// This method should be called only once
        /// </remarks>
        private void Initialize(Sdmx.Model.Registry.DataflowRefBean dataflowRef)
        {
            this._mappingSet = DataAccess.GetMappingSet(
                this._connectionStringSettings, 
                dataflowRef.Id, 
                dataflowRef.Version, 
                dataflowRef.AgencyID, 
                this._allowedDataflows);
            if (this._mappingSet == null)
            {
                return;
            }

            NormallizeDatabaseProvider(this._connectionStringSettings);
            this._mastoreAccess = new StructureAccess(this._connectionStringSettings);
            this._xsMeasureDimensionConstraints.Clear();

            this._componentMapping = new Dictionary<string, ComponentInfo>(this._mappingSet.Mappings.Count);
            this._innerSqlQuery = this._mappingSet.DataSet.Query;
            this._measureComponent = null;
            this._timeTranscoder = null;
            this._timeMapping = null;
            this._timeDimension = null;
            bool measureDimensionMapped = false;

            foreach (MappingEntity mapping in this._mappingSet.Mappings)
            {
                foreach (ComponentEntity component in mapping.Components)
                {
                    if (component.ComponentType == SdmxComponentType.TimeDimension)
                    {
                        this._timeTranscoder = TimeDimensionMapping.Create(
                            mapping, this._mappingSet.DataSet.Connection.DBType);
                        this._timeMapping = mapping;
                        this._timeDimension = component.Concept.Id;
                    }
                    else if (component.CodeList != null)
                    {
                        if (component.MeasureDimension)
                        {
                            measureDimensionMapped = true;
                        }

                        var compInfo = new ComponentInfo();
                        compInfo.Mapping = mapping;
                        compInfo.ComponentMapping = ComponentMapping.CreateComponentMapping(component, mapping);
                        compInfo.CodelistRef.Id = component.CodeList.Id;
                        compInfo.CodelistRef.Version = component.CodeList.Version;
                        compInfo.CodelistRef.AgencyID = component.CodeList.Agency;
                        this._componentMapping.Add(component.Concept.Id, compInfo);
                    }
                }
            }

            if (!measureDimensionMapped)
            {
                foreach (ComponentEntity component in this._mappingSet.Dataflow.Dsd.Dimensions)
                {
                    if (component.MeasureDimension)
                    {
                        this._measureComponent = component.Concept.Id;
                    }
                }

                foreach (ComponentEntity xsMeasure in this._mappingSet.Dataflow.Dsd.CrossSectionalMeasures)
                {
                    this._xsMeasureDimensionConstraints.Add(xsMeasure.Concept.Id, xsMeasure);
                }
            }
        }
        /// <summary>
        /// Execute the query generated from <see cref="GenerateSql"/>  against the DDB and populate the given set of available codes.
        /// </summary>
        /// <param name="timeTranscoding">
        /// The ITimeDimension implementation to use for mapping and transcoding
        /// </param>
        /// <param name="timeCodeList">
        /// The Codelist to populate with the min and max values
        /// </param>
        /// <param name="sqlQuery">
        /// The SQL Query to execute
        /// </param>
        /// <exception cref="DbException">
        /// DDB communication error
        /// </exception>
        private void ExecuteSql(ITimeDimension timeTranscoding, CodeListBean timeCodeList, string sqlQuery)
        {
            var max = new CodeBean();
            var min = new CodeBean();
            max.Id = CustomCodelistConstants.TimePeriodMin;
            min.Id = CustomCodelistConstants.TimePeriodMax;

            using (DbConnection ddbConnection = this.CreateDdbConnection())
            {
                using (DbCommand cmd = ddbConnection.CreateCommand())
                {
                    SetupUpCmd(cmd);
                    cmd.CommandText = sqlQuery;
                    using (IDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string period = timeTranscoding.MapComponent(reader);
                            if (!string.IsNullOrEmpty(period))
                            {
                                if (string.CompareOrdinal(period, min.Id) < 0)
                                {
                                    min.Id = period;
                                }

                                if (string.CompareOrdinal(period, max.Id) > 0)
                                {
                                    max.Id = period;
                                }
                            }
                        }
                    }
                }
            }

            if (min.Id.Equals(max.Id))
            {
                max = null;
            }

            SetupTimeCodelist(min, max, timeCodeList);

            // TODO discuss this
            // if (min.Id.Equals("9999")) {
            // timeCodeList.Items.Clear();
            // }
        }