/// <summary>
        /// Retrieve TimeDimension transcoding, if <paramref name="timeDimensionMapping"/> is null
        /// </summary>
        /// <param name="mappingStoreDb">
        /// The mapping store DB.
        /// </param>
        /// <param name="timeDimensionMapping">
        /// The time dimension mapping.
        /// </param>
        private static void RetrieveTimeDimensionTranscoding(
            Database mappingStoreDb, MappingEntity timeDimensionMapping)
        {
            if (timeDimensionMapping == null)
            {
                return;
            }

            RetrieveTimeTranscoding(mappingStoreDb, timeDimensionMapping);
            TimeTranscodingCollection timeTranscodingCollection = timeDimensionMapping.Transcoding.TimeTranscodingCollection;
            if (timeTranscodingCollection.Count == 1
                && TimeFormat.GetTimeFormatFromCodeId(timeTranscodingCollection[0].FrequencyValue).EnumType == TimeFormatEnumType.Year)
            {
                return;
            }

            var codelists = PeriodCodelist.PeriodCodelistIdMap;
            IDictionary<string, TimeTranscodingEntity> transcodingMap = new Dictionary<string, TimeTranscodingEntity>(StringComparer.Ordinal);
            foreach (var periodObject in codelists)
            {
                if (timeTranscodingCollection.Contains(periodObject.Key))
                {
                    var timeTranscodingEntity = timeTranscodingCollection[periodObject.Key];
                    if (timeTranscodingEntity.PeriodColumnId > 0)
                    {
                        var timeDimensionRules = new TranscodingRulesEntity();
                        timeDimensionRules.AddColumn(timeTranscodingEntity.PeriodColumnId, 0);
                        timeDimensionRules.AddComponent(timeDimensionMapping.Components[0].SysId, 0);
                        timeTranscodingEntity.TranscodingRules = timeDimensionRules;
                        transcodingMap.Add(periodObject.Value.Id, timeTranscodingEntity);
                    }

                }
            }

            DbParameter parameter = mappingStoreDb.CreateInParameter(ParameterNameConstants.TranscodingId, DbType.Int64, timeDimensionMapping.Transcoding.SysId);

            using (DbCommand command = mappingStoreDb.GetSqlStringCommandFormat(MappingStoreSqlStatements.TranscodingRulesTimeDimension, parameter))
            using (IDataReader dataReader = mappingStoreDb.ExecuteReader(command))
            {
                int dsdcodeIdx = dataReader.GetOrdinal("DSDCODE");
                int localcodeIdx = dataReader.GetOrdinal("LOCALCODE");
                int codelistId = dataReader.GetOrdinal("CODELIST_ID");

                while (dataReader.Read())
                {
                    var periodCodelist = DataReaderHelper.GetString(dataReader, codelistId);
                    TimeTranscodingEntity timeTranscodingEntity;
                    if (transcodingMap.TryGetValue(periodCodelist, out timeTranscodingEntity))
                    {
                        var timeDimensionRules = timeTranscodingEntity.TranscodingRules;
                        var dsdperiod = new CodeCollection();
                        var localperiod = new CodeCollection();
                        dsdperiod.Add(DataReaderHelper.GetString(dataReader, dsdcodeIdx));
                        localperiod.Add(DataReaderHelper.GetString(dataReader, localcodeIdx));
                        timeDimensionRules.Add(localperiod, dsdperiod);
                    }
                }
            }
        }
        /// <summary>
        /// The method retrieves the <see cref="TranscodingRulesEntity"/> object
        /// by the transcoding system identifier
        /// </summary>
        /// <param name="mappingStoreDb">
        /// The <see cref="Database"/> instance for Mapping Store database
        /// </param>
        /// <param name="sysId">
        /// The mapping set with the specified system identifier
        /// </param>
        /// <param name="timeDimensionMapping">
        /// The TRANSCODING.TR_ID value of the TimeDimension, if there is TimeDimension and is transcoded; otherwise set to null 
        /// </param>
        /// <param name="mappingsMap">
        /// The map between COMPONENT_MAPPING.MAP_ID value and MappingEntity 
        /// </param>
        private static void GetTranscodingRulesByMapSetId(Database mappingStoreDb, long sysId, MappingEntity timeDimensionMapping, IDictionary<long, MappingEntity> mappingsMap)
        {
            string paramId = mappingStoreDb.BuildParameterName(ParameterNameConstants.IdParameter);
            string timeDimensionTranscodingIdParam = mappingStoreDb.BuildParameterName(ParameterNameConstants.TranscodingId);

            object timedimensionID = timeDimensionMapping == null ? (object)null : timeDimensionMapping.Transcoding.SysId;

            // holds the dsd codes by rule
            var componentsByRule = new Dictionary<long, CodeCollection>();

            string sql = string.Format(
               CultureInfo.InvariantCulture,
               MappingStoreSqlStatements.TranscodingRulesDsdCodes,
               paramId,
               timeDimensionTranscodingIdParam);

            using (DbCommand command = mappingStoreDb.GetSqlStringCommand(sql))
            {
                mappingStoreDb.AddInParameter(command, ParameterNameConstants.IdParameter, DbType.Int64, sysId);
                mappingStoreDb.AddInParameter(command, ParameterNameConstants.TranscodingId, DbType.Int64, timedimensionID);

                using (IDataReader dataReader = mappingStoreDb.ExecuteReader(command))
                {
                    int pos = 0;

                    long prevMapId = -1;

                    TranscodingRulesEntity currentRules = null;
                    long firstTrRuleID = -1;
                    long curTrRuleID = -1;
                    long prevTrRuleID = -1;
                    
                    var componentList = new CodeCollection();

                    int ruleIDIdx = dataReader.GetOrdinal("TR_RULE_ID");
                    int componentIdx = dataReader.GetOrdinal("COMPONENT");
                    int codeIdx = dataReader.GetOrdinal("CODE");
                    int mapIdIdx = dataReader.GetOrdinal("MAP_ID");
                    while (dataReader.Read())
                    {
                        long curMapId = DataReaderHelper.GetInt64(dataReader, mapIdIdx);
                        if (prevMapId != curMapId || currentRules == null)
                        {
                            prevMapId = curMapId;
                            if (curTrRuleID > -1)
                            {
                                componentsByRule.Add(curTrRuleID, componentList);
                            }

                            componentList = new CodeCollection();
                            firstTrRuleID = -1;
                            prevTrRuleID = -1;
                            pos = 0;
                            MappingEntity curretMapping = mappingsMap[curMapId];
                            currentRules = new TranscodingRulesEntity();
                            curretMapping.Transcoding.TranscodingRules = currentRules;
                        }

                        curTrRuleID = DataReaderHelper.GetInt64(dataReader, ruleIDIdx);
                        if (firstTrRuleID == -1 || firstTrRuleID == curTrRuleID)
                        {
                            firstTrRuleID = curTrRuleID;
                            prevTrRuleID = curTrRuleID;
                            currentRules.AddComponent(DataReaderHelper.GetInt64(dataReader, componentIdx), pos);

                            pos++;
                        }
                        else if (prevTrRuleID != curTrRuleID)
                        {
                            componentsByRule.Add(prevTrRuleID, componentList);
                            componentList = new CodeCollection();
                            prevTrRuleID = curTrRuleID;
                        }

                        componentList.Add(DataReaderHelper.GetString(dataReader, codeIdx));
                    }

                    if (curTrRuleID > -1)
                    {
                        componentsByRule.Add(curTrRuleID, componentList);
                    }
                }
            }

            if (componentsByRule.Count > 0)
            {
                sql = string.Format(
              CultureInfo.InvariantCulture,
              MappingStoreSqlStatements.TranscodingRulesLocalCodes,
              paramId,
              timeDimensionTranscodingIdParam);
                using (DbCommand command = mappingStoreDb.GetSqlStringCommand(sql))
                {
                    mappingStoreDb.AddInParameter(command, ParameterNameConstants.IdParameter, DbType.Int64, sysId);
                    mappingStoreDb.AddInParameter(command, ParameterNameConstants.TranscodingId, DbType.Int64, timedimensionID);

                    using (IDataReader dataReader = mappingStoreDb.ExecuteReader(command))
                    {
                        long prevMapId = -1;

                        int pos = 0;
                        TranscodingRulesEntity currentRules = null;
                        long firstTrRuleID = -1;
                        long curTrRuleID = -1;
                        long prevTrRuleID = -1;
                        var columnList = new CodeCollection();

                        int ruleIDIdx = dataReader.GetOrdinal("TR_RULE_ID");
                        int columnIdx = dataReader.GetOrdinal("LOCAL_COLUMN");
                        int codeIdx = dataReader.GetOrdinal("CODE");
                        int mapIdIdx = dataReader.GetOrdinal("MAP_ID");
                        while (dataReader.Read())
                        {
                            long curMapId = DataReaderHelper.GetInt64(dataReader, mapIdIdx);
                            if (prevMapId != curMapId || currentRules == null)
                            {
                                prevMapId = curMapId;
                                if (curTrRuleID > -1 && currentRules != null)
                                {
                                    currentRules.Add(columnList, componentsByRule[curTrRuleID]);
                                }

                                firstTrRuleID = -1;
                                prevTrRuleID = -1;
                                pos = 0;
                                columnList = new CodeCollection();
                                MappingEntity curretMapping = mappingsMap[curMapId];
                                currentRules = curretMapping.Transcoding.TranscodingRules;
                            }

                            curTrRuleID = DataReaderHelper.GetInt64(dataReader, ruleIDIdx);
                            if (firstTrRuleID == -1 || firstTrRuleID == curTrRuleID)
                            {
                                firstTrRuleID = curTrRuleID;
                                prevTrRuleID = curTrRuleID;
                                currentRules.AddColumn(DataReaderHelper.GetInt64(dataReader, columnIdx), pos);
                                pos++;
                            }
                            else if (prevTrRuleID != curTrRuleID)
                            {
                                currentRules.Add(columnList, componentsByRule[prevTrRuleID]);
                                columnList = new CodeCollection();
                                prevTrRuleID = curTrRuleID;
                            }

                            columnList.Add(DataReaderHelper.GetString(dataReader, codeIdx));
                        }

                        if (curTrRuleID > -1 && currentRules != null)
                        {
                            currentRules.Add(columnList, componentsByRule[curTrRuleID]);
                        }
                    }
                }
            }
        }