Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Configuration;
- using System.Data;
- using System.IO;
- using System.Reflection;
- using EPiServer.Data;
- using EPiServer.DataAccess;
- namespace Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage
- {
- public class ChangeMasterLanguageDataAccess : DataAccessBase
- {
- public const string GetChangeMasterLanguageScript = "Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage.Scripts.GetChangeMasterLanguageStoreProcedure.sql";
- public const string CreateChangeMasterLanguageScript = "Dlw.EpiBase.Web.Module.Features.ChangeMasterLanguage.Scripts.CreateChangeMasterLanguageStoreProcedure.sql";
- public const string EPiServerDbConnectionStringName = "EPiServerDB";
- private readonly IDatabaseExecutor _databaseExecuor;
- public ChangeMasterLanguageDataAccess(IDatabaseExecutor databaseExecuor) : base(databaseExecuor)
- {
- _databaseExecuor = databaseExecuor;
- }
- /// <summary>
- /// Method to create the change master language store procedure in EPiServerDB if it is not exist and then execute it
- /// </summary>
- /// <param name="pageId">Root id of the site</param>
- /// <param name="language">target language name of the master lanaguage</param>
- /// <param name="recursive">enable recursive change master language, by default is true</param>
- /// <returns></returns>
- public bool ChangeMasterLanguage(int pageId, string language, bool recursive = true)
- {
- if (pageId <= 0 || string.IsNullOrEmpty(language)) return false;
- var dbConnection = _databaseExecuor.DbFactory.CreateConnection();
- if (dbConnection == null) return false;
- // Look for the name in the connectionStrings section.
- var settings =
- ConfigurationManager.ConnectionStrings[EPiServerDbConnectionStringName];
- dbConnection.ConnectionString = settings.ConnectionString;
- var assembly = Assembly.GetExecutingAssembly();
- try
- {
- dbConnection.Open();
- var command = dbConnection.CreateCommand();
- //Check if Store Procedure is exsit or not
- var stream = assembly.GetManifestResourceStream(GetChangeMasterLanguageScript);
- if (stream == null) throw new Exception("Could not find GetChangeMasterLanguageScript");
- command.CommandText = new StreamReader(stream).ReadToEnd();
- command.CommandType = CommandType.Text;
- var dbDataReader = command.ExecuteReader();
- //If store procedure is not exist, create store procedure
- if (!dbDataReader.Read())
- {
- stream = assembly.GetManifestResourceStream(CreateChangeMasterLanguageScript);
- if (stream == null) throw new Exception("Could not find CreateChangeMasterLanguageScript");
- command.CommandText = new StreamReader(stream).ReadToEnd();
- command.CommandType = CommandType.Text;
- dbDataReader.Close();
- command.ExecuteNonQuery();
- }
- else
- {
- dbDataReader.Close();
- }
- //Execute created store procedure
- command.CommandText = "ChangeSiteMasterLanguage";
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.Add(CreateParameter("page_id", pageId));
- command.Parameters.Add(CreateParameter("language_branch", language));
- command.Parameters.Add(CreateParameter("recursive", recursive));
- command.ExecuteNonQuery();
- return true;
- }
- finally
- {
- dbConnection.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement