Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.IO;
  5. using System.Reflection;
  6. using EPiServer.Data;
  7. using EPiServer.DataAccess;
  8.  
  9. namespace Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage
  10. {
  11. public class ChangeMasterLanguageDataAccess : DataAccessBase
  12. {
  13. public const string GetChangeMasterLanguageScript = "Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage.Scripts.GetChangeMasterLanguageStoreProcedure.sql";
  14. public const string CreateChangeMasterLanguageScript = "Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage.Scripts.CreateChangeMasterLanguageStoreProcedure.sql";
  15.  
  16. public const string EPiServerDbConnectionStringName = "EPiServerDB";
  17.  
  18. private readonly IDatabaseExecutor _databaseExecuor;
  19.  
  20. public ChangeMasterLanguageDataAccess(IDatabaseExecutor databaseExecuor) : base(databaseExecuor)
  21. {
  22. _databaseExecuor = databaseExecuor;
  23. }
  24.  
  25. /// <summary>
  26. /// Method to create the change master language store procedure in EPiServerDB if it is not exist and then execute it
  27. /// </summary>
  28. /// <param name="pageId">Root id of the site</param>
  29. /// <param name="language">target language name of the master lanaguage</param>
  30. /// <param name="recursive">enable recursive change master language, by default is true</param>
  31. /// <returns></returns>
  32. public bool ChangeMasterLanguage(int pageId, string language, bool recursive = true)
  33. {
  34. if (pageId <= 0 || string.IsNullOrEmpty(language)) return false;
  35.  
  36. var dbConnection = _databaseExecuor.DbFactory.CreateConnection();
  37. if (dbConnection == null) return false;
  38.  
  39. // Look for the name in the connectionStrings section.
  40. var settings =
  41. ConfigurationManager.ConnectionStrings[EPiServerDbConnectionStringName];
  42.  
  43. dbConnection.ConnectionString = settings.ConnectionString;
  44.  
  45. var assembly = Assembly.GetExecutingAssembly();
  46.  
  47. try
  48. {
  49. dbConnection.Open();
  50. var command = dbConnection.CreateCommand();
  51.  
  52. //Check if Store Procedure is exsit or not
  53. var stream = assembly.GetManifestResourceStream(GetChangeMasterLanguageScript);
  54.  
  55. if (stream == null) throw new Exception("Could not find GetChangeMasterLanguageScript");
  56.  
  57. command.CommandText = new StreamReader(stream).ReadToEnd();
  58. command.CommandType = CommandType.Text;
  59. var dbDataReader = command.ExecuteReader();
  60.  
  61. //If store procedure is not exist, create store procedure
  62. if (!dbDataReader.Read())
  63. {
  64. stream = assembly.GetManifestResourceStream(CreateChangeMasterLanguageScript);
  65.  
  66. if (stream == null) throw new Exception("Could not find CreateChangeMasterLanguageScript");
  67.  
  68. command.CommandText = new StreamReader(stream).ReadToEnd();
  69. command.CommandType = CommandType.Text;
  70. dbDataReader.Close();
  71. command.ExecuteNonQuery();
  72. }
  73. else
  74. {
  75. dbDataReader.Close();
  76. }
  77.  
  78. //Execute created store procedure
  79. command.CommandText = "ChangeSiteMasterLanguage";
  80. command.CommandType = CommandType.StoredProcedure;
  81. command.Parameters.Add(CreateParameter("page_id", pageId));
  82. command.Parameters.Add(CreateParameter("language_branch", language));
  83. command.Parameters.Add(CreateParameter("recursive", recursive));
  84. command.ExecuteNonQuery();
  85. return true;
  86.  
  87. }
  88. finally
  89. {
  90. dbConnection.Close();
  91. }
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement