/// <summary>
        /// To import indicator information from mapped indicator
        /// </summary>
        /// <param name="indicatorInfo"></param>
        /// <param name="NidInSourceDB"></param>
        /// <param name="NidInTrgDB"></param>
        /// <param name="sourceQurey"></param>
        /// <param name="sourceDBConnection"></param>
        /// <returns></returns>
        public int ImportIndicatorFrmMappedIndicator(IndicatorInfo indicatorInfo, int NidInSourceDB, int NidInTrgDB, DIQueries sourceQurey, DIConnection sourceDBConnection)
        {
            int RetVal = -1;
            string metadataInfo = string.Empty;
            string SqlString = string.Empty;
            DataRow Row;
            DataTable TempTable;
            Dictionary<String, String> OldIconNId_NewIconNId = new Dictionary<string, string>();
            MetaDataBuilder MetaDataBuilderObj;
            IndicatorInfo TrgIndicatorInfo;

            try
            {
                // set RetVal to targetNID
                RetVal = NidInTrgDB;

                if (RetVal > 0)
                {
                    TrgIndicatorInfo = this.GetIndicatorInfo(FilterFieldType.NId, RetVal.ToString(), FieldSelection.Light);

                    // dont import if trg indicator is global but source indicator is local
                    if (TrgIndicatorInfo.Global & indicatorInfo.Global == false)
                    {
                        // dont import if trg indicator is global but source indicator is local
                    }
                    else
                    {
                        //update the gid,name and global on the basis of nid
                        this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.Indicator.Update.UpdateByNid(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, indicatorInfo.Name, indicatorInfo.GID, indicatorInfo.Global, indicatorInfo.Info, RetVal));
                    }

                }

                //update/insert icon
                DIIcons.ImportElement(NidInSourceDB, RetVal, IconElementType.Indicator, sourceQurey, sourceDBConnection, this.DBQueries, this.DBConnection);

                OldIconNId_NewIconNId = DIIcons.ImportElement(NidInSourceDB, RetVal, IconElementType.MetadataIndicator, sourceQurey, sourceDBConnection, this.DBQueries, this.DBConnection);

                // get metadata info.
                metadataInfo = indicatorInfo.Info;

                // Update IconNids in xml if exists
                foreach (string OldIconName in OldIconNId_NewIconNId.Keys)
                {
                    metadataInfo = metadataInfo.Replace(OldIconName, OldIconNId_NewIconNId[OldIconName].ToString());
                }

                metadataInfo = DICommon.CheckNConvertMetadataXml(metadataInfo);
                // Update Metadata
                this.DBConnection.ExecuteNonQuery(DALQueries.Indicator.Update.UpdateIndicatorInfo(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, DICommon.RemoveQuotes(metadataInfo), FilterFieldType.GId, indicatorInfo.GID));

                // -- insert records in xslt tables

                SqlString = sourceQurey.Xslt.GetXSLT(NidInSourceDB.ToString(), MetadataElementType.Indicator);
                TempTable = sourceDBConnection.ExecuteDataTable(SqlString);

                if (TempTable.Rows.Count > 0)
                {
                    Row = TempTable.Rows[0];
                    MetaDataBuilderObj = new MetaDataBuilder(this.DBConnection, this.DBQueries);
                    MetaDataBuilderObj.ImportTransformInfo(Row[XSLT.XSLTText].ToString(), Row[XSLT.XSLTFile].ToString(), RetVal.ToString(), MetadataElementType.Indicator);
                }

            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }

            return RetVal;
        }
        /// <summary>
        /// Import Area map,layer,etc from given source database or template into current databse/template
        /// </summary>
        /// <param name="selectedNIDs"></param>
        /// <param name="selectionCount">Send -1 to import all availble maps</param>
        /// <param name="sourceDBConnection"></param>
        /// <param name="sourceDBQueries"></param>
        /// <param name="requiredAreaLevel">Send -1 to import all  levels area maps</param>
        public void ImportAreaMaps(string selectedNIDs, int selectionCount, DIConnection sourceDBConnection, DIQueries sourceDBQueries, int requiredAreaLevel)
        {
            int LayerNId = -1;
            int TrgtLayerNId = -1;
            int AreaNId;
            int MetaDataNId;
            int FeatureNId = -1;
            int CurrentRecordIndex = 0;

            bool IsSameLayer = true;
            bool IsItFeatureLayer = false;

            string LayerName = string.Empty;
            string Metadata = string.Empty;
            string FeatureType = string.Empty;
            string SqlString = string.Empty;

            //DataTable AreaTable;
            DataTable LayerTable = new DataTable();
            DataTable TempTable;
            //DataTable TempFeatureTable;
            //DataTable TempXsltTable;
            //DbCommand Command;
            //DbDataAdapter adapter;
            //DbCommandBuilder CommandBuilder;
            //DataSet TempDataset;
            DataRow NewLayerRow;
            DataRow layerRow;
            DataRow TempFeatureTableRow;
            MetaDataBuilder MetaDataBuilderObj;

            List<int> NewAddedLayerNidList = null;
            string LangCode = string.Empty;

            try
            {
                // --------- MAPS ---------
                // -- Multiple Areas can be associated with the same Layer. So get the Unique list of AreaGIDs and LayerNIds against the
                // -- selected AreaNIds from the source Database. Loop through them and insert these Maps into Target Database

                try
                {
                    this.RaiseStartProcessEvent();

                    if (selectionCount <= 0)
                    {
                        // -- GET ALL
                        if (requiredAreaLevel <= 0)
                        {
                            // get all levels area
                            TempTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapLayerInfo(FilterFieldType.None, string.Empty, FieldSelection.Heavy));
                        }
                        else
                        {
                            // get required level areas
                            TempTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapLayerInfo(FilterFieldType.Search, " A." + Area.AreaLevel + " <= " + requiredAreaLevel.ToString() + " ", FieldSelection.Heavy));
                        }

                        LayerTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapLayer(DevInfo.Lib.DI_LibDAL.Queries.Area.Select.MapFilterFieldType.None, string.Empty, FieldSelection.Heavy));
                    }

                    else
                    {
                        // -- GET SELECTED
                        TempTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapLayerInfo(FilterFieldType.NId, selectedNIDs, FieldSelection.Heavy));

                        LayerTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapLayer(DevInfo.Lib.DI_LibDAL.Queries.Area.Select.MapFilterFieldType.AreaNId, selectedNIDs, FieldSelection.Heavy));
                    }
                }
                catch (Exception ex)
                {
                    throw new ApplicationException(ex.ToString());
                }

                //-- read records and insert it into oTempDT
                {
                    this.RaiseBeforeProcessEvent(TempTable.Rows.Count);

                    NewAddedLayerNidList = new List<int>();

                    //for (Index = 0; Index <= TempTable.Rows.Count - 1; Index++)
                    foreach (DataRow TempTableRow in TempTable.Rows)
                    {
                        CurrentRecordIndex++;

                        if (LayerNId != Convert.ToInt32(TempTableRow[Area_Map_Layer.LayerNId]))
                        {
                            LayerNId = Convert.ToInt32(TempTableRow[Area_Map_Layer.LayerNId]);
                            IsSameLayer = false;
                        }
                        else
                        {
                            IsSameLayer = true;
                        }

                        // -- STEP 1: Check target AREA ID has any layer associated
                        // -- Assumption: Source AreaId will be there in the Taget Database.

                        //import maps by mapping AREA_ID
                        using (DataTable AreaTable = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetArea(FilterFieldType.ID, "'" + TempTableRow[Area.AreaID].ToString() + "'")))
                        {
                            if (AreaTable.Rows.Count == 1)
                            {
                                AreaNId = Convert.ToInt32(AreaTable.Rows[0][Area.AreaNId]);
                            }
                            else
                            {
                                // -- This case will happen when 2 Areas with the same name are lying under the same Parent at the same level.
                                // -- Because of that, only one Area would be created in the Target database
                                AreaNId = -1;
                            }
                        }

                        if (!(AreaNId == -1))
                        {

                            // -- STEP 2: Remove the Layer Association and Add the Source Layer
                            this.RemoveAreaMapAssociations(AreaNId,NewAddedLayerNidList, true);

                            // -- STEP 3: Add Layer, Feature Layer and MetaData
                            if (!IsSameLayer)
                            {
                                #region -- When layer is not same --

                                //-- Get the Map Layer Information into the Target Database

                                if (LayerTable.Select(Area_Map_Layer.LayerNId + "= " + TempTableRow[Area_Map_Layer.LayerNId]).Length > 0)
                                {
                                    layerRow = LayerTable.Select(Area_Map_Layer.LayerNId + "= " + TempTableRow[Area_Map_Layer.LayerNId])[0];
                                    LayerName = Convert.ToString(TempTableRow[Area_Map_Metadata.LayerName]);

                                    TrgtLayerNId = this.UpdateAreamap(this.DBQueries.TablesName.AreaMapLayer, Convert.ToString(layerRow[Area_Map_Layer.LayerSize]), layerRow[Area_Map_Layer.LayerShp], layerRow[Area_Map_Layer.LayerShx], layerRow[Area_Map_Layer.Layerdbf], Convert.ToString(layerRow[Area_Map_Layer.LayerType]), Convert.ToString(layerRow[Area_Map_Layer.MinX]), Convert.ToString(layerRow[Area_Map_Layer.MinY]), Convert.ToString(layerRow[Area_Map_Layer.MaxX]), Convert.ToString(layerRow[Area_Map_Layer.MaxY]), Convert.ToString(layerRow[Area_Map_Layer.StartDate]), Convert.ToString(layerRow[Area_Map_Layer.EndDate]), Convert.ToString(layerRow[Area_Map_Layer.UpdateTimestamp]));

                                    NewAddedLayerNidList.Add(TrgtLayerNId);

                                    ////if (TrgtLayerNId == -111)
                                    ////{
                                    ////    // when the size of temp database/template file exceeds 1.90GB then compact database and reimport layer
                                    ////    this.CompactDataBase(ref this.DBConnection);
                                    ////    TrgtLayerNId = this.UpdateAreamap(this.DBQueries.TablesName.AreaMapLayer, Convert.ToString(layerRow[Area_Map_Layer.LayerSize]), layerRow[Area_Map_Layer.LayerShp], layerRow[Area_Map_Layer.LayerShx], layerRow[Area_Map_Layer.Layerdbf], Convert.ToString(layerRow[Area_Map_Layer.LayerType]), Convert.ToString(layerRow[Area_Map_Layer.MinX]), Convert.ToString(layerRow[Area_Map_Layer.MinY]), Convert.ToString(layerRow[Area_Map_Layer.MaxX]), Convert.ToString(layerRow[Area_Map_Layer.MaxY]), Convert.ToString(layerRow[Area_Map_Layer.StartDate]), Convert.ToString(layerRow[Area_Map_Layer.EndDate]), Convert.ToString(layerRow[Area_Map_Layer.UpdateTimestamp]));

                                    ////}
                                }
                                else
                                {
                                    TrgtLayerNId = -1;
                                }

                                // -- Feature Layer
                                IsItFeatureLayer = Convert.ToBoolean(TempTableRow[Area_Map.FeatureLayer]);
                                if (IsItFeatureLayer)
                                {
                                    // -- Add the Feature Layer Reference

                                    // -- Get Feature Name from the Source Database
                                    using (DataTable TempFeatureTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaFeatureByNid(TempTableRow[Area_Feature_Type.FeatureTypeNId].ToString())))
                                    {
                                        if (TempFeatureTable.Rows.Count > 0)
                                        {
                                            TempFeatureTableRow = TempFeatureTable.Rows[0];

                                            FeatureType = TempFeatureTableRow[Area_Feature_Type.FeatureType].ToString();
                                            TempFeatureTable.Dispose();

                                            // -- Check if this feature exists in the Target Database
                                            using (DataTable TempFeatureTable1 = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetAreaFeatureByFeatureType("'" + FeatureType + "'")))
                                            {

                                                if (TempFeatureTable1.Rows.Count > 0)
                                                {
                                                    TempFeatureTableRow = TempFeatureTable1.Rows[0];

                                                    // -- If it exists then pick its reference
                                                    FeatureNId = Convert.ToInt32(TempFeatureTableRow[Area_Feature_Type.FeatureTypeNId]);

                                                    //-- Check If the Area already has the relation with this Feature
                                                    using (DataTable TempFeatureTable2 = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetDuplicateAreaFeature(AreaNId, FeatureNId)))
                                                    {
                                                        if (TempFeatureTable2.Rows.Count > 0)
                                                        {
                                                            // -- Same relation already exists
                                                            // -- remove this relation
                                                            this.DBConnection.ExecuteNonQuery(this.DBQueries.Delete.Area.DeleteAreaMap(AreaNId.ToString(), FeatureNId.ToString()));
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    // -- if it does not then create a new feature
                                                    FeatureType = DICommon.RemoveQuotes(FeatureType);
                                                    this.DBConnection.ExecuteScalarSqlQuery(DevInfo.Lib.DI_LibDAL.Queries.Area.Insert.InsertAreaMapFeature(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, FeatureType));
                                                    FeatureNId = Convert.ToInt32(this.DBConnection.ExecuteScalarSqlQuery("SELECT @@IDENTITY"));
                                                }
                                            }
                                            //    TempFeatureTable.Dispose();
                                            //    TempFeatureTable = null;
                                        }
                                    }
                                }

                                // -- Add MetaData Text
                                MetaDataNId = -1;
                                Metadata = "";
                                if (!Information.IsDBNull(TempTableRow[Area_Map_Metadata.MetadataText]))
                                {
                                    if (!(TempTableRow[Area_Map_Metadata.MetadataText].ToString().Length == 0))
                                    {
                                        Metadata = TempTableRow[Area_Map_Metadata.MetadataText].ToString();
                                    }
                                }

                                if (TrgtLayerNId > 0)
                                {
                                    //add language based maps
                                    foreach (DataRow LangRow in this.DBConnection.DILanguages(this.DBQueries.DataPrefix).Rows)
                                    {
                                        LangCode = "_" + (LangRow[Language.LanguageCode].ToString().Trim("-".ToCharArray()));

                                        // use removeQuotes method for sLayerName
                                        this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.Area.Insert.InsertAreaMapMetadata(this.DBQueries.DataPrefix, LangCode, DICommon.RemoveQuotes(Metadata), TrgtLayerNId, DICommon.RemoveQuotes(LayerName)));
                                    }

                                    // -- STEP 4: Create the Area Layer Association
                                    // -- Create a New Relation of the Area_Map with the LayerNId selected
                                    this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.Area.Insert.InsertAreaMap(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, AreaNId, IsItFeatureLayer, FeatureNId, TrgtLayerNId));

                                    #region -- update area xslt info --

                                    //-- get info from source database
                                    SqlString = sourceDBQueries.Xslt.GetXSLT(LayerNId.ToString(), MetadataElementType.Area);

                                    using (DataTable TempXsltTable = sourceDBConnection.ExecuteDataTable(SqlString))
                                    {
                                        // -- update xslt tables in the target database
                                        if (TempXsltTable.Rows.Count > 0)
                                        {

                                            MetaDataBuilderObj = new MetaDataBuilder(this.DBConnection, this.DBQueries);
                                            MetaDataBuilderObj.ImportTransformInfo(TempXsltTable.Rows[0][XSLT.XSLTText].ToString(), TempXsltTable.Rows[0][XSLT.XSLTFile].ToString(), TrgtLayerNId.ToString(), MetadataElementType.Area);
                                        }
                                    }

                                    #endregion
                                }

                                #endregion
                            }

                            else
                            {
                                #region -- if bSameLayer is false --

                                //"If more than 1 area is associated to the same map, then the map association happens only with one of the areas"

                                // -- Feature Layer
                                IsItFeatureLayer = Convert.ToBoolean(TempTableRow[Area_Map.FeatureLayer]);

                                // -- Get Feature Name from the Source Database
                                using (DataTable TempFeatureTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaFeatureByNid(TempTableRow[Area_Feature_Type.FeatureTypeNId].ToString())))
                                {

                                    if (TempFeatureTable.Rows.Count > 0)
                                    {
                                        TempFeatureTableRow = TempFeatureTable.Rows[0];
                                        FeatureType = TempFeatureTableRow[Area_Feature_Type.FeatureType].ToString();
                                        TempFeatureTable.Dispose();

                                        // -- Check if this feature exists in the Target Database
                                        using (DataTable TempFeatureTable1 = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetAreaFeatureByFeatureType("'" + FeatureType + "'")))
                                        {
                                            if (TempFeatureTable1.Rows.Count > 0)
                                            {
                                                TempFeatureTableRow = TempFeatureTable1.Rows[0];

                                                // -- If it exists then pick its reference
                                                FeatureNId = Convert.ToInt32(TempFeatureTableRow[Area_Feature_Type.FeatureTypeNId]);

                                                //-- Check If the Area already has the relation with this Feature
                                                using (DataTable TempFeatureTable2 = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetDuplicateAreaFeature(AreaNId, FeatureNId)))
                                                {
                                                    if (TempFeatureTable2.Rows.Count > 0)
                                                    {
                                                        // -- Same relation already exists
                                                        // -- remove this relation
                                                        this.DBConnection.ExecuteNonQuery(this.DBQueries.Delete.Area.DeleteAreaMap(AreaNId.ToString(), FeatureNId.ToString()));

                                                    }
                                                }
                                            }
                                            else
                                            {
                                                // -- if it does not then create a new feature
                                                FeatureType = DICommon.RemoveQuotes(FeatureType);
                                                this.DBConnection.ExecuteScalarSqlQuery(DevInfo.Lib.DI_LibDAL.Queries.Area.Insert.InsertAreaMapFeature(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, FeatureType));
                                                FeatureNId = Convert.ToInt32(this.DBConnection.ExecuteScalarSqlQuery("SELECT @@IDENTITY"));
                                            }
                                        }
                                        //TempFeatureTable.Dispose();
                                        //TempFeatureTable = null;
                                    }

                                }

                                // -- STEP 4: Create the Area Layer Association
                                // -- Create a New Relation of the Area_Map with the LayerNId selected
                                this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.Area.Insert.InsertAreaMap(this.DBQueries.DataPrefix, this.DBQueries.LanguageCode, AreaNId, IsItFeatureLayer, FeatureNId, TrgtLayerNId));
                                #endregion
                            }
                        }

                        this.RaiseProcessInfoEvent(CurrentRecordIndex);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }
        }