public override void Import(string selectedNids)
        {
            DataTable                    Table           = null;
            int                          ProgressCounter = 0;
            DI6SubgroupBuilder           SGBuilderObj    = null;
            DI6SubgroupInfo              SGInfoObj       = null;
            Dictionary <string, DataRow> FileWithNids    = new Dictionary <string, DataRow>();

            DIConnection       SourceDBConnection = null;
            DIQueries          SourceDBQueries    = null;
            DI6SubgroupBuilder SrcSGBuilder       = null;

            //-- Step 1: Get TempTable with Sorted SourceFileName
            Table = this._TargetDBConnection.ExecuteDataTable(this.ImportQueries.GetImportSubgroupDimensionValues(selectedNids));

            //-- Step 2:Initialise DI6SubgroupBuilder Builder with Target DBConnection
            SGBuilderObj = new DI6SubgroupBuilder(this.TargetDBConnection, this.TargetDBQueries);

            // Initialize progress bar
            this.RaiseProgressBarInitialize(selectedNids.Split(',').GetUpperBound(0) + 1);

            //-- Step 3: Import Nids for each SourceFile
            foreach (DataRow Row in Table.Copy().Rows)
            {
                try
                {
                    string SourceFileWPath = Convert.ToString(Row[MergetTemplateConstants.Columns.COLUMN_SOURCEFILENAME]);

                    SourceDBConnection = new DIConnection(DIServerType.MsAccess, String.Empty, String.Empty, SourceFileWPath, String.Empty, MergetTemplateConstants.DBPassword);
                    SourceDBQueries    = DataExchange.GetDBQueries(SourceDBConnection);


                    // get subgroup info
                    SrcSGBuilder = new DI6SubgroupBuilder(SourceDBConnection, SourceDBQueries);
                    SGInfoObj    = SrcSGBuilder.GetSubgroupInfo(FilterFieldType.NId, Convert.ToString(Row[MergetTemplateConstants.Columns.COLUMN_SRCNID]));


                    SGBuilderObj.ImportSubgroup(SGInfoObj, Convert.ToInt32(Row[MergetTemplateConstants.Columns.COLUMN_SRCNID]), SourceDBQueries, SourceDBConnection);

                    ProgressCounter += 1;
                    this.RaiseProgressBarIncrement(ProgressCounter);
                }
                catch (Exception ex) { ExceptionFacade.ThrowException(ex); }
                finally
                {
                    if (SourceDBConnection != null)
                    {
                        SourceDBConnection.Dispose();
                    }
                    if (SourceDBQueries != null)
                    {
                        SourceDBQueries.Dispose();
                    }
                }
            }
            this._AvailableTable = this.GetAvailableTable();
            this._UnmatchedTable = this.GetUnmatchedTable();
            // Close ProgressBar
            this.RaiseProgressBarClose();
        }
        /// <summary>
        /// Imports records from source database to target database/template
        /// </summary>
        /// <param name="selectedNids"></param>
        /// <param name="allSelected">Set true to import all records</param>
        public override void ImportValues(List <string> selectedNids, bool allSelected)
        {
            DI6SubgroupBuilder SGBuilderObj = new DI6SubgroupBuilder(this._TargetDBConnection, this._TargetDBQueries);
            DI6SubgroupInfo    SourceDBSubgroup;
            DataRow            Row;
            int ProgressBarValue = 0;


            foreach (string Nid in selectedNids)
            {
                try
                {
                    //get subgroup from source table
                    Row = this.SourceTable.Select(Subgroup.SubgroupNId + "=" + Nid)[0];
                    SourceDBSubgroup        = new DI6SubgroupInfo();
                    SourceDBSubgroup.Name   = DICommon.RemoveQuotes(Row[Subgroup.SubgroupName].ToString());
                    SourceDBSubgroup.GID    = Row[Subgroup.SubgroupGId].ToString();
                    SourceDBSubgroup.Global = Convert.ToBoolean(Row[Subgroup.SubgroupGlobal]);
                    SourceDBSubgroup.Nid    = Convert.ToInt32(Row[Subgroup.SubgroupNId]);
                    SourceDBSubgroup.Type   = Convert.ToInt32(Row[Subgroup.SubgroupType]);

                    //import into target database
                    SGBuilderObj.ImportSubgroup(SourceDBSubgroup, SourceDBSubgroup.Nid, this.SourceDBQueries, this.SourceDBConnection);
                }
                catch (Exception ex)
                {
                    ExceptionFacade.ThrowException(ex);
                }
                this.RaiseIncrementProgessBarEvent(ProgressBarValue);
                ProgressBarValue++;
            }
        }
        private void AddSubgroupNIds(DI6SubgroupBuilder DI6SGBuilder, SerializableDictionary <string, DI6SubgroupInfo> subgroups, List <string> SGNIds)
        {
            int SGNId = -1;

            foreach (DI6SubgroupInfo SGInfo in subgroups.Values)
            {
                SGNId = DI6SGBuilder.GetSubgroupNid(SGInfo.GID, string.Empty);
                if (SGNId > 0 && SGNIds.Contains(SGNId.ToString()) == false)
                {
                    SGNIds.Add(SGNId.ToString());
                }
            }
        }
        private int CheckNCreateTotalSubgroupUnderLocation(string TotalSGText)
        {
            int RetVal = 0;

            DI6SubgroupInfo SGInfo = new DI6SubgroupInfo();

            DI6SubgroupBuilder     SGBuilder     = new DI6SubgroupBuilder(this.DBConnection, this.DBQueries);
            DI6SubgroupTypeBuilder SGTypeBuilder = new DI6SubgroupTypeBuilder(this.DBConnection, this.DBQueries);

            SGInfo.Name           = TotalSGText;
            SGInfo.Type           = this.LocationTypeInfo.Nid;
            SGInfo.DISubgroupType = this.LocationTypeInfo;

            RetVal = SGBuilder.CheckNCreateSubgroup(SGInfo);

            return(RetVal);
        }
        /// <summary>
        /// Returns cell mapping values
        /// </summary>
        /// <param name="selectedTableInfo"></param>
        /// <param name="rowIndex"></param>
        /// <param name="colIndex"></param>
        /// <returns></returns>
        public Mapping GetCellMapping(int rowIndex, int colIndex, Database dbDatabase)
        {
            Mapping RetVal = null;

            Mapping            ColumnMapping;
            Mapping            RowMapping;
            int                SGValNID = -1;
            List <string>      SGNIds   = new List <string>();
            DI6SubgroupBuilder DI6SGBuilder;

            try
            {
                // Get mapping information

                if (rowIndex >= 0 && colIndex >= 0)
                {
                    ColumnMapping = this.ColumnsMapping[colIndex].Mappings.CellMap;
                    RowMapping    = this.RowsMapping[rowIndex].Mappings.CellMap;

                    // Step1: copy column mapping
                    RetVal = ColumnMapping.Copy();

                    // Step2: set the blank mapping value with the values available in row's mapping
                    RetVal.ReplaceEmptyValues(RowMapping);

                    // Step3: IF SubgoupVal is empty then get subgroupval on the basis of column's subgroups NID + row's subgroups NID
                    if (string.IsNullOrEmpty(RetVal.SubgroupVal))
                    {
                        DI6SGBuilder = new DI6SubgroupBuilder(dbDatabase.DBConnection, dbDatabase.DBQueries);

                        // Step 3a: get subgroup nids(dimensionvalues nid)
                        this.AddSubgroupNIds(DI6SGBuilder, RetVal.Subgroups, SGNIds);
                        this.AddSubgroupNIds(DI6SGBuilder, RowMapping.Subgroups, SGNIds);

                        if (SGNIds.Count > 0)
                        {
                            // Step 3b: get subgroupval for the selected dimensions
                            DI6SubgroupValBuilder SGValBuilder = new DI6SubgroupValBuilder(dbDatabase.DBConnection, dbDatabase.DBQueries);
                            DI6SubgroupValInfo    SGValInfo;

                            SGValNID = SGValBuilder.GetSubgroupValNIdBySugbroups(SGNIds);

                            if (SGValNID > 0)
                            {
                                SGValInfo             = SGValBuilder.GetSubgroupValInfo(FilterFieldType.NId, SGValNID.ToString());
                                RetVal.SubgroupVal    = SGValInfo.Name;
                                RetVal.SubgroupValGID = SGValInfo.GID;
                            }
                        }
                    }

                    // Step 4: set the blank mapping value with the values available in Default mapping
                    RetVal.ReplaceEmptyValues(this._DefaultMapping);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }

            return(RetVal);
        }
        /// <summary>
        /// Checks and updates the SubgroupVal text for the given SubgroupValNIds
        /// </summary>
        /// <param name="SubgroupValNIds"></param>
        public void CheckNUpdateSubgroupValText(int SubgroupValNId, DI6SubgroupValBuilder SGValBuilder, DIQueries TempQueries)
        {
            string SqlQuery                        = string.Empty;
            string SubgroupNids                    = string.Empty;
            string DI5SubgroupValText              = string.Empty;
            string NewSubgroupValText              = string.Empty;
            string NewSubgroupValTextWTotal        = string.Empty;
            string NewSubgroupValTextWTotalNPrefix = string.Empty;

            string TotalTextValue = string.Empty;

            int  TotalNId             = 0;
            bool IsTotalSGFound       = false;
            bool IsSubgroupValInvalid = true;

            DI6SubgroupValInfo SGValInfo;

            DI6SubgroupBuilder SGBuilder;
            DI6SubgroupInfo    SGInfo = new DI6SubgroupInfo();



            bool   IsLocationSubgroupExistsInSubgroup = false;
            string LocationSubgroupName = string.Empty;
            int    LocationSubgroupNId  = 0;
            int    NewSubgroupNId       = 0;

            try
            {
                // get total text value for the curent langauge
                TotalTextValue = this.GetTotalTextString(TempQueries.LanguageCode);


                SGBuilder = new DI6SubgroupBuilder(this.DBConnection, TempQueries);

                // Step1: get SubgroupVal info
                SGValInfo = SGValBuilder.GetSubgroupValInfo(FilterFieldType.NId, SubgroupValNId.ToString());
                //NewSubgroupValText

                if (SGValInfo != null)
                {
                    DI5SubgroupValText = DICommon.RemoveQuotes(Convert.ToString(SGValInfo.Name));


                    // Get subgroup Nids and check location subgroup exists in the template/databse.
                    foreach (DI6SubgroupInfo DimensionValue in SGValInfo.Dimensions)
                    {
                        if (!string.IsNullOrEmpty(SubgroupNids))
                        {
                            SubgroupNids += ",";
                        }
                        SubgroupNids += DimensionValue.Nid;

                        if (DimensionValue.Nid == 0 || DimensionValue.DISubgroupType == null)
                        {
                            // subgroup missing in DI5 but relationship of that subgroup exists in Subgroup_VAls table
                        }
                        else
                        {
                            if (DimensionValue.DISubgroupType.Order == 1) // 1 order is for location in DI5 DB
                            {
                                IsLocationSubgroupExistsInSubgroup = true;
                                LocationSubgroupName = DimensionValue.Name;
                                LocationSubgroupNId  = DimensionValue.Nid;
                            }
                        }
                    }
                }
                // Step3: Check and update SubgroupVal text
                if (!string.IsNullOrEmpty(SubgroupNids))
                {
                    // Step 3.1: Get New subgroup val text
                    NewSubgroupValText = DICommon.RemoveQuotes(SGValBuilder.CreateSubgroupValTextBySubgroupNids(SubgroupNids));
                }
                // step 3.2: compare New subgroup Val text with DI5SubgroupVal text
                // step 3.2.1: compare Normal text value
                if (NewSubgroupValText.ToLower() != DI5SubgroupValText.ToLower())
                {
                    // step 3.2.2: if text doesnot match then compare the subgroup val with Total
                    NewSubgroupValTextWTotal = TotalTextValue + " " + NewSubgroupValText;

                    if (NewSubgroupValTextWTotal.ToLower() == DI5SubgroupValText.ToLower())
                    {
                        IsTotalSGFound = true;
                    }
                    else
                    {
                        // step 3.2.3: if text still doesnot match then compare the subgroup val with Total and prefix (#)
                        NewSubgroupValTextWTotalNPrefix = Constants.PrefixForNewValue + TotalTextValue + " " + NewSubgroupValText;

                        if (NewSubgroupValTextWTotalNPrefix.ToLower() == DI5SubgroupValText.ToLower())
                        {
                            TotalTextValue = Constants.PrefixForNewValue + TotalTextValue;
                            IsTotalSGFound = true;
                        }
                    }



                    // if New subgroup text matched with DI5 subgroup text
                    if (IsTotalSGFound)
                    {
                        // Check New DI6 subgrop text has Location subgroup
                        //      If location subgroup dimension exists,
                        if (IsLocationSubgroupExistsInSubgroup)
                        {
                            //          Delete the relationship of subgroup with the location subgroup
                            SGValBuilder.DeleteSubgroupValRelations(SubgroupValNId, LocationSubgroupNId.ToString());

                            //          Create new subgroup under location where subgroup value is equal to "Total" + available value of location subgroup dimension
                            SGInfo = new DI6SubgroupInfo();
                            SGInfo.DISubgroupType = this.LocationTypeInfo;
                            SGInfo.Type           = this.LocationTypeInfo.Nid;
                            SGInfo.Name           = TotalTextValue + " " + LocationSubgroupName;

                            NewSubgroupNId = SGBuilder.CheckNCreateSubgroup(SGInfo);

                            //          Create relationship with new subgoup
                            SGValBuilder.InsertSubgroupValRelations(SubgroupValNId, NewSubgroupNId);
                        }
                        else
                        {
                            //      And If not
                            //          Check TOTAL is available under location or not
                            //              If not then create it under location
                            TotalNId = this.CheckNCreateTotalSubgroupUnderLocation(TotalTextValue);
                            //          Create relationship with Total
                            SGValBuilder.InsertSubgroupValRelations(SubgroupValNId, TotalNId);
                        }

                        IsSubgroupValInvalid = false;
                    }
                }
                else
                {
                    IsSubgroupValInvalid = false;
                }

                // Step 4.1: Get New subgroup val text
                NewSubgroupValText = DICommon.RemoveQuotes(SGValBuilder.CreateSubgroupValTextBySubgroupNids(string.Join(",", SGValBuilder.GetAssocaitedSubgroupsNId(SubgroupValNId).ToArray())));

                // Step 4.2: Update Subgroup Val text
                // SGValBuilder.UpdateSubgroupVals(SubgroupValNId, NewSubgroupValText, SGValInfo.Global, SGValInfo.GID);

                if (IsSubgroupValInvalid)
                {
                    // 4.2.1 create new subgorup under others dimension
                    SGInfo = new DI6SubgroupInfo();

                    // if subgorup val is equal to "total" only and no relationship found, then create total subgroup under location otherwise create it under others

                    if (string.IsNullOrEmpty(NewSubgroupValText) && (DI5SubgroupValText.Trim().ToLower() == TotalTextValue.Trim().ToLower() || DI5SubgroupValText.Trim().ToLower() == Constants.PrefixForNewValue.Trim().ToLower() + TotalTextValue.Trim().ToLower()))
                    {
                        SGInfo.DISubgroupType = this.LocationTypeInfo;
                        SGInfo.Type           = this.LocationTypeInfo.Nid;
                    }
                    else
                    {
                        SGInfo.DISubgroupType = this.OthersTypeInfo;
                        SGInfo.Type           = this.OthersTypeInfo.Nid;
                    }

                    SGInfo.Name = DI5SubgroupValText;

                    // check it is already exist or not. If not then only show it under log file
                    NewSubgroupNId = SGBuilder.GetSubgroupNid(string.Empty, DI5SubgroupValText);

                    if (NewSubgroupNId <= 0)
                    {
                        NewSubgroupNId = SGBuilder.CheckNCreateSubgroup(SGInfo);

                        if (SGInfo.Type != this.LocationTypeInfo.Nid)
                        {
                            // add subgorup into mismatch list
                            this.MismatchSubgroups.Add(DI5SubgroupValText);
                        }
                    }


                    // 4.2.2 delete relationship of subgroup val nid from subgroup_val_subgroup table
                    SGValBuilder.DeleteSubgroupValRelations(SubgroupValNId);

                    // 4.2.3 create subgroupval relationship with new subgroup
                    SGValBuilder.InsertSubgroupValRelations(SubgroupValNId, NewSubgroupNId);
                }
                else
                {
                    if (IsTotalSGFound)
                    {
                        if (IsLocationSubgroupExistsInSubgroup)
                        {
                            this.SubgroupsAddedWNewSubgorup.Add(NewSubgroupValText, SGInfo.Name);
                        }
                        else
                        {
                            this.SubgroupsAddedWithTotal.Add(NewSubgroupValText);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }
        }