/// <summary>
        ///  Updating XSLT from Resourse File into database.
        /// </summary>
        /// <param name="dataprefix"></param>
        public void UpdateXSLT(string dataprefix)
        {
            string xsltString = DI7MetadataCategoryBuilder.GetMetadataXslt();
            string SqlQuery   = DevInfo.Lib.DI_LibDAL.Queries.Xslt.Update.UpdateXSLT(dataprefix, DICommon.RemoveQuotes(xsltString));

            this.DBConnection.ExecuteNonQuery(SqlQuery);
        }
        /// <summary>
        /// Import Metdata report
        /// </summary>
        /// <param name="xmlFileNameWPath"></param>
        /// <param name="mdType"></param>
        /// <param name="selectedNidInTrgDatabase"></param>
        /// <param name="selectedFiles"></param>
        /// <param name="XsltFolderPath"></param>
        public void ImportMetadataFromXML(string xmlString, MetadataElementType metadataElementType, int selectedNidInTrgDatabase, string XsltFolderPath)
        {
            XmlDocument XmlDoc = new XmlDocument();
            DI7MetadataCategoryBuilder MDBuilder = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);

            XmlDoc.LoadXml(xmlString);
            try
            {
                //--  check that Value/Name attribute exists or not
                if (this.IsMetadataNameAttributeExists(XmlDoc))
                {
                    this.CheckNInsertMetadataReportXml(XmlDoc, selectedNidInTrgDatabase, true, DIQueries.MetadataElementTypeText[metadataElementType]);
                }
                else
                {
                    // if not then use ID for mapping .
                    // If category ID exists in trg database then only import metadata report
                    this.CheckNInsertMetadataReportXml(XmlDoc, selectedNidInTrgDatabase, false, DIQueries.MetadataElementTypeText[metadataElementType]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private DI7MetadataCategoryInfo GetMedataCategoryInfo(DataTable table)
        {
            DI7MetadataCategoryInfo RetVal = new DI7MetadataCategoryInfo();

            foreach (DataRow Row in table.Rows)
            {
                RetVal = DI7MetadataCategoryBuilder.GetMetadataCategoryInfo(Row, this.DBConnection, this.DBQueries);
                break;
            }

            return(RetVal);
        }
        private void CheckNInsertMetadataReportXml(XmlDocument XmlDoc, int targetNid, bool isNameAttributeExist, string metadataTypeText)
        {
            XmlNamespaceManager        nsManager = new XmlNamespaceManager(XmlDoc.NameTable);
            DI7MetadataCategoryBuilder MDBuilder = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);

            nsManager.AddNamespace("generic", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/data/generic");
            nsManager.AddNamespace("message", "http://www.sdmx.org/resources/sdmxml/schemas/v2_1/message");
            XmlNodeList NodeListObj = null;


            try
            {
                //-- this code not wotk due to "name" attribute
                //SDMXObjectModel.Message.GenericMetadataType Obj = new SDMXObjectModel.Message.GenericMetadataType();
                //Obj = (SDMXObjectModel.Message.GenericMetadataType)SDMXObjectModel.Deserializer.LoadFromXmlDocument(typeof(SDMXObjectModel.Message.GenericMetadataType), XmlDoc);

                NodeListObj = XmlDoc.SelectNodes(METADATASET_XPATH, nsManager);
                foreach (XmlNode Node in NodeListObj[0].ChildNodes)
                {
                    if (Node.Name == REPORT_ELEMENT)
                    {
                        foreach (XmlNode ReportNode in Node.ChildNodes)
                        {
                            if (ReportNode.Name == ATTRIBUTESET_ELEMENT)
                            {
                                this.InsertMetadataReportFromXML(targetNid, isNameAttributeExist, MDBuilder, ReportNode, metadataTypeText, -1);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Imports all metadata categories from given source database to current database
        /// </summary>
        /// <param name="srcDBConnection"></param>
        /// <param name="srcDBQueries"></param>
        /// <param name="categoryType"></param>
        public void ImportAllMetadataCategories(DIConnection srcDBConnection, DIQueries srcDBQueries, MetadataElementType categoryType)
        {
            // import by GID. If GID exists then update existing category otherwise insert it into current database            // update process will update in current langauge but insert process will insert in all langauges

            DataTable SrcCategoryTable = null;
            DI7MetadataCategoryInfo SrcCategoryInfo = null;
            DataTable TrgCategoryTable     = null;
            int       TrgCategoryNid       = 0;
            int       TrgCategoryParentNid = -1;

            try
            {
                // get source category table
                SrcCategoryTable = srcDBConnection.ExecuteDataTable(srcDBQueries.Metadata_Category.GetMetadataCategories(FilterFieldType.Type, DIQueries.MetadataElementTypeText[categoryType]));

                // get target(current database) category table
                TrgCategoryTable = this.DBConnection.ExecuteDataTable(this.DBQueries.Metadata_Category.GetMetadataCategories(FilterFieldType.Type, DIQueries.MetadataElementTypeText[categoryType]));

                // import categories & sub categories from source database into current database
                foreach (DataRow SrcRow in SrcCategoryTable.Select(Metadata_Category.ParentCategoryNId + "=-1"))
                {
                    // get category from source database
                    SrcCategoryInfo = DI7MetadataCategoryBuilder.GetMetadataCategoryInfo(SrcRow, srcDBConnection, srcDBQueries);
                    // check src category gid exists in current(target) database of not
                    TrgCategoryNid       = 0;
                    TrgCategoryParentNid = -1;

                    foreach (DataRow TrgRow in TrgCategoryTable.Select(Metadata_Category.CategoryGId + "='" + DIQueries.RemoveQuotesForSqlQuery(SrcCategoryInfo.CategoryGID) + "' OR " + Metadata_Category.CategoryName + "='" + DIQueries.RemoveQuotesForSqlQuery(SrcCategoryInfo.CategoryName) + "'"))
                    {
                        // if exists then get nid and parent nid
                        TrgCategoryNid       = Convert.ToInt32(TrgRow[Metadata_Category.CategoryNId]);
                        TrgCategoryParentNid = Convert.ToInt32(TrgRow[Metadata_Category.ParentCategoryNId]);
                    }
                    // update nid & parent nid in  src category info
                    SrcCategoryInfo.CategoryNId = TrgCategoryNid;
                    SrcCategoryInfo.ParentNid   = TrgCategoryParentNid;

                    if (TrgCategoryNid > 0)
                    {
                        // update category info
                        this.UpdateMetadataCategory(SrcCategoryInfo);
                    }
                    else
                    {
                        // insert category into current database
                        TrgCategoryNid = this.InsertIntoDatabase(SrcCategoryInfo);
                    }

                    #region -- insert/update sub categories into current database --

                    // insert/update only if target category parent nid is equal to -1 (means at level1)
                    if (TrgCategoryParentNid == -1)
                    {
                        foreach (DI7MetadataCategoryInfo SrcSubCategory in SrcCategoryInfo.SubCategories.Values)
                        {
                            SrcSubCategory.CategoryNId = 0;
                            SrcSubCategory.ParentNid   = TrgCategoryNid;

                            // check sub category exists ( where gid=<src gid> and parent nid=<trg nid>
                            foreach (DataRow TrgSubCategoryRow in TrgCategoryTable.Select(Metadata_Category.CategoryGId + "='" + DIQueries.RemoveQuotesForSqlQuery(SrcSubCategory.CategoryGID) + "' AND " + Metadata_Category.ParentCategoryNId + "=" + TrgCategoryNid))
                            {
                                // if exists then get nid
                                SrcSubCategory.CategoryNId = Convert.ToInt32(TrgSubCategoryRow[Metadata_Category.CategoryNId]);
                            }

                            if (SrcSubCategory.CategoryNId > 0)
                            {
                                // update sub category into current database
                                this.UpdateMetadataCategory(SrcSubCategory);
                            }
                            else
                            {
                                // insert sub category into current database
                                this.InsertIntoDatabase(SrcSubCategory);
                            }
                        }
                    }

                    #endregion
                }
            }
            catch (Exception ex)
            {
                ExceptionHandler.ExceptionFacade.ThrowException(ex);
            }
        }
        private void InsertMetadataReportFromXML(int targetNid, bool isNameAttributeExist, DI7MetadataCategoryBuilder MDBuilder, XmlNode ReportNode, string metadataTypeText, int categoryParentNId)
        {
            string MDCategotyID   = string.Empty;
            string MDCategotyName = string.Empty;
            DI7MetadataCategoryInfo MDCateInfo = null;
            int CategotyNid = -1;

            foreach (XmlNode ReportAttNode in ReportNode.ChildNodes)
            {
                if (ReportAttNode.Name == ATTRIBUTESET_ELEMENT)
                {
                    this.InsertMetadataReportFromXML(targetNid, isNameAttributeExist, MDBuilder, ReportAttNode, metadataTypeText, categoryParentNId);
                }

                if (ReportAttNode.Name == "ReportedAttribute")
                {
                    MDCategotyID = Convert.ToString(ReportAttNode.Attributes[ID_ATTRIBUTE].Value);

                    if (isNameAttributeExist)
                    {
                        MDCategotyName = Convert.ToString(ReportAttNode.Attributes[NAME_ATTRIBUTE].Value);
                    }
                    else
                    {
                        MDCategotyName = MDCategotyID;
                    }

                    try
                    {
                        if (!string.IsNullOrEmpty(ReportAttNode.InnerXml) && Convert.ToString(ReportAttNode[COMMONTEXT_ELEMENT].Attributes[XMLLANG_ATTRIBUTE].Value).Trim('_') == this.DBQueries.LanguageCode.Trim('_'))
                        {
                            string MetadataReport = Convert.ToString(ReportAttNode[COMMONTEXT_ELEMENT].InnerText);

                            if (!string.IsNullOrEmpty(MetadataReport))
                            {
                                MDCateInfo              = new DI7MetadataCategoryInfo();
                                MDCateInfo.CategoryGID  = MDCategotyID;
                                MDCateInfo.CategoryName = MDCategotyName;
                                MDCateInfo.CategoryType = metadataTypeText;
                                MDCateInfo.ParentNid    = categoryParentNId;

                                CategotyNid = MDBuilder.CheckNInsertCategory(MDCateInfo);
                                //--
                                this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.MetadataReport.Insert.InsertMetadataReport(this.DBQueries.TablesName.MetadataReport, targetNid.ToString(), CategotyNid.ToString(), MetadataReport));

                                if (ReportAttNode.ChildNodes.Count > 0)
                                {
                                    this.InsertMetadataReportFromXML(targetNid, isNameAttributeExist, MDBuilder, ReportAttNode, metadataTypeText, CategotyNid);
                                }
                            }
                            else
                            {
                                //this.DBConnection.ExecuteNonQuery(DevInfo.Lib.DI_LibDAL.Queries.MetadataReport.Delete.DeleteMetadataReport(this.DBQueries.TablesName.MetadataReport, targetNid.ToString(), CategotyNid.ToString()));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }
        }
        public bool ImportMetataFromExcel(MetadataElementType categoryType, MetaDataType metadataType, int elementNId, string xlsFileNameWPath, string xsltFldrPath)
        {
            bool      RetVal            = true;
            string    MetadataText      = string.Empty;
            string    FirstColumnValue  = string.Empty;
            string    SecondColumnValue = string.Empty;
            string    ThirdColumnValue  = string.Empty;
            string    ElementName       = string.Empty;
            string    ElementGID        = string.Empty;
            DataTable ExcelDataTable    = null;
            DataRow   Row;

            DI7MetaDataBuilder         MDBuilder;
            DI7MetadataCategoryBuilder MDCategoryBuilder;
            DI7MetadataCategoryInfo    MetadataCategory;

            DIExcel ExcelFile = null;

            try
            {
                // -- Get data table from excel file
                ExcelFile      = new DIExcel(xlsFileNameWPath);
                ExcelDataTable = ExcelFile.GetDataTableFromSheet(ExcelFile.GetSheetName(0));

                // -- create database builder objects
                MDCategoryBuilder = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);
                MDBuilder         = new DI7MetaDataBuilder(this.DBConnection, this.DBQueries);

                // -- import metadata reports with category
                for (int RowIndex = 1; RowIndex < ExcelDataTable.Rows.Count; RowIndex++)
                {
                    Row = ExcelDataTable.Rows[RowIndex];
                    FirstColumnValue  = Convert.ToString(Row[0]);
                    SecondColumnValue = Convert.ToString(Row[1]);
                    ThirdColumnValue  = Convert.ToString(Row[2]);

                    // get element name
                    if (string.IsNullOrEmpty(ElementName))
                    {
                        if (string.IsNullOrEmpty(FirstColumnValue))
                        {
                            break;
                        }
                        else
                        {
                            ElementName = FirstColumnValue;
                            continue;
                        }
                    }

                    // get element gid/id
                    if (string.IsNullOrEmpty(ElementGID))
                    {
                        if (string.IsNullOrEmpty(FirstColumnValue))
                        {
                            break;
                        }
                        else
                        {
                            ElementGID = FirstColumnValue;

                            // get element nid by element gid
                            if (metadataType == MetaDataType.Source)
                            {
                                elementNId = this.GetElementNidByGID(ElementName, metadataType);
                            }
                            else
                            {
                                elementNId = this.GetElementNidByGID(ElementGID, metadataType);
                            }

                            // Skip title row by incrementing  row index
                            RowIndex++;

                            continue;
                        }
                    }

                    // continue if row is blank
                    if (string.IsNullOrEmpty(FirstColumnValue) && string.IsNullOrEmpty(SecondColumnValue) && string.IsNullOrEmpty(ThirdColumnValue))
                    {
                        // reset element  value
                        elementNId  = 0;
                        ElementName = string.Empty;
                        ElementGID  = string.Empty;
                        continue;
                    }
                    else if (elementNId > 0)
                    {
                        // import metadata report with metadata category

                        // get metadata category and metedata report
                        MetadataCategory = new DI7MetadataCategoryInfo();
                        MetadataCategory.CategoryName = SecondColumnValue;
                        MetadataCategory.CategoryGID  = FirstColumnValue;
                        MetadataCategory.CategoryType = DIQueries.MetadataElementTypeText[categoryType];

                        // import metadata category
                        MetadataCategory.CategoryNId = MDCategoryBuilder.CheckNInsertCategory(MetadataCategory);

                        // import metadata report
                        if (MetadataCategory.CategoryNId > 0)
                        {
                            MDBuilder.InsertORUpdateMetadataInfo(metadataType, MetadataCategory.CategoryNId.ToString(), elementNId, this.ReplaceNewLineInMetadataReport(ThirdColumnValue));
                        }
                    }
                }
            }
            catch (Exception)
            {
                RetVal = false;
            }
            finally
            {
                if (ExcelFile != null)
                {
                    ExcelFile.Close();
                }
            }

            return(RetVal);
        }
        /// <summary>
        /// Imports area metadata from template/database
        /// </summary>
        /// <param name="dataPrefix"></param>
        /// <param name="sourceDBConnection"></param>
        /// <param name="sourceDBQueries"></param>
        /// <param name="selectedNIDs"></param>
        /// <param name="selectionCount"></param>
        /// <param name="metadataType"></param>
        public void ImportAreaMetadata(string dataPrefix, DIConnection sourceDBConnection, DIQueries sourceDBQueries, string selectedNIDs, int selectionCount, MetaDataType metadataType)
        {
            DataTable TempDataTable;
            int       CurrentRecordIndex = 0;
            DI7MetadataCategoryBuilder AreaMetadataCategoryBuilder;
            DataTable TempTargetTable;

            try
            {
                this.RaiseStartProcessEvent();

                // import area categories
                AreaMetadataCategoryBuilder = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);
                AreaMetadataCategoryBuilder.ImportAllMetadataCategories(sourceDBConnection, sourceDBQueries, MetadataElementType.Area);


                if (selectionCount == -1)
                {
                    // -- GET ALL
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapMetadata(string.Empty));
                }
                else
                {
                    // -- GET SELECTED
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Area.GetAreaMapMetadata(selectedNIDs));
                }



                // -- Initialize Progress Bar
                this.RaiseBeforeProcessEvent(TempDataTable.Rows.Count);
                CurrentRecordIndex = 0;

                //import area metadata
                foreach (DataRow Row in TempDataTable.Rows)
                {
                    CurrentRecordIndex++;
                    //-- Get Target table against LayerName
                    TempTargetTable = this.DBConnection.ExecuteDataTable(this.DBQueries.Area.GetAreaMapMetadataByName(
                                                                             Row[Area_Map_Metadata.LayerName].ToString()));


                    foreach (DataRow TrgRow in TempTargetTable.Rows)
                    {
                        this.ImportMetadata(sourceDBConnection, sourceDBQueries, Convert.ToInt32(Row[Area_Map_Layer.LayerNId]), Convert.ToInt32(TrgRow[Area_Map_Layer.LayerNId]), MetadataElementType.Area, MetaDataType.Map, IconElementType.MetadataArea);
                        break;
                    }

                    // -- Increemnt the Progress Bar Value
                    this.RaiseProcessInfoEvent(CurrentRecordIndex);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }
            finally
            {
                this.RaiseEndProcessEvent();
            }
        }
        /// <summary>
        /// Imports source metadata from template/database
        /// </summary>
        /// <param name="dataPrefix"></param>
        /// <param name="sourceDBConnection"></param>
        /// <param name="sourceDBQueries"></param>
        /// <param name="selectedNIDs"></param>
        /// <param name="selectionCount"></param>
        /// <param name="metadataType"></param>
        public void ImportSourceMetadata(string dataPrefix, DIConnection sourceDBConnection, DIQueries sourceDBQueries, string selectedNIDs, int selectionCount, MetaDataType metadataType)
        {
            DataTable TempDataTable;
            ICType    ClassificationType;
            int       CurrentRecordIndex = 0;
            int       SrcElementNid;
            int       TrgElementNid;
            DI7MetadataCategoryBuilder SourceMetadataCategoryBuilder;

            SourceBuilder ICBuilder;

            try
            {
                this.RaiseStartProcessEvent();


                ICBuilder = new SourceBuilder(this.DBConnection, this.DBQueries);

                // import source categories
                SourceMetadataCategoryBuilder = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);
                SourceMetadataCategoryBuilder.ImportAllMetadataCategories(sourceDBConnection, sourceDBQueries, MetadataElementType.Source);


                ClassificationType = ICType.Source;
                if (selectionCount == -1)
                {
                    // -- GET ALL
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.IndicatorClassification.GetIC(FilterFieldType.None, string.Empty, ClassificationType, FieldSelection.Heavy));
                }
                else
                {
                    // -- GET SELECTED
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.IndicatorClassification.GetIC(FilterFieldType.NId, selectedNIDs, ClassificationType, FieldSelection.Heavy));
                }



                ////// -- Initialize Progress Bar
                this.RaiseBeforeProcessEvent(TempDataTable.Rows.Count);
                CurrentRecordIndex = 0;

                foreach (DataRow Row in TempDataTable.Rows)
                {
                    CurrentRecordIndex++;
                    SrcElementNid = Convert.ToInt32(Row[IndicatorClassifications.ICNId]);
                    TrgElementNid = ICBuilder.CheckSourceExists(Convert.ToString(Row[IndicatorClassifications.ICName]));

                    // import source metadadta
                    if (TrgElementNid > 0)
                    {
                        this.ImportMetadata(sourceDBConnection, sourceDBQueries, SrcElementNid, TrgElementNid, MetadataElementType.Source, MetaDataType.Source, IconElementType.MetadataSource);
                    }

                    ////// -- Increemnt the Progress Bar Value
                    this.RaiseProcessInfoEvent(CurrentRecordIndex);
                }
                // -- Dispose the Data Table object
                TempDataTable.Dispose();
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }
        }
        /// <summary>
        /// Imports indicator metadata from template/database
        /// </summary>
        /// <param name="dataPrefix"></param>
        /// <param name="sourceDBConnection"></param>
        /// <param name="sourceDBQueries"></param>
        /// <param name="selectedNIDs"></param>
        /// <param name="selectionCount"></param>
        /// <param name="metadataType"></param>
        public void ImportIndicatorMetadata(string dataPrefix, DIConnection sourceDBConnection, DIQueries sourceDBQueries, string selectedNIDs, int selectionCount, MetaDataType metadataType)
        {
            DataTable TempDataTable;
            int       CurrentRecordIndex = 0;
            DI7MetadataCategoryBuilder MetadataCategoryBuilderObj;
            IndicatorInfo    SourceIndicatorInfo = new IndicatorInfo();
            IndicatorBuilder IndBuilder          = null;

            try
            {
                this.RaiseStartProcessEvent();
                IndBuilder = new IndicatorBuilder(this.DBConnection, this.DBQueries);

                // 1. import metadta categories
                // import indicator metadata categories
                MetadataCategoryBuilderObj = new DI7MetadataCategoryBuilder(this.DBConnection, this.DBQueries);
                MetadataCategoryBuilderObj.ImportAllMetadataCategories(sourceDBConnection, sourceDBQueries, MetadataElementType.Indicator);


                // 2. Imort metadata reports
                if (selectionCount == -1)
                {
                    // -- GET ALL
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Indicators.GetIndicator(FilterFieldType.None, string.Empty, FieldSelection.Heavy));
                }
                else
                {
                    // -- GET SELECTED
                    TempDataTable = sourceDBConnection.ExecuteDataTable(sourceDBQueries.Indicators.GetIndicator(FilterFieldType.NId, selectedNIDs, FieldSelection.Heavy));
                }


                //////// -- Initialize Progress Bar
                this.RaiseBeforeProcessEvent(TempDataTable.Rows.Count);
                foreach (DataRow Row in TempDataTable.Rows)
                {
                    CurrentRecordIndex++;

                    // get source indicator info
                    SourceIndicatorInfo        = new IndicatorInfo();
                    SourceIndicatorInfo.Nid    = Convert.ToInt32(Row[Indicator.IndicatorNId]);
                    SourceIndicatorInfo.GID    = Convert.ToString(Row[Indicator.IndicatorGId]);
                    SourceIndicatorInfo.Name   = Convert.ToString(Row[Indicator.IndicatorName]);
                    SourceIndicatorInfo.Global = Convert.ToBoolean(Row[Indicator.IndicatorGlobal]);

                    //import metadata
                    IndBuilder.ImportIndicatorMetadata(SourceIndicatorInfo, SourceIndicatorInfo.Nid, sourceDBQueries, sourceDBConnection);

                    // -- Increemnt the Progress Bar Value
                    this.RaiseProcessInfoEvent(CurrentRecordIndex);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.ToString());
            }
            finally
            {
                this.RaiseEndProcessEvent();
            }
        }