public bool CheckValidManifestFile(XmlElement root, PaymentGateWayModuleInfo module)
 {
     if (root.Name == "sageframe")                       //need to change the root node for valid manifest file at root node
     {
         string packageType = root.GetAttribute("type"); //root.NodeType
         //module.PackageType = PackageType;
         switch (packageType.ToLower())
         {
         //need to check for many cases for like skin /..
         case "paymentgateway":
             return(true);
         }
     }
     return(false);
 }
        public string CheckFormanifestFile(string tempUnzippedPath, PaymentGateWayModuleInfo module)
        {
            DirectoryInfo dir = new DirectoryInfo(tempUnzippedPath);

            foreach (FileInfo f in dir.GetFiles("*.*"))
            {
                if (f.Extension.ToLower() == ".sfe")
                {
                    module.ManifestFile = f.Name;
                    return(module.ManifestFile);
                }
                else
                {
                    module.ManifestFile = "";
                }
            }
            return(module.ManifestFile);
        }
        public int Step1CheckLogic(string tempUnzippedPath, PaymentGateWayModuleInfo module)
        {
            if (CheckFormanifestFile(tempUnzippedPath, module) != "")
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(tempUnzippedPath + '\\' + module.ManifestFile);
                XmlElement root = doc.DocumentElement;
                if (CheckValidManifestFile(root, module))
                {
                    XmlNodeList xnList = doc.SelectNodes("sageframe/folders/folder");
                    foreach (XmlNode xn in xnList)
                    {
                        module.PaymentGatewayTypeName = xn["paymentgatewayname"].InnerXml.ToString();
                        module.FolderName             = xn["foldername"].InnerXml.ToString();
                        module.FriendlyName           = xn["friendlyname"].InnerXml.ToString();
                        module.Description            = xn["description"].InnerXml.ToString();
                        module.Version     = xn["version"].InnerXml.ToString();
                        module.Name        = xn["name"].InnerXml.ToString();
                        module.StoreID     = GetStoreID;  //int.Parse(xn["storeid"].InnerXml.ToString());
                        module.PortalID    = GetPortalID; //int.Parse(xn["portalid"].InnerXml.ToString());
                        module.CultureName = xn["culturename"].InnerXml.ToString();

                        if (!String.IsNullOrEmpty(module.PaymentGatewayTypeName) && IsModuleExist(module.PaymentGatewayTypeName.ToLower(), module.FolderName.ToString(), module.FriendlyName.ToString(), int.Parse(GetStoreID.ToString()), int.Parse(GetPortalID.ToString())))
                        {
                            string path       = HttpContext.Current.Server.MapPath("~/");
                            string targetPath = path + SageFrame.Core.RegisterModule.Common.ModuleFolder + '\\' + module.FolderName;
                            module.InstalledFolderPath = targetPath;
                            // DeleteTempDirectory(tempUnzippedPath);
                            return(1);//Already exist
                        }
                        else
                        {
                            return(2);//Not Exists
                        }
                    }
                }
                else
                {
                    return(-1);//Invalid Manifest file
                }
            }
            return(0);//No manifest file
        }
        public void InstallPackage(PaymentGateWayModuleInfo module, int update)
        {
            XmlDocument doc                = new XmlDocument();
            ArrayList   dllFiles           = new ArrayList();
            string      unistallScriptFile = string.Empty;

            doc.Load(module.TempFolderPath + '\\' + module.ManifestFile);
            XmlElement root = doc.DocumentElement;

            if (!String.IsNullOrEmpty(root.ToString()))
            {
                XmlNodeList xnList = doc.SelectNodes("sageframe/folders/folder");
                foreach (XmlNode xn in xnList)
                {
                    #region Module Exist check
                    try
                    {
                        System.Nullable <Int32> newModuleID     = 0;
                        System.Nullable <Int32> newModuleDefID  = 0;
                        System.Nullable <Int32> moduleControlID = 0;
                        //System.Nullable<Int32> _newPortalmoduleID = 0;
                        //System.Nullable<Int32> _newModuleDefPermissionID = 0;
                        //System.Nullable<Int32> _newPortalModulePermissionID = 0;
                        #region "Module Creation Logic"
                        SQLHandler    sqhl        = new SQLHandler();
                        SqlConnection sqlConn     = new SqlConnection(SystemSetting.SageFrameConnectionString);
                        SqlCommand    sqlCmd      = new SqlCommand();
                        int           ReturnValue = -1;
                        sqlCmd.Connection  = sqlConn;
                        sqlCmd.CommandText = "[dbo].[usp_Aspx_PaymentGatewayTypeAdd]";
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.Add(new SqlParameter("@PaymentGatewayTypeName", module.PaymentGatewayTypeName));
                        sqlCmd.Parameters.Add(new SqlParameter("@StoreID", module.StoreID));
                        sqlCmd.Parameters.Add(new SqlParameter("@PortalID", module.PortalID));
                        sqlCmd.Parameters.Add(new SqlParameter("@FolderName", module.FolderName));
                        sqlCmd.Parameters.Add(new SqlParameter("@FriendlyName", module.FriendlyName));
                        sqlCmd.Parameters.Add(new SqlParameter("@CultureName", module.CultureName));
                        sqlCmd.Parameters.Add(new SqlParameter("@Description", module.Description));
                        sqlCmd.Parameters.Add(new SqlParameter("@Version", module.Version));
                        sqlCmd.Parameters.Add(new SqlParameter("@AddedBy", GetUsername));
                        sqlCmd.Parameters.Add(new SqlParameter("@Update", update));
                        sqlCmd.Parameters.Add(new SqlParameter("@NewModuleId", SqlDbType.Int));
                        sqlCmd.Parameters["@NewModuleId"].Direction = ParameterDirection.Output;

                        sqlConn.Open();
                        sqlCmd.ExecuteNonQuery();
                        if (update == 0)
                        {
                            ReturnValue = (int)sqlCmd.Parameters["@NewModuleId"].Value;
                            module.PaymentGatewayTypeID = ReturnValue;
                        }
                        sqlConn.Close();

                        XmlNodeList xnList5      = doc.SelectNodes("sageframe/folders/folder/modules/module/controls/control");
                        int         displayOrder = 0;
                        foreach (XmlNode xn5 in xnList5)
                        {
                            displayOrder++;
                            string ctlKey       = xn5["key"].InnerXml.ToString();
                            string ctlSource    = xn5["src"].InnerXml.ToString();
                            string ctlTitle     = xn5["title"].InnerXml.ToString();
                            string _ctlType     = xn5["type"].InnerXml.ToString();
                            int    ctlType      = checkControlType(_ctlType);
                            string ctlHelpUrl   = xn5["helpurl"].InnerXml.ToString();
                            string ctlSupportPr = xn5["supportspartialrendering"].InnerXml.ToString();


                            List <KeyValuePair <string, object> > paramCol = new List <KeyValuePair <string, object> >();
                            paramCol.Add(new KeyValuePair <string, object>("@PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair <string, object>("@ControlName", ctlKey));
                            paramCol.Add(new KeyValuePair <string, object>("@ControlType", ctlType));
                            paramCol.Add(new KeyValuePair <string, object>("@ControlSource", ctlSource));
                            paramCol.Add(new KeyValuePair <string, object>("@DisplayOrder", displayOrder));
                            paramCol.Add(new KeyValuePair <string, object>("@StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair <string, object>("@PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair <string, object>("@CultureName", module.CultureName));
                            paramCol.Add(new KeyValuePair <string, object>("@AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair <string, object>("@Update", update));
                            paramCol.Add(new KeyValuePair <string, object>("@HelpUrl", ctlHelpUrl));
                            paramCol.Add(new KeyValuePair <string, object>("@SupportsPartialRendering", bool.Parse(ctlSupportPr.ToString())));
                            if (xn5.Attributes["type"] == null)
                            {
                                sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_PaymentGateWayControlAdd]", paramCol);
                            }
                            else
                            {
                                if (xn5.Attributes["type"] != null && xn5.Attributes["type"].Value.ToString().ToLower() == "page")
                                {
                                    if (update == 0)
                                    {
                                        try
                                        {
                                            ModulesDataContext db = new ModulesDataContext(SystemSetting.SageFrameConnectionString);
                                            //var LINQModuleInfo = db.sp_ModulesAdd(ref _newModuleID, ref _newModuleDefID, objModule.Name, objModule.PackageType, objModule.License,objModule.Owner, objModule.Organization, objModule.URL, objModule.Email, objModule.ReleaseNotes, objModule.FriendlyName,objModule.Description, objModule.Version, isPremium, false, objModule.BusinessControllerClass, objModule.FolderName,objModule.ModuleName, supportedFeatures, objModule.CompatibleVersions, dependencies, permissions, 0, true, DateTime.Now,GetPortalID, GetUsername);
                                            var LINQModuleInfo             = db.sp_ModulesAdd(ref newModuleID, ref newModuleDefID, "AspxCommerce." + "" + module.FriendlyName + "", "Module", "", "AspxCommerce", "", "", "", "", ctlKey, ctlKey, module.Version, true, false, "", module.FolderName, "AspxCommerce." + module.FriendlyName, 0, "", "", "", 0, true, DateTime.Now, GetPortalID, GetUsername);
                                            ModuleControlsDataContext dbMC = new ModuleControlsDataContext(SystemSetting.SageFrameConnectionString);

                                            //PermissionDataContext dbPermission = new PermissionDataContext(SystemSetting.SageFrameConnectionString);

                                            //db.sp_PortalModulesAdd(ref _newPortalmoduleID, GetPortalID, _newModuleID, true, DateTime.Now, GetUsername);

                                            //// get the default module VIEW permissions
                                            //var LINQModuleViewPermission = dbPermission.sp_GetPermissionByCodeAndKey("SYSTEM_VIEW", "VIEW").SingleOrDefault();
                                            //int _permissionIDView = LINQModuleViewPermission.PermissionID;
                                            ////insert into module permissions i.e., ModuleDefPermission and PortalModulePermission
                                            //dbPermission.sp_ModulesPermissionAdd(ref _newModuleDefPermissionID, _newModuleDefID, _permissionIDView, GetPortalID, ref _newPortalModulePermissionID, _newPortalmoduleID, true, null, true, DateTime.Now, GetUsername);


                                            //// get the default module EDIT permissions
                                            //var LINQModuleEditPermission = dbPermission.sp_GetPermissionByCodeAndKey("SYSTEM_EDIT", "EDIT").SingleOrDefault();
                                            //int _permissionIDEdit = LINQModuleEditPermission.PermissionID;
                                            ////insert into module permissions i.e., ModuleDefPermission and PortalModulePermission
                                            //dbPermission.sp_ModulesPermissionAdd(ref _newModuleDefPermissionID, _newModuleDefID, _permissionIDEdit, GetPortalID, ref _newPortalModulePermissionID, _newPortalmoduleID, true, null, true, DateTime.Now, GetUsername);

                                            //modules control add
                                            dbMC.sp_ModuleControlsAdd(ref moduleControlID, newModuleDefID, ctlKey + "View", ctlTitle + "View", ctlSource, null, ctlType, 0, ctlHelpUrl, false, true, DateTime.Now, GetPortalID, GetUsername);

                                            //sp_ModuleDefPermissionAdd
                                            string ModuleDefPermissionID;
                                            List <KeyValuePair <string, object> > paramDef = new List <KeyValuePair <string, object> >();
                                            paramDef.Add(new KeyValuePair <string, object>("@ModuleDefID", newModuleDefID));
                                            paramDef.Add(new KeyValuePair <string, object>("@PermissionID", 1));
                                            paramDef.Add(new KeyValuePair <string, object>("@IsActive", true));
                                            paramDef.Add(new KeyValuePair <string, object>("@AddedOn", DateTime.Now));
                                            paramDef.Add(new KeyValuePair <string, object>("@PortalID", GetPortalID));
                                            paramDef.Add(new KeyValuePair <string, object>("@AddedBy", GetUsername));
                                            ModuleDefPermissionID = sqlH.ExecuteNonQueryAsGivenType <string>("[dbo].[sp_ModuleDefPermissionAdd]", paramDef, "@ModuleDefPermissionID");

                                            //ModuleDefPermissionID
                                            List <KeyValuePair <string, object> > paramPage = new List <KeyValuePair <string, object> >();
                                            paramPage.Add(new KeyValuePair <string, object>("@ModuleDefID", newModuleDefID));
                                            paramPage.Add(new KeyValuePair <string, object>("@PageName", ctlKey));
                                            paramPage.Add(new KeyValuePair <string, object>("@PortalID", GetPortalID));
                                            paramPage.Add(new KeyValuePair <string, object>("@ModuleDefPermissionID", int.Parse(ModuleDefPermissionID)));;
                                            sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_CreatePaymentGatewayPage]", paramPage);
                                        }
                                        catch (Exception ex)
                                        {
                                            ProcessException(ex);
                                        }
                                    }
                                }
                            }
                        }


                        XmlNodeList xnList2 = doc.SelectNodes("sageframe/folders/folder/settings/setting");
                        int         onetime = 0;
                        foreach (XmlNode xn2 in xnList2)
                        {
                            onetime++;
                            string settingkey   = xn2["key"].InnerXml.ToString();
                            string settingvalue = xn2["value"].InnerXml.ToString();
                            List <KeyValuePair <string, object> > paramCol = new List <KeyValuePair <string, object> >();
                            paramCol.Add(new KeyValuePair <string, object>("@PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair <string, object>("@StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair <string, object>("@PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair <string, object>("@SettingKey", settingkey));
                            paramCol.Add(new KeyValuePair <string, object>("@SettingValue", settingvalue));
                            paramCol.Add(new KeyValuePair <string, object>("@AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair <string, object>("@Update", update));
                            paramCol.Add(new KeyValuePair <string, object>("@onetime", onetime));
                            sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_PaymentGateWaySettingByKeyAdd]", paramCol);
                        }

                        XmlNodeList xnList3 = doc.SelectNodes("sageframe/folders/folder/files/file");
                        if (xnList3.Count != 0)
                        {
                            foreach (XmlNode xn3 in xnList3)
                            {
                                string fileName = xn3["name"].InnerXml;
                                try
                                {
                                    #region CheckValidDataSqlProvider
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(module.Version + ".SqlDataProvider"))
                                    {
                                        _exceptions = ReadSQLFile(module.TempFolderPath, fileName);
                                    }
                                    #endregion

                                    #region CheckAlldllFiles
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(".dll"))
                                    {
                                        dllFiles.Add(fileName);
                                    }
                                    #endregion

                                    #region ReadUninstall SQL FileName
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains("Uninstall.SqlDataProvider"))
                                    {
                                        unistallScriptFile = fileName;
                                    }
                                    #endregion
                                }
                                catch (Exception ex)
                                {
                                    _exceptions += ex.Message;
                                    break;
                                }
                            }
                        }

                        if (_exceptions != string.Empty)
                        {
                            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                            {
                                //Run unstallScript
                                if (unistallScriptFile != "")
                                {
                                    _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                                }
                                //Delete Module info from data base
                                PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                                module.PaymentGatewayTypeID = -1;
                            }
                        }
                        #endregion
                    }
                    catch
                    {
                        if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                        {
                            //Run unstallScript
                            if (unistallScriptFile != "")
                            {
                                _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                            }
                            //Delete Module info from data base
                            if (update == 0)
                            {
                                PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                            }
                            module.PaymentGatewayTypeID = -1;
                        }
                    }
                    #endregion
                }
            }

            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0 && _exceptions == string.Empty)
            {
                string path       = HttpContext.Current.Server.MapPath("~/");
                string flPath     = module.FolderName.ToString().Replace("/", "\\");
                string targetPath = path + SageFrame.Core.RegisterModule.Common.ModuleFolder + '\\' + flPath;
                CopyDirectory(module.TempFolderPath, targetPath);
                for (int i = 0; i < dllFiles.Count; i++)
                {
                    string sourcedllFile = module.TempFolderPath + '\\' + dllFiles[i].ToString();
                    string targetdllPath = path + SageFrame.Core.RegisterModule.Common.DLLTargetPath + '\\' + dllFiles[i].ToString();
                    File.Copy(sourcedllFile, targetdllPath, true);
                    //File.Move();
                }
            }
            DeleteTempDirectory(module.TempFolderPath);
        }
        public void InstallPackage(PaymentGateWayModuleInfo module,int update)
        {
            XmlDocument doc = new XmlDocument();
            ArrayList dllFiles = new ArrayList();
            string unistallScriptFile = string.Empty;
            doc.Load(module.TempFolderPath + '\\' + module.ManifestFile);
            XmlElement root = doc.DocumentElement;
            if (!String.IsNullOrEmpty(root.ToString()))
            {
                XmlNodeList xnList = doc.SelectNodes("sageframe/folders/folder");
                foreach (XmlNode xn in xnList)
                {
                    #region Module Exist check
                    try
                    {
                        System.Nullable<Int32> newModuleID = 0;
                        System.Nullable<Int32> newModuleDefID = 0;
                        System.Nullable<Int32> moduleControlID = 0;
                        //System.Nullable<Int32> _newPortalmoduleID = 0;
                        //System.Nullable<Int32> _newModuleDefPermissionID = 0;
                        //System.Nullable<Int32> _newPortalModulePermissionID = 0;
                        #region "Module Creation Logic"
                        SQLHandler sqhl = new SQLHandler();
                        SqlConnection sqlConn = new SqlConnection(SystemSetting.SageFrameConnectionString);
                        SqlCommand sqlCmd = new SqlCommand();
                        int ReturnValue = -1;
                        sqlCmd.Connection = sqlConn;
                        sqlCmd.CommandText = "[dbo].[usp_Aspx_PaymentGatewayTypeAdd]";
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.Add(new SqlParameter("@PaymentGatewayTypeName", module.PaymentGatewayTypeName));
                        sqlCmd.Parameters.Add(new SqlParameter("@StoreID", module.StoreID));
                        sqlCmd.Parameters.Add(new SqlParameter("@PortalID", module.PortalID));
                        sqlCmd.Parameters.Add(new SqlParameter("@FolderName", module.FolderName));
                        sqlCmd.Parameters.Add(new SqlParameter("@FriendlyName", module.FriendlyName));
                        sqlCmd.Parameters.Add(new SqlParameter("@CultureName", module.CultureName));
                        sqlCmd.Parameters.Add(new SqlParameter("@Description", module.Description));
                        sqlCmd.Parameters.Add(new SqlParameter("@Version", module.Version));
                        sqlCmd.Parameters.Add(new SqlParameter("@AddedBy", GetUsername));
                        sqlCmd.Parameters.Add(new SqlParameter("@Update", update));
                        sqlCmd.Parameters.Add(new SqlParameter("@NewModuleId", SqlDbType.Int));
                        sqlCmd.Parameters["@NewModuleId"].Direction = ParameterDirection.Output;

                        sqlConn.Open();
                        sqlCmd.ExecuteNonQuery();
                        if (update==0)
                        {
                            ReturnValue = (int)sqlCmd.Parameters["@NewModuleId"].Value;
                            module.PaymentGatewayTypeID = ReturnValue;
                        }
                        sqlConn.Close();

                        XmlNodeList xnList5 = doc.SelectNodes("sageframe/folders/folder/modules/module/controls/control");
                        int displayOrder = 0;
                        foreach (XmlNode xn5 in xnList5)
                        {
                            displayOrder++;
                            string ctlKey = xn5["key"].InnerXml.ToString();
                            string ctlSource = xn5["src"].InnerXml.ToString();
                            string ctlTitle = xn5["title"].InnerXml.ToString();
                            string _ctlType = xn5["type"].InnerXml.ToString();
                            int ctlType = checkControlType(_ctlType);
                            string ctlHelpUrl = xn5["helpurl"].InnerXml.ToString();
                            string ctlSupportPr = xn5["supportspartialrendering"].InnerXml.ToString();


                            List<KeyValuePair<string, object>> paramCol = new List<KeyValuePair<string, object>>();
                            paramCol.Add(new KeyValuePair<string, object>("@PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair<string, object>("@ControlName", ctlKey));
                            paramCol.Add(new KeyValuePair<string, object>("@ControlType", ctlType));
                            paramCol.Add(new KeyValuePair<string, object>("@ControlSource", ctlSource));
                            paramCol.Add(new KeyValuePair<string, object>("@DisplayOrder", displayOrder));
                            paramCol.Add(new KeyValuePair<string, object>("@StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair<string, object>("@PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair<string, object>("@CultureName", module.CultureName));
                            paramCol.Add(new KeyValuePair<string, object>("@AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair<string, object>("@Update", update));
                            paramCol.Add(new KeyValuePair<string, object>("@HelpUrl", ctlHelpUrl));
                            paramCol.Add(new KeyValuePair<string, object>("@SupportsPartialRendering", bool.Parse(ctlSupportPr.ToString())));
                            if (xn5.Attributes["type"] == null)
                            {
                                sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_PaymentGateWayControlAdd]", paramCol);
                            }
                            else
                            {
                                if (xn5.Attributes["type"] != null && xn5.Attributes["type"].Value.ToString().ToLower() == "page")
                                {
                                    if (update == 0)
                                    {
                                        try
                                        {
                                            ModulesDataContext db = new ModulesDataContext(SystemSetting.SageFrameConnectionString);
                                            //var LINQModuleInfo = db.sp_ModulesAdd(ref _newModuleID, ref _newModuleDefID, objModule.Name, objModule.PackageType, objModule.License,objModule.Owner, objModule.Organization, objModule.URL, objModule.Email, objModule.ReleaseNotes, objModule.FriendlyName,objModule.Description, objModule.Version, isPremium, false, objModule.BusinessControllerClass, objModule.FolderName,objModule.ModuleName, supportedFeatures, objModule.CompatibleVersions, dependencies, permissions, 0, true, DateTime.Now,GetPortalID, GetUsername);
                                            var LINQModuleInfo = db.sp_ModulesAdd(ref newModuleID, ref newModuleDefID, "AspxCommerce." + "" + module.FriendlyName + "", "Module", "", "AspxCommerce", "", "", "", "", ctlKey, ctlKey, module.Version, true, false, "", module.FolderName, "AspxCommerce." + module.FriendlyName, 0, "", "", "", 0, true, DateTime.Now, GetPortalID, GetUsername);
                                            ModuleControlsDataContext dbMC = new ModuleControlsDataContext(SystemSetting.SageFrameConnectionString);
                                         
                                            //PermissionDataContext dbPermission = new PermissionDataContext(SystemSetting.SageFrameConnectionString);

                                            //db.sp_PortalModulesAdd(ref _newPortalmoduleID, GetPortalID, _newModuleID, true, DateTime.Now, GetUsername);

                                            //// get the default module VIEW permissions
                                            //var LINQModuleViewPermission = dbPermission.sp_GetPermissionByCodeAndKey("SYSTEM_VIEW", "VIEW").SingleOrDefault();
                                            //int _permissionIDView = LINQModuleViewPermission.PermissionID;
                                            ////insert into module permissions i.e., ModuleDefPermission and PortalModulePermission
                                            //dbPermission.sp_ModulesPermissionAdd(ref _newModuleDefPermissionID, _newModuleDefID, _permissionIDView, GetPortalID, ref _newPortalModulePermissionID, _newPortalmoduleID, true, null, true, DateTime.Now, GetUsername);


                                            //// get the default module EDIT permissions
                                            //var LINQModuleEditPermission = dbPermission.sp_GetPermissionByCodeAndKey("SYSTEM_EDIT", "EDIT").SingleOrDefault();
                                            //int _permissionIDEdit = LINQModuleEditPermission.PermissionID;
                                            ////insert into module permissions i.e., ModuleDefPermission and PortalModulePermission
                                            //dbPermission.sp_ModulesPermissionAdd(ref _newModuleDefPermissionID, _newModuleDefID, _permissionIDEdit, GetPortalID, ref _newPortalModulePermissionID, _newPortalmoduleID, true, null, true, DateTime.Now, GetUsername);

                                            //modules control add
                                            dbMC.sp_ModuleControlsAdd(ref moduleControlID, newModuleDefID, ctlKey + "View", ctlTitle + "View", ctlSource, null, ctlType, 0, ctlHelpUrl, false, true, DateTime.Now, GetPortalID, GetUsername);

                                            //sp_ModuleDefPermissionAdd
                                            string ModuleDefPermissionID;
                                            List<KeyValuePair<string, object>> paramDef = new List<KeyValuePair<string, object>>();
                                            paramDef.Add(new KeyValuePair<string, object>("@ModuleDefID", newModuleDefID));
                                            paramDef.Add(new KeyValuePair<string, object>("@PermissionID", 1));
                                            paramDef.Add(new KeyValuePair<string, object>("@IsActive", true));
                                            paramDef.Add(new KeyValuePair<string, object>("@AddedOn", DateTime.Now));
                                            paramDef.Add(new KeyValuePair<string, object>("@PortalID", GetPortalID));
                                            paramDef.Add(new KeyValuePair<string, object>("@AddedBy", GetUsername));
                                            ModuleDefPermissionID = sqlH.ExecuteNonQueryAsGivenType<string>("[dbo].[sp_ModuleDefPermissionAdd]", paramDef, "@ModuleDefPermissionID");

                                            //ModuleDefPermissionID
                                            List<KeyValuePair<string, object>> paramPage = new List<KeyValuePair<string, object>>();
                                            paramPage.Add(new KeyValuePair<string, object>("@ModuleDefID", newModuleDefID));
                                            paramPage.Add(new KeyValuePair<string, object>("@PageName", ctlKey));
                                            paramPage.Add(new KeyValuePair<string, object>("@PortalID", GetPortalID));
                                            paramPage.Add(new KeyValuePair<string, object>("@ModuleDefPermissionID", int.Parse(ModuleDefPermissionID))); ;
                                            sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_CreatePaymentGatewayPage]", paramPage);

                                        }
                                        catch (Exception ex )
                                        {

                                            ProcessException(ex);
                                        }
                                    }                                   

                                }
                            }
                        }


                        XmlNodeList xnList2 = doc.SelectNodes("sageframe/folders/folder/settings/setting");
                        int onetime = 0;
                        foreach (XmlNode xn2 in xnList2)
                        {
                            onetime++;
                            string settingkey = xn2["key"].InnerXml.ToString();
                            string settingvalue = xn2["value"].InnerXml.ToString();
                            List<KeyValuePair<string, object>> paramCol = new List<KeyValuePair<string, object>>();
                            paramCol.Add(new KeyValuePair<string, object>("@PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair<string, object>("@StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair<string, object>("@PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair<string, object>("@SettingKey", settingkey));
                            paramCol.Add(new KeyValuePair<string, object>("@SettingValue", settingvalue));
                            paramCol.Add(new KeyValuePair<string, object>("@AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair<string, object>("@Update", update));
                            paramCol.Add(new KeyValuePair<string, object>("@onetime", onetime));
                            sqlH.ExecuteNonQuery("[dbo].[usp_Aspx_PaymentGateWaySettingByKeyAdd]", paramCol);
                        }

                        XmlNodeList xnList3 = doc.SelectNodes("sageframe/folders/folder/files/file");
                        if (xnList3.Count != 0)
                        {
                            foreach (XmlNode xn3 in xnList3)
                            {
                                string fileName = xn3["name"].InnerXml;
                                try
                                {
                                    #region CheckValidDataSqlProvider
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(module.Version + ".SqlDataProvider"))
                                    {
                                        _exceptions = ReadSQLFile(module.TempFolderPath, fileName);
                                    }
                                    #endregion

                                    #region CheckAlldllFiles
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(".dll"))
                                    {
                                        dllFiles.Add(fileName);
                                    }
                                    #endregion

                                    #region ReadUninstall SQL FileName
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains("Uninstall.SqlDataProvider"))
                                    {
                                        unistallScriptFile = fileName;
                                    }
                                    #endregion
                                }
                                catch (Exception ex)
                                {
                                    _exceptions += ex.Message;
                                    break;
                                }
                            }
                        }

                        if (_exceptions != string.Empty)
                        {
                            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                            {
                                //Run unstallScript
                                if (unistallScriptFile != "")
                                {
                                    _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                                }
                                //Delete Module info from data base
                                PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                                module.PaymentGatewayTypeID = -1;
                            }
                        }
                        #endregion
                    }
                    catch
                    {
                        if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                        {
                            //Run unstallScript
                            if (unistallScriptFile != "")
                            {
                                _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                            }
                            //Delete Module info from data base
                            if (update == 0)
                            {
                               PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                            }
                            module.PaymentGatewayTypeID = -1;
                        }
                    }
                    #endregion
                }
            }

            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0 && _exceptions == string.Empty)
            {
                string path = HttpContext.Current.Server.MapPath("~/");
                string flPath = module.FolderName.ToString().Replace("/","\\"); 
                string targetPath = path + SageFrame.Core.RegisterModule.Common.ModuleFolder + '\\' + flPath;
                CopyDirectory(module.TempFolderPath, targetPath);
                for (int i = 0; i < dllFiles.Count; i++)
                {
                    string sourcedllFile = module.TempFolderPath + '\\' + dllFiles[i].ToString();
                    string targetdllPath = path + SageFrame.Core.RegisterModule.Common.DLLTargetPath + '\\' + dllFiles[i].ToString();
                    File.Copy(sourcedllFile, targetdllPath, true);
                    //File.Move();
                }
            }
            DeleteTempDirectory(module.TempFolderPath);
        }
        public int Step1CheckLogic(string tempUnzippedPath, PaymentGateWayModuleInfo module)
        {
            if (CheckFormanifestFile(tempUnzippedPath, module) != "")
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(tempUnzippedPath + '\\' + module.ManifestFile);
                XmlElement root = doc.DocumentElement;
                if (CheckValidManifestFile(root, module))
                {
                    XmlNodeList xnList = doc.SelectNodes("sageframe/folders/folder");
                    foreach (XmlNode xn in xnList)
                    {
                        module.PaymentGatewayTypeName = xn["paymentgatewayname"].InnerXml.ToString();
                        module.FolderName = xn["foldername"].InnerXml.ToString();
                        module.FriendlyName = xn["friendlyname"].InnerXml.ToString();   
                        module.Description = xn["description"].InnerXml.ToString();  
                        module.Version = xn["version"].InnerXml.ToString();  
                        module.Name = xn["name"].InnerXml.ToString();
                        module.StoreID = GetStoreID;//int.Parse(xn["storeid"].InnerXml.ToString());
                        module.PortalID = GetPortalID;//int.Parse(xn["portalid"].InnerXml.ToString());
                        module.CultureName = xn["culturename"].InnerXml.ToString();

                        if (!String.IsNullOrEmpty(module.PaymentGatewayTypeName) && IsModuleExist(module.PaymentGatewayTypeName.ToLower(), module.FolderName.ToString(), module.FriendlyName.ToString(),int.Parse(GetStoreID.ToString()),int.Parse(GetPortalID.ToString())))
                        {
                            string path = HttpContext.Current.Server.MapPath("~/");
                            string targetPath = path + SageFrame.Core.RegisterModule.Common.ModuleFolder + '\\' + module.FolderName;
                            module.InstalledFolderPath = targetPath;
                           // DeleteTempDirectory(tempUnzippedPath);
                            return 1;//Already exist
                        }
                        else
                        {
                            return 2;//Not Exists
                        }
                    }
                }
                else
                {
                    return -1;//Invalid Manifest file
                }
            }
            return 0;//No manifest file
        }
 public string CheckFormanifestFile(string tempUnzippedPath, PaymentGateWayModuleInfo module)
 {
     DirectoryInfo dir = new DirectoryInfo(tempUnzippedPath);
     foreach (FileInfo f in dir.GetFiles("*.*"))
     {
         if (f.Extension.ToLower() == ".sfe")
         {
             module.ManifestFile = f.Name;
             return module.ManifestFile;
         }
         else
         {
             module.ManifestFile = "";
         }
     }
     return module.ManifestFile;
 }
 public bool CheckValidManifestFile(XmlElement root, PaymentGateWayModuleInfo module)
 {
     if (root.Name == "sageframe")//need to change the root node for valid manifest file at root node  
     {
         string packageType = root.GetAttribute("type"); //root.NodeType
         //module.PackageType = PackageType;
         switch (packageType.ToLower())
         {
             //need to check for many cases for like skin /..
             case "paymentgateway":
                 return true;
         }
     }
     return false;
 }
    private void UninstallPaymentGateway(PaymentGateWayModuleInfo paymentGateWay, bool deleteModuleFolder, int gatewayID)
    {
        PaymentGatewayInstaller installerClass = new PaymentGatewayInstaller();
        string path = HttpContext.Current.Server.MapPath("~/");

        //checked if directory exist for current Payment Gateway foldername
        string paymentGatewayFolderPath = paymentGateWay.InstalledFolderPath;
        if (!string.IsNullOrEmpty(paymentGatewayFolderPath))
        {
            if (Directory.Exists(paymentGatewayFolderPath))
            {
                //check for valid .sfe file exist or not
                XmlDocument doc = new XmlDocument();
                doc.Load(paymentGatewayFolderPath + '\\' + paymentGateWay.ManifestFile);

                try
                {
                    if (paymentGateWay.PaymentGatewayTypeID > 0)
                    {
                        //Run script  
                        ReadUninstallScriptAndDLLFiles(doc, paymentGatewayFolderPath, installerClass);
                        //Rollback PaymentGatewayTypeID
                        //installerClass.PaymentGatewayRollBack(paymentGateWay.PaymentGatewayTypeID, GetPortalID, GetStoreID);
                        if (deleteModuleFolder == true)
                        {
                            //Delete Payment GateWay's Original Folder
                            installerClass.DeleteTempDirectory(paymentGateWay.InstalledFolderPath);
                        }
                        installhelp.InstallPackage(paymentModule, gatewayID);
                    }
                }
                catch (Exception ex)
                {
                    _exceptions = ex.Message;
                }
            }
            else if (!Directory.Exists(paymentGatewayFolderPath))
            {
                installhelp.InstallPackage(paymentModule, gatewayID);
            }
            else
            {
                ShowMessage(SageMessageTitle.Exception.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "PaymentGatewayFolderDoesnotExist"), "", SageMessageType.Error);
            }
        }
    }
 private  void InstallPaymentGateWay()
 {
     try
     {
         ArrayList arrColl = installhelp.Step0CheckLogic(fuPGModule);
         int ReturnValue;
         if (arrColl != null && arrColl.Count > 0)
         {
             ReturnValue = (int)arrColl[0];
             paymentModule = (PaymentGateWayModuleInfo)arrColl[1];
             ViewState["PaymentGateway"] = paymentModule.PaymentGatewayTypeID.ToString();
             if (ReturnValue == 0)
             {
                 // ViewState["PaymentGateway"] = null;
                 ShowMessage(SageMessageTitle.Notification.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "YouNeedToSelectAFileToUploadFirst"), "", SageMessageType.Alert);
                 lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "YouNeedToSelectAFileToUploadFirst");
                 lblLoadMessage.Visible = true;
                 ErrorCode = 1;
                 return;
             }
             else if (ReturnValue == -1)
             {
                 // ViewState["PaymentGateway"] = null;
                 ShowMessage(SageMessageTitle.Exception.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "InvalidFileExtension") + this.fuPGModule.FileName, "", SageMessageType.Alert);
                 lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "InvalidFileExtension") + this.fuPGModule.FileName;
                 lblLoadMessage.Visible = true;
                 ErrorCode = 1;
                 return;
             }
             else if (ReturnValue == 1)
             {
                 //paymentModule = (PaymentGateWayModuleInfo)ViewState["PaymentGateway"];
                 //if (paymentModule != null)
                 //{
                 installhelp.InstallPackage(paymentModule, 0);
                 ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "PaymentGatewayInstalledSuccessfully"), "", SageMessageType.Success);
                 ErrorCode = 0;
                 Response.Redirect(pageURL + "?installed=true", false);
                 return;
                 //}
             }
             else if (ReturnValue == 2)
             {
                 if (chkRepairInstall.Checked == true)
                 {
                     //paymentModule = (PaymentGateWayModuleInfo)ViewState["PaymentGateway"];
                     int gatewayID = int.Parse(ViewState["PaymentGateway"].ToString());
                     if (paymentModule != null)
                     {
                         UninstallPaymentGateway(paymentModule, true, gatewayID);
                         ViewState["PaymentGateway"] = null;
                         ShowMessage(SageMessageTitle.Information.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "PaymentGatewayInstalledSuccessfully"), "", SageMessageType.Success);
                         ErrorCode = 0;
                         Response.Redirect(pageURL + "?installed=true", false);
                         return;
                     }
                 }
                 else
                 {
                     //ViewState["PaymentGateway"] = null;                    
                     ShowMessage(SageMessageTitle.Notification.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "AlreadyExistPaymentGateway"), "", SageMessageType.Alert);
                  //   lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "AlreadyExistPaymentGateway");
                  //   lblLoadMessage.Visible = true;
                     ErrorCode = 1;
                     return;
                 }
             }
             else if (ReturnValue == 3)
             {
                 // ViewState["PaymentGateway"] = null;
                 ShowMessage(SageMessageTitle.Notification.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThisPackageIsNotValid"), "", SageMessageType.Alert);
                 lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThisPackageIsNotValid");
                 lblLoadMessage.Visible = true;
                 ErrorCode = 1;
                 return;
             }
             else if (ReturnValue == 4)
             {
                 // ViewState["PaymentGateway"] = null;
                 ShowMessage(SageMessageTitle.Notification.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThisPackageDoesNotAppearToBeValid"), "", SageMessageType.Alert);
                 lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThisPackageDoesNotAppearToBeValid");
                 lblLoadMessage.Visible = true;
                 ErrorCode = 1;
                 return;
             }
             else
             {
                 // ViewState["PaymentGateway"] = null;
                 ShowMessage(SageMessageTitle.Exception.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThereIsErrorWhileInstallingThisModule"), "", SageMessageType.Error);
                 lblLoadMessage.Text = SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "ThereIsErrorWhileInstallingThisModule");
                 lblLoadMessage.Visible = true;
                 ErrorCode = 1;
                 return;
             }
         }
     }
     catch (Exception ex)
     {
         ProcessException(ex);
     }
 }
    private void UninstallPaymentGateway(PaymentGateWayModuleInfo paymentGateWay, bool deleteModuleFolder, int gatewayID)
    {
        PaymentGatewayInstaller installerClass = new PaymentGatewayInstaller();
        string path = HttpContext.Current.Server.MapPath("~/");

        string paymentGatewayFolderPath = paymentGateWay.InstalledFolderPath;
        if (!string.IsNullOrEmpty(paymentGatewayFolderPath))
        {
            if (Directory.Exists(paymentGatewayFolderPath))
            {
                XmlDocument doc = new XmlDocument();
                if (File.Exists(paymentGatewayFolderPath + '\\' + paymentGateWay.ManifestFile))
                {

                    doc.Load(paymentGatewayFolderPath + '\\' + paymentGateWay.ManifestFile);

                    try
                    {
                        if (paymentGateWay.PaymentGatewayTypeID > 0)
                        {
                            ReadUninstallScriptAndDLLFiles(doc, paymentGatewayFolderPath, installerClass);
                            if (deleteModuleFolder == true)
                            {
                                installerClass.DeleteTempDirectory(paymentGateWay.InstalledFolderPath);
                            }
                            installhelp.InstallPackage(paymentModule, gatewayID);
                        }
                    }
                    catch (Exception ex)
                    {
                        _exceptions = ex.Message;
                    }
                }
                else
                {
                    if (deleteModuleFolder == true)
                    {
                        installerClass.DeleteTempDirectory(paymentGateWay.InstalledFolderPath);
                    }
                    installhelp.InstallPackage(paymentModule, gatewayID);
                }
            }
            else if (!Directory.Exists(paymentGatewayFolderPath))
            {
                installhelp.InstallPackage(paymentModule, gatewayID);
            }
            else
            {
                ShowMessage(SageMessageTitle.Exception.ToString(), SageMessage.GetSageModuleLocalMessageByVertualPath("Modules/AspxCommerce/AspxPaymentGateWayManagement/ModuleLocalText", "PaymentGatewayFolderDoesnotExist"), "", SageMessageType.Error);
            }
        }
    }
        public void InstallPackage(PaymentGateWayModuleInfo module, int update)
        {
            ModuleController moduleCtr          = new ModuleController();
            XmlDocument      doc                = new XmlDocument();
            ArrayList        dllFiles           = new ArrayList();
            string           unistallScriptFile = string.Empty;

            doc.Load(module.TempFolderPath + '\\' + module.ManifestFile);
            XmlElement root = doc.DocumentElement;

            if (!String.IsNullOrEmpty(root.ToString()))
            {
                XmlNodeList xnList = doc.SelectNodes("sageframe/folders/folder");
                foreach (XmlNode xn in xnList)
                {
                    #region Module Exist check
                    try
                    {
                        System.Nullable <Int32> newModuleID       = 0;
                        System.Nullable <Int32> newModuleDefID    = 0;
                        System.Nullable <Int32> newPortalmoduleID = 0;
                        //System.Nullable<Int32> _newPortalmoduleID = 0;
                        //System.Nullable<Int32> _newModuleDefPermissionID = 0;
                        //System.Nullable<Int32> _newPortalModulePermissionID = 0;
                        #region "Module Creation Logic"
                        OracleHandler    sqhl    = new OracleHandler();
                        OracleConnection sqlConn = new OracleConnection(SystemSetting.SageFrameConnectionString);
                        OracleCommand    sqlCmd  = new OracleCommand();
                        int ReturnValue          = -1;
                        sqlCmd.Connection  = sqlConn;
                        sqlCmd.CommandText = "usp_Aspx_PaymentGatewayTypeAdd";
                        sqlCmd.CommandType = CommandType.StoredProcedure;
                        sqlCmd.Parameters.Add(new OracleParameter("PaymentGatewayTypeName", module.PaymentGatewayTypeName));
                        sqlCmd.Parameters.Add(new OracleParameter("StoreID", module.StoreID));
                        sqlCmd.Parameters.Add(new OracleParameter("PortalID", module.PortalID));
                        sqlCmd.Parameters.Add(new OracleParameter("FolderName", module.FolderName));
                        sqlCmd.Parameters.Add(new OracleParameter("FriendlyName", module.FriendlyName));
                        sqlCmd.Parameters.Add(new OracleParameter("CultureName", module.CultureName));
                        sqlCmd.Parameters.Add(new OracleParameter("Description", module.Description));
                        sqlCmd.Parameters.Add(new OracleParameter("Version", module.Version));
                        sqlCmd.Parameters.Add(new OracleParameter("AddedBy", GetUsername));
                        sqlCmd.Parameters.Add(new OracleParameter("Update", update));
                        sqlCmd.Parameters.Add(new OracleParameter("NewModuleId", SqlDbType.Int));
                        sqlCmd.Parameters["NewModuleId"].Direction = ParameterDirection.Output;

                        sqlConn.Open();
                        sqlCmd.ExecuteNonQuery();
                        if (update == 0)
                        {
                            ReturnValue = (int)sqlCmd.Parameters["NewModuleId"].Value;
                            module.PaymentGatewayTypeID = ReturnValue;
                        }
                        sqlConn.Close();

                        XmlNodeList xnList5      = doc.SelectNodes("sageframe/folders/folder/modules/module/controls/control");
                        int         displayOrder = 0;
                        foreach (XmlNode xn5 in xnList5)
                        {
                            displayOrder++;
                            string ctlKey       = xn5["key"].InnerXml.ToString();
                            string ctlSource    = xn5["src"].InnerXml.ToString();
                            string ctlTitle     = xn5["title"].InnerXml.ToString();
                            string _ctlType     = xn5["type"].InnerXml.ToString();
                            int    ctlType      = checkControlType(_ctlType);
                            string ctlHelpUrl   = xn5["helpurl"].InnerXml.ToString();
                            string ctlSupportPr = xn5["supportspartialrendering"].InnerXml.ToString();


                            List <KeyValuePair <string, object> > paramCol = new List <KeyValuePair <string, object> >();
                            paramCol.Add(new KeyValuePair <string, object>("PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair <string, object>("ControlName", ctlKey));
                            paramCol.Add(new KeyValuePair <string, object>("ControlType", ctlType));
                            paramCol.Add(new KeyValuePair <string, object>("ControlSource", ctlSource));
                            paramCol.Add(new KeyValuePair <string, object>("DisplayOrder", displayOrder));
                            paramCol.Add(new KeyValuePair <string, object>("StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair <string, object>("PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair <string, object>("CultureName", module.CultureName));
                            paramCol.Add(new KeyValuePair <string, object>("AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair <string, object>("Update", update));
                            paramCol.Add(new KeyValuePair <string, object>("HelpUrl", ctlHelpUrl));
                            paramCol.Add(new KeyValuePair <string, object>("SupportsPartialRendering", bool.Parse(ctlSupportPr.ToString())));
                            if (xn5.Attributes["type"] == null)
                            {
                                sqlH.ExecuteNonQuery("usp_Aspx_PaymentGateWayControlAdd", paramCol);
                            }
                            else
                            {
                                if (xn5.Attributes["type"] != null && xn5.Attributes["type"].Value.ToString().ToLower() == "page")
                                {
                                    if (!IsPageExists(ctlKey))
                                    {
                                        //if (update == 0)
                                        // {
                                        try
                                        {
                                            #region "Module Creation Logic"

                                            // add into module table
                                            ModuleInfo moduleObj = new ModuleInfo();
                                            moduleObj.ModuleName              = "AspxCommerce." + module.FriendlyName;
                                            moduleObj.Name                    = module.Name;
                                            moduleObj.PackageType             = "Module";
                                            moduleObj.Owner                   = "AspxCommerce";
                                            moduleObj.Organization            = "";
                                            moduleObj.URL                     = "";
                                            moduleObj.Email                   = "";
                                            moduleObj.ReleaseNotes            = "";
                                            moduleObj.FriendlyName            = ctlKey;
                                            moduleObj.Description             = ctlKey;
                                            moduleObj.Version                 = module.Version;
                                            moduleObj.isPremium               = true;
                                            moduleObj.BusinessControllerClass = "";
                                            moduleObj.FolderName              = module.FolderName;
                                            moduleObj.supportedFeatures       = 0;
                                            moduleObj.CompatibleVersions      = "";
                                            moduleObj.dependencies            = "";
                                            moduleObj.permissions             = "";

                                            int[] outputValue;
                                            outputValue = moduleCtr.AddModules(moduleObj, false, 0, true,
                                                                               DateTime.Now, GetPortalID,
                                                                               GetUsername);
                                            moduleObj.ModuleID    = outputValue[0];
                                            moduleObj.ModuleDefID = outputValue[1];
                                            newModuleID           = moduleObj.ModuleID;
                                            newModuleDefID        = moduleObj.ModuleDefID;


                                            //insert into ProtalModule table

                                            newPortalmoduleID = moduleCtr.AddPortalModules(GetPortalID,
                                                                                           newModuleID, true,
                                                                                           DateTime.Now,
                                                                                           GetUsername);
                                            #endregion

                                            //install permission for the installed module in ModuleDefPermission table with ModuleDefID and PermissionID
                                            int controlType = 0;
                                            controlType = ctlType;
                                            string IconFile = "";

                                            //add into module control table
                                            moduleCtr.AddModuleCoontrols(newModuleDefID, ctlKey + "View",
                                                                         ctlTitle + "View", ctlSource,
                                                                         IconFile, controlType, 0, ctlHelpUrl,
                                                                         bool.Parse(ctlSupportPr), true,
                                                                         DateTime.Now,
                                                                         GetPortalID, GetUsername);

                                            //sp_ModuleDefPermissionAdd
                                            string ModuleDefPermissionID;
                                            List <KeyValuePair <string, object> > paramDef =
                                                new List <KeyValuePair <string, object> >();
                                            paramDef.Add(new KeyValuePair <string, object>("ModuleDefID",
                                                                                           newModuleDefID));
                                            paramDef.Add(new KeyValuePair <string, object>("PortalModuleID",
                                                                                           newPortalmoduleID));
                                            paramDef.Add(new KeyValuePair <string, object>("PermissionID", 1));
                                            paramDef.Add(new KeyValuePair <string, object>("IsActive", true));
                                            paramDef.Add(new KeyValuePair <string, object>("AddedOn", DateTime.Now));
                                            paramDef.Add(new KeyValuePair <string, object>("PortalID", GetPortalID));
                                            paramDef.Add(new KeyValuePair <string, object>("AddedBy", GetUsername));
                                            ModuleDefPermissionID =
                                                sqlH.ExecuteNonQueryAsGivenType <string>(
                                                    "sp_ModuleDefPermissionAdd", paramDef,
                                                    "ModuleDefPermissionID");

                                            //ModuleDefPermissionID
                                            List <KeyValuePair <string, object> > paramPage =
                                                new List <KeyValuePair <string, object> >();
                                            paramPage.Add(new KeyValuePair <string, object>("ModuleDefID",
                                                                                            newModuleDefID));
                                            paramPage.Add(new KeyValuePair <string, object>("PageName", ctlKey));
                                            paramPage.Add(new KeyValuePair <string, object>("PortalID", GetPortalID));
                                            paramPage.Add(new KeyValuePair <string, object>(
                                                              "ModuleDefPermissionID",
                                                              int.Parse(ModuleDefPermissionID)));
                                            ;
                                            sqlH.ExecuteNonQuery("usp_Aspx_CreatePaymentGatewayPage",
                                                                 paramPage);
                                        }
                                        catch (Exception ex)
                                        {
                                            ProcessException(ex);
                                        }
                                        //   }
                                    }
                                }
                            }
                        }


                        XmlNodeList xnList2 = doc.SelectNodes("sageframe/folders/folder/settings/setting");
                        int         onetime = 0;
                        foreach (XmlNode xn2 in xnList2)
                        {
                            onetime++;
                            string settingkey   = xn2["key"].InnerXml.ToString();
                            string settingvalue = xn2["value"].InnerXml.ToString();
                            List <KeyValuePair <string, object> > paramCol = new List <KeyValuePair <string, object> >();
                            paramCol.Add(new KeyValuePair <string, object>("PaymentGatewayTypeID", module.PaymentGatewayTypeID));
                            paramCol.Add(new KeyValuePair <string, object>("StoreID", module.StoreID));
                            paramCol.Add(new KeyValuePair <string, object>("PortalID", module.PortalID));
                            paramCol.Add(new KeyValuePair <string, object>("SettingKey", settingkey));
                            paramCol.Add(new KeyValuePair <string, object>("SettingValue", settingvalue));
                            paramCol.Add(new KeyValuePair <string, object>("AddedBy", GetUsername));
                            paramCol.Add(new KeyValuePair <string, object>("Update", update));
                            paramCol.Add(new KeyValuePair <string, object>("onetime", onetime));
                            sqlH.ExecuteNonQuery("usp_Aspx_PaymentGateWaySettingByKeyAdd", paramCol);
                        }

                        XmlNodeList xnList3 = doc.SelectNodes("sageframe/folders/folder/files/file");
                        if (xnList3.Count != 0)
                        {
                            foreach (XmlNode xn3 in xnList3)
                            {
                                string fileName = xn3["name"].InnerXml;
                                try
                                {
                                    #region CheckValidDataSqlProvider
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(module.Version + ".SqlDataProvider"))
                                    {
                                        _exceptions = ReadSQLFile(module.TempFolderPath, fileName);
                                    }
                                    #endregion

                                    #region CheckAlldllFiles
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains(".dll"))
                                    {
                                        dllFiles.Add(fileName);
                                    }
                                    #endregion

                                    #region ReadUninstall SQL FileName
                                    if (!String.IsNullOrEmpty(fileName) && fileName.Contains("Uninstall.SqlDataProvider"))
                                    {
                                        unistallScriptFile = fileName;
                                    }
                                    #endregion
                                }
                                catch (Exception ex)
                                {
                                    _exceptions += ex.Message;
                                    break;
                                }
                            }
                        }

                        if (_exceptions != string.Empty)
                        {
                            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                            {
                                //Run unstallScript
                                if (unistallScriptFile != "")
                                {
                                    _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                                }
                                //Delete Module info from data base
                                PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                                module.PaymentGatewayTypeID = -1;
                            }
                        }
                        #endregion
                    }
                    catch
                    {
                        if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0)
                        {
                            //Run unstallScript
                            if (unistallScriptFile != "")
                            {
                                _exceptions = ReadSQLFile(module.TempFolderPath, unistallScriptFile);
                            }
                            //Delete Module info from data base
                            if (update == 0)
                            {
                                PaymentGatewayRollBack(module.PaymentGatewayTypeID, GetPortalID, module.StoreID);
                            }
                            module.PaymentGatewayTypeID = -1;
                        }
                    }
                    #endregion
                }
            }

            if (module.PaymentGatewayTypeID.ToString() != null && module.PaymentGatewayTypeID > 0 && _exceptions == string.Empty)
            {
                string path       = HttpContext.Current.Server.MapPath("~/");
                string flPath     = module.FolderName.ToString().Replace("/", "\\");
                string targetPath = path + SageFrame.Common.RegisterModule.Common.ModuleFolder + '\\' + flPath;
                CopyDirectory(module.TempFolderPath, targetPath);
                for (int i = 0; i < dllFiles.Count; i++)
                {
                    string sourcedllFile = module.TempFolderPath + '\\' + dllFiles[i].ToString();
                    string targetdllPath = path + SageFrame.Common.RegisterModule.Common.DLLTargetPath + '\\' + dllFiles[i].ToString();
                    File.Copy(sourcedllFile, targetdllPath, true);
                    //File.Move();
                }
            }
            DeleteTempDirectory(module.TempFolderPath);
        }