PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/CMSModules/System/System_Deployment.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 244 lines | 186 code | 40 blank | 18 comment | 40 complexity | c38c48758e644e83ac85c137dd6270a8 MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using System.Text.RegularExpressions;
  4. using CMS.GlobalHelper;
  5. using CMS.IO;
  6. using CMS.PortalEngine;
  7. using CMS.SettingsProvider;
  8. using CMS.SiteProvider;
  9. using CMS.UIControls;
  10. public partial class CMSModules_System_System_Deployment : SiteManagerPage
  11. {
  12. private static Regex codeFileRegex = RegexHelper.GetRegex("CodeFile=\"([^\"]*)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  13. private static Regex inheritsRegex = RegexHelper.GetRegex("Inherits=\"([^\"]*)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (AzureHelper.IsRunningOnAzure)
  17. {
  18. lblInfo.Text = GetString("Deployment.AzureDisabled");
  19. btnSaveAll.Enabled = false;
  20. }
  21. lblInfo.Text = GetString("Deployment.SaveAllInfo");
  22. btnSaveAll.Text = GetString("Deployment.SaveAll");
  23. }
  24. protected void btnSaveAll_Click(object sender, EventArgs e)
  25. {
  26. try
  27. {
  28. // Save all the document transformations
  29. DataSet ds = DataClassInfoProvider.GetAllDataClass();
  30. if (!DataHelper.DataSourceIsEmpty(ds))
  31. {
  32. foreach (DataRow dr in ds.Tables[0].Rows)
  33. {
  34. int classId = ValidationHelper.GetInteger(dr["ClassID"], 0);
  35. string className = ValidationHelper.GetString(dr["ClassName"], "");
  36. bool isDocumentType = ValidationHelper.GetBoolean(dr["ClassIsDocumentType"], false);
  37. if (isDocumentType)
  38. {
  39. ProcessTransformations(classId, className);
  40. }
  41. }
  42. }
  43. // Save all the custom table transformations
  44. ds = DataClassInfoProvider.GetCustomTableClasses(null, null, 0, "ClassID,ClassName,ClassIsCustomTable");
  45. if (!DataHelper.DataSourceIsEmpty(ds))
  46. {
  47. foreach (DataRow dr in ds.Tables[0].Rows)
  48. {
  49. int classId = ValidationHelper.GetInteger(dr["ClassID"], 0);
  50. string className = ValidationHelper.GetString(dr["ClassName"], "");
  51. bool isCustomTable = ValidationHelper.GetBoolean(dr["ClassIsCustomTable"], false);
  52. if (isCustomTable)
  53. {
  54. ProcessTransformations(classId, className);
  55. }
  56. }
  57. }
  58. // Save all the layouts
  59. ds = LayoutInfoProvider.GetAllLayouts();
  60. if (!DataHelper.DataSourceIsEmpty(ds))
  61. {
  62. foreach (DataRow dr in ds.Tables[0].Rows)
  63. {
  64. string layoutName = ValidationHelper.GetString(dr["LayoutCodeName"], "");
  65. string layoutCode = ValidationHelper.GetString(dr["LayoutCode"], "");
  66. int checkedOutByUserId = ValidationHelper.GetInteger(dr["LayoutCheckedOutByUserID"], 0);
  67. string checkedOutMachineName = ValidationHelper.GetString(dr["LayoutCheckedOutMachineName"], "");
  68. if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))
  69. {
  70. string filename = LayoutInfoProvider.GetLayoutUrl(layoutName, null);
  71. // Prepare the code
  72. string code = layoutCode;
  73. code = LayoutInfoProvider.AddLayoutDirectives(code);
  74. SiteManagerFunctions.SaveCodeFile(filename, code);
  75. }
  76. }
  77. }
  78. // Save all the page template layouts
  79. ds = PageTemplateInfoProvider.GetAllTemplates();
  80. if (!DataHelper.DataSourceIsEmpty(ds))
  81. {
  82. foreach (DataRow dr in ds.Tables[0].Rows)
  83. {
  84. string templateName = ValidationHelper.GetString(dr["PageTemplateCodeName"], "");
  85. string templateLayout = ValidationHelper.GetString(dr["PageTemplateLayout"], "");
  86. bool isReusable = ValidationHelper.GetBoolean(dr["PageTemplateIsReusable"], false);
  87. int checkedOutByUserId = ValidationHelper.GetInteger(dr["PageTemplateLayoutCheckedOutByUserID"], 0);
  88. string checkedOutMachineName = ValidationHelper.GetString(dr["PageTemplateLayoutCheckedOutMachineName"], "");
  89. bool isPortalTemplate = ValidationHelper.GetBoolean(dr["PageTemplateIsPortal"], false);
  90. string pageTemplateType = ValidationHelper.GetString(dr["PageTemplateType"], "");
  91. bool isDashboard = pageTemplateType.Equals(PageTemplateInfoProvider.GetPageTemplateTypeCode(PageTemplateTypeEnum.Dashboard));
  92. if ((isPortalTemplate || isDashboard) && !String.IsNullOrEmpty(templateLayout) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
  93. {
  94. string filename = null;
  95. if (isReusable)
  96. {
  97. filename = PageTemplateInfoProvider.GetLayoutUrl(templateName, null);
  98. }
  99. else
  100. {
  101. filename = PageTemplateInfoProvider.GetAdhocLayoutUrl(templateName, null);
  102. }
  103. // Prepare the code
  104. string code = templateLayout;
  105. code = LayoutInfoProvider.AddLayoutDirectives(code);
  106. SiteManagerFunctions.SaveCodeFile(filename, code);
  107. }
  108. }
  109. }
  110. // Save all the web part layouts
  111. ds = WebPartLayoutInfoProvider.GetWebPartLayouts(null, null);
  112. if (!DataHelper.DataSourceIsEmpty(ds))
  113. {
  114. foreach (DataRow dr in ds.Tables[0].Rows)
  115. {
  116. string webPartLayoutCodeName = ValidationHelper.GetString(dr["WebPartLayoutCodeName"], "");
  117. string webPartLayoutCode = ValidationHelper.GetString(dr["WebPartLayoutCode"], "");
  118. WebPartInfo wpi = WebPartInfoProvider.GetWebPartInfo(ValidationHelper.GetInteger(dr["WebPartLayoutWebPartID"], 0));
  119. if (wpi != null)
  120. {
  121. int checkedOutByUserId = ValidationHelper.GetInteger(dr["WebPartLayoutCheckedOutByUserID"], 0);
  122. string checkedOutMachineName = ValidationHelper.GetString(dr["WebPartLayoutCheckedOutMachineName"], "");
  123. if (!String.IsNullOrEmpty(webPartLayoutCode) && ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower())))
  124. {
  125. // Get layout filename
  126. string filename = WebPartLayoutInfoProvider.GetWebPartLayoutUrl(wpi.WebPartName, webPartLayoutCodeName, "");
  127. // Get path to the code file
  128. string codeFilePath = URLHelper.GetVirtualPath(filename) + ".cs";
  129. // Get path to the original code behind file
  130. string originalCodeFilePath = String.Empty;
  131. if (codeFileRegex.IsMatch(webPartLayoutCode, 0))
  132. {
  133. originalCodeFilePath = codeFileRegex.Match(webPartLayoutCode).Result("$1");
  134. }
  135. // Get original class name
  136. string originalClassName = String.Empty;
  137. if (inheritsRegex.IsMatch(webPartLayoutCode))
  138. {
  139. originalClassName = inheritsRegex.Match(webPartLayoutCode).Result("$1");
  140. }
  141. if (codeFileRegex.IsMatch(webPartLayoutCode))
  142. {
  143. webPartLayoutCode = codeFileRegex.Replace(webPartLayoutCode, "CodeFile=\"" + codeFilePath + "\"");
  144. }
  145. if (inheritsRegex.IsMatch(webPartLayoutCode))
  146. {
  147. webPartLayoutCode = inheritsRegex.Replace(webPartLayoutCode, "Inherits=\"$1_Web_Deployment\"");
  148. }
  149. // Read original codefile and change classname
  150. string codeFileCode = String.Empty;
  151. if (!String.IsNullOrEmpty(originalCodeFilePath) && FileHelper.FileExists(originalCodeFilePath))
  152. {
  153. codeFileCode = File.ReadAllText(Server.MapPath(originalCodeFilePath));
  154. codeFileCode = codeFileCode.Replace(originalClassName, originalClassName + "_Web_Deployment");
  155. // Save code file
  156. SiteManagerFunctions.SaveCodeFile(filename, webPartLayoutCode);
  157. // Save code behind file
  158. SiteManagerFunctions.SaveCodeFile(codeFilePath, codeFileCode);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. lblResult.Text = GetString("Deployment.ObjectsSaved");
  165. }
  166. catch (Exception ex)
  167. {
  168. CMS.EventLog.EventLogProvider ep = new CMS.EventLog.EventLogProvider();
  169. ep.LogEvent("System deployment", "E", ex);
  170. lblError.Visible = true;
  171. lblError.Text = ex.Message;
  172. }
  173. }
  174. private void ProcessTransformations(int classId, string className)
  175. {
  176. // Get the transformations
  177. DataSet transformationsDS = TransformationInfoProvider.GetTransformations(classId);
  178. if (!DataHelper.DataSourceIsEmpty(transformationsDS))
  179. {
  180. foreach (DataRow transformationRow in transformationsDS.Tables[0].Rows)
  181. {
  182. // Get the type
  183. string type = ValidationHelper.GetString(transformationRow["TransformationType"], "ascx");
  184. TransformationTypeEnum transformationType = TransformationInfoProvider.GetTransformationTypeEnum(type);
  185. // Only export ASCX transformations
  186. if (transformationType == TransformationTypeEnum.Ascx)
  187. {
  188. string transformationName = ValidationHelper.GetString(transformationRow["TransformationName"], "");
  189. string transformationCode = ValidationHelper.GetString(transformationRow["TransformationCode"], "");
  190. int checkedOutByUserId = ValidationHelper.GetInteger(transformationRow["TransformationCheckedOutByUserID"], 0);
  191. string checkedOutMachineName = ValidationHelper.GetString(transformationRow["TransformationCheckedOutMachineName"], "");
  192. if ((checkedOutByUserId == 0) || (checkedOutMachineName.ToLower() != HTTPHelper.MachineName.ToLower()))
  193. {
  194. string filename = TransformationInfoProvider.GetTransformationUrl(className + "." + transformationName, null, TransformationTypeEnum.Ascx);
  195. // Prepare the code
  196. string code = transformationCode;
  197. code = TransformationInfoProvider.AddTransformationDirectives(code, true);
  198. SiteManagerFunctions.SaveCodeFile(filename, code);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }